java getconstructor_Java Constructor getAnnotatedReceiverType()用法及代码示例

构造函数类的getAnnotatedReceiverType()方法用于返回表示AnnotatedType的AnnotatedType对象,以指定此构造函数的接收者类型。如果构造函数具有接收方参数,则构造函数的接收方类型可用。如果此构造函数没有接收方参数,或者具有其类型上没有注释的接收方参数,则返回值是一个AnnotatedType对象,该对象表示没有注释的元素。如果此构造函数是top-level静态成员,则返回值为null。

用法:

public AnnotatedType getAnnotatedReceiverType()

参数:此方法不接受任何内容。

返回值:此方法返回AnnotatedType对象,该对象表示此Executable表示的方法或构造方法的接收器类型;如果此Executable不能具有接收器参数,则返回null。

以下示例程序旨在说明getAnnotatedReceiverType()方法:

示例1:

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

}

输出:

sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@12a3a380

Type = Test

示例2:

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

}

输出:

sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@12a3a380

Type = Demo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值