1 7 8 package java.lang.reflect; 9 10 import sun.reflect.FieldAccessor; 11 import sun.reflect.Reflection; 12 import sun.reflect.generics.repository.FieldRepository; 13 import sun.reflect.generics.factory.CoreReflectionFactory; 14 import sun.reflect.generics.factory.GenericsFactory; 15 import sun.reflect.generics.scope.ClassScope; 16 import java.lang.annotation.Annotation ; 17 import java.util.Map ; 18 import sun.reflect.annotation.AnnotationParser; 19 20 21 40 public final 41 class Field extends AccessibleObject implements Member { 42 43 private Class clazz; 44 private int slot; 45 private String name; 48 private Class type; 49 private int modifiers; 50 private transient String signature; 52 private transient FieldRepository genericInfo; 54 private byte[] annotations; 55 private FieldAccessor fieldAccessor; 57 private FieldAccessor overrideFieldAccessor; 59 private Field root; 63 64 private volatile Class securityCheckTargetClassCache; 67 68 70 private String getGenericSignature() {return signature;} 71 72 private GenericsFactory getFactory() { 74 Class <?> c = getDeclaringClass(); 75 return CoreReflectionFactory.make(c, ClassScope.make(c)); 77 } 78 79 private FieldRepository getGenericInfo() { 81 if (genericInfo == null) { 83 genericInfo = FieldRepository.make(getGenericSignature(), 85 getFactory()); 86 } 87 return genericInfo; } 89 90 91 96 Field(Class declaringClass, 97 String name, 98 Class type, 99 int modifiers, 100 int slot, 101 String signature, 102 byte[] annotations) 103 { 104 this.clazz = declaringClass; 105 this.name = name; 106 this.type = type; 107 this.modifiers = modifiers; 108 this.slot = slot; 109 this.signature = signature; 110 this.annotations = annotations; 111 } 112 113 118 Field copy() { 119 Field res = new Field (clazz, name, type, modifiers, slot, signature, annotations); 127 res.root = this; 128 res.fieldAccessor = fieldAccessor; 130 res.overrideFieldAccessor = overrideFieldAccessor; 131 return res; 132 } 133 134 138 public Class <?> getDeclaringClass() { 139 return clazz; 140 } 141 142 145 public String getName() { 146 return name; 147 } 148 149 156 public int getModifiers() { 157 return modifiers; 158 } 159 160 168 public boolean isEnumConstant() { 169 return (getModifiers() & Modifier.ENUM) != 0; 170 } 171 172 180 public boolean isSynthetic() { 181 return Modifier.isSynthetic(getModifiers()); 182 } 183 184 192 public Class <?> getType() { 193 return type; 194 } 195 196 220 public Type getGenericType() { 221 if (getGenericSignature() != null) 222 return getGenericInfo().getGenericType(); 223 else 224 return getType(); 225 } 226 227 228 234 public boolean equals(Object obj) { 235 if (obj != null && obj instanceof Field ) { 236 Field other = (Field )obj; 237 return (getDeclaringClass() == other.getDeclaringClass()) 238 && (getName() == other.getName()) 239 && (getType() == other.getType()); 240 } 241 return false; 242 } 243 244 249 public int hashCode() { 250 return getDeclaringClass().getName().hashCode() ^ getName().hashCode(); 251 } 252 253 271 public String toString() { 272 int mod = getModifiers(); 273 return (((mod == 0) ? "" : (Modifier.toString(mod) + " ")) 274 + getTypeName(getType()) + " " 275 + getTypeName(getDeclaringClass()) + "." 276 + getName()); 277 } 278 279 298 public String toGenericString() { 299 int mod = getModifiers(); 300 Type fieldType = getGenericType(); 301 return (((mod == 0) ? "" : (Modifier.toString(mod) + " ")) 302 + ((fieldType instanceof Class ) ? 303 getTypeName((Class )fieldType): fieldType.toString())+ " " 304 + getTypeName(getDeclaringClass()) + "." 305 + getName()); 306 } 307 308 354 public Object get(Object obj) 355 throws IllegalArgumentException , IllegalAccessException 356 { 357 return getFieldAccessor(obj).get(obj); 358 } 359 360 381 public boolean getBoolean(Object obj) 382 throws IllegalArgumentException , IllegalAccessException 383 { 384 return getFieldAccessor(obj).getBoolean(obj); 385 } 386 387 408 public byte getByte(Object obj) 409 throws IllegalArgumentException , IllegalAccessException 410 { 411 return getFieldAccessor(obj).getByte(obj); 412 } 413 414 437 public char getChar(Object obj) 438 throws IllegalArgumentException , IllegalAccessException 439 { 440 return getFieldAccessor(obj).getChar(obj); 441 } 442 443 466 public short getShort(Object obj) 467 throws IllegalArgumentException , IllegalAccessException 468 { 469 return getFieldAccessor(obj).getShort(obj); 470 } 471 472 495 public int getInt(Object obj) 496 throws IllegalArgumentException , IllegalAccessException 497 { 498 return getFieldAccessor(obj).getInt(obj); 499 } 500 501 524 public long getLong(Object obj) 525 throws IllegalArgumentException , IllegalAccessException 526 { 527 return getFieldAccessor(obj).getLong(obj); 528 } 529 530 553 public float getFloat(Object obj) 554 throws IllegalArgumentException , IllegalAccessException 555 { 556 return getFieldAccessor(obj).getFloat(obj); 557 } 558 559 582 public double getDouble(Object obj) 583 throws IllegalArgumentException , IllegalAccessException 584 { 585 return getFieldAccessor(obj).getDouble(obj); 586 } 587 588 653 public void set(Object obj, Object value) 654 throws IllegalArgumentException , IllegalAccessException 655 { 656 getFieldAccessor(obj).set(obj, value); 657 } 658 659 682 public void setBoolean(Object obj, boolean z) 683 throws IllegalArgumentException , IllegalAccessException 684 { 685 getFieldAccessor(obj).setBoolean(obj, z); 686 } 687 688 711 public void setByte(Object obj, byte b) 712 throws IllegalArgumentException , IllegalAccessException 713 { 714 getFieldAccessor(obj).setByte(obj, b); 715 } 716 717 740 public void setChar(Object obj, char c) 741 throws IllegalArgumentException , IllegalAccessException 742 { 743 getFieldAccessor(obj).setChar(obj, c); 744 } 745 746 769 public void setShort(Object obj, short s) 770 throws IllegalArgumentException , IllegalAccessException 771 { 772 getFieldAccessor(obj).setShort(obj, s); 773 } 774 775 798 public void setInt(Object obj, int i) 799 throws IllegalArgumentException , IllegalAccessException 800 { 801 getFieldAccessor(obj).setInt(obj, i); 802 } 803 804 827 public void setLong(Object obj, long l) 828 throws IllegalArgumentException , IllegalAccessException 829 { 830 getFieldAccessor(obj).setLong(obj, l); 831 } 832 833 856 public void setFloat(Object obj, float f) 857 throws IllegalArgumentException , IllegalAccessException 858 { 859 getFieldAccessor(obj).setFloat(obj, f); 860 } 861 862 885 public void setDouble(Object obj, double d) 886 throws IllegalArgumentException , IllegalAccessException 887 { 888 getFieldAccessor(obj).setDouble(obj, d); 889 } 890 891 private FieldAccessor getFieldAccessor(Object obj) 893 throws IllegalAccessException 894 { 895 doSecurityCheck(obj); 896 boolean ov = override; 897 FieldAccessor a = (ov)? overrideFieldAccessor : fieldAccessor; 898 return (a != null)? a : acquireFieldAccessor(ov); 899 } 900 901 private FieldAccessor acquireFieldAccessor(boolean overrideFinalCheck) { 906 FieldAccessor tmp = null; 909 if (root != null) tmp = root.getFieldAccessor(overrideFinalCheck); 910 if (tmp != null) { 911 if (overrideFinalCheck) 912 overrideFieldAccessor = tmp; 913 else 914 fieldAccessor = tmp; 915 } else { 916 tmp = reflectionFactory.newFieldAccessor(this, overrideFinalCheck); 918 setFieldAccessor(tmp, overrideFinalCheck); 919 } 920 return tmp; 921 } 922 923 private FieldAccessor getFieldAccessor(boolean overrideFinalCheck) { 926 return (overrideFinalCheck)? overrideFieldAccessor : fieldAccessor; 927 } 928 929 private void setFieldAccessor(FieldAccessor accessor, boolean overrideFinalCheck) { 932 if (overrideFinalCheck) 933 overrideFieldAccessor = accessor; 934 else 935 fieldAccessor = accessor; 936 if (root != null) { 938 root.setFieldAccessor(accessor, overrideFinalCheck); 939 } 940 } 941 942 private void doSecurityCheck(Object obj) throws IllegalAccessException { 946 if (!override) { 947 if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { 948 Class caller = Reflection.getCallerClass(4); 949 Class targetClass = ((obj == null || !Modifier.isProtected(modifiers)) 950 ? clazz 951 : obj.getClass()); 952 if (securityCheckCache != caller || 953 targetClass != securityCheckTargetClassCache) { 954 Reflection.ensureMemberAccess(caller, clazz, obj, modifiers); 955 securityCheckCache = caller; 956 securityCheckTargetClassCache = targetClass; 957 } 958 } 959 } 960 } 961 962 965 static String getTypeName(Class type) { 966 if (type.isArray()) { 967 try { 968 Class cl = type; 969 int dimensions = 0; 970 while (cl.isArray()) { 971 dimensions++; 972 cl = cl.getComponentType(); 973 } 974 StringBuffer sb = new StringBuffer (); 975 sb.append(cl.getName()); 976 for (int i = 0; i < dimensions; i++) { 977 sb.append("[]"); 978 } 979 return sb.toString(); 980 } catch (Throwable e) { } 981 } 982 return type.getName(); 983 } 984 985 public <T extends Annotation > T getAnnotation(Class <T> annotationClass) { 986 if (annotationClass == null) 987 throw new NullPointerException (); 988 989 return (T) declaredAnnotations().get(annotationClass); 990 } 991 992 private static final Annotation [] EMPTY_ANNOTATION_ARRAY=new Annotation [0]; 993 994 public Annotation [] getDeclaredAnnotations() { 995 return declaredAnnotations().values().toArray(EMPTY_ANNOTATION_ARRAY); 996 } 997 998 private transient Map <Class , Annotation > declaredAnnotations; 999 1000 private synchronized Map <Class , Annotation > declaredAnnotations() { 1001 if (declaredAnnotations == null) { 1002 declaredAnnotations = AnnotationParser.parseAnnotations( 1003 annotations, sun.misc.SharedSecrets.getJavaLangAccess(). 1004 getConstantPool(getDeclaringClass()), 1005 getDeclaringClass()); 1006 } 1007 return declaredAnnotations; 1008 } 1009} 1010 | Popular Tags |