Java – how to convert a class to something like a class at run time?

I have a class similar to the following:

class FooClassAnalyser<T extends Foo> extends ClassAnalyser<T>

(classanalyzer is an abstract base class for many concrete implementations, while fooclassanalyzer is a specific implementation for t extending foo) It has a constructor as follows:

FooClassAnalyser(Class<T> classToAnalyse)

In another class, I have a static factory method of classanalysers, which calls the appropriate constructor according to the type of classtoanalyse:

static <U> ClassAnalyser<U> constructClassAnalyser(Class<U> classToAnalyse)

The function I want is to check u instanceof foo, then construct a fooclassanalyzer and return it, if so

However, I can't find a method suitable for Java type system Type erasure means we can't do anything smart with u directly However, the fact that we passed classtoanalyse as a parameter makes it possible to test whether u instanceof foo:

if (Foo.class.isAssignableFrom(classToAnalyse))

My problem is that, unlike instanceof, this "reflective instance" is invisible to the Java type system In particular, the constructor that directly passes classtoanalyse as a parameter to fooclassanalyzer will fail due to type mismatch, because Java does not know that classtoanalyse is actually a class < u extensions foo >

So far, the best solution I've found is to use unchecked cast to make classtoanalyse a class (it's actually checked, but Java doesn't realize it's checked) This can at least pass it as a parameter to the new fooclassanalyzer and get a fooclassanalyzer Object in return However, the problem is that this will not be converted back to classanalyzer < U >, because Java will not recognize the use of different generic bindings to construct classtoanalyse, but it will not change that the class object is still the same object (so it is still a class < U >); In other words, what all Java can see is fooclassanalyzer It cannot recognize that it is also a fooclassanalyzer < U >, so another unchecked cast is required for conversion back The result is compiled and run code, but there are many warnings about type safety

Most of the other things I've tried are syntax errors (for example, a variable of type < u extends foo > cannot be declared directly; Java cannot parse it correctly) It should be noted that I don't actually have any type of U-shaped object; I try to analyze the class itself, so only class < U > objects to use

Can you write such code in a type safe manner?

Solution

This happens whenever you pass a class < U > to constructClassAnalyser. You haven't provided us with much code, so I can't write a clear solution for you; However, if you can find a way to pass u to constructclassanalyzer instead of class < U >, it will be easier

Static < U > classanalyzer < U > constructclassanalyzer (U objecttoanalyse)

To check its class, you can use objecttoanalyse #getclass; If objecttoanalyse instanceof foo returns true, you can also use instanceof to verify the U extension foo, and then cast it to their respective classes when necessary

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>