Token interface or annotations?

I need to mark some classes as Invokable - just say that I can call class methods using reflection. But I don't like the idea of ​​having an empty interface just for this purpose. Can this be done with annotations and save the behavior in the example below? (I never created my own annotations, so I am not familiar with them in detail)

Example

class ClassOne implements Invokable {
}

class ClassTwo implements Invokable {
}

void someMethod(Invokable inv) {
}
+3
source share
3 answers

Due to someMethodyou cannot use annotations. You may later find that you Invokablemay need useful methods.

In addition, be sure to check existing interfaces such as Callable, Futureetc. before re-creating the wheel .

+6

mit Class.getAnnotations() Invokable. , , .

+2

the decision will depend on how you are going to use these classes. if you have many classes, some Invokable and some not, you might be better off with annotations, just pass the objects / classes and check the annotation. if you know that the classes you would pass would be Invokable, you could go with the token interface.

i.e. assuming you want to keep the behavior, not the method :-)

+2
source

All Articles