应用反射的API,获取类的信息(类的名字、属性、方法、构造器等)
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* 应用反射的API,获取类的信息(类的名字、属性、方法、构造器等)
*/
public class Demo02 {
public static void main(String[] args) {
String path="com.annotation.Student";
try {
Class<Student> studentClass=(Class<Student>)Class.forName(path);
//获取类的名字
System.out.println(studentClass.getName());
System.out.println(studentClass.getSimpleName());
//获取类的方法
Method[] methods=studentClass.getDeclaredMethods();
for (Method m:methods
) {
System.out.println("方法:"+m);
}
Method method=studentClass.getMethod("setName",String.class);
System.out.println(method);
//获得属性信息
//Field[] fields=studentClass.getFields();//只能获得public的field
Field[] fields=studentClass.getDeclaredFields();
for (Field f:fields
) {
System.out.println("属性:"+f);
}
//获取构造器
Constructor[] constructor=studentClass.getDeclaredConstructors();
for (Constructor c:constructor
) {
System.out.println("构造器:"+c);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
应用反射的API,获取类的信息(类的名字、属性、方法、构造器等)
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* 应用反射的API,获取类的信息(类的名字、属性、方法、构造器等)
*/
public class Demo02 {
public static void main(String[] args) {
String path="com.annotation.Student";
try {
Class<Student> studentClass=(Class<Student>)Class.forName(path);
//获取类的名字
System.out.println(studentClass.getName());
System.out.println(studentClass.getSimpleName());
//获取类的方法
Method[] methods=studentClass.getDeclaredMethods();
for (Method m:methods
) {
System.out.println("方法:"+m);
}
Method method=studentClass.getMethod("setName",String.class);
System.out.println(method);
//获得属性信息
//Field[] fields=studentClass.getFields();//只能获得public的field
Field[] fields=studentClass.getDeclaredFields();
for (Field f:fields
) {
System.out.println("属性:"+f);
}
//获取构造器
Constructor[] constructor=studentClass.getDeclaredConstructors();
for (Constructor c:constructor
) {
System.out.println("构造器:"+c);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
通过反射API调用构造方法、构造对象
通过API调用普通方法
通过反射API操作属性
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
*通过反射API调用构造方法、构造对象
* 通过API调用普通方法
* 通过反射API操作属性
*/
public class Demo03 {
public static void main(String[] args) {
String path="com.annotation.Student";
Class aClass;
{
try {
aClass = Class.forName(path);
//通过反射API调用构造方法、构造对象
Student student=(Student)aClass.newInstance();//调用无参构造方法
System.out.println(student);
Constructor<Student> c=aClass.getConstructor(int.class,String.class,int.class);
Student student1=c.newInstance(1001,"zhang",21);
System.out.println(student1.getName());
//通过API调用普通方法
Student student2=(Student)aClass.newInstance();
Method method=aClass.getMethod("setName",String.class);
method.invoke(student2,"zhang2");
System.out.println(student2.getName());
//通过反射API操作属性
Student student3=(Student)aClass.newInstance();
Field field=aClass.getDeclaredField("name");
field.setAccessible(true);//调用private属性
field.set(student3,"zhang3");
System.out.println(student3.getName());
System.out.println(field.get(student3));//通过反射直接读取
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
}
}
注解的使用
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.*;
public class Demo04 {
public void test01(Map<String,String> map, List<Student> list){
}
public List<Student> test02(){
return new ArrayList<>();
}
public static void main(String[] args) throws NoSuchMethodException {
Method m=Demo04.class.getDeclaredMethod("test01", Map.class, List.class);
Type[] types=m.getGenericParameterTypes();
for (Type t:types
) {
System.out.println("*"+t);
if(t instanceof ParameterizedType){
Type[] generictypes=((ParameterizedType)t).getActualTypeArguments();
for (Type t2:generictypes
) {
System.out.println(t2);
}
}
}
Method m2=Demo04.class.getMethod("test02",null);
Type types1=m2.getGenericReturnType();
System.out.println("*"+types1);
if(types1 instanceof ParameterizedType){
Type[] types2=((ParameterizedType)types1).getActualTypeArguments();
for (Type temp:types2
) {
System.out.println(temp);
}
}
}
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value = {ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String studentName() default "张瑞";
int age() default 0;
int id() default -1;
String[] shools() default {"清华大学","西安工业大学"};
}
package com.annotation;
@Table(value = "tb_student")
public class Student {
@TableField(columnName = "id",type = "int",length = 10)
private int id;
@TableField(columnName = "name",type = "varchar",length = 10)
private String name;
@TableField(columnName = "age",type = "int",length = 3)
private int age;
public Student() {
}
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {
String value();
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TableField {
String columnName();
String type();
int length();
}