java获取注解,java获取注解的值

发布时间:2022-11-27

本文目录一览:

  1. java如何获取类上的注解
  2. java反射机制 怎样获取到类上面的注解
  3. Java 注解的读取注解信息的方法
  4. java获取当前类上的注解内容
  5. java 获取调用此方法的方法的注解

java如何获取类上的注解

如何获取类的注解和注解的内容 java 反射

Class someClass = Some.getClass();
注解 somtAnnotation = someClass.getAnnotation(注解.class)
属性类型 属性值 = someAnnotation.属性();

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

java反射机制 怎样获取到类上面的注解

// 定义注解并指定java注解保留策略为运行时RUNTIME,运行时注入到JAVA字节码文件里
// 这样才可以在运行时反射并获取它。
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@interface MyAnnotation{
    String key() default "";
    int value()  default 0; 
}
// 使用注解
@MyAnnotation(key="key1",value=200)
class MyClass{}
// 反射注解
public static void main(String[] args){
    MyClass myClass=new MyClass();
    MyAnnotation annotation=myClass.getClass().getAnnotation(MyAnnotation.class);
    System.out.println("key="+annotation.key()+"\tvalue="+annotation.value());
}

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

属于重点,在系统中用到注解权限时非常有用,可以精确控制权限的粒度 注意:要想使用反射去读取注解,必须将Retention的值选为Runtime

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 获取调用此方法的方法的注解

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实例
        Class<AnnonTestA> 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");
    }
}

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