Close

Java Reflection - Method.getAnnotatedParameterTypes() Examples

Java Reflection Java Java API 


Class:

java.lang.reflect.Method

java.lang.Objectjava.lang.Objectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AnnotatedElementAnnotatedElementjava.lang.reflect.Executablejava.lang.reflect.Executablejava.lang.reflect.MemberMemberjava.lang.reflect.GenericDeclarationGenericDeclarationjava.lang.reflect.Methodjava.lang.reflect.MethodLogicBig

Method:

public AnnotatedType[] getAnnotatedParameterTypes()

Returns an array of AnnotatedType objects that represent the use of types to specify formal parameter types of the method (see also Java 8 Type Annotations).


Examples


package com.logicbig.example.method;

import java.io.Serializable;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.AnnotatedParameterizedType;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.AnnotatedTypeVariable;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

public class GetAnnotatedParameterTypesExample {

private static class Test {
private <T extends @ServerObject Serializable> void process(List<@Immutable T> list) { }
}

public static void main(String... args) throws NoSuchMethodException {
Method m = Test.class.getDeclaredMethod("process", List.class);
AnnotatedType[] a = m.getAnnotatedParameterTypes();
for (AnnotatedType t : a) {
printAnnotatedType(t, 1);
}
}

private static void printAnnotatedType(AnnotatedType annotatedType, int level) {
print(level, "Type = " + annotatedType.getType().getTypeName());
print(level, "Annotations: " + Arrays.toString(annotatedType.getAnnotations()));
print(level, "Declared Annotations: " + Arrays.toString(annotatedType.getDeclaredAnnotations()));
AnnotatedType annotatedOwnerType = annotatedType.getAnnotatedOwnerType();//Java 9
print(level, "Annotated owner type: " + annotatedOwnerType);
print(level, "AnnotatedType class: " + annotatedType.getClass().getName());
print(level, "AnnotatedType class implementing interfaces: " +
Arrays.toString(annotatedType.getClass().getInterfaces()));
if (annotatedType instanceof AnnotatedParameterizedType) {
printAnnotatedParameterizedType((AnnotatedParameterizedType) annotatedType, level);
}
if (annotatedType instanceof AnnotatedTypeVariable) {
printAnnotatedTypeVariable((AnnotatedTypeVariable) annotatedType, level);
}
}

private static void printAnnotatedTypeVariable(AnnotatedTypeVariable annotatedType, int level) {
AnnotatedType[] aType = annotatedType.getAnnotatedBounds();
print(level, "AnnotatedTypeVariable#getAnnotatedBounds(): " + Arrays
.toString(aType));
for (AnnotatedType type : aType) {
print(level, "AnnotatedTypeVariable#bound : " + type);
printAnnotatedType(type, level + 1);
}
}

private static void printAnnotatedParameterizedType(AnnotatedParameterizedType annotatedType,
int level) {
AnnotatedType[] aTypes = annotatedType.getAnnotatedActualTypeArguments();
print(level, "AnnotatedParameterizedType#getAnnotatedActualTypeArguments() : " +
Arrays.toString(aTypes));
for (AnnotatedType aType : aTypes) {
print(level, "AnnotatedParameterizedType#annotatedActualTypeArgument: " + aType);
printAnnotatedType(aType, level + 1);
}
}

private static void print(int level, String string) {
System.out.printf("%" + (level * 4 - 3) + "s\u00A6- %s%n", "", string);
}


@Target({ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
private @interface Immutable {}

@Target({ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
private @interface ServerObject {}
}

Output

 ¦- Type = java.util.List<T>
¦- Annotations: []
¦- Declared Annotations: []
¦- Annotated owner type: null
¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedParameterizedTypeImpl
¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedParameterizedType]
¦- AnnotatedParameterizedType#getAnnotatedActualTypeArguments() : [sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeVariableImpl@59ce7140]
¦- AnnotatedParameterizedType#annotatedActualTypeArgument: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeVariableImpl@59ce7140
¦- Type = T
¦- Annotations: [@com.logicbig.example.method.GetAnnotatedParameterTypesExample$Immutable()]
¦- Declared Annotations: [@com.logicbig.example.method.GetAnnotatedParameterTypesExample$Immutable()]
¦- Annotated owner type: null
¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeVariableImpl
¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedTypeVariable]
¦- AnnotatedTypeVariable#getAnnotatedBounds(): [sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@3076b9fa]
¦- AnnotatedTypeVariable#bound : sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@3076b9fa
¦- Type = java.io.Serializable
¦- Annotations: [@com.logicbig.example.method.GetAnnotatedParameterTypesExample$ServerObject()]
¦- Declared Annotations: [@com.logicbig.example.method.GetAnnotatedParameterTypesExample$ServerObject()]
¦- Annotated owner type: null
¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl
¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]




package com.logicbig.example.method;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.AnnotatedParameterizedType;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.AnnotatedWildcardType;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Map;

public class GetAnnotatedParameterTypesExample2 {
private static class Test {
private void process(
@Immutable Map<? super @Immutable Key, ? extends @ServerObject RequestInfo> map) {
}
}

public static void main(String... args) throws NoSuchMethodException {
Method m = Test.class.getDeclaredMethod("process", Map.class);
AnnotatedType[] a = m.getAnnotatedParameterTypes();
for (AnnotatedType t : a) {
printAnnotatedType(t, 1);
}
}

private static void printAnnotatedType(AnnotatedType annotatedType, int level) {
print(level, "Type = " + annotatedType.getType().getTypeName());
print(level, "Annotations: " + Arrays.toString(annotatedType.getAnnotations()));
print(level, "Declared Annotations: " + Arrays.toString(annotatedType.getDeclaredAnnotations()));
AnnotatedType annotatedOwnerType = annotatedType.getAnnotatedOwnerType();//Java 9
print(level, "Annotated owner type: " + annotatedOwnerType);
print(level, "AnnotatedType class: " + annotatedType.getClass().getName());
print(level, "AnnotatedType class implementing interfaces: " +
Arrays.toString(annotatedType.getClass().getInterfaces()));
if (annotatedType instanceof AnnotatedParameterizedType) {
printAnnotatedParameterizedType((AnnotatedParameterizedType) annotatedType, level);
}

if (annotatedType instanceof AnnotatedWildcardType) {
printAnnotatedWildcardType((AnnotatedWildcardType) annotatedType, level);
}
}

private static void printAnnotatedParameterizedType(AnnotatedParameterizedType annotatedType,
int level) {
AnnotatedType[] aTypes = annotatedType.getAnnotatedActualTypeArguments();
print(level, "AnnotatedParameterizedType#getAnnotatedActualTypeArguments() : " +
Arrays.toString(aTypes));
for (AnnotatedType aType : aTypes) {
print(level, "AnnotatedParameterizedType#annotatedActualTypeArgument: " + aType);
printAnnotatedType(aType, level + 1);
}
}

private static void printAnnotatedWildcardType(AnnotatedWildcardType annotatedWildcardType,
int level) {
AnnotatedType[] lBounds = annotatedWildcardType.getAnnotatedLowerBounds();
print(level, "AnnotatedWildcardType#getAnnotatedLowerBounds(): " + Arrays.toString(lBounds));
for (AnnotatedType lBound : lBounds) {
print(level, "AnnotatedWildcardType#getAnnotatedLowerBound: " + lBound);
printAnnotatedType(lBound, level + 1);
}

AnnotatedType[] uBounds = annotatedWildcardType.getAnnotatedUpperBounds();
print(level, "AnnotatedWildcardType#getAnnotatedUpperBounds(): " + Arrays.toString(uBounds));
for (AnnotatedType uBound : uBounds) {
print(level, "AnnotatedWildcardType#getAnnotatedUpperBound: " + uBound);
printAnnotatedType(uBound, level + 1);
}
}

private static void print(int level, String string) {
System.out.printf("%" + (level * 4 - 3) + "s\u00A6- %s%n", "", string);
}

private static class RequestInfo {}

private static class Key {}

@Target({ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
private @interface Immutable {}

@Target({ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
private @interface ServerObject {}
}

Output

 ¦- Type = java.util.Map<? super com.logicbig.example.method.GetAnnotatedParameterTypesExample2$Key, ? extends com.logicbig.example.method.GetAnnotatedParameterTypesExample2$RequestInfo>
¦- Annotations: [@com.logicbig.example.method.GetAnnotatedParameterTypesExample2$Immutable()]
¦- Declared Annotations: [@com.logicbig.example.method.GetAnnotatedParameterTypesExample2$Immutable()]
¦- Annotated owner type: null
¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedParameterizedTypeImpl
¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedParameterizedType]
¦- AnnotatedParameterizedType#getAnnotatedActualTypeArguments() : [sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedWildcardTypeImpl@376bd7b7, sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedWildcardTypeImpl@77b03b8b]
¦- AnnotatedParameterizedType#annotatedActualTypeArgument: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedWildcardTypeImpl@376bd7b7
¦- Type = ? super com.logicbig.example.method.GetAnnotatedParameterTypesExample2$Key
¦- Annotations: []
¦- Declared Annotations: []
¦- Annotated owner type: null
¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedWildcardTypeImpl
¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedWildcardType]
¦- AnnotatedWildcardType#getAnnotatedLowerBounds(): [sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@6f8b6ff]
¦- AnnotatedWildcardType#getAnnotatedLowerBound: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@6f8b6ff
¦- Type = com.logicbig.example.method.GetAnnotatedParameterTypesExample2$Key
¦- Annotations: [@com.logicbig.example.method.GetAnnotatedParameterTypesExample2$Immutable()]
¦- Declared Annotations: [@com.logicbig.example.method.GetAnnotatedParameterTypesExample2$Immutable()]
¦- Annotated owner type: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@3ea75de0
¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl
¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]
¦- AnnotatedWildcardType#getAnnotatedUpperBounds(): [sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@5718f3ec]
¦- AnnotatedWildcardType#getAnnotatedUpperBound: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@5718f3ec
¦- Type = java.lang.Object
¦- Annotations: []
¦- Declared Annotations: []
¦- Annotated owner type: null
¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl
¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]
¦- AnnotatedParameterizedType#annotatedActualTypeArgument: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedWildcardTypeImpl@77b03b8b
¦- Type = ? extends com.logicbig.example.method.GetAnnotatedParameterTypesExample2$RequestInfo
¦- Annotations: []
¦- Declared Annotations: []
¦- Annotated owner type: null
¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedWildcardTypeImpl
¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedWildcardType]
¦- AnnotatedWildcardType#getAnnotatedLowerBounds(): []
¦- AnnotatedWildcardType#getAnnotatedUpperBounds(): [sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@2102e6e7]
¦- AnnotatedWildcardType#getAnnotatedUpperBound: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@2102e6e7
¦- Type = com.logicbig.example.method.GetAnnotatedParameterTypesExample2$RequestInfo
¦- Annotations: [@com.logicbig.example.method.GetAnnotatedParameterTypesExample2$ServerObject()]
¦- Declared Annotations: [@com.logicbig.example.method.GetAnnotatedParameterTypesExample2$ServerObject()]
¦- Annotated owner type: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@628ef376
¦- AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl
¦- AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]




See Also