Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing/returning a class with type parameter to a function

Tags:

java

generics

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.

like image 692
Jirka Avatar asked Jun 03 '12 10:06

Jirka


People also ask

How do I return a class type?

The getReturnType() method of Method class Every Method has a return type whether it is void, int, double, string or any other datatype. The getReturnType() method of Method class returns a Class object that represent the return type, declared in method at time of creating the method.

How do you pass a class object as a parameter?

To pass an object as an argument we write the object name as the argument while calling the function the same way we do it for other variables. Syntax: function_name(object_name); Example: In this Example there is a class which has an integer variable 'a' and a function 'add' which takes an object as argument.

Which method returns class type of the argument passed as parameter?

clone() method or define a constructor that takes an object of its class as a parameter.

Can the return value of a method be used as a parameter?

In general you can use the return value as a method parameter in the same way as you can use any variable.


1 Answers

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;
like image 62
Ben Schulz Avatar answered Sep 30 '22 14:09

Ben Schulz