DaveAKing / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Support for Proxying Generic Interfaces #1569

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The Reflection.newProxy(Class, InvocationHandler) method works well as long the 
client is proxying a raw interface that does not involve generics.  Type 
erasure becomes a problem for the client when a Class object for a generic 
interface is supplied.  

Would it be possible to supply another version of this method where the first 
argument is a TypeToken<T> rather than a Class<T> so the client can create 
proxy instances for an interface that involves generics without having to 
perform a cast?

Original issue reported on code.google.com by compulin...@gmail.com on 5 Nov 2013 at 9:39

GoogleCodeExporter commented 9 years ago
Ben, I thought that we must have discussed this before, but I couldn't find a 
record of it. What do you think? On the one hand, I get only a dozen hits in 
Google code when searching for '<.*[)].*newProxy(Instance)?[(]'. On the other 
hand, TypeToken support sounds logical, we don't require a lot of usages to 
justify most reflection utilities, and I like to make unchecked warnings go 
away. Of course, that's all assuming that this *is* safe.... I can never 
convince myself for sure with TypeToken :) And I guess you could argue that, 
since proxies necessarily use reflection, you're doing something "unchecked," 
anyway....

Original comment by cpov...@google.com on 5 Nov 2013 at 10:20

GoogleCodeExporter commented 9 years ago
It doesn't seem safe to me. For example:

static <T> List<T> sneakyCast(final List<?> list) {
  return newProxy(new TypeToken<List<T>>() {}, new InvocationHandler() {
    return method.invoke(list, args);
  });
}

List<String> stringList = new ArrayList<>();
List<Integer> intList = sneakyCast(stringList);
intList.add(1);

If user has to provide a Class (IntList.class for example), the line of 
method.invoke(list, args) will fail with CCE.

Original comment by be...@google.com on 5 Nov 2013 at 11:32

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:12

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:17

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08