Creating Java class unconfiguration via the Reflection API

I have a Java class similar to the following:

public final class Node { public Node() {} } 

I already learned how to change the accessibility change of the โ€œfinalโ€ fields through the reflection API, but is this also possible for classes? Can I turn the last class into a non-finite class at runtime?

+6
source share
2 answers

You can overwrite a class file using the ASM library.

It is not possible to change the status of a final class at runtime because it must be non-final at compile time to compile a subclass.

+4
source

I donโ€™t see how this can work, because the compiler checks to see if the class you are trying to make is not final at compile time. You will get an error if you try to inherit it.

I think itโ€™s all right to ask a question if you are interested, but the question that you ask yourself is why you want to do this? Reflection allows you to do many things to get around the final, private, etc., But this does not mean that it is a good idea. If a third-party library developer thought the final version was a good idea, you might be advised to abide by this.

+1
source

Source: https://habr.com/ru/post/916474/


All Articles