Relative Content

Tag Archive for javareflection

Using reflection to modify only own class without “Illegal Access”

If I want to automate building of an object of my class from some Map or JSON I can use reflection inside some method or constructor of this class. To modify only fields of the method’s object, to which it has access. But in this case, I still need to use field.setAccessible(true) for private fields. As I see it, in this case, I don’t need to use “Illegal Access”. Even more, I’d prefer that compiler ensure my code doesn’t rely on “Illegal Access”. Is there a mechanism of more precise control of which rules reflection breaks? And if not, what is the reason?