我们如何编写注释

时间:2014-08-20 10:15:08

标签: java annotations

我对使用注释很新。 任何人都可以告诉我如何声明一个注释,并调用所有使用该注释声明的方法/变量

我正在使用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 个答案:

答案 0 :(得分:0)

以下是使用Reflection声明注释和检索带注释字段的简单示例。

    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了解有关注释和反射的更多信息。