Passing/returning a class with type parameter to a

2020-07-16 09:18发布

Let's assume the following method (say from Guava's Iterables):

public static <T> Iterable<T> filter(final Iterable<?> unfiltered, final Class<T> type) {
    return null;
}

and this collection:

Set<?> objs = ...;

then the following code compiles and the generics are correctly derived

Iterable<String> a2 = Iterables.filter(objs, String.class);

(In Guava this would return an iterable of all Strings in objs.)

But now lets assume the following class:

static class Abc<E> {
    E someField;
}

I have no idea how to call filter and get Iterable<Abc<?>>:

Iterable<Abc>    a3 = Iterables.filter(objs, Abc.class);
Iterable<Abc<?>> a4 = Iterables.filter(objs, Abc.class); // Compile error - Abc and Abc<?> are incompatible types
Iterable<Abc<?>> a5 = Iterables.filter(objs, Abc<?>.class); // Compile error
Iterable<Abc<?>> a6 = Iterables.<Abc<?>>filter(objs, Abc.class); // Compile error
Iterable<Abc<?>> a7 = (Iterable<Abc<?>>) Iterables.filter(objs, Abc.class); //  Compile error - inconvertible types
Iterable<Abc<?>> a8 = Iterables.filter(objs, new Abc<?>().getClass()); // Compile error
Iterable<Abc<?>> a8a = Iterables.filter(objs, new Abc<Object>().getClass()); // Compile error

Only a3 compiles, but then I do not have the parameter on Abc, and thus no generic type checking in subsequent code is done.

I know that the type parameters are not present at runtime and so I do not atempt to write code like:

Iterable<Abc<String>> a9 = Iterables.filter(objs, Abc<String>.class); // Compile error

I just want to filter all objects of the type Abc (as a3 does) but having the generic parameter in the result. The only way of doing this I found is the following, which is silly:

Iterable<Abc<?>> a10 = new HashSet<Abc<?>>();
for (Abc<?> a : Iterables.filter(objs, Abc.class)) {
    ((Set<Abc<?>>)a10).add(a);
}

Thanks.

标签: java generics
2条回答
Root(大扎)
2楼-- · 2020-07-16 09:21

There is no satisfactory answer to this question. Class literals for types parameterized with unbounded wildcards only would work out in theory, we just don't have them.

You can produce the Class<Abc<?>>-typed class object with an unchecked cast and move it to a utility method or field. As long as there are few Abcs, this works quite nicely.

@SuppressWarnings("unchecked")
public static Class<Abc<?>> ABC = (Class<Abc<?>>)(Object) Abc.class;
查看更多
该账号已被封号
3楼-- · 2020-07-16 09:28

I found this compiles..

public static <T> Iterable<Abc<T>> myFilter (Iterable<Abc<T>> myIterator) {
    Class<Abc<T>> myClass = null;
    Iterable<Abc<T>> a3 = Iterables.filter(myIterator, myClass); 
    return a3;
}

But it just moves the same problem up to the caller of myFilter(). I suspect Ben Schulz has the correct answer.

查看更多
登录 后发表回答