S
S
synapse_people2018-04-16 22:08:42
Java
synapse_people, 2018-04-16 22:08:42

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

2 answer(s)
L
lgorSL, 2018-04-17
@synapse_people

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

I don’t explicitly give class B outward, say that it’s just “some implementation” of interface A. In the core, store it exactly as an object of class B and call whatever you want.

S
Sergey Gornostaev, 2018-04-17
@sergey-gornostaev

Reflection is only allowed for tool developers and, in rare cases, framework developers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question