【问题标题】:How to create custom annotation in java?如何在java中创建自定义注释?
【发布时间】:2012-09-04 09:00:09
【问题描述】:

我想在 java 中为DirtyChecking 创建自定义注解。就像我想使用这个注解比较两个字符串值,比较后它会返回一个boolean 值。

例如:我会将@DirtyCheck("newValue","oldValue") 放在属性上。

假设我做了一个界面:

 public @interface DirtyCheck {
    String newValue();
    String oldValue();
 }

我的问题是

  1. 我在哪里创建一个类来创建比较两个字符串值的方法?我的意思是,这个注解如何通知我必须调用这个方法?
  2. 如何检索此方法的返回值?

【问题讨论】:

  • 你能详细说明你在追求什么吗?你想让编译器识别那个注解吗?还是IDE?或您正在使用的任何特定框架或什么?
  • 据我所知oracle's website,您不能使注释具有方法/功能。它们旨在存储元数据值或可以识别其用途的数据。
  • 我正在添加一个关于custom annotation from Oracle的链接

标签: java annotations


【解决方案1】:

首先您需要标记注解是针对类、字段还是方法。假设它是用于方法:所以你在注释定义中写下这个:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DirtyCheck {
    String newValue();
    String oldValue();
}

接下来你必须编写假设DirtyChecker 类,它将使用反射来检查方法是否有注释并做一些工作,例如说oldValuenewValue 是否相等:

final class DirtyChecker {

    public boolean process(Object instance) {
        Class<?> clazz = instance.getClass();
        for (Method m : clazz.getDeclaredMethods()) {
            if (m.isAnnotationPresent(DirtyCheck.class)) {
                DirtyCheck annotation = m.getAnnotation(DirtyCheck.class);
                String newVal = annotation.newValue();
                String oldVal = annotation.oldValue();
                return newVal.equals(oldVal);
            }
        }
        return false;
    }
}

干杯, 迈克尔

【讨论】:

  • 感谢 Michal 抽出宝贵时间并回答我有一些疑问,例如 1-假设我有注释 @DirtyCheck(newValue,OldValue) 现在我想告诉我的开发人员使用它,这样可以吗开发人员将在他们的 Set Method 中仅使用此 Annotation 语法,并且 annotation 将返回 True 或 False,以便开发人员知道值是否相同。
  • @psisodia 我没听懂你的想法,你能举个例子吗?
  • 假设我已经创建了一个注解 @DirtyCheck(newValue,oldValue) 及其类 DirtyChecker.java 当我在我的应用程序中使用这个注解时,例如 - @DirtyCheck("hello","hi") public boolean compare() { ...mycode... } 我想知道这个注解如何引用我的 DirtyChecker.java 中给出的方法
  • @DirtyCheck 注释只是一个标记。您可以使用注释标记类、字段或方法。并检查对象是否在其他代码中具有此注释。并相应地运行一些动作。看this link
  • 感谢 Michal 的帮助和支持!您已经编写了“对象在其他一些代码中具有此注释。并相应地运行一些操作”这有助于我弄清楚如何解决我的问题。
【解决方案2】:

回答您的第二个问题:您的注释无法返回值。处理您的注释的类可以对您的对象做一些事情。例如,这通常用于记录。 我不确定使用注释检查对象是否脏是否有意义,除非您想在这种情况下抛出异常或通知某种DirtyHandler

对于您的第一个问题:您确实可以自己花一些精力来找到它。这里有足够的关于 stackoverflow 和 web 的信息。

【讨论】:

    【解决方案3】:

    CustomAnnotation.java

    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface CustomAnnotation {
         int studentAge() default 21;
         String studentName();
         String stuAddress();
         String stuStream() default "CS";
    }
    

    在Java中如何使用Annotation这个字段?

    TestCustomAnnotation.java

    package annotations;
    import java.lang.reflect.Method;
    public class TestCustomAnnotation {
         public static void main(String[] args) {
               new TestCustomAnnotation().testAnnotation();
         }
         @CustomAnnotation(
                    studentName="Rajesh",
                    stuAddress="Mathura, India"
         )
         public void testAnnotation() {
               try {
                    Class<? extends TestCustomAnnotation> cls = this.getClass();
                    Method method = cls.getMethod("testAnnotation");
    
                    CustomAnnotation myAnno = method.getAnnotation(CustomAnnotation.class);
    
                    System.out.println("Name: "+myAnno.studentName());
                    System.out.println("Address: "+myAnno.stuAddress());
                    System.out.println("Age: "+myAnno.studentAge());
                    System.out.println("Stream: "+myAnno.stuStream());
    
               } catch (NoSuchMethodException e) {
               }
         }
    }
    Output:
    Name: Rajesh
    Address: Mathura, India
    Age: 21
    Stream: CS
    

    Reference

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-05
      • 1970-01-01
      相关资源
      最近更新 更多