1, Field class and InvocationTargetException class
InvocationTargetException is a checked exception that wraps the exception thrown by the called method or constructor. Field provides information and dynamic access about a single} field of a class or interface. The reflection field can be a class (static) field or an instance field. Next, I will introduce the internal source code of field class and InvocationTargetException class in detail from the perspective of source code
2, Field class source code introduction
The Field class inherits the AccessibleObject class and implements the interface Member
public final class Field extends AccessibleObject implements Member { }
Some basic variables are defined below
private Class<?> clazz; private int slot; private String name; private Class<?> type; private int modifiers; private transient String signature; private transient FieldRepository genericInfo; private byte[] annotations; private FieldAccessor fieldAccessor; private FieldAccessor overrideFieldAccessor; private Field root;
Get generic signature
private String getGenericSignature() {return signature;}
Get generic factory
private GenericsFactory getFactory() { Class<?> c = getDeclaringClass(); // create scope and factory return CoreReflectionFactory.make(c, ClassScope.make(c)); }
Get field repository
private FieldRepository getGenericInfo() { // lazily initialize repository if necessary if (genericInfo == null) { // create and cache generic info repository genericInfo = FieldRepository.make(getGenericSignature(), getFactory()); } return genericInfo; //return cached repository }
Construction method of Field
Field(Class<?> declaringClass, String name, Class<?> type, int modifiers, int slot, String signature, byte[] annotations) { this.clazz = declaringClass; this.name = name; this.type = type; this.modifiers = modifiers; this.slot = slot; this.signature = signature; this.annotations = annotations; }
The package private routine (exposed to java.lang.Class through ReflectAccess) returns a copy of this field. The root field of the replica points to this field.
Field copy() { if (this.root != null) throw new IllegalArgumentException("Can not copy a non-root Field"); Field res = new Field(clazz, name, type, modifiers, slot, signature, annotations); res.root = this; res.fieldAccessor = fieldAccessor; res.overrideFieldAccessor = overrideFieldAccessor; return res; }
Returns a Class object representing a Class or interface that declares a Field represented by this Field object
public Class<?> getDeclaringClass() { return clazz; }
Get field name
public String getName() { return name; }
Get modifier
public int getModifiers() { return modifiers; }
Determine whether it is an enumeration
public boolean isEnumConstant() { return (getModifiers() & Modifier.ENUM) != 0; }
Determine whether it is a composite field
public boolean isSynthetic() { return Modifier.isSynthetic(getModifiers()); }
Define acquisition type
public Class<?> getType() { return type; }
Get generic type
public Type getGenericType() { if (getGenericSignature() != null) return getGenericInfo().getGenericType(); else return getType(); }
Compares this Field with the specified object
public boolean equals(Object obj) { if (obj != null && obj instanceof Field) { Field other = (Field)obj; return (getDeclaringClass() == other.getDeclaringClass()) && (getName() == other.getName()) && (getType() == other.getType()); } return false; }
Get hash value
public int hashCode() { return getDeclaringClass().getName().hashCode() ^ getName().hashCode(); }
Definition returns a string describing this Field
public String toString() { int mod = getModifiers(); return (((mod == 0) ? "" : (Modifier.toString(mod) + " ")) + getType().getTypeName() + " " + getDeclaringClass().getTypeName() + "." + getName()); }
Convert to generic string
public String toGenericString() { int mod = getModifiers(); Type fieldType = getGenericType(); return (((mod == 0) ? "" : (Modifier.toString(mod) + " ")) + fieldType.getTypeName() + " " + getDeclaringClass().getTypeName() + "." + getName()); }
Returns the value of the Field represented by this Field on the specified object.
public Object get(Object obj) throws IllegalArgumentException, IllegalAccessException { if (!override) { if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { Class<?> caller = Reflection.getCallerClass(); checkAccess(caller, clazz, obj, modifiers); } } return getFieldAccessor(obj).get(obj); }
Gets the value of a static or instance Boolean field.
public boolean getBoolean(Object obj) throws IllegalArgumentException, IllegalAccessException { if (!override) { if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { Class<?> caller = Reflection.getCallerClass(); checkAccess(caller, clazz, obj, modifiers); } } return getFieldAccessor(obj).getBoolean(obj); }
Gets different types of values for static or instance byte fields.
public byte getByte(Object obj) throws IllegalArgumentException, IllegalAccessException { if (!override) { if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { Class<?> caller = Reflection.getCallerClass(); checkAccess(caller, clazz, obj, modifiers); } } return getFieldAccessor(obj).getByte(obj); } public char getChar(Object obj) throws IllegalArgumentException, IllegalAccessException { if (!override) { if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { Class<?> caller = Reflection.getCallerClass(); checkAccess(caller, clazz, obj, modifiers); } } return getFieldAccessor(obj).getChar(obj); } public short getShort(Object obj) throws IllegalArgumentException, IllegalAccessException { if (!override) { if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { Class<?> caller = Reflection.getCallerClass(); checkAccess(caller, clazz, obj, modifiers); } } return getFieldAccessor(obj).getShort(obj); } public int getInt(Object obj) throws IllegalArgumentException, IllegalAccessException { if (!override) { if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { Class<?> caller = Reflection.getCallerClass(); checkAccess(caller, clazz, obj, modifiers); } } return getFieldAccessor(obj).getInt(obj); } public long getLong(Object obj) throws IllegalArgumentException, IllegalAccessException { if (!override) { if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { Class<?> caller = Reflection.getCallerClass(); checkAccess(caller, clazz, obj, modifiers); } } return getFieldAccessor(obj).getLong(obj); } public float getFloat(Object obj) throws IllegalArgumentException, IllegalAccessException { if (!override) { if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { Class<?> caller = Reflection.getCallerClass(); checkAccess(caller, clazz, obj, modifiers); } } return getFieldAccessor(obj).getFloat(obj); } public double getDouble(Object obj) throws IllegalArgumentException, IllegalAccessException { if (!override) { if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { Class<?> caller = Reflection.getCallerClass(); checkAccess(caller, clazz, obj, modifiers); } } return getFieldAccessor(obj).getDouble(obj); }
Set the value of an object.
public void set(Object obj, Object value) throws IllegalArgumentException, IllegalAccessException { if (!override) { if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { Class<?> caller = Reflection.getCallerClass(); checkAccess(caller, clazz, obj, modifiers); } } getFieldAccessor(obj).set(obj, value); }
Sets the boolean type of an object
public void setBoolean(Object obj, boolean z) throws IllegalArgumentException, IllegalAccessException { if (!override) { if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { Class<?> caller = Reflection.getCallerClass(); checkAccess(caller, clazz, obj, modifiers); } } getFieldAccessor(obj).setBoolean(obj, z); }
Sets the byte type word of an object
public void setByte(Object obj, byte b) throws IllegalArgumentException, IllegalAccessException { if (!override) { if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { Class<?> caller = Reflection.getCallerClass(); checkAccess(caller, clazz, obj, modifiers); } } getFieldAccessor(obj).setByte(obj, b); }
Sets the char type word of an object
public void setChar(Object obj, char c) throws IllegalArgumentException, IllegalAccessException { if (!override) { if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { Class<?> caller = Reflection.getCallerClass(); checkAccess(caller, clazz, obj, modifiers); } } getFieldAccessor(obj).setChar(obj, c); }
Sets the short type word of an object
public void setShort(Object obj, short s) throws IllegalArgumentException, IllegalAccessException { if (!override) { if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { Class<?> caller = Reflection.getCallerClass(); checkAccess(caller, clazz, obj, modifiers); } } getFieldAccessor(obj).setShort(obj, s); }
Sets the int type word of an object
public void setInt(Object obj, int i) throws IllegalArgumentException, IllegalAccessException { if (!override) { if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { Class<?> caller = Reflection.getCallerClass(); checkAccess(caller, clazz, obj, modifiers); } } getFieldAccessor(obj).setInt(obj, i); }
Set the long type word of an object
public void setLong(Object obj, long l) throws IllegalArgumentException, IllegalAccessException { if (!override) { if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { Class<?> caller = Reflection.getCallerClass(); checkAccess(caller, clazz, obj, modifiers); } } getFieldAccessor(obj).setLong(obj, l); }
Sets the float type word of an object
public void setFloat(Object obj, float f) throws IllegalArgumentException, IllegalAccessException { if (!override) { if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { Class<?> caller = Reflection.getCallerClass(); checkAccess(caller, clazz, obj, modifiers); } } getFieldAccessor(obj).setFloat(obj, f); }
Sets the double type word of an object
public void setDouble(Object obj, double d) throws IllegalArgumentException, IllegalAccessException { if (!override) { if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { Class<?> caller = Reflection.getCallerClass(); checkAccess(caller, clazz, obj, modifiers); } } getFieldAccessor(obj).setDouble(obj, d); }
Get the field address, the middle is the specific implementation class, and the last is the external interface class
private FieldAccessor getFieldAccessor(Object obj) throws IllegalAccessException { boolean ov = override; FieldAccessor a = (ov) ? overrideFieldAccessor : fieldAccessor; return (a != null) ? a : acquireFieldAccessor(ov); } private FieldAccessor acquireFieldAccessor(boolean overrideFinalCheck) { // First check to see if one has been created yet, and take it // if so FieldAccessor tmp = null; if (root != null) tmp = root.getFieldAccessor(overrideFinalCheck); if (tmp != null) { if (overrideFinalCheck) overrideFieldAccessor = tmp; else fieldAccessor = tmp; } else { // Otherwise fabricate one and propagate it up to the root tmp = reflectionFactory.newFieldAccessor(this, overrideFinalCheck); setFieldAccessor(tmp, overrideFinalCheck); } return tmp; } private FieldAccessor getFieldAccessor(boolean overrideFinalCheck) { return (overrideFinalCheck)? overrideFieldAccessor : fieldAccessor; }
Set field address
private void setFieldAccessor(FieldAccessor accessor, boolean overrideFinalCheck) { if (overrideFinalCheck) overrideFieldAccessor = accessor; else fieldAccessor = accessor; // Propagate up if (root != null) { root.setFieldAccessor(accessor, overrideFinalCheck); } }
Depending on annotation type
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { Objects.requireNonNull(annotationClass); return annotationClass.cast(declaredAnnotations().get(annotationClass)); }
Defines the type of annotation to get
public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) { Objects.requireNonNull(annotationClass); return AnnotationSupport.getDirectlyAndIndirectlyPresent(declaredAnnotations(), annotationClass); }
Return the comment ## 3 directly appearing on this element. Introduction to the source code of InvocationTargetException class
InvocationTargetException class
public Annotation[] getDeclaredAnnotations() { return AnnotationParser.toArray(declaredAnnotations()); }
Define annotation generics and annotation key value pairs
private transient Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
Generate generic annotation key value pairs
private synchronized Map<Class<? extends Annotation>, Annotation> declaredAnnotations() { if (declaredAnnotations == null) { Field root = this.root; if (root != null) { declaredAnnotations = root.declaredAnnotations(); } else { declaredAnnotations = AnnotationParser.parseAnnotations( annotations, sun.misc.SharedSecrets.getJavaLangAccess().getConstantPool(getDeclaringClass()), getDeclaringClass()); } } return declaredAnnotations; }
Call JVM to get annotation byte array
private native byte[] getTypeAnnotationBytes0();
Define the external interface class to get annotation type
public AnnotatedType getAnnotatedType() { return TypeAnnotationParser.buildAnnotatedType(getTypeAnnotationBytes0(), sun.misc.SharedSecrets.getJavaLangAccess(). getConstantPool(getDeclaringClass()), this, getDeclaringClass(), getGenericType(), TypeAnnotation.TypeAnnotationTarget.FIELD); }
3, InvocationTargetException class
The InvocationTargetException class inherits the ReflectiveOperationException class
public class InvocationTargetException extends ReflectiveOperationException { }
The universal serial ID is defined below
private static final long serialVersionUID = 4085088731926701167L;
Return to target
private Throwable target;
Construct an InvocationTargetException with null as the target exception.
protected InvocationTargetException() { super((Throwable)null);
Construct an InvocationTargetException with a target exception.
public InvocationTargetException(Throwable target) { super((Throwable)null); // Disallow initCause this.target = target; }
Construct an InvocationTargetException with a target exception and a detailed message
public InvocationTargetException(Throwable target, String s) { super(s, null); // Disallow initCause this.target = target; }
Gets the target exception thrown
public Throwable getTargetException() { return target; }
Returns the reason for this exception (the target exception thrown, which may be null)
public Throwable getCause() { return target; }