0

I hope I have chosen a title as clear as possible. I have classes annotated with a custom annotation

@MyAnnotation
public class MyClass1 {
   //...
}
@MyAnnotation
public class MyClass2 {
   //...
}

I have another class that I want to enrich with two methods with the same name: one method that takes only the annotated class as parameters, and the other method that takes other classes as parameters

public class MyOtherClass {
   public void myMethod(Object obj) {
       //.....
   }
   public void myMethod (@MyAnnotation Object obj) {
       // this method is applied for only classes annotated with @MyAnnotation
   }
}

I know that public void myMethod (@MyAnnotation Object obj) is a wrong way we can not pass annotation as parameter. But I want to find the proper way to handle this need.

3
  • Why can the method only take classes with the annotation? Wouldn't using an interface instead of annotations achieve the same purpose? Commented Feb 28, 2020 at 9:27
  • I thought that annotaion is proper way to distinguish my classes, especially since I don't need an interface for my classes at the moment Commented Feb 28, 2020 at 9:31
  • The more Important question here is why? Why do you need to distinguish classes? What's different about them? If one group of classes can indeed do something that another group can't do, then you need an interface. If the two groups of classes are actually the same, then your method could just accept both of them. Commented Feb 28, 2020 at 9:34

2 Answers 2

2

You can have a single myMethod(Object obj) and inside it manually check if the obj passed to this method was annotated with @MyAnnotation (supposing that the retention policy is to keep it at runtime). Then dispatch calls to some private void processAnnotated(Object obj) and private void processNotAnnotated(Object obj) based on the result.

public void myMethod(Object obj) {
  if (obj != null && obj.getClass().isAnnotationPresent(MyAnnotation.class))  {
    processAnnottated(obj);
  } else {
    processNotAnnottated(obj);
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

II thought of this solution, but I wanted to make sure that there is no other way to do it even simpler and that does not need to do tests inside my method.
Then make a marker interface instead of annotation.
1

I am not sure what you are trying to achieve. As you say, you can't annotate a parameter. You can mark an annotation as Runtime, but then you can only check with reflection if an annotation is present or not. So you could start myMethod with a call to a private validation method validateMyAnnotationIsSet(parameter) that checks if the annotation is set on the given class or not.

Perhaps it's better however to work with a MarkerInterface. You can create an empty interface that the class may or may not implement. Within your method you can accept that interface as parameter and then do an 'instanceof' and a cast to continue to work with it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.