Close

Java Reflection - Method.getAnnotatedReceiverType() 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 getAnnotatedReceiverType()

Returns an AnnotatedType object that represents the use of a type to specify the receiver type of the method. (see also Java 8 Explicit Receiver Parameters).


Examples


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.AnnotatedType;
import java.lang.reflect.Method;
import java.util.Arrays;

public class GetAnnotatedReceiverTypeExample {

private static class Test {
private void process(@Immutable Test this) { }

private void init(@Immutable Test initFrom) {}
}

public static void main(String... args) throws NoSuchMethodException {
System.out.println("-- explicit receiver parameter in process() --");
Method m = Test.class.getDeclaredMethod("process");
AnnotatedType at = m.getAnnotatedReceiverType();
printAnnotatedType(at);

System.out.println("-- normal parameter in init() --");
Method m2 = Test.class.getDeclaredMethod("init", Test.class);
AnnotatedType at2 = m2.getAnnotatedReceiverType();
printAnnotatedType(at2);
}

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

private static void print(String string) {
System.out.printf(" %s%n", string);
}

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

Output

-- explicit receiver parameter in process() --
Type = com.logicbig.example.method.GetAnnotatedReceiverTypeExample$Test
Annotations: [@com.logicbig.example.method.GetAnnotatedReceiverTypeExample$Immutable()]
Declared Annotations: [@com.logicbig.example.method.GetAnnotatedReceiverTypeExample$Immutable()]
Annotated owner type: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@7db42b5d
AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl
AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]
-- normal parameter in init() --
Type = com.logicbig.example.method.GetAnnotatedReceiverTypeExample$Test
Annotations: []
Declared Annotations: []
Annotated owner type: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@f110421
AnnotatedType class: sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl
AnnotatedType class implementing interfaces: [interface java.lang.reflect.AnnotatedType]




See Also