转:

java.lang.reflect.Method.getAnnotation()方法示例

java.lang.reflect.Method.getAnnotation(Class <T> annotationClass)方法如果存在这样的注释,则返回指定类型的元素的注释,否则为null

声明

以下是java.lang.reflect.Method.getAnnotation(Class <T> annotationClass)方法的声明。

public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
Java

参数

  • annotationClass - Class对象对相应的注释类型。

返回值

  • 如果存在于此元素,则返回该元素注释指定的注释类型,否则返回为null

异常

  • NullPointerException - 如果给定的注释类为空。

以下示例显示java.lang.reflect.Method.getAnnotation(Class<T> annotationClass)方法的用法。

package sync;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method; public class MethodDemo {
public static void main(String[] args) {
Method[] methods = SampleClass.class.getMethods(); Annotation annotation = methods[0].getAnnotation(CustomAnnotation.class);
if(annotation instanceof CustomAnnotation){
CustomAnnotation customAnnotation = (CustomAnnotation) annotation;
System.out.println("name: " + customAnnotation.name());
System.out.println("value: " + customAnnotation.value());
}
}
} @CustomAnnotation(name="SampleClass", value = "Sample Class Annotation")
class SampleClass {
private String sampleField; @CustomAnnotation(name="getSampleMethod", value = "Sample Method Annotation")
public String getSampleField() {
return sampleField;
} public void setSampleField(String sampleField) {
this.sampleField = sampleField;
}
} @Retention(RetentionPolicy.RUNTIME)
@interface CustomAnnotation {
public String name();
public String value();
}
Java

让我们编译并运行上面的程序,这将产生以下结果 -

name: getSampleMethod
value: Sample Method Annotation
Shell

原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/javareflect/javareflect_method_getannotation.html

java.lang.reflect.Method.getAnnotation()方法示例【通过反射获取到方法对象再获取方法对象上的注解信息】的更多相关文章

  1. java.lang.reflect.Method.getAnnotation()方法示例

    转: java.lang.reflect.Method.getAnnotation()方法示例 作者: 初生不惑 Java技术QQ群:227270512 / Linux QQ群:479429477   ...

  2. (转)Java.lang.reflect.Method invoke方法 实例

    背景:今天在项目中用到Method 的invoke方法,但是并不理解,查完才知道,原来如此! import java.lang.reflect.Method; /** * Java.lang.refl ...

  3. java.lang.reflect.Method

    java.lang.reflect.Method 一.Method类是什么 Method是一个类,位于java.lang.reflect包下. 在Java反射中 Method类描述的是 类的方法信息, ...

  4. JAVA中反射机制五(java.lang.reflect包)

    一.简介 java.lang.reflect包提供了用于获取类和对象的反射信息的类和接口.反射API允许对程序访问有关加载类的字段,方法和构造函数的信息进行编程访问.它允许在安全限制内使用反射的字段, ...

  5. Java反射API研究(2)——java.lang.reflect详细内容与关系

    对于最新的java1.8而言,reflect中接口的结构是这样的: java.lang.reflect.AnnotatedElement java.lang.reflect.AnnotatedType ...

  6. JAVA中反射机制六(java.lang.reflect包)

    一.简介 java.lang.reflect包提供了用于获取类和对象的反射信息的类和接口.反射API允许对程序访问有关加载类的字段,方法和构造函数的信息进行编程访问.它允许在安全限制内使用反射的字段, ...

  7. 错误: java.lang.reflect.InvocationTargetException

    错误: java.lang.reflect.InvocationTargetException    at sun.reflect.NativeMethodAccessorImpl.invoke0(N ...

  8. 自己写一个java.lang.reflect.Proxy代理的实现

    前言 Java设计模式9:代理模式一文中,讲到了动态代理,动态代理里面用到了一个类就是java.lang.reflect.Proxy,这个类是根据代理内容为传入的接口生成代理用的.本文就自己写一个Pr ...

  9. java.sql.Date赋值给了java.util.Date.转化成JSONArray时出错net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

    net.sf.json.JSONException: java.lang.reflect.InvocationTargetExceptionat net.sf.json.JSONObject.defa ...

随机推荐

  1. Python3.5环境安装及使用 Speech问题解决(转)

    修改speech.py line59 修改import thread,改成import threading line157 修改print prompt,改成print(prompt) 对最后的函数_ ...

  2. PostScript

    https://baike.baidu.com/item/PostScript/2192822?fr=aladdin PostScript是一种编程语言,最适用于列印图像和文字(无论是在纸.胶片或非物 ...

  3. (转)VMware虚拟机三种网络模式的区别及配置方法;

    我的一点实际经验理解桥接和NAT 桥接是虚拟机完全作为一个独立的地址接在局域网中,NAT是虚拟机依赖宿主主机地址转换的一种方式 例子我的虚拟机如果用桥接模式,连接外部网站如百度时会提示此pc没有装公司 ...

  4. C# 继承(3)持续更新

    类继承 和 接口继承 类继承        一个类型派生于一个基类行,它拥有该基类型的所有成员字段和函数. 接口继承     一个类型继承函数的签名,不需要实现代码. 多重继承 一个类派生自多个类.多 ...

  5. 设置pycharm环境下python内存

    有的时候在pycharm下的python需要加载很大的内存,那我们如何去修改pycharm的环境的内存呢?? 第一个栈内存,第二是堆内存.

  6. python之collections模块(nametuple,Counter)

    前言: import collections print([name for name in dir(collections) if not name.startswith("_" ...

  7. 正整数n拆分成几个不同的平方数——DFS&&打表

    考虑将正整数n拆分成几个不同的平方数之和,比如30=1^2 + 2^2 + 5^2=1^2 + 2^2 + 3^2 + 4^2,而8不存在这样的拆分. #include<bits/stdc++. ...

  8. [Algorithm] Find The Vowels

    // --- Directions // Write a function that returns the number of vowels // used in a string. Vowels ...

  9. HDU 6068 - Classic Quotation | 2017 Multi-University Training Contest 4

    /* HDU 6068 - Classic Quotation [ KMP,DP ] | 2017 Multi-University Training Contest 4 题意: 给出两个字符串 S[ ...

  10. docker管理

    查看容器名 [root@docker ~]# docker inspect -f "{{.Name}}" a2f /u1 停止/启动终止状态的容器 [root@docker ~]# ...