开发者

dynamic proxy in gwt

It is possible to create dynamic proxy in gwt? I want create library which changes behaviour of methods of some class (interface will be parameter), for example, when user call method of class implementing interface, it will return always String "abc". In java it would be something like this:

public class proxyHandler implements InvocationHandler { 
    private Object proxied; 
    public proxyHandler(Object proxied) { 
            this.proxied = proxied; 
    } 
    public Object invoke(Object proxy, Method method, Object[] args) 
                    throws Throwable { 
            return "abc"; 
    } 
}
public static void main(String[] args) {
    Object object = new Object();
    Interface proxy = (Interface) Proxy.newProxyInstance(
            Interface.class.getClassLoader(),
            new Class[] { Interface.class }, new proxyHandl开发者_如何转开发er(object));
    System.out.println(proxy.getName());
}

It is possible to do something like this in gwt (using deferred binding, whatever)? Thanks for any help


But I don't want to create code for MyClassOne or MyClassTwo. I want to create dynamic proxy - create one class (like proxyHandler in java example) which serves any type of object. I wany to user give me an interface/class and I return him an object (like Interface proxy above) which he can use like normal class, but it would do something more.


GWT reflection is not updated from 2007. I reviewed also gwittir and gwt-ent but I cannot find any information how to dynamic proxies in them


Yes, GWT provides a way to "plug-in" you own implementation of classes and interfaces. This is done via generators.

An example:

public class MyGenerator extends Generator{

    public String generate(TreeLogger logger, GeneratorContext context, 
                           java.lang.String typeName)
                           throws UnableToCompleteException {

        if (someCondition) {
            return "com.mypackage.MyClassOne"
        } else {
            return "com.mypackage.MyClassTwo"
        }
    }
}

then use it in your .gwt.xml file:

<generate-with class="com.mypackage.MyGenerator">  
    <when-type-assignable class="com.otherpackage.SomeClass" />  
</generate-with> 

So when SomeClass is needed, generator will plug in MyClassOne or MyClassTwo based on some condition.

If you just need to replace one class/interface with your implementation, then you don't need to use generator, just use <replace-with> in your gwt module declaration:

<replace-with class="com.mypackage.MyClass">               
    <when-type-is class="com.otherpackage.SomeClass"/>
</replace-with>

Take a look at this blogposts for more info on generators:

http://blog.jdevelop.eu/2010/01/17/use-generators-to-create-boilerplate-code-in-gwt-20/

http://blog.jdevelop.eu/2010/01/17/use-generators-to-create-boilerplate-code-in-gwt-20/


AFAIK you can't: you don't have reflection on GWT. There exists GWT reflection, but I never used it.


You could use GWT AutoBeans for this. All non getter/setter methods are handled by Categories that can have method Interceptors:

A category implementation may additionally declare an interceptor method to examine and possibly replace the return values of all non-void methods in the target interface:

public static <T> T __intercept(AutoBean<?> bean, T returnValue) {
  // Do stuff
  return maybeAlteredReturnValue;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜