1 19 20 25 26 27 package soot; 28 29 import soot.util.*; 30 import java.util.*; 31 32 33 41 public class ArrayType extends RefLikeType 42 { 43 51 public final Type baseType; 52 53 54 public final int numDimensions; 55 56 57 private ArrayType(Type baseType, int numDimensions) 58 { 59 if( !( baseType instanceof PrimType || baseType instanceof RefType ) ) 60 throw new RuntimeException ( "oops" ); 61 if( numDimensions < 1 ) throw new RuntimeException ( "attempt to create array with "+numDimensions+" dimensions" ); 62 this.baseType = baseType; 63 this.numDimensions = numDimensions; 64 } 65 66 72 public static ArrayType v(Type baseType, int numDimensions) 73 { 74 if( numDimensions < 1 ) throw new RuntimeException ( "attempt to create array with "+numDimensions+" dimensions" ); 75 ArrayType ret; 76 Type elementType; 77 if( numDimensions == 1 ) { 78 elementType = baseType; 79 } else { 80 elementType = ArrayType.v( baseType, numDimensions-1 ); 81 } 82 ret = elementType.getArrayType(); 83 if( ret == null ) { 84 ret = new ArrayType(baseType, numDimensions); 85 elementType.setArrayType( ret ); 86 } 87 return ret; 88 } 89 90 96 public boolean equals(Object t) 97 { 98 return t == this; 99 110 } 111 112 public void toString(UnitPrinter up) 113 { 114 up.type( baseType ); 115 116 for(int i = 0; i < numDimensions; i++) 117 up.literal("[]"); 118 } 119 120 public String toString() 121 { 122 StringBuffer buffer = new StringBuffer (); 123 124 buffer.append(baseType.toString()); 125 126 for(int i = 0; i < numDimensions; i++) 127 buffer.append("[]"); 128 129 return buffer.toString(); 130 } 131 132 public int hashCode() 133 { 134 return baseType.hashCode() + 0x432E0341 * numDimensions; 135 } 136 137 public void apply(Switch sw) 138 { 139 ((TypeSwitch) sw).caseArrayType(this); 140 } 141 142 151 public Type getArrayElementType() { 152 return getElementType(); 153 } 154 160 public Type getElementType() { 161 if( numDimensions > 1 ) { 162 return ArrayType.v( baseType, numDimensions-1 ); 163 } else { 164 return baseType; 165 } 166 } 167 public ArrayType makeArrayType() { 168 return ArrayType.v( baseType, numDimensions+1 ); 169 } 170 } 171 172 | Popular Tags |