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 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");
	}
	

}

控制台:
在这里插入图片描述

泛型的返回类型信息:

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;
	}
	
}

控制台:
在这里插入图片描述




二:反射获取注解信息

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();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Deeeelete

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值