前言
java中的反射的底层实现是,在jvm的Classloader获得全限定名来创建Class。
正文
获得反射类型对象
1.类名.class
不执行静态快
2..Class<?> getClass()
Returns the runtime class of this Object.
3.static Class<?> forName(String className)
Returns the Class object associated with the class or interface with the given string name.会执行静态快
通过Class对象获得当前类的构造器、属性和方法
1.Constructor<?>[] getDeclaredConstructors()
Returns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object.获得当前类声明的所有构造器
2.Constructor<?>[] getConstructors()
Returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object.获得当前类公共的构造器
3.Field[] getDeclaredFields()
Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object. 获得当前类自身声明的属性
4.Field[] getFields()
Returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object. 获得当前类继承的以及自身定义的公共属性
5.Method[] getDeclaredMethods()
Returns an array of Method objects reflecting all the methods declared by the class or interface represented by this Class object. 获得当前类自身声明定义的方法
6.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.获得当前类继承的以及自身定义的公共的方法
注:
在类的普通方法中加同步锁,所在的是当前类的对象;对静态方法加同步锁,所在的是当前类的Class对象
7.获得所有声明的方法的代码示例
通过反射创建当前类的对象
1.将目标类加载进内存,如果当前内存中存在目标类则不加载
2.采用延迟加载模式,不会再程序代码执行的时候马上加载目标类,而是在目标类被使用的时候在对他进行加载。
3.T newInstance()
Creates a new instance of the class represented by this Class object.
Throws:
IllegalAccessException - if the class or its nullary constructor is not accessible.
InstantiationException - if this Class represents an abstract class, an interface, an array class, a primitive type, or void; or if the class has no nullary constructor; or if the instantiation fails for some other reason.
ExceptionInInitializerError - if the initialization provoked by this method fails.
SecurityException - If a security manager, s, is present and any of the following conditions is met: ?invocation of s.checkMemberAccess(this, Member.PUBLIC) denies creation of new instances of this class ?the caller‘s class loader is not the same as or an ancestor
of the class loader for the current class and invocation of s.checkPackageAccess() denies access to the package of this class
通过反射访问属性和执行方法
1.FieldAPI
Methods inherited from class java.lang.reflect.AccessibleObject
1)public void setAccessible(boolean flag)
throws SecurityExceptionSet the accessible flag for this object to the indicated boolean value. A value of true indicates that the reflected object should suppress Java language access checking when it is used. A value of false indicates
that the reflected object should enforce Java language access checks.设置当前属性可访问
2.代码示例