20

I want to create custom annotation in java for DirtyChecking. Like I want to compare two string values using this annotation and after comparing it will return a boolean value.

For instance: I will put @DirtyCheck("newValue","oldValue") over properties.

Suppose I made an interface:

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

My Questions are:

  1. Where I make a class to create a method for comparison for two string values? I mean, how this annotation notifies that this method I have to call?
  2. How to retreive returning values of this method ?
3
  • can you elaborate better what you are after? do you want the compiler to recognize that annotation? or the IDE? or any specific framework that you are using or what? Commented Sep 4, 2012 at 9:07
  • As far as I can read from oracle's website, you can't make annotations have methods/functions. They're meant to store metadata value, or data that can identify their purpose. Commented Sep 4, 2012 at 9:08
  • I am adding a link speaking of custom annotation from Oracle Commented Oct 12, 2012 at 4:43

2 Answers 2

23

First you need to mark if annotation is for class, field or method. Let's say it is for method: so you write this in your annotation definition:

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

Next you have to write let's say DirtyChecker class which will use reflection to check if method has annotation and do some job for example say if oldValue and newValue are equal:

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

Cheers, Michal

Sign up to request clarification or add additional context in comments.

11 Comments

Thanks Michal for your time and answer i have some doubts like 1-Let us suppose i have annotation @DirtyCheck(newValue,OldValue) Now i will want to tell my developer to use it so is it possible developer will use only this Annotation syntax in their Set Method and annotation will return me True or False so that developer know values are same or not.
@psisodia I did not catch your idea, could you provide an example?
Lets suppose I have created an annotation @DirtyCheck(newValue,oldValue) and its class DirtyChecker.java When I use this annotation in my application like- @DirtyCheck("hello","hi") public boolean compare() { ...mycode... } I want to Know how this annotation refers to the method given in my DirtyChecker.java
@DirtyCheck annotation is just a mark. You can mark class,field or method with annotation. And check if object has this annotation in some other code. And accordingly run some actions. Look at this link
@JyotiYadav i know i am coming 4 years from the future but i could really use the answer to your question, did you find an answer after all ?
|
2

To answer your second question: your annotation can't return a value. The class which processes your annotation can do something with your object. This is commonly used for logging for example. I'm not sure if using an annotation for checking if an object is dirty makes sense except you want to throw an exception in this case or inform some kind of DirtyHandler.

For your first question: you could really spent some effort in finding this yourself. There are enough information here on stackoverflow and the web.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.