2012-09-04 49 views
12

我想在java中为DirtyChecking创建自定义注释。就像我想比较使用此注释的两个字符串值,并在比较后它将返回一个boolean值。如何在java中创建自定义注释?

例如:我会把@DirtyCheck("newValue","oldValue")超过属性。

想我做的接口:

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

我的问题是

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

,你可以更好地阐述你所追求的?你想让编译器识别该注释吗?或IDE?或者你正在使用什么特定的框架? –

+0

据我可以从[oracle的网站](http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html)阅读,你不能使注释有方法/功能。它们旨在存储元数据值或可以识别其用途的数据。 –

+0

我正在添加一个链接[来自Oracle的自定义注释](http://www.oracle.com/technetwork/articles/hunter-meta-2-098036。html) – AurA

回答

17

首先,您需要标记注释是否用于类,字段或方法。比方说,这是方法:所以你写在你的注释定义:

@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; 
    } 
} 

干杯, 米哈尔

+0

感谢米哈尔你的时间和回答我有一些疑问,如 1-让我们假设我有注释@DirtyCheck(newValue,OldValue) 现在我想告诉我的开发人员使用它,因此可能开发人员将使用在它们的Set方法和注解中只有这个Annotation语法会返回True或False,以便开发人员知道值是否相同。 – psisodia

+0

@psisodia我没有听到你的想法,你能提供一个例子吗? – Michal

+0

假设我创建了一个注解@DirtyCheck(newValue,oldValue)及其类DirtyChecker.java 当我在我的应用程序中使用此注释时,如 @DirtyCheck(“hello”,“hi”) public boolean compare() { ... mycode ... } 我想知道这个注解是如何引用我在DirtyChecker.java中给出的方法 – psisodia

2

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

对于你的第一个问题:你真的可以花费一些努力找到你自己。在这里有足够的信息在计算器和网络上。

1

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中?

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

相关问题