反编译
反编译Field(属性)
public class ReflectTest06 {
public static void main ( String [ ] args) throws Exception {
StringBuilder s = new StringBuilder ( ) ;
Class studentName = Class . forName ( "java.lang.Thread" ) ;
s. append ( Modifier . toString ( studentName. getModifiers ( ) ) + " class " + studentName. getSimpleName ( ) + "{\n" ) ;
Field [ ] fields = studentName. getDeclaredFields ( ) ;
for ( Field field : fields) {
s. append ( "\t" ) ;
s. append ( Modifier . toString ( field. getModifiers ( ) ) ) ;
s. append ( " " ) ;
s. append ( field. getType ( ) ) ;
s. append ( " " ) ;
s. append ( field. getName ( ) ) ;
s. append ( ";\n" ) ;
}
s. append ( "}" ) ;
System . out. println ( s) ;
}
}
获取Field中的属性并赋值
import java. lang. reflect. Field ;
public class ReflectTest07 {
public static void main ( String [ ] args) throws Exception {
Class studentClass = Class . forName ( "番外篇反射机制.bean.Student" ) ;
Object obj = studentClass. newInstance ( ) ;
Field noField = studentClass. getDeclaredField ( "no" ) ;
noField. set ( obj, 12323 ) ;
System . out. println ( noField. get ( obj) ) ;
Field nameField = studentClass. getDeclaredField ( "name" ) ;
nameField. setAccessible ( true ) ;
nameField. set ( obj, "xiaoluo" ) ;
System . out. println ( nameField. get ( obj) ) ;
}
}
反编译Method(方法)
import java. lang. reflect. Method ;
import java. lang. reflect. Modifier ;
public class ReflectTest09 {
public static void main ( String [ ] args) throws Exception {
StringBuilder s = new StringBuilder ( ) ;
Class userService = Class . forName ( "java.lang.String" ) ;
s. append ( Modifier . toString ( userService. getModifiers ( ) ) + " class " + userService. getSimpleName ( ) + "{\n" ) ;
Method [ ] methods = userService. getDeclaredMethods ( ) ;
for ( Method method : methods) {
s. append ( "\t" ) ;
s. append ( Modifier . toString ( method. getModifiers ( ) ) ) ;
s. append ( " " ) ;
s. append ( method. getReturnType ( ) ) ;
s. append ( " " ) ;
s. append ( method. getName ( ) ) ;
s. append ( "(" ) ;
Class [ ] parameterTypes = method. getParameterTypes ( ) ;
for ( Class parameterType : parameterTypes) {
s. append ( parameterType. getSimpleName ( ) ) ;
s. append ( "," ) ;
}
if ( parameterTypes. length> 0 ) {
s. deleteCharAt ( s. length ( ) - 1 ) ;
}
s. append ( "){}\n" ) ;
}
s. append ( "}" ) ;
System . out. println ( s) ;
}
}
获取Method方法并赋值
import 番外篇反射机制. setvice. User;
import java. lang. reflect. Method ;
public class ReflectTest10 {
public static void main ( String [ ] args) throws Exception {
User userService = new User ( ) ;
boolean loginSuccess = userService. login ( "admin" , "123" ) ;
System . out. println ( loginSuccess ? "登录成功" : "登录失败" ) ;
Class userServiceClass = Class . forName ( "番外篇反射机制.setvice.User" ) ;
Object obj = userServiceClass. newInstance ( ) ;
Method loginMethod = userServiceClass. getDeclaredMethod ( "login" , String . class , String . class ) ;
Object retValue = loginMethod. invoke ( obj, "admin" , "123" ) ;
System . out. println ( retValue) ;
}
}
反编译Constructor构造方法
package 番外篇反射机制;
import java. lang. module. ModuleFinder ;
import java. lang. reflect. Constructor ;
import java. lang. reflect. Modifier ;
public class ReflectTest11 {
public static void main ( String [ ] args) throws Exception {
StringBuilder s = new StringBuilder ( ) ;
Class userClass = Class . forName ( "番外篇反射机制.setvice.User" ) ;
s. append ( Modifier . toString ( userClass. getModifiers ( ) ) ) ;
s. append ( " class " ) ;
s. append ( userClass. getSimpleName ( ) ) ;
s. append ( "{\n" ) ;
Constructor [ ] constructors = userClass. getDeclaredConstructors ( ) ;
for ( Constructor constructor : constructors) {
s. append ( "\t" ) ;
s. append ( Modifier . toString ( userClass. getModifiers ( ) ) ) ;
s. append ( " " ) ;
s. append ( userClass. getSimpleName ( ) ) ;
s. append ( "(" ) ;
Class [ ] parameterTypes = constructor. getParameterTypes ( ) ;
for ( Class parameterType : parameterTypes) {
s. append ( parameterType. getSimpleName ( ) ) ;
s. append ( "," ) ;
}
if ( parameterTypes. length> 0 ) {
s. deleteCharAt ( s. length ( ) - 1 ) ;
}
s. append ( "){}\n" ) ;
}
s. append ( "}" ) ;
System . out. println ( s) ;
}
}
获取构造方法并赋值
package 番外篇反射机制;
import 番外篇反射机制. setvice. User;
import java. lang. reflect. Constructor ;
public class ReflectTest12 {
public static void main ( String [ ] args) throws Exception {
User user = new User ( ) ;
Class c = Class . forName ( "番外篇反射机制.setvice.User" ) ;
Object obj = c. newInstance ( ) ;
System . out. println ( obj) ;
Constructor con = c. getDeclaredConstructor ( int . class ) ;
Object newobj = con. newInstance ( 11 ) ;
System . out. println ( newobj) ;
}
}
通过反射机制获取注解中的属性
package Annotation. Test03 ;
import java. lang. annotation. ElementType ;
import java. lang. annotation. Retention ;
import java. lang. annotation. RetentionPolicy ;
import java. lang. annotation. Target ;
@Target ( { ElementType . TYPE, ElementType . METHOD} )
@Retention ( RetentionPolicy . RUNTIME)
public @interface MyAnnotation {
String value ( ) default "dff" ;
}
package Annotation. Test03 ;
@MyAnnotation ( "www" )
public class MyAnnotationTest {
int i;
}
package Annotation. Test03 ;
public class ReflectAnnotationTest {
public static void main ( String [ ] args) throws Exception {
Class c = Class . forName ( "Annotation.Test03.MyAnnotationTest" ) ;
if ( c. isAnnotationPresent ( MyAnnotation . class ) ) {
MyAnnotation myAnnotation = ( MyAnnotation ) c. getAnnotation ( MyAnnotation . class ) ;
System . out. println ( "类上面的注解对象" + myAnnotation) ;
String value = myAnnotation. value ( ) ;
System . out. println ( value) ;
}
System . out. println ( ) ;
}
}
package Annotation. Test04 ;
import java. lang. reflect. Method ;
public class ReflectAnnotationTest {
@MyAnnotation ( zhanghao = "zhangsan" , password = "123" )
public void doSome ( ) {
}
public static void main ( String [ ] args) throws Exception {
Class c = Class . forName ( "Annotation.Test04.ReflectAnnotationTest" ) ;
Method deSomeMethod = c. getDeclaredMethod ( "doSome" ) ;
if ( deSomeMethod. isAnnotationPresent ( MyAnnotation . class ) ) {
MyAnnotation myAnnotation = deSomeMethod. getAnnotation ( MyAnnotation . class ) ;
System . out. println ( myAnnotation. zhanghao ( ) ) ;
System . out. println ( myAnnotation. password ( ) ) ;
}
}
}