1. 什么叫反射
package com. hzu. reflection;
public class Test01 extends Object {
public static void main ( String[ ] args) throws ClassNotFoundException {
Class c1 = Class. forName ( "com.hzu.reflection.User" ) ;
System. out. println ( c1) ;
Class c2 = Class. forName ( "com.hzu.reflection.User" ) ;
Class c3 = Class. forName ( "com.hzu.reflection.User" ) ;
System. out. println ( c2. hashCode ( ) ) ;
System. out. println ( c3. hashCode ( ) ) ;
}
}
class User {
private int id;
private int age;
private String name;
public String sex;
public User ( ) {
}
private User ( int id) {
this . id= id;
}
public User ( int id, int age, String name) {
this . id = id;
this . age = age;
this . name = name;
}
public int getId ( ) {
return id;
}
public void setId ( int id) {
this . id = id;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
private void test ( ) {
}
@Override
public String toString ( ) {
return "User{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}' ;
}
}
2. 元注解
package com. hzu. annotation;
import java. util. ArrayList;
import java. util. List;
public class Test01 extends Object {
@Override
public String toString ( ) {
return super . toString ( ) ;
}
@SuppressWarnings ( "all" )
public void test02 ( ) {
List list= new ArrayList ( ) ;
}
@Deprecated
public static void test ( ) {
System. out. println ( "Deprecated" ) ;
}
public static void main ( String[ ] args) {
test ( ) ;
}
}
package com. hzu. annotation;
import java. lang. annotation. *;
@MyAnnotation
public class Test02 {
@MyAnnotation
public void test ( ) {
}
}
@Target ( value = { ElementType. METHOD, ElementType. TYPE} )
@Retention ( value = RetentionPolicy. RUNTIME)
@Documented
@Inherited
@interface MyAnnotation { }
package com. hzu. annotation;
import java. lang. annotation. ElementType;
import java. lang. annotation. Retention;
import java. lang. annotation. RetentionPolicy;
import java. lang. annotation. Target;
public class Test03 {
@MyAnnotation2 ( age = 18 , name = "hqf" , schools = { "大学" , "中学" } )
public void test ( ) {
}
@MyAnnotation3 ( "广州" )
public void test1 ( ) {
}
}
@Target ( { ElementType. TYPE, ElementType. METHOD} )
@Retention ( RetentionPolicy. RUNTIME)
@interface MyAnnotation2 {
String name ( ) default "" ;
int age ( ) ;
int id ( ) default - 1 ;
String[ ] schools ( ) default { "哔哩哔哩大学" } ;
}
@Target ( { ElementType. TYPE, ElementType. METHOD} )
@Retention ( RetentionPolicy. RUNTIME)
@interface MyAnnotation3 {
String value ( ) ;
}
package com. hzu. annotation;
import java. lang. annotation. *;
@MyAnnotation
public class Test02 {
@MyAnnotation
public void test ( ) {
}
}
@Target ( value = { ElementType. METHOD, ElementType. TYPE} )
@Retention ( value = RetentionPolicy. RUNTIME)
@Documented
@Inherited
@interface MyAnnotation { }
3.所有类型的class
package com. hzu. reflection;
import java. lang. annotation. ElementType;
public class Test03 {
public static void main ( String[ ] args) {
Class c1 = Object. class ;
Class c2 = Comparable. class ;
Class c3 = String[ ] . class ;
Class c4 = int [ ] [ ] . class ;
Class c5 = Override. class ;
Class c6 = ElementType. class ;
Class c7 = Integer. class ;
Class c8 = void . class ;
Class c9 = Class. class ;
System. out. println ( c1) ;
System. out. println ( c2) ;
System. out. println ( c3) ;
System. out. println ( c4) ;
System. out. println ( c5) ;
System. out. println ( c6) ;
System. out. println ( c7) ;
System. out. println ( c8) ;
System. out. println ( c9) ;
int a[ ] = new int [ 10 ] ;
int b[ ] = new int [ 100 ] ;
System. out. println ( a. getClass ( ) . hashCode ( ) ) ;
System. out. println ( b. getClass ( ) . hashCode ( ) ) ;
}
}
4.类加载的内存分析
package com. hzu. reflection;
public class Test04 {
public static void main ( String[ ] args) {
A a= new A ( ) ;
System. out. println ( A. m) ;
}
}
class A {
static {
System. out. println ( "A类的静态代码块" ) ;
m= 30 ;
}
static int m= 100 ;
public A ( ) {
System. out. println ( "A类的构造函数" ) ;
}
}
package com. hzu. reflection;
public class Test05 {
static {
System. out. println ( "Main类加载" ) ;
}
public static void main ( String[ ] args) throws ClassNotFoundException {
System. out. println ( Son. M) ;
}
}
class Father {
static {
System. out. println ( "父类加载" ) ;
}
static int b= 10 ;
}
class Son extends Father {
static {
System. out. println ( "子类加载" ) ;
}
static int m= 100 ;
static final int M= 50 ;
}
5.获取类的信息
package com. hzu. reflection;
import java. lang. reflect. Constructor;
import java. lang. reflect. Field;
import java. lang. reflect. Method;
public class Test06 {
public static void main ( String[ ] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
Class c1 = Class. forName ( "com.hzu.reflection.User" ) ;
System. out. println ( "c1:" + c1. getName ( ) ) ;
System. out. println ( "c1:" + c1. getSimpleName ( ) ) ;
System. out. println ( "==========getFields(public属性)===============" ) ;
Field[ ] fields = c1. getFields ( ) ;
for ( Field field : fields) {
System. out. println ( field) ;
}
System. out. println ( "==========getDeclaredFields(所有属性)===============" ) ;
Field[ ] declaredFields = c1. getDeclaredFields ( ) ;
for ( Field declaredField : declaredFields) {
System. out. println ( declaredField) ;
}
Field name = c1. getDeclaredField ( "name" ) ;
System. out. println ( "============获取指定的属性=============" ) ;
System. out. println ( name) ;
Method[ ] methods = c1. getMethods ( ) ;
System. out. println ( "================getMethods()===========" ) ;
for ( Method method : methods) {
System. out. println ( method) ;
}
Method[ ] declaredMethods = c1. getDeclaredMethods ( ) ;
System. out. println ( "================getDeclaredMethods()===========" ) ;
for ( Method declaredMethod : declaredMethods) {
System. out. println ( declaredMethod) ;
}
System. out. println ( "============获得指定的方法==================" ) ;
Method getName = c1. getMethod ( "getName" , null) ;
Method setName = c1. getMethod ( "setName" , String. class ) ;
System. out. println ( getName) ;
System. out. println ( setName) ;
System. out. println ( "==============指定定构造器=============" ) ;
Constructor[ ] constructors = c1. getConstructors ( ) ;
for ( Constructor constructor : constructors) {
System. out. println ( "constructor:" + constructor) ;
}
Constructor[ ] declaredConstructors = c1. getDeclaredConstructors ( ) ;
for ( Constructor declaredConstructor : declaredConstructors) {
System. out. println ( "declaredConstructor:" + declaredConstructor) ;
}
Constructor declaredConstructor = c1. getDeclaredConstructor ( int . class , int . class , String. class ) ;
System. out. println ( declaredConstructor) ;
}
}
6.动态创建对象
package com. hzu. reflection;
import java. lang. reflect. Constructor;
import java. lang. reflect. Field;
import java. lang. reflect. InvocationTargetException;
import java. lang. reflect. Method;
public class Test07 {
public static void main ( String[ ] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
Class c1 = Class. forName ( "com.hzu.reflection.User" ) ;
User user = ( User) c1. newInstance ( ) ;
System. out. println ( "user:" + user) ;
Constructor declaredConstructor = c1. getDeclaredConstructor ( int . class , int . class , String. class ) ;
User user1 = ( User) declaredConstructor. newInstance ( 10 , 10 , "小明" ) ;
System. out. println ( "user1:" + user1) ;
Constructor declaredConstructor1 = c1. getDeclaredConstructor ( int . class ) ;
declaredConstructor1. setAccessible ( true ) ;
User user2 = ( User) declaredConstructor1. newInstance ( 20 ) ;
System. out. println ( "user2:" + user2) ;
User user3= ( User) c1. newInstance ( ) ;
Method setName = c1. getMethod ( "setName" , String. class ) ;
setName. invoke ( user3, "我的貂蝉在哪里" ) ;
System. out. println ( user3. getName ( ) ) ;
User user4= ( User) c1. newInstance ( ) ;
Field name = c1. getDeclaredField ( "name" ) ;
name. setAccessible ( true ) ;
name. set ( user4, "吕布" ) ;
System. out. println ( user4. getName ( ) ) ;
}
}
7.性能检测
package com. hzu. reflection;
import com. sun. deploy. util. SystemUtils;
import sun. plugin2. util. SystemUtil;
import java. lang. reflect. InvocationTargetException;
import java. lang. reflect. Method;
public class Test08 {
public static void test01 ( ) {
User user= new User ( ) ;
long startTime = System. currentTimeMillis ( ) ;
for ( int i= 0 ; i< 1000000000 ; i++ ) {
user. getName ( ) ;
}
long endTime = System. currentTimeMillis ( ) ;
System. out. println ( "普通调用10亿次:" + ( endTime- startTime) + "毫秒" ) ;
}
public static void test02 ( ) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
User user= new User ( ) ;
Class c1 = user. getClass ( ) ;
Method getName = c1. getMethod ( "getName" , null) ;
long startTime = System. currentTimeMillis ( ) ;
for ( int i= 0 ; i< 1000000000 ; i++ ) {
getName. invoke ( user, null) ;
}
long endTime = System. currentTimeMillis ( ) ;
System. out. println ( "通过反射调用10亿次:" + ( endTime- startTime) + "毫秒" ) ;
}
public static void test03 ( ) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
User user= new User ( ) ;
Class c1 = user. getClass ( ) ;
Method getName = c1. getMethod ( "getName" , null) ;
getName. setAccessible ( true ) ;
long startTime = System. currentTimeMillis ( ) ;
for ( int i= 0 ; i< 1000000000 ; i++ ) {
getName. invoke ( user, null) ;
}
long endTime = System. currentTimeMillis ( ) ;
System. out. println ( "关闭性能检测调用10亿次:" + ( endTime- startTime) + "毫秒" ) ;
}
public static void main ( String[ ] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
test01 ( ) ;
test02 ( ) ;
test03 ( ) ;
}
}
8.通过反射获取泛型
package com. hzu. reflection;
import java. lang. reflect. Method;
import java. lang. reflect. ParameterizedType;
import java. lang. reflect. Type;
import java. util. List;
import java. util. Map;
public class Test09 {
public void test01 ( Map< String, User> map, List< User> list) {
System. out. println ( "test01" ) ;
}
public Map< String, User> test02 ( ) {
System. out. println ( "test02" ) ;
return null;
}
public static void main ( String[ ] args) throws NoSuchMethodException {
System. out. println ( "==================test01===================" ) ;
Method method = Test09. class . getMethod ( "test01" , Map. class , List. class ) ;
Type[ ] genericParameterTypes = method. getGenericParameterTypes ( ) ;
for ( Type genericParameterType : genericParameterTypes) {
System. out. println ( "#" + genericParameterType) ;
if ( genericParameterType instanceof ParameterizedType ) {
Type[ ] actualTypeArguments = ( ( ParameterizedType) genericParameterType) . getActualTypeArguments ( ) ;
for ( Type actualTypeArgument : actualTypeArguments) {
System. out. println ( genericParameterType) ;
}
}
}
System. out. println ( "==================test02===================" ) ;
method= Test09. class . getMethod ( "test02" , null) ;
Type genericReturnType = method. getGenericReturnType ( ) ;
if ( genericReturnType instanceof ParameterizedType ) {
Type[ ] actualTypeArguments = ( ( ParameterizedType) genericReturnType) . getActualTypeArguments ( ) ;
for ( Type actualTypeArgument : actualTypeArguments) {
System. out. println ( actualTypeArgument) ;
}
}
}
}
9. 反射操作注解
package com. hzu. reflection;
import java. lang. annotation. *;
import java. lang. reflect. Field;
public class Test10 {
public static void main ( String[ ] args) throws NoSuchFieldException {
System. out. println ( "========获取类注解=============" ) ;
Annotation[ ] annotations = Student2. class . getAnnotations ( ) ;
for ( Annotation annotation : annotations) {
System. out. println ( annotation) ;
}
TableTop annotation = Student2. class . getAnnotation ( TableTop. class ) ;
System. out. println ( annotation. value ( ) ) ;
System. out. println ( "========获取属性注解=============" ) ;
Field field = Student2. class . getDeclaredField ( "name" ) ;
FieldTop annotation1 = field. getAnnotation ( FieldTop. class ) ;
System. out. println ( annotation1. columName ( ) ) ;
System. out. println ( annotation1. length ( ) ) ;
System. out. println ( annotation1. type ( ) ) ;
}
}
@TableTop ( value = "db_student" )
class Student2 {
@FieldTop ( columName = "db_id" , type = "int" , length = 10 )
private int id;
@FieldTop ( columName = "db_age" , type = "int" , length = 10 )
private int age;
@FieldTop ( columName = "db_name" , type = "varchar" , length = 3 )
private String name;
public Student2 ( ) {
}
public Student2 ( int id, int age, String name) {
this . id = id;
this . age = age;
this . name = name;
}
public int getId ( ) {
return id;
}
public void setId ( int id) {
this . id = id;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
@Override
public String toString ( ) {
return "Student2{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
'}' ;
}
}
@Target ( ElementType. TYPE)
@Retention ( RetentionPolicy. RUNTIME)
@interface TableTop {
String value ( ) ;
}
@Target ( ElementType. FIELD)
@Retention ( RetentionPolicy. RUNTIME)
@interface FieldTop {
String columName ( ) ;
String type ( ) ;
int length ( ) ;
}