关键方法
getFields()
返回一个数组,包含了该Class对象表示的类或者接口 所有可访问的public字段。
@CallerSensitive
public Field[] getFields() throws SecurityException {
checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
return copyFields(privateGetPublicFields(null));
}
注释的详细解释
* Returns an array containing {@code Field} objects reflecting all
* the accessible public fields of the class or interface represented by
* this {@code Class} object.
*
* If this {@code Class} object represents a class, then this method
* returns the public fields of the class and of all its superclasses.
*
* If this {@code Class} object represents an interface, then this
* method returns the fields of the interface and of all its
* superinterfaces.
*
* If this {@code Class} object represents an array type, a primitive
* type, or void, then this method returns an array of length 0.
*
* The elements in the returned array are not sorted and are not in any
* particular order.
getMethods()
返回一个数组,包含了该Class对象表示的类或者接口 所有可访问的公共方法。包括继承的。
@CallerSensitive
public Method[] getMethods() throws SecurityException {
checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
return copyMethods(privateGetPublicMethods());
}
注释的详细解释
* Returns an array containing {@code Method} objects reflecting all the
* public methods of the class or interface represented by this {@code
* Class} object, including those declared by the class or interface and
* those inherited from superclasses and superinterfaces.
*
* If this {@code Class} object represents a type that has multiple
* public methods with the same name and parameter types, but different
* return types, then the returned array has a {@code Method} object for
* each such method.
*
* If this {@code Class} object represents a type with a class
* initialization method {@code <clinit>}, then the returned array does
* <em>not</em> have a corresponding {@code Method} object.
getConstructors()
返回该对象表示的接口或者类 的所有构造器。如果没有公共的构造器,或者这个类是一个数组类,或者类对应了一个基础类型或者void,那么返回长度为0的数组。
@CallerSensitive
public Constructor<?>[] getConstructors() throws SecurityException {
checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
return copyConstructors(privateGetDeclaredConstructors(true));
}
注释的详细解释
* Returns an array containing {@code Constructor} objects reflecting
* all the public constructors of the class represented by this
* {@code Class} object. An array of length 0 is returned if the
* class has no public constructors, or if the class is an array class, or
* if the class reflects a primitive type or void.
getField(String name)
Returns a {@code Field} object that reflects the specified public member field of the class or interface represented by this {@code Class} object.
@CallerSensitive
public Field getField(String name)
throws NoSuchFieldException, SecurityException {
checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
Field field = getField0(name);
if (field == null) {
throw new NoSuchFieldException(name);
}
return field;
}
getDeclaredClasses()
返回一组 Class 对象的数组,这些对象反映了声明为此 Class 对象表示的类的成员的所有类和接口。这包括公共,受保护的,默认(程序包)访问以及由该类声明的私有类和接口,但不包括继承的类和接口。如果类未声明任何类或接口作为成员,或者此 Class 对象表示原始类型,数组类或void,则此方法返回长度为0的数组。
@CallerSensitive
public Class<?>[] getDeclaredClasses() throws SecurityException {
checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), false);
return getDeclaredClasses0();
}
注释的详细解释
* Returns an array of {@code Class} objects reflecting all the
* classes and interfaces declared as members of the class represented by
* this {@code Class} object. This includes public, protected, default
* (package) access, and private classes and interfaces declared by the
* class, but excludes inherited classes and interfaces. This method
* returns an array of length 0 if the class declares no classes or
* interfaces as members, or if this {@code Class} object represents a
* primitive type, an array class, or void.
getDeclaredFields()
返回一组 Field 对象的数组,这些对象反映了此 Class 对象表示的类或接口声明的所有字段。这包括公共,受保护,默认(程序包)访问和私有字段,但不包括继承的字段。如果此 Class 对象表示没有声明字段的类或接口,则此方法返回长度为0的数组。如果此 Class对象表示数组类型,原始类型或void,则此类型方法返回长度为0的数组。返回的数组中的元素未排序,并且没有任何特定顺序。
public Field[] getDeclaredFields() throws SecurityException {
checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
return copyFields(privateGetDeclaredFields(false));
}
getDeclaredMethods()
返回一个包含Method对象的数组,这些对象反映此Class对象表示的类或接口的所有已声明方法, 包括公共,受保护,默认(程序包)访问和私有方法,但不包括继承的方法。 如果此Class对象表示具有多个声明的方法的类型,这些方法具有相同的名称和参数类型,但返回类型不同, 则对于每个此类方法,返回的数组都有一个Method对象。 如果此Class对象表示具有类初始化方法{@code}的类型,则返回的数组没有相应的Method对象。 如果此Class对象表示没有声明方法的类或接口,则返回的数组长度为0。 如果此Class对象表示数组类型,原始类型或void,则返回数组长度为0。 返回数组中的元素未排序,并且没有任何特定顺序。
@CallerSensitive
public Method[] getDeclaredMethods() throws SecurityException {
checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
return copyMethods(privateGetDeclaredMethods(false));
}
getDeclaredConstructors()
返回一组 Constructor 对象的数组,这些数组反映此Class对象表示的类声明的所有构造函数。 包括publi, protected, default和 private 构造函数。返回的数组中的元素未排序,并且没有任何特定顺序。 如果该类具有默认构造函数,则它将包含在返回的数组中。如果此Class对象表示接口,原始类型,数组类或void,则此方法返回长度为0的数组。
@CallerSensitive
public Constructor<?>[] getDeclaredConstructors() throws SecurityException {
checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
return copyConstructors(privateGetDeclaredConstructors(false));
}
getResourceAsStream()
public InputStream getResourceAsStream(String name) {
name = resolveName(name);
ClassLoader cl = getClassLoader0();
if (cl==null) {
// A system class.
return ClassLoader.getSystemResourceAsStream(name);
}
return cl.getResourceAsStream(name);
}
注释的详细解释
* Finds a resource with a given name. The rules for searching resources
* associated with a given class are implemented by the defining
* {@linkplain ClassLoader class loader} of the class. This method
* delegates to this object's class loader. If this object was loaded by
* the bootstrap class loader, the method delegates to {@link
* ClassLoader#getSystemResourceAsStream}.
getPrimitiveClass(String name)
static native Class<?> getPrimitiveClass(String name);
isEnum()
当且仅当该类在代码中被声明为一个枚举类型,返回true
public boolean isEnum() {
// An enum must both directly extend java.lang.Enum and have
// the ENUM bit set; classes for specialized enum constants
// don't do the former.
return (this.getModifiers() & ENUM) != 0 &&
this.getSuperclass() == java.lang.Enum.class;
}
getEnumConstants()
如果该类是一个枚举类,返回枚举类的元素数组。 如果不是枚举类,返回false
public T[] getEnumConstants()
cast()
将对象强制转换为此类对象表示的类或接口。
@SuppressWarnings("unchecked")
public T cast(Object obj)
asSubclass()
强制转化为子类,如果不能转成功,会抛出ClassCastException异常。
public <U> Class<? extends U> asSubclass(Class<U> clazz) {
if (clazz.isAssignableFrom(this))
return (Class<? extends U>) this;
else
throw new ClassCastException(this.toString());
}
getAnnotation()
获取注解
public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
Objects.requireNonNull(annotationClass);
return (A) annotationData().annotations.get(annotationClass);
}
isAnnotationPresent()
如果指定类型的注释存在于此元素上, 否则返回false。这种方法的设计主要是为了方便访问标记注释.
@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
return GenericDeclaration.super.isAnnotationPresent(annotationClass);
}
getAnnotationsByType()
根据类型获取注解
@Override
public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationClass) {
Objects.requireNonNull(annotationClass);
AnnotationData annotationData = annotationData();
return AnnotationSupport.getAssociatedAnnotations(annotationData.declaredAnnotations,
this,
annotationClass);
}
getAnnotations()
获取所有注解
public Annotation[] getAnnotations() {
return AnnotationParser.toArray(annotationData().annotations);
}
getDeclaredAnnotation()
返回直接存在于此对象的所有注释。与此接口中的其他方法不同,该方法将忽略继承的注释。
@Override
@SuppressWarnings("unchecked")
public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationClass)
getDeclaredAnnotationsByType()
根据类型获取所有直接存在于此对象上的所有注解。
public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationClass) {
Objects.requireNonNull(annotationClass);
return (A) annotationData().declaredAnnotations.get(annotationClass);
}
getDeclaredAnnotations()
获取所有直接作用在此对象上的注解
public Annotation[] getDeclaredAnnotations() {
return AnnotationParser.toArray(annotationData().declaredAnnotations);
}
getAnnotatedSuperclas()
返回父类的注解的AnnotatedType
public AnnotatedType getAnnotatedSuperclass() {
if (this == Object.class ||
isInterface() ||
isArray() ||
isPrimitive() ||
this == Void.TYPE) {
return null;
}
return TypeAnnotationParser.buildAnnotatedSuperclass(getRawTypeAnnotations(), getConstantPool(), this);
}
getAnnotatedInterfaces()
得到该类的所有注解的类类型数组。
public AnnotatedType[] getAnnotatedInterfaces() {
return TypeAnnotationParser.buildAnnotatedInterfaces(getRawTypeAnnotations(), getConstantPool(), this);
}