Is it possible to extend in a class via reflection in java?

1

I work with an API that changes the class name and package name whenever a big update comes out, however, I'm forced to support older versions of the API and for that I use reflections in these cases, I always I have worked with Class.forName() and I have always been able to "dribble" this problem from different class and package names, however I came across a situation where I need to create a class X and extend a class Y but the name of this class Y is different depending on the version . Is it possible to extend a class via reflection?

    
asked by anonymous 16.12.2018 / 01:56

2 answers

2

As I understand your problem, you want to extend a runtime class.

I think you can use CGlib for this:

  

cglib is a powerful, high performance and quality Code Generation   Library, It is used to extend JAVA classes and implements interfaces    at runtime .

    
17.12.2018 / 13:07
3
  

I work with an API that changes the class name and package name whenever a big update comes out

No, you do not work with an API. APIs are stable. This is a gambiarra that someone who does not understand what API is called an API, or even an actual API and you are using it as if it were one.

Nearly all reflection use cases are wrong. If you know the name you need to use does not need reflection, if you do not know you can not do much useful there. You can print everything without special intelligence or do something very standardized.

I do not know how you solved this, the question does not make it clear about how this is, but you almost certainly do not need the reflection for it. Now that you need to inherit in someone else it becomes clearer that it is not what you want. You are now trying to resolve something in the runtime that should be resolved at compile time. Certainly there is a solution, but it is such a big game that it is better not to use it. There is no specific reflection feature that creates an extension, but it is possible.

I would review even the decision to use reflection. I'm just not going to make sure that I do not have a better solution without it because I do not know what the problem is, but I've been able to eliminate every reflection someone has made to date, and almost every case was a clearly better solution, / p>     

16.12.2018 / 12:24