Constructor getAnnotatedReceiverType() method in Java with Examples

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 {
}

chevron_right


Output:

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

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 {
}

chevron_right


Output:

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

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.