0

I tried to make a reflection to my code but I came across a problem because I had to extend class with reflected class, it was something like this:

I have this classes reflected:

"some.package.a.SomeClass"
"some.package.b.SomeClass"

and now I need to extend another class with one of them

public MyClass extends SomeClass {
    @Override
    public Object...

is there any way that I can achieve this?

Dici
  • 25,226
  • 7
  • 41
  • 82
  • 1
    Can you give more details on what this is meant to achieve? – flakes Sep 23 '18 at 11:16
  • 3
    Reflection is something you do at runtime, extending classes is usually done at/before compile time. Are you using runtime byte code generation (cglib, byte buddy, ...)? – Hero Wanders Sep 23 '18 at 11:25
  • aw, if extending is done in compiling process then I cant do this, thanks for explaining... – DisplayName Sep 23 '18 at 11:30
  • Possible duplicate of [Extending class at runtime](https://stackoverflow.com/questions/19233923/extending-class-at-runtime) – Joe Feb 02 '19 at 13:01

1 Answers1

0

Using off-the-shelf Java, you cannot extend a class given to you only through reflection. There are third-party libraries that let you overcome this restriction (see this Q&A).

Extending a class requires defining a new class, and compiling it into bytecode. Reflection happens at run-time. It is inherently a read-only API, so you cannot emit new classes with it.

Implementing an interface gives you another option of using a Proxy, but you cannot specify your own base class.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523