| 1 package gnu.kawa.reflect; 2 import gnu.mapping.*; 3 import gnu.bytecode.*; 4 import gnu.mapping.Location; import gnu.expr.*; 6 7 11 12 public class ClassMemberLocation extends Location 13 { 14 Object instance; 15 ClassType type; 16 17 String mname; 18 java.lang.reflect.Field rfield; 19 20 public final Object getInstance () { return instance; } 21 public final void setInstance (Object obj) { instance = obj; } 22 23 public ClassMemberLocation(Object instance, ClassType type, String mname) 24 { 25 this.instance = instance; 26 this.type = type; 27 this.mname = mname; 28 } 29 30 public ClassMemberLocation(Object instance, Class clas, String mname) 31 { 32 this.instance = instance; 33 this.type = (ClassType) Type.make(clas); 34 this.mname = mname; 35 } 36 37 public String getMemberName() 38 { 39 return mname; 40 } 41 42 public ClassType getDeclaringClass() 43 { 44 return type; 45 } 46 47 public ClassMemberLocation(Object instance, java.lang.reflect.Field field) 48 { 49 this.instance = instance; 50 this.rfield = field; 51 this.mname = field.getName(); 52 } 53 54 void setup () 55 { 56 if (rfield == null) 57 { 58 Class clas; 59 try 60 { 61 clas = type.getReflectClass(); 62 } 63 catch (RuntimeException ex) 64 { 65 RuntimeException uex 66 = new UnboundLocationException(null, "Unbound location - " 67 + ex.toString()); 68 69 uex.initCause(ex); 70 71 throw uex; 72 } 73 try 74 { 75 rfield = clas.getField(mname); 76 } 77 catch (java.lang.NoSuchFieldException ex) 78 { 79 RuntimeException uex 80 = new UnboundLocationException(null, "Unbound location " 81 + " - no field " + mname 82 + " in " + type.getName()); 83 84 uex.initCause(ex); 85 86 throw uex; 87 } 88 } 89 } 90 91 public java.lang.reflect.Field getRField () 92 { 93 java.lang.reflect.Field rfld = this.rfield; 94 if (rfld == null) 95 { 96 Class clas 97 = null; 98 try 99 { 100 clas = type.getReflectClass(); 101 rfld = clas.getField(mname); 102 this.rfield = rfld; 103 } 104 catch (Exception ex) 105 { 106 return null; 107 } 108 } 109 return rfld; 110 } 111 112 public Object get (Object defaultValue) 113 { 114 java.lang.reflect.Field rfld = getRField(); 115 if (rfld == null) 116 return defaultValue; 117 118 try 119 { 120 return rfld.get(instance); 121 } 122 catch (IllegalAccessException ex) 123 { 124 throw WrappedException.wrapIfNeeded(ex); 125 } 126 } 127 128 public boolean isConstant () 129 { 130 java.lang.reflect.Field rfld = getRField(); 131 return rfld != null && (rfield.getModifiers() & Access.FINAL) != 0; 132 } 133 134 public boolean isBound () 135 { 136 java.lang.reflect.Field rfld = getRField(); 137 return rfld != null; 138 } 139 140 public void set (Object value) 141 { 142 setup(); 143 try 144 { 145 rfield.set(instance, value); 146 return; 147 } 148 catch (IllegalAccessException ex) 149 { 150 throw WrappedException.wrapIfNeeded(ex); 151 } 152 } 156 157 public static void define (Object instance, java.lang.reflect.Field rfield, 158 String uri, Language language, Environment env) 159 throws IllegalAccessException  160 { 161 Object fvalue = rfield.get(instance); 162 Type ftype = Type.make(rfield.getType()); 163 boolean isAlias = ftype.isSubtype(Compilation.typeLocation); 164 boolean isProcedure = ftype.isSubtype(Compilation.typeProcedure); 165 Object fdname = ((fvalue instanceof Named && ! isAlias) 166 ? ((Named) fvalue).getSymbol() 167 : Compilation.demangleName(rfield.getName(), true)); 168 Symbol sym; 169 if (fdname instanceof Symbol) 170 sym = (Symbol) fdname; 171 else 172 { 173 sym = Symbol.make(uri == null ? "" : uri, 174 fdname.toString().intern()); 175 } 176 boolean isFinal = (rfield.getModifiers() & Access.FINAL) != 0; 177 Location loc; 178 Object property = null; 179 if (isAlias && isFinal) 180 { 181 loc = (Location) fvalue; 182 } 183 else 184 { 185 if (isFinal) 186 property = language.getEnvPropertyFor(rfield, fvalue); 187 loc = new ClassMemberLocation(instance, rfield); 188 } 189 env.addLocation(sym, property, loc); 190 } 191 192 193 public static void defineAll(Object instance, Environment env) 194 throws IllegalAccessException  195 { 196 Class clas = instance.getClass(); 197 Language language = Language.getDefaultLanguage(); 198 java.lang.reflect.Field [] fields = clas.getFields(); 199 for (int i = fields.length; --i >= 0; ) 200 { 201 java.lang.reflect.Field field = fields[i]; 202 String fname = field.getName(); 203 if (fname.startsWith(Declaration.PRIVATE_PREFIX) 204 || fname.endsWith("$instance")) 205 continue; 206 define(instance, field, null, language, env); 207 } 208 } 209 } 210 | Popular Tags |