Os seguintes blocos de código estão disponíveis nos métodos básicos usados na reflexão. Os comentários foram escritos e você pode usá-los diretamente, copiando-os diretamente, evitando o trabalho de verificar a API
Abaixo está uma classe encapsulada, bem como várias formas de reflexão e alguns métodos comumente usados, geralmente são suficientes e os comentários são muito completos.
import java. lang. annotation. Annotation;
import java. lang. reflect. Array;
import java. lang. reflect. Constructor;
import java. lang. reflect. Field;
import java. lang. reflect. Method;
import java. lang. reflect. ParameterizedType;
import java. lang. reflect. Type;
import java. util. ArrayList;
import java. util. Arrays;
import java. util. List;
import java. util. Map;
import javax. annotation. Resource;
@Resource
@SuppressWarnings ( "unchecked" )
public class ReflectTest {
public int i;
private double d;
static String s;
protected Map< Integer, String> map;
public List< String> list = new ArrayList < String> ( ) ;
public ReflectTest ( ) {
this . i = 10 ;
this . d = 20.0 ;
this . s = "hello" ;
System. out. println ( "调用无参构造器" ) ;
}
private ReflectTest ( String s) {
this . s = s;
System. out. println ( "这是一个String类型的构造器" ) ;
}
public double getD ( ) {
return d;
}
public void show ( ) {
System. out. println ( "这是一个空can的方法" ) ;
}
private String show ( String s) {
System. out. println ( this . s) ;
System. out. println ( "这是一个String参数的方法" ) ;
return s;
}
class Inner1 {
}
private class Inner2 {
}
public static void main ( String[ ] args) throws Exception {
Class< ? > clazz1 = Class. forName ( "com.briup.day16.ReflectTest" ) ;
System. out. println ( "=====属性=====" ) ;
Field[ ] fs = clazz1. getDeclaredFields ( ) ;
for ( Field field : fs) {
System. out. println ( field. getName ( ) ) ;
System. out. println ( field. getType ( ) ) ;
System. out. println ( field. getModifiers ( ) ) ;
System. out. println ( field) ;
System. out. println ( "==========" ) ;
}
System. out. println ( "=====方法=====" ) ;
Method[ ] ms = clazz1. getDeclaredMethods ( ) ;
for ( Method method : ms) {
System. out. println ( method) ;
}
System. out. println ( "=====构造器=====" ) ;
Constructor< ? > [ ] cs = clazz1. getDeclaredConstructors ( ) ;
for ( Constructor< ? > constructor : cs) {
System. out. println ( constructor) ;
}
System. out. println ( "=====内部类=====" ) ;
Class< ? > [ ] inners = clazz1. getDeclaredClasses ( ) ;
for ( Class< ? > class1 : inners) {
System. out. println ( class1. getName ( ) ) ;
}
Class< ? > innerOne = inners[ 0 ] ;
String className = innerOne. getName ( ) ;
Class< ? > innerOO = Class. forName ( "com.briup.day16.ReflectTest$Inner1" ) ;
Class< ? > outClass = innerOne. getDeclaringClass ( ) ;
System. out. println ( outClass. getName ( ) ) ;
System. out. println ( "=====注解=====" ) ;
Annotation[ ] as = clazz1. getDeclaredAnnotations ( ) ;
for ( Annotation annotation : as ) {
System. out. println ( annotation) ;
}
System. out. println ( "=====构造器创建对象=====" ) ;
Object obj = clazz1. newInstance ( ) ;
ReflectTest rt1 = null ;
if ( obj instanceof ReflectTest ) {
rt1 = ( ReflectTest) obj;
}
Constructor< ? > c1 = clazz1. getDeclaredConstructor ( String. class ) ;
ReflectTest rt2 = ( ReflectTest) c1. newInstance ( "hello world" ) ;
System. out. println ( "=====反射调用方法=====" ) ;
Method m1 = clazz1. getDeclaredMethod ( "show" , null ) ;
Method m2 = clazz1. getDeclaredMethod ( "show" , String. class ) ;
m1. invoke ( rt2, null ) ;
m2. invoke ( rt2, "tom" ) ;
System. out. println ( "=====属性值的访问=====" ) ;
Field f1 = clazz1. getDeclaredField ( "s" ) ;
f1. set ( rt2, "hello toms" ) ;
System. out. println ( rt2. s) ;
System. out. println ( "=========================以上的部分要掌握,下面的作为拓展======================" ) ;
System. out. println ( "=====访问泛型=====" ) ;
Field f2 = clazz1. getDeclaredField ( "map" ) ;
Type gf = f2. getGenericType ( ) ;
if ( gf instanceof ParameterizedType ) {
ParameterizedType pt = ( ParameterizedType) gf;
System. out. println ( "原始类型: " + pt. getRawType ( ) . getTypeName ( ) ) ;
Type[ ] ata = pt. getActualTypeArguments ( ) ;
for ( int i = 0 ; i < ata. length; i++ ) {
System. out. println ( "第" + ( i + 1 ) + "个参数类型为: " + ata[ i] . getTypeName ( ) ) ;
}
}
System. out. println ( "======数组======" ) ;
Object o = Array. newInstance ( String. class , 10 ) ;
Array. set ( o, 0 , "hello" ) ;
Array. set ( o, 1 , "world" ) ;
Array. set ( o, 2 , "tom" ) ;
System. out. println ( Array. get ( o, 2 ) ) ;
System. out. println ( Array. getLength ( o) ) ;
System. out. println ( "======其他功能======" ) ;
System. out. println ( clazz1. getPackage ( ) ) ;
System. out. println ( clazz1. getSuperclass ( ) ) ;
System. out. println ( Arrays. toString ( clazz1. getInterfaces ( ) ) ) ;
}
}
Quais são as regras antigas? Se você tiver algum bug ou não entender, pode bater um papo comigo em particular qq: 1638580498