Java反射

来源:互联网 发布:horner算法 编辑:程序博客网 时间:2024/05/29 13:12

1,什么是Java反射机制?
     JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法;这种动态获取的以及动态调用对象的方法的功能称为Java的反射机制。

2,反射的使用方法:

假设有个类Person:

package com.gch;

class Person{}

获取类对象的方法:

    1.1   Person.class;

    1.2   Class.forName("com.gch.Person");

    1.3   new Person().getClass();

区别是方法一不执行静态块和动态构造块,方法二执行静态块、不执行动态构造块,方法三需要创建对象,静态块和动态构造块均会执行;

获取类对象之后,便可以获取class对象的属性、方法、构造函数。

Modifier and TypeMethod and Description<U> Class<? extends U>asSubclass(Class<U> clazz)

Casts this Class object to represent a subclass of the class represented by the specified class object.
Tcast(Object obj)
Casts an object to the class or interface represented by this Class object.
booleandesiredAssertionStatus()
Returns the assertion status that would be assigned to this class if it were to be initialized at the time this method is invoked.
static Class<?>forName(String className)
Returns the Class object associated with the class or interface with the given string name.
static Class<?>forName(String name, boolean initialize, ClassLoader loader)
Returns the Class object associated with the class or interface with the given string name, using the given class loader.
<A extends Annotation> 
A
getAnnotation(Class<A> annotationClass)
Returns this element's annotation for the specified type if such an annotation is present, else null.
Annotation[]getAnnotations()
Returns all annotations present on this element.
StringgetCanonicalName()
Returns the canonical name of the underlying class as defined by the Java Language Specification.
Class<?>[]getClasses()
Returns an array containing Class objects representing all the public classes and interfaces that are members of the class represented by this Class object.
ClassLoadergetClassLoader()
Returns the class loader for the class.
Class<?>getComponentType()
Returns the Class representing the component type of an array.
Constructor<T>getConstructor(Class<?>... parameterTypes)
Returns a Constructor object that reflects the specified public constructor of the class represented by this Class object.
Constructor<?>[]getConstructors()
Returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object.
Annotation[]getDeclaredAnnotations()
Returns all annotations that are directly present on this element.
Class<?>[]getDeclaredClasses()
Returns an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object.
Constructor<T>getDeclaredConstructor(Class<?>... parameterTypes)
Returns a Constructor object that reflects the specified constructor of the class or interface represented by this Class object.
Constructor<?>[]getDeclaredConstructors()
Returns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object.
FieldgetDeclaredField(String name)
Returns a Field object that reflects the specified declared field of the class or interface represented by this Class object.
Field[]getDeclaredFields()   获得全部属性
Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object.
MethodgetDeclaredMethod(String name, Class<?>... parameterTypes)
Returns a Method object that reflects the specified declared method of the class or interface represented by this Class object.
Method[]getDeclaredMethods()
Returns an array of Method objects reflecting all the methods declared by the class or interface represented by this Class object.
Class<?>getDeclaringClass()
If the class or interface represented by this Class object is a member of another class, returns the Class object representing the class in which it was declared.
Class<?>getEnclosingClass()
Returns the immediately enclosing class of the underlying class.
Constructor<?>getEnclosingConstructor()
If this Class object represents a local or anonymous class within a constructor, returns a Constructor object representing the immediately enclosing constructor of the underlying class.
MethodgetEnclosingMethod()
If this Class object represents a local or anonymous class within a method, returns a Method object representing the immediately enclosing method of the underlying class.
T[]getEnumConstants()
Returns the elements of this enum class or null if this Class object does not represent an enum type.
FieldgetField(String name)
Returns a Field object that reflects the specified public member field of the class or interface represented by this Class object.
Field[]getFields()
Returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object.
Type[]getGenericInterfaces()
Returns the Types representing the interfaces directly implemented by the class or interface represented by this object.
TypegetGenericSuperclass()
Returns the Type representing the direct superclass of the entity (class, interface, primitive type or void) represented by this Class.
Class<?>[]getInterfaces()
Determines the interfaces implemented by the class or interface represented by this object.
MethodgetMethod(String name, Class<?>... parameterTypes)
Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object.
Method[]getMethods()
Returns an array containing Method objects reflecting all the public member methods of the class or interface represented by this Class object, including those declared by the class or interface and those inherited from superclasses and superinterfaces.
intgetModifiers()
Returns the Java language modifiers for this class or interface, encoded in an integer.
StringgetName()
Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String.
PackagegetPackage()
Gets the package for this class.
ProtectionDomaingetProtectionDomain()
Returns the ProtectionDomain of this class.
URLgetResource(String name)
Finds a resource with a given name.
InputStreamgetResourceAsStream(String name)
Finds a resource with a given name.
Object[]getSigners()
Gets the signers of this class.
StringgetSimpleName()
Returns the simple name of the underlying class as given in the source code.
Class<? super T>getSuperclass()
Returns the Class representing the superclass of the entity (class, interface, primitive type or void) represented by this Class.
TypeVariable<Class<T>>[]getTypeParameters()
Returns an array of TypeVariable objects that represent the type variables declared by the generic declaration represented by thisGenericDeclaration object, in declaration order.
booleanisAnnotation()
Returns true if this Class object represents an annotation type.
booleanisAnnotationPresent(Class<? extends Annotation> annotationClass)
Returns true if an annotation for the specified type is present on this element, else false.
booleanisAnonymousClass()
Returns true if and only if the underlying class is an anonymous class.
booleanisArray()
Determines if this Class object represents an array class.
booleanisAssignableFrom(Class<?> cls)
Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter.
booleanisEnum()
Returns true if and only if this class was declared as an enum in the source code.
booleanisInstance(Object obj)
Determines if the specified Object is assignment-compatible with the object represented by this Class.
booleanisInterface()
Determines if the specified Class object represents an interface type.
booleanisLocalClass()
Returns true if and only if the underlying class is a local class.
booleanisMemberClass()
Returns true if and only if the underlying class is a member class.
booleanisPrimitive()
Determines if the specified Class object represents a primitive type.
booleanisSynthetic()
Returns true if this class is a synthetic class; returns false otherwise.
TnewInstance()  获得一个实例
Creates a new instance of the class represented by this Class object.
StringtoString()
Converts the object to a string.
然后获得class对象后,可以使用newInstance()创建对象。

3,反射和new的区别:

     1,new出来的对象我们无法访问其中的私有属性,但是通过反射出来的对象我们可以通过setAccessible()方法来访问其中的私有属性。

     2,在使用new创建一个对象实例的时候必须知道类名,但是通过反射创建对象有时候不需要知道类名也可以。因为动态代理不需要类名便可以动态的调用方法。

4,反射的使用场景:

    逆向代码 ,例如反编译

    与注解相结合的框架 例如Retrofit

    单纯的反射机制应用框架 例如EventBus 2.x

    动态生成类框架 例如Gson


原创粉丝点击