通过反射获取类的注解信息
package com.seven.classTest;
import java.lang.annotation.*;
import java.lang.reflect.Field;
public class TestAnnotation {
public static void main(String[] args) throws Exception {
Class c1 = Person01.class;
Annotation declaredAnnotation = c1.getDeclaredAnnotation(TableAnnotation.class);
TableAnnotation tableAnnotation = (TableAnnotation) declaredAnnotation;
System.out.println(tableAnnotation.value());
Field name = c1.getDeclaredField("name");
FieldAnnotation declaredAnnotation1 = name.getDeclaredAnnotation(FieldAnnotation.class);
FieldAnnotation fieldAnnotation = declaredAnnotation1;
System.out.println(fieldAnnotation.fieldName());
System.out.println(fieldAnnotation.fieldType());
System.out.println(fieldAnnotation.length());
}
}
@TableAnnotation("db_person")
class Person01{
@FieldAnnotation(fieldName = "id",fieldType = "varchar",length = 10)
private int id;
@FieldAnnotation(fieldName = "name",fieldType = "varchar",length = 5)
private String name;
public Person01() {
}
public Person01(int id, String name) {
this.id = id;
this.name = name;
}
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;
}
@Override
public String toString() {
return "Person01{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface TableAnnotation{
String value();
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface FieldAnnotation{
String fieldName();
String fieldType();
int length();
}
通过反射获取类的结构
package com.seven.classTest;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class TestClassGet {
public static void main(String[] args) throws Exception {
Class c1 = Class.forName("com.seven.classTest.Dog");
Field age =c1.getField("age");
Field id1 = c1.getDeclaredField("id");
System.out.println(age);
System.out.println(id1);
Field[] fields = c1.getFields();
for (Field field : fields) {
System.out.println(field);
}
Field[] fields1 = c1.getDeclaredFields();
for (Field field : fields1) {
System.out.println(field);
}
System.out.println("---------------------------------------------------");
Method getId = c1.getMethod("getId", null);
Method setId = c1.getMethod("setId", int.class);
System.out.println(getId);
System.out.println(setId);
System.out.println("---------------------------------------------------");
Constructor[] constructors = c1.getConstructors();
for (Constructor constructor : constructors) {
System.out.println(constructor);
}
Constructor constructor = c1.getConstructor(int.class, String.class, int.class);
System.out.println(constructor);
}
}
class Dog{
private int id;
private String name;
public int age;
public Dog() {
}
public Dog(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;
}
private void test(){}
@Override
public String toString() {
return "Dog{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
通过反射来操作对象
package com.seven.classTest;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class TestClassNew {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
Class c1 = Class.forName("com.seven.classTest.Dog");
Dog dog = (Dog) c1.newInstance();
System.out.println(dog.toString());
Constructor constructor = c1.getDeclaredConstructor(int.class, String.class, int.class);
Dog dog1 = (Dog) constructor.newInstance(12,"lili",43);
System.out.println(dog1.toString());
Object o = c1.newInstance();
Dog dog2 = (Dog) o;
Method setName = c1.getMethod("setName", String.class);
setName.invoke(dog2,"wuwan");
System.out.println(dog2);
Field name = c1.getDeclaredField("name");
name.setAccessible(true);
name.set(dog2,"seven");
System.out.println(dog2);
}
}
通过反射获取泛型
package com.seven.classTest;
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 TestGeneric {
public void test01(Map<String,Dog> dogMap, List<Dog> dogList ){
System.out.println("test01");
}
public Map<String,Dog> test02(){
System.out.println("test02");
return null;
}
public static void main(String[] args) throws Exception {
Method test01 = new TestGeneric().getClass().getMethod("test01", Map.class, List.class);
Method test02 = new TestGeneric().getClass().getMethod("test02");
Type[] parameterTypes = test01.getGenericParameterTypes();
for (Type parameterType : parameterTypes) {
System.out.println(parameterType);
if (parameterType instanceof ParameterizedType)
{
Type[] arguments = ((ParameterizedType) parameterType).getActualTypeArguments();
for (Type argument : arguments) {
System.out.println("-----"+argument);
}
}
}
Type genericReturnType = test02.getGenericReturnType();
System.out.println(genericReturnType);
if (genericReturnType instanceof ParameterizedType){
Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();
for (Type actualTypeArgument : actualTypeArguments) {
System.out.println("========"+actualTypeArgument);
}
}
}
}