欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java反射获取泛型、注解信息

程序员文章站 2024-03-14 18:18:10
...

一:反射获取泛型信息


泛型的参数信息:

package 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 test10 {

	public static void main(String[] args) throws NoSuchMethodException, SecurityException {
		
		Method method = test10.class.getMethod("test01",Map.class,List.class);
		//getGenericParameterTypes()获得泛型的参数类型
		Type[] genericParameterType = method.getGenericParameterTypes();
		
		for(Type parameterizedType:genericParameterType) {			
			System.out.println(parameterizedType);
			//泛型的参数类型是否等于参数化类型
			if(parameterizedType instanceof ParameterizedType) {
			//getActualTypeArguments获得真实的参数类型信息
			Type[] actualTypeArguments = ((ParameterizedType) parameterizedType).getActualTypeArguments();
			for(Type actualTypeArgument:actualTypeArguments) {
				System.out.println(actualTypeArgument);
			}			
		  }
		}
	}

	public void test01(Map<String,User> map,List<User> list) {
		System.out.println("test01");
	}
	

}

控制台:
java反射获取泛型、注解信息

泛型的返回类型信息:

package 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 test10 {

	public static void main(String[] args) throws NoSuchMethodException, SecurityException {

		Method method2 = test10.class.getMethod("test02",null);
		Type genericReturnType =  method2.getGenericReturnType();
		if(genericReturnType instanceof ParameterizedType) {	
			//getActualTypeArguments获得真实的参数类型信息
			Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();
			for(Type actualTypeArgument:actualTypeArguments) {
				System.out.println("<<"+actualTypeArgument);
			}			
		  }
	}

	public Map<String,User> test02(){
		System.out.println("test02");
		return null;
	}
	
}

控制台:
java反射获取泛型、注解信息




二:反射获取注解信息

package Reflection;

import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;

//反射获取注解信息
public class test11 {

	public static void main(String[] args) throws NoSuchFieldException, SecurityException {
		Class c1 = Student2.class;

		//通过反射获取注解
		Annotation[] annoations = c1.getAnnotations();
		for(Annotation annoation:annoations) {
			System.out.println(annoation);
		}
		
		//获得注解的value值
		Tablename tablename = (Tablename)c1.getAnnotation(Tablename.class);
		String value = tablename.value();
		System.out.println(value);
		
		//获得类指定的注解
		Field f = c1.getDeclaredField("id");
		Fieldname annoation = f.getAnnotation(Fieldname.class);
		System.out.println(annoation.columnName());
		System.out.println(annoation.Type());
		System.out.println(annoation.Length());
	}
	
		

}

@Tablename("dbname")
class Student2 {
	@Fieldname(columnName = "db_id",Type = "int",Length = 10)
	private int id;
	@Fieldname(columnName = "db_age",Type = "int",Length = 2)
	private int age;
	
	public Student2() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Student2(int id, int age) {
		super();
		this.id = id;
		this.age = age;
	}
	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;
	}
	@Override
	public String toString() {
		return "Student2 [id=" + id + ", age=" + age + "]";
	}
	
}

//类的注解
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface Tablename{
	String value();
}


//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Fieldname{
	String columnName();
	String Type();
	int Length();
}