A
A
Antony2019-09-18 10:59:21
Java
Antony, 2019-09-18 10:59:21

[I feel weird] Is it possible to change a class when importing into Java?

There is a Jar file. It has classes A and B. Class A uses class B internally. And there, let's say, everything is so nailed down that the methods from B are used almost in every line (I exaggerate). There's something like logging, ie. from replacing class B with a dummy, the functionality of class A will not suffer at all.
Is it possible, without making changes to the Jar, to use class A in my program so that fakeB is used instead of B? (for example, with a different implementation of behavior, or generally as a stub)
If necessary, the source codes of classes A and B can be corrected, there is such a possibility, but nevertheless I want exactly A and B to be in the jar
. comes than to make interfaceB, normalB, fakeB and explicitly indicate in the constructor of class A what to use when creating.
ps
Perhaps I want something strange and I need to do something completely different.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Alexandrov, 2019-09-18
@jamakasi666

Smoke reflections, classloaders, agents. Choose what suits you best.

A
Alexander, 2019-09-18
@cudu

The simplest solution is to create a package in your project that contains class b, and in this package create a fakeB class with signature class b. All this will take off, but to understand - read the classloaders.
ps When using any devtools from spring when debugging and developing on a dev machine, it may not take off.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question