Is it possible to extend a class via java Reflection?

Asked

Viewed 148 times

0

I work with an API that changes the class name and package name whenever a major update comes out, however, I am required to support older versions of the API and for that I use Reflections in these cases, I have always worked with Class.forName() and I always managed to "circumvent" this problem of different class names and Packages, but I came across a situation where I need to create an X class and extend a Y class but the name of this Y class is different depending on the version. It is possible to extend a class via Reflection?

2 answers

3

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

No, you do not work with an API. Apis are stable. This is a scam that someone who doesn’t understand what API is called an API, or isn’t even a real API and you’re using it as if it’s a.

Almost all cases of use of reflection are wrong. If you know the name you need to use you don’t need reflection, if you don’t know you can’t do much useful there. You can print everything without a special intelligence or do something very standardized.

I don’t know how you solved it, the question doesn’t make it clear what it’s like, but it’s almost certain you don’t need the reflection for that. Now that you need to inherit in some other one, it becomes clearer that it’s not what you want. Now you want to solve something in temp ode execution that should be solved at compile time. Surely there is a solution, but it is a gambiarra so great that it is better not to use. There is no specific resource of reflection that creates an extension, but it is possible.

I would review even the decision to use reflection. I’m just not going to guarantee that you don’t have a better solution without it because I don’t even know what the problem is, but I was able to eliminate every thought that anyone has done to date, and almost every case was a clearly better solution, the others were debatable.

  • Whenever you have a Software you want your customers to use the most updated version right? So basically this "company" whenever it updates the software changes the path of the package so that people who use the "API" update their software as well. This software is currently in version 1.13.2 but the overwhelming majority of users (80%) prefer version 1.8.8. This way when creating my Software using this API I would need to Reflection to make my Software run in both versions.

  • I’ve worked a lot with Reflection, including, I’ve even posted a "mini-course" with 10 lessons on YT talking about Reflection, in which case I will choose to create 1 build for each version of the API, I honestly don’t like to do this, It’s like I created 1 Software and the Windows7 installer was different from 8 and 10... Thanks for the answer.

  • 1

    No, none of this makes sense. What certainly does not have in these classes is that all this is not necessary, reflection is only necessary when it depends on a user typing something and starting from that something in the code needs to be changed. To do this with some restraint continues to render reflection unnecessary, if to do so is highly reckless and completely unsafe. People use reflection because they don’t know other ways to do it, that’s all. When one learns another way he sees it is not necessary.

  • The programming is very broad and from what I see you are "locked" to this idea that reflection is only useful or necessary in these cases you know. As I explained, in my case I could very well generate 1 build of my Software for each version of the "API" but I can simply use reflection and generate only 1 build that runs in all versions.

  • 1

    I’ve been developing software for 35 years, I’ve done all kinds of software, I’ve studied deeply all the consequences of software decisions, I know reflection in all its details for having already created mechanisms of reflection and I am recognized for seeing what people do not see easily because they do not have the foundations, And this is one of the most obvious cases where you don’t need reflection because you know everything you need at compile time, and I’m the one locked in a fixed idea? Okay then, I did what I could, I’m sorry I didn’t talk you out of it, you can do as you see fit.

  • I guess I forgot to comment on the fact that I use reflection in at most 2 or 3 classes, there are few methods, if I were to generate 1 build for each version of the API I would have to create more than 30 different builds. (1983 ?)

Show 1 more comment

2


From what I understand of your problem, you want to extend a class at runtime (Runtime).

I believe you can use the Cglib for this:

cglib is a Powerful, high performance and quality Code Generation Library, It is used to extend JAVA classes and Implements interfaces Runtime.

  • Take a look at the comments I put up there. Thank you for the answer but I probably wouldn’t think about it. Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.