1 4 package gnu.bytecode; 5 6 10 11 public class ObjectType extends Type 12 { 13 protected ObjectType () 14 { 15 size = 4; 16 } 17 18 public ObjectType (String name) 19 { 20 this_name = name; 21 size = 4; 22 } 23 24 final static int ADD_FIELDS_DONE = 1; 26 final static int ADD_METHODS_DONE = 2; 27 final static int EXISTING_CLASS = 4; 29 30 final static int HAS_OUTER_LINK = 8; 31 32 public int flags; 33 34 public final boolean isExisting() 35 { 36 return (flags & EXISTING_CLASS) != 0; 37 } 38 39 public final void setExisting(boolean existing) 40 { 41 if (existing) flags |= EXISTING_CLASS; 42 else flags &= ~ EXISTING_CLASS; 43 } 44 45 51 public String getInternalName() 52 { 53 return getName().replace('.', '/'); 54 } 55 56 57 public Class getReflectClass() 58 { 59 try 60 { 61 if (reflectClass == null) 62 { 63 String cname = getInternalName().replace('/', '.'); 64 65 66 reflectClass = Class.forName(cname, 67 false, getClass().getClassLoader()); 68 69 71 } 72 flags |= EXISTING_CLASS; 73 } 74 catch (java.lang.ClassNotFoundException ex) 75 { 76 if ((flags & EXISTING_CLASS) != 0) 77 { 78 RuntimeException rex 79 = new RuntimeException ("no such class: "+getName()); 80 81 rex.initCause(ex); 82 83 throw rex; 84 } 85 } 86 return reflectClass; 87 } 88 89 public Type getImplementationType() 90 { 91 return this == nullType ? pointer_type 92 : this == tostring_type ? string_type : this; 93 } 94 95 public Type promote () 96 { 97 return this == nullType ? pointer_type : this; 98 } 99 100 public boolean isInstance (Object obj) 101 { 102 if (this == nullType) 103 return obj == null; 104 return super.isInstance(obj); 105 } 106 107 public int compare(Type other) 108 { 109 return other == nullType ? 0 : -1; 111 } 112 113 114 116 118 public Object coerceFromObject (Object obj) 119 { 120 if (obj != null) 121 { 122 if (this == Type.tostring_type) 123 return obj.toString(); 124 Class clas = getReflectClass(); 125 Class objClass = obj.getClass(); 126 if (! clas.isAssignableFrom(objClass)) 127 throw new ClassCastException ("don't know how to coerce " 128 + objClass.getName() + " to " 129 + getName()); 130 } 131 return obj; 132 } 133 134 135 public void emitCoerceFromObject (CodeAttr code) 136 { 137 if (this == Type.tostring_type) 138 { 139 code.emitDup(); 146 code.emitIfNull(); 147 code.emitPop(1); 148 code.emitPushNull(); 149 code.emitElse(); 150 code.emitInvokeVirtual(Type.toString_method); 151 code.emitFi(); 152 } 153 else if (this != Type.pointer_type) 154 code.emitCheckcast(this); 155 } 156 } 157 | Popular Tags |