前言
- {@code方法}提供有关类或接口上单个方法的信息,以及对它们的访问。
- 反映的方法可以是类方法或实例方法(包括抽象方法)。
- {@code方法}允许在匹配实际参数以使用基础方法的形式参数调用时进行扩展转换,但是如果发生缩小转换,则会抛出{@code IllegalArgumentException}。
源码
package java.lang.reflect;
public final class Method extends Executable {
private Class<?> clazz;
private int slot;
// This is guaranteed to be interned by the VM in the 1.4
// reflection implementation
private String name;
private Class<?> returnType;
private Class<?>[] parameterTypes;
private Class<?>[] exceptionTypes;
private int modifiers;
// Generics and annotations support
private transient String signature;
// generic info repository; lazily initialized
private transient MethodRepository genericInfo;
private byte[] annotations;
private byte[] parameterAnnotations;
private byte[] annotationDefault;
private volatile MethodAccessor methodAccessor;
// For sharing of MethodAccessors. This branching structure is
// currently only two levels deep (i.e., one root Method and
// potentially many Method objects pointing to it.)
//
// If this branching structure would ever contain cycles, deadlocks can
// occur in annotation code.
private Method root;
// Generics infrastructure
private String getGenericSignature() {return signature;}
// Accessor for factory
private GenericsFactory getFactory() {
// create scope and factory
return CoreReflectionFactory.make(this, MethodScope.make(this));
}
// Accessor for generic info repository
@Override
MethodRepository getGenericInfo() {
// lazily initialize repository if necessary
if (genericInfo == null) {
// create and cache generic info repository
genericInfo = MethodRepository.make(getGenericSignature(),
getFactory());
}
return genericInfo; //return cached repository
}
/**
* ReflectAccess用于通过sun.reflect.LangReflectAccess从java.lang包中实例化Java代码中这些对象的程序包专用构造函数。
*/
Method(Class<?> declaringClass,
String name,
Class<?>[] parameterTypes,
Class<?> returnType,
Class<?>[] checkedExceptions,
int modifiers,
int slot,
String signature,
byte[] annotations,
byte[] parameterAnnotations,
byte[] annotationDefault) {
this.clazz = declaringClass;
this.name = name;
this.parameterTypes = parameterTypes;
this.returnType = returnType;
this.exceptionTypes = checkedExceptions;
this.modifiers = modifiers;
this.slot = slot;
this.signature = signature;
this.annotations = annotations;
this.parameterAnnotations = parameterAnnotations;
this.annotationDefault = annotationDefault;
}
/**
* Package-private routine (exposed to java.lang.Class via
* ReflectAccess) which returns a copy of this Method. The copys
* "root" field points to this Method.
*/
Method copy() {
// This routine enables sharing of MethodAccessor objects
// among Method objects which refer to the same underlying
// method in the VM. (All of this contortion is only necessary
// because of the "accessibility" bit in AccessibleObject,
// which implicitly requires that new java.lang.reflect
// objects be fabricated for each reflective call on Class
// objects.)
if (this.root != null)
throw new IllegalArgumentException("Can not copy a non-root Method");
Method res = new Method(clazz, name, parameterTypes, returnType,
exceptionTypes, modifiers, slot, signature,
annotations, parameterAnnotations, annotationDefault);
res.root = this;
// Might as well eagerly propagate this if already present
res.methodAccessor = methodAccessor;
return res;
}
/**
* Used by Excecutable for annotation sharing.
*/
@Override
Executable getRoot() {
return root;
}
@Override
boolean hasGenericInformation() {
return (getGenericSignature() != null);
}
@Override
byte[] getAnnotationBytes() {
return annotations;
}
/**
* {@inheritDoc}
*/
@Override
public Class<?> getDeclaringClass() {
return clazz;
}
/**
* Returns the name of the method represented by this {@code Method}
* object, as a {@code String}.
*/
@Override
public String getName() {
return name;
}
/**
* {@inheritDoc}
*/
@Override
public int getModifiers() {
return modifiers;
}
/**
* {@inheritDoc}
* @throws GenericSignatureFormatError {@inheritDoc}
* @since 1.5
*/
@Override
@SuppressWarnings({"rawtypes", "unchecked"})
public TypeVariable<Method>[] getTypeParameters() {
if (getGenericSignature() != null)
return (TypeVariable<Method>[])getGenericInfo().getTypeParameters();
else
return (TypeVariable<Method>[])new TypeVariable[0];
}
/**
* Returns a {@code Class} object that represents the formal return type
* of the method represented by this {@code Method} object.
*
* @return the return type for the method this object represents
*/
public Class<?> getReturnType() {
return returnType;
}
/**
* Returns a {@code Type} object that represents the formal return
* type of the method represented by this {@code Method} object.
*
* <p>If the return type is a parameterized type,
* the {@code Type} object returned must accurately reflect
* the actual type parameters used in the source code.
*
* <p>If the return type is a type variable or a parameterized type, it
* is created. Otherwise, it is resolved.
*
* @return a {@code Type} object that represents the formal return
* type of the underlying method
* @throws GenericSignatureFormatError
* if the generic method signature does not conform to the format
* specified in
* <cite>The Java™ Virtual Machine Specification</cite>
* @throws TypeNotPresentException if the underlying method's
* return type refers to a non-existent type declaration
* @throws MalformedParameterizedTypeException if the
* underlying method's return typed refers to a parameterized
* type that cannot be instantiated for any reason
* @since 1.5
*/
public Type getGenericReturnType() {
if (getGenericSignature() != null) {
return getGenericInfo().getReturnType();
} else { return getReturnType();}
}
/**
* {@inheritDoc}
*/
@Override
public Class<?>[] getParameterTypes() {
return parameterTypes.clone();
}
/**
* {@inheritDoc}
* @since 1.8
*/
public int getParameterCount() { return parameterTypes.length; }
/**
* {@inheritDoc}
* @throws GenericSignatureFormatError {@inheritDoc}
* @throws TypeNotPresentException {@inheritDoc}
* @throws MalformedParameterizedTypeException {@inheritDoc}
* @since 1.5
*/
@Override
public Type[] getGenericParameterTypes() {
return super.getGenericParameterTypes();
}
/**
* {@inheritDoc}
*/
@Override
public Class<?>[] getExceptionTypes() {
return exceptionTypes.clone();
}
/**
* {@inheritDoc}
* @throws GenericSignatureFormatError {@inheritDoc}
* @throws TypeNotPresentException {@inheritDoc}
* @throws MalformedParameterizedTypeException {@inheritDoc}
* @since 1.5
*/
@Override
public Type[] getGenericExceptionTypes() {
return super.getGenericExceptionTypes();
}
/**
* 将此{@code方法}与指定对象进行比较。如果对象相同,则返回true。
* 如果两个{@code方法}由同一类声明,并且具有相同的名称,形式参数类型和返回类型,则它们是相同的。
*/
public boolean equals(Object obj) {
if (obj != null && obj instanceof Method) {
Method other = (Method)obj;
if ((getDeclaringClass() == other.getDeclaringClass())
&& (getName() == other.getName())) {
if (!returnType.equals(other.getReturnType()))
return false;
return equalParamTypes(parameterTypes, other.parameterTypes);
}
}
return false;
}
public int hashCode() {
return getDeclaringClass().getName().hashCode() ^ getName().hashCode();
}
public String toString() {
return sharedToString(Modifier.methodModifiers(),
isDefault(),
parameterTypes,
exceptionTypes);
}
@Override
void specificToStringHeader(StringBuilder sb) {
sb.append(getReturnType().getTypeName()).append(' ');
sb.append(getDeclaringClass().getTypeName()).append('.');
sb.append(getName());
}
@Override
public String toGenericString() {
return sharedToGenericString(Modifier.methodModifiers(), isDefault());
}
@Override
void specificToGenericStringHeader(StringBuilder sb) {
Type genRetType = getGenericReturnType();
sb.append(genRetType.getTypeName()).append(' ');
sb.append(getDeclaringClass().getTypeName()).append('.');
sb.append(getName());
}
/**
* 在具有指定参数的指定对象上调用此{@code Method}对象表示的基础方法。
* 各个参数将自动解包以匹配原始形式参数,并且原始参数和引用参数都需要进行方法调用转换。
* 如果基础方法是静态的,则忽略指定的{@code obj}参数。
* 它可以为空。
* 如果基础方法所需的形式参数数量为0,则提供的{@code args}数组的长度可以为0或为null。
* 如果基础方法是实例方法,则使用《 Java语言规范,第二版》第15.12.4.4节中所述的动态方法查找来调用该方法。
* 特别是,将发生基于目标对象的运行时类型的覆盖。
* 如果基础方法是静态的,则声明该方法的类(如果尚未初始化)将被初始化。
* 如果该方法正常完成,则它将返回的值返回给invoke的调用者;如果值具有原始类型,则首先将其适当包装在一个对象中。
* 但是,如果该值具有原始类型的数组的类型,则该数组的元素不会包装在对象中;而是将其包装在对象中。
* 换句话说,将返回原始类型的数组。
* 如果基础方法的返回类型为void,则调用返回null。
*/
@CallerSensitive
public Object invoke(Object obj, Object... args)
throws IllegalAccessException, IllegalArgumentException,
InvocationTargetException
{
if (!override) {
if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
Class<?> caller = Reflection.getCallerClass();
checkAccess(caller, clazz, obj, modifiers);
}
}
MethodAccessor ma = methodAccessor; // read volatile
if (ma == null) {
ma = acquireMethodAccessor();
}
return ma.invoke(obj, args);
}
/**
* 如果此方法是桥接方法,则返回{@code true};否则返回false。否则返回{@code false}。
*/
public boolean isBridge() {
return (getModifiers() & Modifier.BRIDGE) != 0;
}
/**
* {@inheritDoc}
* @since 1.5
*/
@Override
public boolean isVarArgs() {
return super.isVarArgs();
}
/**
* {@inheritDoc}
* @jls 13.1 The Form of a Binary
* @since 1.5
*/
@Override
public boolean isSynthetic() {
return super.isSynthetic();
}
/**
* 如果此方法是默认方法,则返回{@code true};否则,返回false。
* 否则返回{@code false}。默认方法是公共的非抽象实例方法,即以接口类型声明的带有主体的非静态方法。
*/
public boolean isDefault() {
// Default methods are public non-abstract instance methods
// declared in an interface.
return ((getModifiers() & (Modifier.ABSTRACT | Modifier.PUBLIC | Modifier.STATIC)) ==
Modifier.PUBLIC) && getDeclaringClass().isInterface();
}
// NOTE that there is no synchronization used here. It is correct
// (though not efficient) to generate more than one MethodAccessor
// for a given Method. However, avoiding synchronization will
// probably make the implementation more scalable.
private MethodAccessor acquireMethodAccessor() {
// First check to see if one has been created yet, and take it
// if so
MethodAccessor tmp = null;
if (root != null) tmp = root.getMethodAccessor();
if (tmp != null) {
methodAccessor = tmp;
} else {
// Otherwise fabricate one and propagate it up to the root
tmp = reflectionFactory.newMethodAccessor(this);
setMethodAccessor(tmp);
}
return tmp;
}
// Returns MethodAccessor for this Method object, not looking up
// the chain to the root
MethodAccessor getMethodAccessor() {
return methodAccessor;
}
// Sets the MethodAccessor for this Method object and
// (recursively) its root
void setMethodAccessor(MethodAccessor accessor) {
methodAccessor = accessor;
// Propagate up
if (root != null) {
root.setMethodAccessor(accessor);
}
}
/**
* 返回此{@code方法}实例表示的注释成员的默认值。
* 如果成员属于原始类型,则返回相应包装器类型的实例。
* 如果没有默认值与该成员相关联,或者方法实例不表示注释类型的已声明成员,则返回null。
*/
public Object getDefaultValue() {
if (annotationDefault == null)
return null;
Class<?> memberType = AnnotationType.invocationHandlerReturnType(
getReturnType());
Object result = AnnotationParser.parseMemberValue(
memberType, ByteBuffer.wrap(annotationDefault),
sun.misc.SharedSecrets.getJavaLangAccess().
getConstantPool(getDeclaringClass()),
getDeclaringClass());
if (result instanceof sun.reflect.annotation.ExceptionProxy)
throw new AnnotationFormatError("Invalid default: " + this);
return result;
}
/**
* {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @since 1.5
*/
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
return super.getAnnotation(annotationClass);
}
/**
* {@inheritDoc}
* @since 1.5
*/
public Annotation[] getDeclaredAnnotations() {
return super.getDeclaredAnnotations();
}
/**
* {@inheritDoc}
* @since 1.5
*/
@Override
public Annotation[][] getParameterAnnotations() {
return sharedGetParameterAnnotations(parameterTypes, parameterAnnotations);
}
/**
* {@inheritDoc}
* @since 1.8
*/
@Override
public AnnotatedType getAnnotatedReturnType() {
return getAnnotatedReturnType0(getGenericReturnType());
}
@Override
void handleParameterNumberMismatch(int resultLength, int numParameters) {
throw new AnnotationFormatError("Parameter annotations don't match number of parameters");
}
}