Constructor getAnnotatedReceiverType() method in Java with Examples (original) (raw)

Last Updated : 25 Nov, 2019

The getAnnotatedReceiverType() method of a Constructor class is used to return an AnnotatedType object that represents the AnnotatedType to specify the receiver type of this constructor. If the constructor has a receiver parameter then The receiver type of a constructor is available. If this constructor either has no receiver parameter or has a receiver parameter with no annotations on its type, then the return value is an AnnotatedType object representing an element with no annotations. If this constructor is a top-level static member then the return value is null.Syntax:

public AnnotatedType getAnnotatedReceiverType()

Parameters: This method accepts nothing.Return value: This method returns an object of AnnotatedType representing the receiver type of the method or constructor represented by this Executable or null if this Executable can not have a receiver parameter. Below programs illustrate the getAnnotatedReceiverType() method:Program 1:

Java `

// Java program to demonstrate // Constructor.getAnnotatedReceiverType() 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.Constructor;

public class GFG {

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

    // create a constructor class
    Constructor<?> c = Test.class.getConstructors()[0];

    // apply getAnnotatedReceiverType()
    AnnotatedType atr
        = c.getAnnotatedReceiverType();

    // print result
    System.out.println(atr);
    System.out.println("Type = "
                       + atr.getType().getTypeName());
}

} class Test { public Test(@Annotation Test test) {} }

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

`

Output:

sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@12a3a380 Type = Test

Program 2:

Java `

// Java program to demonstrate // Constructor.getAnnotatedReceiverType() 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.Constructor;

public class GFG {

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

    // create a constructor class
    Constructor<?> c
        = Demo.class.getConstructors()[0];

    // apply getAnnotatedReceiverType()
    AnnotatedType atr
        = c.getAnnotatedReceiverType();

    // print result
    System.out.println(atr);
    System.out.println("Type = "
                       + atr.getType().getTypeName());
}

} class Demo { public Demo(@PathVar String str) {} }

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

`

Output:

sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@12a3a380 Type = Demo

References: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Constructor.html#getAnnotatedReceiverType()