-2

Is it possible to ignore java access modifiers during compilation? (besides the javac plugin) I have a java agent that should affect modifiers in runtime. I would be interested in trying to compile the files by tricking (or somehow asking it not to look at the modifiers) the compiler.

Edit: I managed to do one thing: I created a mask class for the target class, made changes to it. Using shadow jar, I moved the mask and then deleted it. Using java agent, I loaded my class instead of the target one. And yes, it works. Perhaps someone will find some other options.

Question_283
  • 11
  • 1
  • 4
  • 2
    This seems like a [XY problem](https://xyproblem.info/), why do you want this? – Rick Dec 19 '22 at 14:54
  • I don't have a specific goal, however, I'm wondering if it's possible to do this. as far as I know, if I show the compiler that the field is available, and in runtime it will be private, it will cause an error only in runtime. Perhaps it would be possible to somehow create a "mask" for the compiler. – Question_283 Dec 19 '22 at 15:00
  • Apart from all the other problems, even if one *could* do this it would break with `private`, because contrary to all other modifiers `private` methods (and calls to them) are not polymorphic, i.e. the method calls actively behave differently, need different bytecode (at least [pre-Java 11](https://stackoverflow.com/questions/67226178/why-does-the-java-compiler-11-use-invokevirtual-to-call-private-methods)) and so on. – Joachim Sauer Dec 19 '22 at 15:06
  • But why would you want the compiler to accept something if it's only going to cause issues at runtime? – Rick Dec 19 '22 at 15:51
  • as I wrote above, I'm going to use java agent – Question_283 Dec 19 '22 at 16:14

1 Answers1

-1

I don't think what you ask is possible (but more research is needed). What you can do is to use reflection in your code.

For a field f in a class C, you can do:

C.class.getField("f").setAccessible(true)

See the Javadoc for reference