java - Accessing a class with the exact same name -


I have a difficult question and I'm not sure it is also possible in Java. I am in the following situation:

I have got a category which uses another class, let's call it B. Now, I'm trying to write the third class (and I do not like it), I call it again (to make sure that 'b' is what the square means, I'll put it in the rest of this post B1 and B2 would say, okay?). All three classes live in three different jar files so far all is well.

In general, class A finds class B (1) on A classpath and will use it. But now I am putting JR-files in which B (2) was started at the beginning of the class, so Class A would get this class instead of the old class. So my class B can work just like the old class B (1) (which is a library in reality, I can not customize it any other way). That's why I'm doing this ...).

And here comes my problem: In my class B (2) I want to load the actual square B (1) and use it. I can do this by using reflection so far I can also call methods through reflection, but I can not put an example of a class filled in a B reference. There is an exception:

  is incompatible with java.lang.ClassCastException: my.a.ClassB my.a.ClassB   

Any one idea Is that how can I use class B (1) in class B (2)?

You are happy with any workspace, it can only be done through reflection.

As far as the runtime system is concerned, your two classes classes are completely different bodies and it can not be put in one another.

This sometimes happens in the OSGi environment (which has complex classloader setup) or if you somehow manage to cross data between web application references.

If you want a general interface to call methods without any reflection, then you need to: A common interface (or parent class) and the interface to be in a jar file These two can see the Classbie.

Example:

First Jar: Interface I

Second Jar: Implementation of Class B I

Third Jar: Class Implementation of B I

Now you have two versions of Class B, but they can be both I can be inserted.

Needless to say, you should get a better solution to whatever problem you have.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -