Class类
反射的使用
package com. ReflectionTest. common;
import org. junit. Test;
import java. lang. annotation. ElementType;
import java. lang. reflect. Constructor;
import java. lang. reflect. Field;
import java. lang. reflect. InvocationTargetException;
import java. lang. reflect. Method;
public class ReflectionTest {
@Test
public void test ( ) {
Person p1 = new Person ( "name" , 12 ) ;
p1. age = 10 ;
System. out. println ( p1. toString ( ) ) ;
p1. show ( ) ;
}
@Test
public void test1 ( )
throws IllegalAccessException, InvocationTargetException, InstantiationException,
NoSuchMethodException, NoSuchFieldException {
Class< Person> clazz = Person. class ;
Constructor< Person> constructor = clazz. getConstructor ( String. class , int. class ) ;
Person person = constructor. newInstance ( "chen" , 25 ) ;
System. out. println ( person) ;
Field age = clazz. getDeclaredField ( "age" ) ;
age. set ( person, 27 ) ;
System. out. println ( person. toString ( ) ) ;
Method show = clazz. getDeclaredMethod ( "show" ) ;
show. invoke ( person) ;
System. out. println ( "**************" ) ;
Constructor< Person> declaredConstructor = clazz. getDeclaredConstructor ( String. class ) ;
declaredConstructor. setAccessible ( true ) ;
Person person1 = declaredConstructor. newInstance ( "Jerry" ) ;
System. out. println ( person1) ;
Field name = clazz. getDeclaredField ( "name" ) ;
name. setAccessible ( true ) ;
name. set ( person1, "cheng" ) ;
System. out. println ( person1) ;
Method showNation = clazz. getDeclaredMethod ( "showNation" , String. class ) ;
showNation. setAccessible ( true ) ;
String nation = ( String) showNation. invoke ( person1, "中国" ) ;
System. out. println ( nation) ;
}
@Test
public void test2 ( ) throws ClassNotFoundException {
Class< Person> aClass = Person. class ;
System. out. println ( aClass) ;
Person p1 = new Person ( ) ;
Class< ? extends Person > aClass1 = p1. getClass ( ) ;
System. out. println ( aClass1) ;
Class< ? > aClass2 = Class. forName ( "com.ReflectionTest.common.Person" ) ;
System. out. println ( aClass2) ;
System. out. println ( aClass == aClass1) ;
System. out. println ( aClass == aClass2) ;
ClassLoader classLoader = ReflectionTest. class . getClassLoader ( ) ;
Class< ? > aClass3 = classLoader. loadClass ( "com.ReflectionTest.common.Person" ) ;
System. out. println ( aClass3) ;
System. out. println ( aClass == aClass3) ;
}
@Test
public void test3 ( ) {
Class c1 = Object. class ;
Class c2 = Comparable. class ;
Class c3 = String[ ] . class ;
Class c4 = int[ ] [ ] . class ;
Class c5 = ElementType. class ;
Class c6 = Override. class ;
Class c7 = int. class ;
Class c8 = void . class ;
Class c9 = Class. class ;
int[ ] a = new int [ 10 ] ;
int[ ] b = new int [ 100 ] ;
Class c10 = a. getClass ( ) ;
Class c11 = b. getClass ( ) ;
System. out. println ( c10 == c11) ;
}
}
创建运行时类的对象
创建运行时类的对象
package com. ReflectionTest. common;
import org. junit. Test;
import java. util. Random;
public class NewInstanceTest {
@Test
public void test ( ) throws IllegalAccessException, InstantiationException {
Class< Person> personClass = Person. class ;
Person person = personClass. newInstance ( ) ;
System. out. println ( person) ;
}
@Test
public void test1 ( ) {
int num = new Random ( ) . nextInt ( 3 ) ;
String classPath = "" ;
switch ( num) {
case 0 :
classPath = "java.util.Date" ;
break ;
case 1 :
classPath = "java.lang.Object" ;
break ;
default :
classPath = "com.ReflectionTest.common.Person" ;
break ;
}
try {
Object instance = getInstance ( classPath) ;
System. out. println ( instance) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
public Object getInstance ( String classPath) throws Exception {
Class< ? > aClass = Class. forName ( classPath) ;
return aClass. newInstance ( ) ;
}
}
获取运行时类的完整结构
获取当前运行时类的属性结构
package com. ReflectionTest. ClassTest1;
import com. ReflectionTest. ClassTest. Person;
import org. junit. Test;
import java. lang. reflect. Field;
import java. lang. reflect. Modifier;
public class FieldTest {
@Test
public void test ( ) {
Class< Person> personClass = Person. class ;
Field[ ] fields = personClass. getFields ( ) ;
for ( Field f : fields) {
System. out. println ( f) ;
}
Field[ ] declaredFields = personClass. getDeclaredFields ( ) ;
for ( Field f : declaredFields) {
System. out. println ( f) ;
}
}
@Test
public void test1 ( ) {
Class< Person> personClass = Person. class ;
Field[ ] declaredFields = personClass. getDeclaredFields ( ) ;
for ( Field f : declaredFields) {
int modifiers = f. getModifiers ( ) ;
System. out. print ( Modifier. toString ( modifiers) + "\t" ) ;
Class< ? > type = f. getType ( ) ;
System. out. print ( type. getName ( ) + "\t" ) ;
String fName = f. getName ( ) ;
System. out. print ( fName) ;
System. out. println ( ) ;
}
}
}
获取当前运行时类的方法
package com. ReflectionTest. ClassTest1;
import com. ReflectionTest. ClassTest. Person;
import org. junit. Test;
import java. lang. annotation. Annotation;
import java. lang. reflect. Method;
import java. lang. reflect. Modifier;
public class MethodTest {
@Test
public void test ( ) {
Class< Person> personClass = Person. class ;
Method[ ] methods = personClass. getMethods ( ) ;
for ( Method m : methods) {
System. out. println ( m) ;
}
System. out. println ( ) ;
Method[ ] declaredMethods = personClass. getDeclaredMethods ( ) ;
for ( Method m : declaredMethods) {
System. out. println ( m) ;
}
System. out. println ( ) ;
}
@Test
public void test1 ( ) {
Class< Person> personClass = Person. class ;
Method[ ] declaredMethods = personClass. getDeclaredMethods ( ) ;
for ( Method m : declaredMethods) {
Annotation[ ] annotations = m. getAnnotations ( ) ;
for ( Annotation a : annotations) {
System. out. println ( a) ;
}
System. out. print ( Modifier. toString ( m. getModifiers ( ) ) + "\t" ) ;
Class< ? > returnType = m. getReturnType ( ) ;
System. out. print ( returnType. getName ( ) + "\t" ) ;
System. out. print ( m. getName ( ) + "\t" ) ;
System. out. print ( "(" ) ;
Class< ? > [ ] parameterTypes = m. getParameterTypes ( ) ;
if ( ! ( parameterTypes == null && parameterTypes. length == 0 ) ) {
for ( int i = 0 ; i < parameterTypes. length; i++ ) {
if ( i == parameterTypes. length - 1 ) {
System. out. print ( parameterTypes[ i] . getName ( ) + " args_" + i) ;
break ;
}
System. out. print ( parameterTypes[ i] . getName ( ) + " args_" + i + "," ) ;
}
}
System. out. print ( ")" ) ;
Class< ? > [ ] exceptionTypes = m. getExceptionTypes ( ) ;
if ( exceptionTypes. length > 0 ) {
System. out. print ( " throws " ) ;
for ( int i = 0 ; i < exceptionTypes. length; i++ ) {
if ( i == parameterTypes. length - 1 ) {
System. out. print ( exceptionTypes[ i] . getName ( ) ) ;
break ;
}
System. out. print ( exceptionTypes[ i] . getName ( ) + "," ) ;
}
}
System. out. println ( ) ;
}
}
}
输出
public int compareTo ( java. lang. String args_0)
public volatile int compareTo ( java. lang. Object args_0)
public void info ( )
@com. ReflectionTest. ClassTest. MyAnnotation ( value= nation)
private java. lang. String show ( java. lang. String args_0)
public java. lang. String display ( java. lang. String args_0, int args_1) throws java. lang. NullPointerException, java. lang. ClassCastException
Process finished with exit code 0
获取运行时类的构造器和父类
package com. ReflectionTest. ClassTest1;
import com. ReflectionTest. ClassTest. Person;
import org. junit. Test;
import java. lang. reflect. Constructor;
import java. lang. reflect. Type;
public class OtherTest {
@Test
public void test ( ) {
Class< Person> personClass = Person. class ;
Constructor< ? > [ ] constructors = personClass. getConstructors ( ) ;
for ( Constructor c : constructors) {
System. out. println ( c) ;
}
System. out. println ( ) ;
Constructor< ? > [ ] declaredConstructors = personClass. getDeclaredConstructors ( ) ;
for ( Constructor c : declaredConstructors) {
System. out. println ( c) ;
}
}
@Test
public void test1 ( ) {
Class< Person> personClass = Person. class ;
Class< ? super Person> superclass = personClass. getSuperclass ( ) ;
System. out. println ( superclass) ;
}
@Test
public void test2 ( ) {
Class< Person> personClass = Person. class ;
Type genericSuperclass = personClass. getGenericSuperclass ( ) ;
System. out. println ( genericSuperclass) ;
}
}
调用运行时类的指定结构
调用运行时类的指定结构
package com. ReflectionTest. ClassTest1;
import com. ReflectionTest. ClassTest. Person;
import org. junit. Test;
import java. lang. reflect. Field;
import java. lang. reflect. Method;
public class ReflectionTest {
@Test
public void test ( ) throws Exception {
Class< Person> personClass = Person. class ;
Person person = personClass. newInstance ( ) ;
Field id = personClass. getField ( "id" ) ;
id. set ( person, 12345 ) ;
int personId = ( int) id. get ( person) ;
System. out. println ( personId) ;
}
@Test
public void testField ( ) throws Exception {
Class< Person> personClass = Person. class ;
Person person = personClass. newInstance ( ) ;
Field name = personClass. getDeclaredField ( "name" ) ;
name. setAccessible ( true ) ;
name. set ( person, "chen" ) ;
Object o = name. get ( person) ;
System. out. println ( o) ;
}
@Test
public void testMethod ( ) throws Exception {
Class< Person> personClass = Person. class ;
Person person = personClass. newInstance ( ) ;
Method show = personClass. getDeclaredMethod ( "show" , String. class ) ;
show. setAccessible ( true ) ;
Object invoke = show. invoke ( person, "CHN" ) ;
System. out. println ( invoke) ;
System. out. println ( "********如何调用静态方法********" ) ;
Method showDesc = personClass. getDeclaredMethod ( "showDesc" ) ;
showDesc. setAccessible ( true ) ;
showDesc. invoke ( person) ;
}
}
反射的应用:动态代理
动态代理的举例
package com. szu. java;
import java. lang. reflect. InvocationHandler;
import java. lang. reflect. Method;
import java. lang. reflect. Proxy;
interface Human {
String getBelief ( ) ;
void eat ( String food) ;
}
class SuperMan implements Human {
@Override
public String getBelief ( ) {
return "I believe I can fly!" ;
}
@Override
public void eat ( String food) {
System. out. println ( "我喜欢吃" + food) ;
}
}
class ProxyFactory {
public static Object getProxyInstance ( Object obj) {
MyInvocationHandler handler = new MyInvocationHandler ( ) ;
handler. bind ( obj) ;
return Proxy. newProxyInstance (
obj. getClass ( ) . getClassLoader ( ) , obj. getClass ( ) . getInterfaces ( ) , handler) ;
}
}
class MyInvocationHandler implements InvocationHandler {
private Object obj;
public void bind ( Object obj) {
this . obj = obj;
}
@Override
public Object invoke ( Object proxy, Method method, Object[ ] args) throws Throwable {
Object returnValue = method. invoke ( obj, args) ;
return returnValue;
}
}
public class ProxyTest {
public static void main ( String[ ] args) {
SuperMan superMan = new SuperMan ( ) ;
Human proxyInstance = ( Human) ProxyFactory. getProxyInstance ( superMan) ;
proxyInstance. getBelief ( ) ;
proxyInstance. eat ( "面包" ) ;
System. out. println ( "********动态性***********" ) ;
NikeClothFactory nikeClothFactory = new NikeClothFactory ( ) ;
ClothFactory proxyClothFactory = ( ClothFactory) ProxyFactory. getProxyInstance ( nikeClothFactory) ;
proxyClothFactory. produceCloth ( ) ;
}
}