Java type inference with erasure

Stas

having some problem with erasure & type inference. I have the following class hierarchy, which doesn't look very complicated:

public class Foo<T> {

}

public class Bar<T> {
    private final Class<T> clazz;

    public Bar(Class<T> clazz) {
        this.clazz = clazz;
    }
}

And what I'm trying to do is something like this:

Bar<Foo<?>> bar = new Bar<>(Foo.class);

which, of course, doesn't work, as Foo is not quite Foo<?>. The question is how to construct such a Bar? I need exactly Bar<Foo<?>>, not Bar<Foo>, as there is method which accepts only Bar<Foo<?>> as a parameter. Appreciate ideas.

rajuGT

You cannot create generic object without providing Type information in <>, as shown in your question post. You must supply a Type or use the raw version. If you are forced to use the wild card parameter type, then just suppress the warning by using raw type, as shown below

@SuppressWarnings("rawtypes")
Bar<Foo<?>> bar = new Bar(Foo.class);       

//Bar is having no type "<>" (but this is not recommended

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Generics type erasure in Java

From Dev

Overcome type erasure in Java

From Dev

type erasure in implementation of ArrayList in Java

From Dev

Type erasure unexpected behavior in Java

From Dev

show the result of type erasure in java

From Dev

Generics and type erasure in Java issue

From Dev

Type inference in java

From Dev

Java generics, type erasure and type of a generic member

From Dev

Java Generics - method after type erasure

From Dev

Using Java Generics without a type - Generic erasure?

From Dev

Java Type Erasure: Rules of cast insertion?

From Dev

Java var and inference type ambiguity

From Dev

Java Type Inference in Static Methods

From Dev

Java Generics, Type Inference, Inheritance?

From Dev

Generic type inference limits in Java

From Dev

Java var and inference type ambiguity

From Dev

Java type inference of generic exception type

From Dev

Does type erasure of Java Generics cause full type casting?

From Dev

Why is Java's type inference so weak?

From Java

A peculiar feature of exception type inference in Java 8

From Dev

Java 8 and Generalized Target-Type Inference

From Dev

Java 8: Generic type inference improvements

From Dev

Java ternary operator influence on generics type inference

From Dev

Confusion over Java generic method type inference

From Dev

Problems using generics and type inference in Java

From Dev

Reflection type inference on Java 8 Lambdas

From Dev

Very confused by Java 8 Comparator type inference

From Dev

Make java compiler output type inference information

From Dev

Java type inference with lower bounded types