The first code snippet is all in the JDK. It uses the java.lang.reflect.Proxy 
class to handle everything. This class dynamically implements interfaces at 
runtime and uses a MethodHandler (or something like that) to handle call to the 
dynamic implementation of the interface.

The second code snippet is just some thoughts on a Guice enhancement that would 
provide the same type of functionality without using the 
java.lang.reflect.Proxy. None of the code in the second code snippet currently 
exists.

-bp


On Nov 18, 2009, at 8:45 PM, Ryan wrote:

> Brian,
> 
> Thank you for the example.  One question: as I was trying to follow
> your example, I couldn't locate a Proxy class that accepts a
> generified type (Proxy<T>).  I assume the class you're using there is
> the one in java.lang.reflect?  Or is it a Guice class that I've missed
> somehow?
> 
> In any case, your pattern there looks very similar to the one Sam
> suggested, so I think I get the idea.
> 
> Ryan
> 
> On Nov 18, 3:09 pm, Brian Pontarelli <[email protected]> wrote:
>>> @Brian:
>> 
>>> Can you give me an example of using dynamic proxies, as you explained?
>> 
>> You create a dynamic proxy that implements your interface and then the 
>> MethodHandler does a lookup based on the parameters passed in. Looks roughly 
>> like:
>> 
>> Service s = Proxy.newProxy(Service.class, new MyHandler());
>> bind(Service.class).toInstance(s);
>> 
>> class MyHandler implements MethodHandler {
>>   Object call(Object[] params) {
>>     if (params[0].equals("foo")) {
>>       Service realService = ...; // Do lookup here
>>       return realService.doSomething((String) params[0]);
>>     }
>>     ...
>>   }
>> 
>> }
>> 
>> This uses the parameters passed in to the service to do the lookup and find 
>> the correct implementation to use. It then invokes the real service and 
>> passes the parameters along.
>> 
>> I like this pattern and it could be wired into Guice. Be nice to do 
>> something like:
>> 
>> bind(Service.class).toProxy(MyServiceProxy.class);
>> 
>> Service s = Injector.newInstance(Service.class);
>> s.doSomething(42);
>> 
>> class MyServiceProxy implements Proxy<Service> {
>>   @Inject FourtyTwoService fourtyTwoService;
>>    ... // Other implementations could be injected here
>> 
>>   Service lookup(Object[] args) {
>>     // Do lookup here using arguments passed to the service
>>     int number = (int) args[0];
>>     if (number == 42) {
>>       return fourtyTwoService;
>>     }
>>     ...
>>   }
>> 
>> }
>> 
>> Guice would use the proxy to find the real instance and then invoke the real 
>> instance instead. This is essentially the same as above, but everything is 
>> wired together and handled by Guice.
>> 
>> -bp
> 
> --
> 
> You received this message because you are subscribed to the Google Groups 
> "google-guice" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/google-guice?hl=.
> 
> 

--

You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=.


Reply via email to