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.
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.
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.
clone() method or define a constructor that takes an object of its class 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.
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 Abc
s, this works quite nicely.
@SuppressWarnings("unchecked")
public static Class<Abc<?>> ABC = (Class<Abc<?>>)(Object) Abc.class;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With