1 28 29 package com.caucho.bytecode; 30 31 import com.caucho.log.Log; 32 33 import java.io.ByteArrayInputStream ; 34 import java.io.IOException ; 35 import java.lang.reflect.Modifier ; 36 import java.util.ArrayList ; 37 import java.util.logging.Level ; 38 import java.util.logging.Logger ; 39 40 43 public class JavaField extends JField { 44 static private final Logger log = Log.open(JavaField.class); 45 46 private JavaClass _jClass; 47 private int _accessFlags; 48 private String _name; 49 private String _descriptor; 50 51 private ArrayList <Attribute> _attributes = new ArrayList <Attribute>(); 52 53 private JavaAnnotation []_annotations; 54 55 58 public void setJavaClass(JavaClass jClass) 59 { 60 _jClass = jClass; 61 } 62 63 66 public JClass getDeclaringClass() 67 { 68 return _jClass; 69 } 70 71 74 public JavaClassLoader getClassLoader() 75 { 76 return _jClass.getClassLoader(); 77 } 78 79 82 public void setName(String name) 83 { 84 _name = name; 85 } 86 87 90 public String getName() 91 { 92 return _name; 93 } 94 95 98 public void setAccessFlags(int flags) 99 { 100 _accessFlags = flags; 101 } 102 103 106 public int getAccessFlags() 107 { 108 return _accessFlags; 109 } 110 111 114 public void setDescriptor(String descriptor) 115 { 116 _descriptor = descriptor; 117 118 if (_jClass != null) 119 _jClass.getConstantPool().addUTF8(descriptor); 120 } 121 122 125 public String getDescriptor() 126 { 127 return _descriptor; 128 } 129 130 133 public JClass getType() 134 { 135 return getClassLoader().descriptorToClass(getDescriptor(), 0); 136 } 137 138 141 public boolean isStatic() 142 { 143 return Modifier.isStatic(getAccessFlags()); 144 } 145 146 149 public boolean isPrivate() 150 { 151 return Modifier.isPrivate(getAccessFlags()); 152 } 153 154 157 public boolean isTransient() 158 { 159 return Modifier.isTransient(getAccessFlags()); 160 } 161 162 165 public JType getGenericType() 166 { 167 SignatureAttribute sigAttr = (SignatureAttribute) getAttribute("Signature"); 168 169 if (sigAttr != null) { 170 return getClassLoader().parseParameterizedType(sigAttr.getSignature()); 171 } 172 173 return getType(); 174 } 175 176 179 public void addAttribute(Attribute attr) 180 { 181 _attributes.add(attr); 182 } 183 184 187 public Attribute getAttribute(String name) 188 { 189 for (int i = _attributes.size() - 1; i >= 0; i--) { 190 Attribute attr = _attributes.get(i); 191 192 if (attr.getName().equals(name)) 193 return attr; 194 } 195 196 return null; 197 } 198 199 202 public JAnnotation []getDeclaredAnnotations() 203 { 204 if (_annotations == null) { 205 Attribute attr = getAttribute("RuntimeVisibleAnnotations"); 206 207 if (attr instanceof OpaqueAttribute) { 208 byte []buffer = ((OpaqueAttribute) attr).getValue(); 209 210 try { 211 ByteArrayInputStream is = new ByteArrayInputStream (buffer); 212 213 ConstantPool cp = _jClass.getConstantPool(); 214 215 _annotations = JavaAnnotation.parseAnnotations(is, cp, 216 getClassLoader()); 217 } catch (IOException e) { 218 log.log(Level.FINER, e.toString(), e); 219 } 220 } 221 222 if (_annotations == null) { 223 _annotations = new JavaAnnotation[0]; 224 } 225 } 226 227 return _annotations; 228 } 229 230 233 public void write(ByteCodeWriter out) 234 throws IOException 235 { 236 out.writeShort(_accessFlags); 237 out.writeUTF8Const(_name); 238 out.writeUTF8Const(_descriptor); 239 out.writeShort(_attributes.size()); 240 241 for (int i = 0; i < _attributes.size(); i++) { 242 Attribute attr = _attributes.get(i); 243 244 attr.write(out); 245 } 246 } 247 248 251 public JavaField export(JavaClass cl, JavaClass target) 252 { 253 JavaField field = new JavaField(); 254 field.setName(_name); 255 field.setDescriptor(_descriptor); 256 field.setAccessFlags(_accessFlags); 257 258 target.getConstantPool().addUTF8(_name); 259 target.getConstantPool().addUTF8(_descriptor); 260 261 for (int i = 0; i < _attributes.size(); i++) { 262 Attribute attr = _attributes.get(i); 263 264 field.addAttribute(attr.export(cl, target)); 265 } 266 267 return field; 268 } 269 270 public boolean equals(Object o) 271 { 272 if (o == null || ! JavaField.class.equals(o.getClass())) 273 return false; 274 275 JavaField field = (JavaField) o; 276 277 return _name.equals(field._name); 278 } 279 280 public String toString() 281 { 282 return "JavaField[" + _name + "]"; 283 } 284 } 285 | Popular Tags |