6

I was recently making a Utilities class with a couple of private variables which I need to make sure will never get changed even if someone tries using reflection.

So I was wondering, is there any way to make a variable immune to reflection?

  • 4
    You simply cannot. If someone is trying to use reflection to break something, there is nothing you can do about it as they will find a way around it. Just state that if someone uses reflection on them, the class breaks, and thus they should not do so. – Pokechu22 Nov 01 '14 at 00:20
  • @Pokechu22 That makes me sad :( but thank you for the information nonetheless! –  Nov 01 '14 at 00:23
  • 2
    @bernhardkiv why? I can do whatever I want to with my machine. Reflection just makes it easier for me. If you absolutely positively do not want someone misusing your code, then serve it up as a service. – emory Nov 01 '14 at 00:25
  • @emory Good point, but I think I will just stick to having a big notice in the Readme file saying not to tamper with any variables :) –  Nov 01 '14 at 00:27
  • 1
    @bernhardkiv - There is no need for that. Just assume that people who use your code are intelligent, and that if they decide to do things like using reflection to break abstraction, they have a good reason for doing so. A standard liability disclaimer (in your license) is all that is really necessary. If you say "use my code at your own risk", then that covers using it in ways that you think are a bad idea. – Stephen C Nov 01 '14 at 01:08
  • @bernhardkiv Besides, if someone wanted to break your code, they could just use [reflection on the `Integer` class and make 2 + 2 = 5](http://codegolf.stackexchange.com/a/28818/32531). You can't stop people from breaking stuff like that. – Pokechu22 Nov 02 '14 at 23:27

0 Answers0