Accessing annotations defined in the interface, in the class implementing it? - java

I'm working with java. I have an interface that has an annotated method in it. Is there a way to access that annotation in a class implementing that interface?
Thanks.
Edit: sorry, I shoudl've been clearer: I'm using reflection to access elements of Java, and I'm wondering how to access the annotations in a class implementing an interface (where the annotation is declared).

Sure. Reflection allows you to check for the presence of an annotation. There are methods at the class, method, paramter levels: reflection and annotations

Related

Is there an Annotation for member/method to be used by reflection?

I have a method that is invoked by the same class using reflection.
I'd like to make the method private, but get a warning by the IDE that the method is not being used.
Well, it is being used - using reflection.
What annotations are there to indicate that a member/method is being used by reflection?

Identify annotation class in java

I have a utility that scans different classes in the classpath, and checks for unused classes.
Some classes need special treatment, like interfaces and abstract classes. Those can be easily identified using reflection.
The problem arises when trying to identify a class that is an annotation class (i.e. #interface).
Is it possible to know if a class is really an annotation?
Yeah.. It's possible to know if a class is really an annotation.
Please try this:
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html#isAnnotation%28%29
http://java.dzone.com/articles/discovering-java-annotations
It may help you

Can annotations totally replace Marker Interfaces?

As marker interfaces are mostly useful for just marking a class, the same thing can be achievable through annotations. For example Cloneable interface can be #Cloneable.
So is there still need for marker interfaces or can be relpaced by Annotations? Is there any advantage/disadvantage of using any of them? I mean prefer one over other?
Marker interfaces are better than annotations when they're used to define a type. For example, Serializable can be used (and should be used) as the type of an argument that must be serializable. An annotation doesn't allow doing that:
public void writeToFile(Serializable object);
If the marker interface doesn't define a type, but only meta-data, then an annotation is better.
One more thing to mention would be the cost of using annotations. To check if object is an instance of an interface one can use instanceof which is a relatively low-cost operation nowadays. Using annotations requires Java reflection calls and is far more costly.

Composition vs Inheritance

I have a Validator interface with one method signature: public List<ParameterError> validateParameters(Parameters parameters); Various classes implement this interface.
There are also some common methods like isInteger, isInRange, etc. Should I have these methods in a ValidationHelper and use composition in the classes implementing the Validator interface, or should I make Validator an abstract class, put these methods there and use inheritance instead of composition?
I am assuming isInteger, isInRange etc. can be used even outside of the Validator classes if they are implemented generically. In that case, they should go into a separate util class, and that class should simply be called Utils rather than ValidationHelper. Classes/methods should always be named in the broadest context where you can use them.
Your helper methods are good candidates for static methods because they are pure functions and they are not expected to be polymorphic. You should use a utility class and put all such functions in it. This results in the least coupling: neither composition, nor inheritance; just pure dependency on the methods themselves.
I'd prefer composition over inheritance since the classes implementing Validator may not necessarily share an is-a relationship. They're more likely to be interested in the methods provided by the Validator interface.
Also, is it not possible to have the generic (isInteger etc) methods implemented in the interface Validator and marked final? They could even be made made static along the lines of this question.

How do I extend Java classes by Reflection?

I have two Strings:
String a="org.test.A";
String b="org.test.B";
I get a class by Reflection
Class aClass = Class.forName(a);
I want aClass extends b, like:
Class okClass=aClass.extends(b);//how to implement this?
how to implement this?
how to get okClass?
thanks!
Apart from using a JDK dynamic proxy, which works only by interface, you can use CGLIB or javassist for extending classes at runtime.
Generally speaking you want to be able to create new classes (not objects) at runtime. You can use bytecode engineering or java compiler api for that.
You could start by reading up on dynamic proxies. Proxies do not extend classes, but they do implement interfaces which you can map on your class implementation through the invocation handler.
As said before I'd recommend to look at CGLIB but there is also javassist which is a class library for editing bytecodes...

Categories

Resources