1 package com.sun.org.apache.bcel.internal.generic; 2 3 56 import com.sun.org.apache.bcel.internal.Constants; 57 import com.sun.org.apache.bcel.internal.Repository; 58 import com.sun.org.apache.bcel.internal.classfile.JavaClass; 59 60 66 public class ReferenceType extends Type { 67 protected ReferenceType(byte t, String s) { 68 super(t, s); 69 } 70 71 73 ReferenceType() { 74 super(Constants.T_OBJECT, "<null object>"); 75 } 76 77 85 public boolean isCastableTo(Type t){ 86 if(this.equals(Type.NULL)) 87 return true; 89 return isAssignmentCompatibleWith(t); 92 } 93 94 99 public boolean isAssignmentCompatibleWith(Type t) { 100 if(!(t instanceof ReferenceType)) 101 return false; 102 103 ReferenceType T = (ReferenceType)t; 104 105 if(this.equals(Type.NULL)) 106 return true; 108 110 if((this instanceof ObjectType) && (((ObjectType) this).referencesClass())) { 111 114 if((T instanceof ObjectType) && (((ObjectType) T).referencesClass())) { 115 if(this.equals(T)) 116 return true; 117 118 if(Repository.instanceOf( ((ObjectType) this).getClassName(), 119 ((ObjectType) T).getClassName())) 120 return true; 121 } 122 123 125 if ((T instanceof ObjectType) && (((ObjectType) T).referencesInterface())) { 126 if (Repository.implementationOf( ((ObjectType) this).getClassName(), 127 ((ObjectType) T).getClassName() )) 128 return true; 129 } 130 } 131 132 134 if ((this instanceof ObjectType) && (((ObjectType) this).referencesInterface())){ 135 137 if ((T instanceof ObjectType) && (((ObjectType) T).referencesClass())){ 138 if (T.equals(Type.OBJECT)) return true; 139 } 140 141 144 if ((T instanceof ObjectType) && (((ObjectType) T).referencesInterface())){ 145 if (this.equals(T)) return true; 146 if (Repository.implementationOf( ((ObjectType) this).getClassName(), 147 ((ObjectType) T).getClassName() )) return true; 148 } 149 } 150 151 154 if(this instanceof ArrayType){ 155 157 if ((T instanceof ObjectType) && (((ObjectType) T).referencesClass())){ 158 if (T.equals(Type.OBJECT)) return true; 159 } 160 161 164 if (T instanceof ArrayType) { 165 167 Type sc = ((ArrayType) this).getElementType(); 168 Type tc = ((ArrayType) this).getElementType(); 169 170 if (sc instanceof BasicType && tc instanceof BasicType && sc.equals(tc)) 171 return true; 172 173 175 if (tc instanceof ReferenceType && sc instanceof ReferenceType && 176 ((ReferenceType) sc).isAssignmentCompatibleWith((ReferenceType) tc)) return true; 177 } 178 179 180 if ((T instanceof ObjectType) && (((ObjectType) T).referencesInterface())){ 186 for (int ii=0; ii<Constants.INTERFACES_IMPLEMENTED_BY_ARRAYS.length; ii++){ 187 if (T.equals(new ObjectType(Constants.INTERFACES_IMPLEMENTED_BY_ARRAYS[ii]))) return true; 188 } 189 } 190 } 191 return false; } 193 194 206 public ReferenceType firstCommonSuperclass(ReferenceType t){ 207 if (this.equals(Type.NULL)) return t; 208 209 if (t.equals(Type.NULL)) return this; 210 211 if (this.equals(t)) return this; 212 213 219 if ((this instanceof ArrayType) || (t instanceof ArrayType)) 220 return Type.OBJECT; 221 223 if ( ((this instanceof ObjectType) && ((ObjectType) this).referencesInterface()) || 224 (( t instanceof ObjectType) && ((ObjectType) t).referencesInterface()) ) 225 return Type.OBJECT; 226 230 231 ObjectType thiz = (ObjectType) this; 233 ObjectType other = (ObjectType) t; 234 JavaClass[] thiz_sups = Repository.getSuperClasses( thiz.getClassName()); 235 JavaClass[] other_sups = Repository.getSuperClasses(other.getClassName()); 236 237 if ((thiz_sups == null) || (other_sups==null)){ 238 return null; 239 } 240 241 JavaClass[] this_sups = new JavaClass[thiz_sups.length+1]; 243 JavaClass[] t_sups = new JavaClass[other_sups.length+1]; 244 System.arraycopy( thiz_sups, 0, this_sups, 1, thiz_sups.length); 245 System.arraycopy(other_sups, 0, t_sups , 1, other_sups.length); 246 this_sups[0] = Repository.lookupClass(thiz.getClassName()); 247 t_sups[0] = Repository.lookupClass(other.getClassName()); 248 249 for (int i=0; i<t_sups.length; i++){ 250 for (int j=0; j<this_sups.length; j++){ 251 if (this_sups[j].equals(t_sups[i])) return new ObjectType(this_sups[j].getClassName()); 252 } 253 } 254 255 return null; 257 } 258 } 259 | Popular Tags |