00001 package de.fub.bytecode.generic;
00002
00003 import de.fub.bytecode.Constants;
00004
00005
00006
00007
00008
00009
00010
00011 public final class ArrayType extends ReferenceType {
00012 private int dimensions;
00013 private Type basic_type;
00014
00015
00016
00017
00018
00019
00020 public ArrayType(byte type, int dimensions) {
00021 this(BasicType.getType(type), dimensions);
00022 }
00023
00024
00025
00026
00027
00028 public ArrayType(Type type, int dimensions) {
00029 super(Constants.T_ARRAY, "<dummy>");
00030
00031 if((dimensions < 1) || (dimensions > Constants.MAX_BYTE))
00032 throw new ClassGenException("Invalid number of dimensions: " + dimensions);
00033
00034 switch(type.getType()) {
00035 case Constants.T_ARRAY:
00036 ArrayType array = (ArrayType)type;
00037 this.dimensions = dimensions + array.dimensions;
00038 basic_type = array.basic_type;
00039 break;
00040
00041 case Constants.T_VOID:
00042 throw new ClassGenException("Invalid type: void[]");
00043
00044 default:
00045 this.dimensions = dimensions;
00046 basic_type = type;
00047 break;
00048 }
00049
00050 StringBuffer buf = new StringBuffer();
00051 for(int i=0; i < this.dimensions; i++)
00052 buf.append('[');
00053
00054 buf.append(basic_type.getSignature());
00055
00056 signature = buf.toString();
00057 }
00058
00059
00060
00061
00062
00063 public ArrayType(String class_name, int dimensions) {
00064 this(new ObjectType(class_name), dimensions);
00065 }
00066
00067
00068 public boolean equals(Object type) {
00069 if(type instanceof ArrayType) {
00070 ArrayType array = (ArrayType)type;
00071 return (array.dimensions == dimensions) && array.basic_type.equals(basic_type);
00072 } else
00073 return false;
00074 }
00075
00076
00077
00078 public Type getBasicType() {
00079 return basic_type;
00080 }
00081
00082
00083 public int getDimensions() { return dimensions; }
00084
00085
00086
00087 public Type getElementType() {
00088 if(dimensions == 1)
00089 return basic_type;
00090 else
00091 return new ArrayType(basic_type, dimensions - 1);
00092 }
00093
00094
00095 public int hashcode() { return basic_type.hashCode() ^ dimensions; }
00096 }