Java Method与泛型和注解

Method && GenericType

Method.getGenericExceptionTypes();
Method.getGenericParameterTypes();
Method.getGenericReturnType();

Method && Annotation

Method.getAnnotations()
Method.getAnnotatedReceiverType()
Method.getAnnotatedParameterTypes()
Method.getAnnotatedExceptionTypes()
Method.getAnnotatedReturnType()

Sample

public class MethodTest {

	@Documented
	@Retention(RetentionPolicy.RUNTIME)
	@Target(value = { METHOD, PARAMETER, LOCAL_VARIABLE, TYPE_USE })
	@Repeatable(Schedules.class)
	private @interface Schedule {
		String value();
	}

	@Target(value = { METHOD, PARAMETER, LOCAL_VARIABLE, TYPE_USE })
	@Documented
	@Retention(RetentionPolicy.RUNTIME)
	private @interface Schedules {
		Schedule[] value();
	}

	@Schedule("before method")
	public @Schedule("before return type") String test(@Schedule("receiver parameters") MethodTest this,
			@Schedule("before method parameter1") String a1, @Schedule("before method parameter2") String a2)
			throws @Schedule("before throws exception") IndexOutOfBoundsException {
		System.out.println(this.toString());

		@Schedule("before local variable")
		String str = "str";

		return str;
	}

	public <E extends Exception,T1,T2,TR extends ArrayList<String>> TR testGeneric(MethodTest this, T1 a1, T2 a2) throws IndexOutOfBoundsException,E {
		return (TR) new ArrayList<String>();
	}

	private static void displayAnnotations(Annotation annotation) {
		System.out.println(annotation);
	}

	private static void displayAnnotatedType(AnnotatedType annotatedType) {
		System.out.println("Type: " + annotatedType.getType().getTypeName());
		System.out.println("Annotations: " + Arrays.toString(annotatedType.getAnnotations()));
		System.out.println("Declared Annotations: " + Arrays.toString(annotatedType.getDeclaredAnnotations()));

		AnnotatedType annotatedOwnerType = annotatedType.getAnnotatedOwnerType();// Java 9
		System.out.println("Annotated owner type: " + annotatedOwnerType);
		System.out.println("AnnotatedType class: " + annotatedType.getClass().getName());
		System.out.println("AnnotatedType class implementing interfaces: "
				+ Arrays.toString(annotatedType.getClass().getInterfaces()));

		if (annotatedOwnerType != null) {
			System.out.println("-- annotatedOwnerType --");
			displayAnnotatedType(annotatedOwnerType);
		}
	}

	private static void displayClass(Class<?> clazz) {
		System.out.println(clazz);
	}

	private static void displayParameter(Parameter p) {
		System.out.println(p);
	}

	private static void printMethodAllAnnotation() throws NoSuchMethodException, SecurityException {
		Method m = MethodTest.class.getMethod("test", String.class, String.class);

		// Annotation on Method
		Annotation[] annotations = m.getAnnotations();
		for (int i = 0; i < annotations.length; i++) {
			Annotation annotation = annotations[i];
			displayAnnotations(annotation);
		}

		// Annotated Method Receiver Parameters
		AnnotatedType annotatedReceiverType = m.getAnnotatedReceiverType();
		displayAnnotatedType(annotatedReceiverType);

		// Annotated Method Parameters
		Parameter[] parameters = m.getParameters();
		for (int i = 0; i < parameters.length; i++) {
			Parameter parameter = parameters[i];
			displayParameter(parameter);
		}

		AnnotatedType[] annotatedParameterTypes = m.getAnnotatedParameterTypes();
		for (int i = 0; i < annotatedParameterTypes.length; i++) {
			AnnotatedType annotatedType = annotatedParameterTypes[i];
			displayAnnotatedType(annotatedType);
		}

		// throws exceptions
		Class<?>[] exceptionTypes = m.getExceptionTypes();
		for (int i = 0; i < exceptionTypes.length; i++) {
			Class<?> class1 = exceptionTypes[i];
			displayClass(class1);
		}

		AnnotatedType[] et = m.getAnnotatedExceptionTypes();
		for (int i = 0; i < et.length; i++) {
			AnnotatedType annotatedType = et[i];
			displayAnnotatedType(annotatedType);
		}

		// AnnotatedReturnType
		System.out.println("--------------------------AnnotatedReturnType------------------------------");
		AnnotatedType annotatedReturnType = m.getAnnotatedReturnType();
		displayAnnotatedType(annotatedReturnType);

	}

	public static void main(String[] args) throws NoSuchMethodException, SecurityException {
		// printMethodAllAnnotation();
		Method m = MethodTest.class.getMethod("testGeneric", Object.class,Object.class );
		Type[] genericParameterTypes = m.getGenericParameterTypes();
		for (int i = 0; i < genericParameterTypes.length; i++) {
			Type type = genericParameterTypes[i];
			System.out.println(type.getClass());
		}

		Type[] genericExceptionTypes = m.getGenericExceptionTypes();
		for (int i = 0; i < genericExceptionTypes.length; i++) {
			Type type = genericExceptionTypes[i];
			System.out.println(type);
		}

		Type genericReturnType = m.getGenericReturnType();
		System.out.println(genericReturnType);

	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

陈振阳

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

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

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

打赏作者

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

抵扣说明:

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

余额充值