「java获取注解」java获取注解内容

博主:adminadmin 2022-11-27 17:38:07 54

今天给各位分享java获取注解的知识,其中也会对java获取注解内容进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java 获取调用此方法的方法的注解

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

@Retention(value=RetentionPolicy.RUNTIME)

public @interface MyAnnotation {

String name();

}

public class AnnonTestA {

public void methodA(){

}

@MyAnnotation(name="111")

public void methodA(String a) throws Exception{

AnnonTestB.methodB("methodA",String.class);

}

}

import java.lang.annotation.Annotation;

import java.lang.reflect.Method;

public class AnnonTestB {

public static void methodB(String methodName, Class?... parameterTypes) throws Exception {

AnnonTestA annonTestA = new AnnonTestA();

// 获取AnnotationTest2的Class实例

ClassAnnonTestA c = AnnonTestA.class;

// 获取需要处理的方法Method实例

Method method = c.getMethod(methodName, parameterTypes);

Method[] methods = c.getMethods();

// 判断该方法是否包含MyAnnotation注解

if (method.isAnnotationPresent(MyAnnotation.class)) {

// 获取该方法的MyAnnotation注解实例

MyAnnotation myAnnotation = method

.getAnnotation(MyAnnotation.class);

// 执行该方法

// method.invoke(annonTestA, "12345");

// 获取myAnnotation

String value1 = myAnnotation.name();

System.out.println(value1);

}

// 获取方法上的所有注解

Annotation[] annotations = method.getAnnotations();

for (Annotation annotation : annotations) {

System.out.println(annotation);

}

}

}

import java.io.*;

import java.lang.annotation.Annotation;

import java.lang.reflect.Method;

public class TestString {

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

AnnonTestA annonTestA = new AnnonTestA();

annonTestA.methodA("123");

}

}

代码都给上了,不明白再追问吧。

参考地址:

java如何获取类上的注解

如何获取类的注解和注解的内容

java 反射

Class someClass = Some.getClass();

注解 somtAnnotation = someClass.getAnnotation(注解.class)

属性类型 属性值 = someAnnotation.属性();

要一一遍历么?

这个要根据需求来顶,谁用谁遍历,

Java 注解的读取注解信息的方法

属于重点,在系统中用到注解权限时非常有用,可以精确控制权限的粒度

注意:要想使用反射去读取注解,必须将Retention的值选为Runtime Java代码import java.lang.annotation.Annotation;import java.lang.reflect.Method;//读取注解信息public class ReadAnnotationInfoTest {    public static void main(String[] args) throws Exception {        // 测试AnnotationTest类,得到此类的类对象        Class c = Class.forName(com.iwtxokhtd.annotation.AnnotationTest);        // 获取该类所有声明的方法        Method[] methods = c.getDeclaredMethods();        // 声明注解集合        Annotation[] annotations;        // 遍历所有的方法得到各方法上面的注解信息        for (Method method : methods) {            // 获取每个方法上面所声明的所有注解信息            annotations = method.getDeclaredAnnotations();            // 再遍历所有的注解,打印其基本信息            System.out.println(method.getName());            for (Annotation an : annotations) {                System.out.println(方法名为: + method.getName() + 其上面的注解为: + an.annotationType().getSimpleName());                Method[] meths = an.annotationType().getDeclaredMethods();                // 遍历每个注解的所有变量                for (Method meth : meths) {                    System.out.println(注解的变量名为: + meth.getName());                }            }        }    }}

java获取当前类上的注解内容

@Retention(RetentionPolicy.RUNTIME) // 注解会在class字节码文件中存在,在运行时可以通过反射获取到

@Target({ElementType.FIELD,ElementType.METHOD})//定义注解的作用目标**作用范围字段、枚举的常量/方法

@Documented//说明该注解将被包含在javadoc中

public @interface FieldMeta {

/**

* 是否为序列号

* @return

*/

boolean id() default false;

/**

* 字段名称

* @return

*/

String name() default "";

/**

* 是否可编辑

* @return

*/

boolean editable() default true;

/**

关于java获取注解和java获取注解内容的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-27,除非注明,否则均为首码项目网原创文章,转载请注明出处。