【问题标题】:How can we write an annotation我们如何写注解
【发布时间】:2014-08-20 10:15:08
【问题描述】:

我对注释的使用非常陌生。 谁能告诉我如何声明一个注解并调用所有使用该注解声明的方法/变量

我用java来实现这个注解

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
public @interface isAnnotatedVariable {
    String varName();
}

并使用了

中的注解
public class Example {

    @isAnnotatedVariable(varName = "S")
    public String var;
    @isAnnotatedVariable(varName = "S")
    public String var1;

}

并尝试使用

获取变量名
public class BuildStepClassDetector {

    public static void main(String[] args) throws IOException {
        BuildStepClassDetector build = new BuildStepClassDetector();
        final Logger4J logger = new Logger4J(build.getClass().getName());
        final HashMap<String, Class<?>> isAnnotatedVariables = new HashMap<String, Class<?>>();
        final TypeReporter reporter = new TypeReporter() {
            @SuppressWarnings("unchecked")
            @Override
            public Class<? extends Annotation>[] annotations() {
                return new Class[] { isAnnotatedVariable.class };
            }

            @SuppressWarnings("unchecked")
            @Override
            public void reportTypeAnnotation(Class<? extends Annotation> arg0, String arg1) {
                Class<? extends isAnnotatedVariable> isAnnotatedVariableClass;
                try {
                    isAnnotatedVariableClass = (Class<? extends isAnnotatedVariable>) Class.forName(arg1);
                    isAnnotatedVariables.put(
                            isAnnotatedVariableClass.getAnnotation(isAnnotatedVariable.class).varName(),
                            isAnnotatedVariableClass);
                } catch (ClassNotFoundException e) {
                    logger.getStackTraceString(e);
                }
            }
        };
        final AnnotationDetector cf = new AnnotationDetector(reporter);
        cf.detect();
        System.out.println(isAnnotatedVariables.keySet());

    }
}

【问题讨论】:

  • 1.您正在研究哪种语言? 2. 你尝试了什么 0.3。是什么导致了问题? 4.你问的是什么注释?...只是......“不清楚你在问什么”

标签: java annotations


【解决方案1】:

这是一个使用反射声明注释和检索带注释字段的简单示例。

    package asif.hossain;

    import java.lang.annotation.*;
    import java.lang.reflect.Field;

    /**
     *
     * Created by sadasidha on 21-Aug-14.
     */
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    @interface MyAnnotation {
        public String value();
    }
    class TestClass
    {
        @MyAnnotation("This is a name field")
        public String name;
    }
    public class Main {
        public static void main(String ... args) throws IllegalAccessException {
            TestClass testObject = new TestClass();
            Field[] fields = testObject.getClass().getFields();
            for (Field field : fields)
            {
                Annotation annotation = field.getAnnotation(MyAnnotation.class);
                if(annotation instanceof MyAnnotation)
                {
                    System.out.println(field.getName());
                    // get field value
                    String value = (String)field.get(testObject);
                   System.out.println("Field Value = "+ value);
                    //Set field value
                    field.set(testObject,"Your Name");
                    System.out.println(testObject.name);
                }
            }
        }
    }

您可以按照本教程http://tutorials.jenkov.com/java-reflection/index.html 了解有关注解和反射的更多信息。

【讨论】:

  • 如果我们想使用该字段,我们该怎么做?例如,如果我想更新'name',我们该怎么做(我的意思是不直接更新 name = "something")
  • field.get(testObject), field.set(testObject,"value") 获取和设置值。查看我的更新答案。
猜你喜欢
  • 1970-01-01
  • 2011-08-28
  • 1970-01-01
  • 2011-07-29
  • 2010-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-26
相关资源
最近更新 更多