Answer the question
In order to leave comments, you need to log in
Is it okay to call private methods with certain annotations using reflection?
Hello!
There is a simple Test class that defines a private method, say test.
This method is marked with the @ Annot annotation.
Is it normal (from all points of view):
- to get such a method using reflection
- method.setAccessible(true);
- to call
?
The fact is that this class also declares some public methods that can be called from other places. At the same time, it is necessary that the method with Annot cannot be called.
This is necessary for such a situation: methods with the Annot annotation will be called (by the application core) if a command has come from the user, while other objects should not have the right to call this method (there is a crutch using cglib that checks if there is an annotation on the method - throws exception).
P.s. do not offer a solution with a subscription to events ... pliz)
Answer the question
In order to leave comments, you need to log in
Why not make the class implement two interfaces? Well, or even one - a class with all methods and an interface with "completely public methods".
interface A{ int getX(); }
class B implements A {
private int x;
int getX(){return x;}
void setX(int v){x = v;}
}
Reflection is only allowed for tool developers and, in rare cases, framework developers.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question