1 Reply Latest reply on Apr 12, 2010 5:21 PM by glammy

    Create a proxy of proxy

    glammy

      I'm using the following code to create a proxy:

       

       

      public Object createProxiedInstance(Object originalInstance) throws Exception {
          Class<?> originalClass = instance.getClass();
          ProxyFactory factory = new ProxyFactory();

          factory.setSuperclass(originalClass);

          factory.setHandler(new MethodHandler() {..});
          Class<T> proxyClass = factory.createClass();

          return proxyClass.newInstance();
      }

       

      It works fine if a pass a normal object. However, if I pass an object that is already a proxy (created by the same method, but with a different method handler), I get:

       

      javassist.bytecode.DuplicateMemberException: duplicate method: setHandler  in com.mypackage.Bean_$$_javassist_0_$$_javassist_1

       

      I could work this around by combining the method handler of the original proy and the one of the new proxy, but is there a way to make proxies of proxies, as if the proxies were normal objects? (using javassist 3.8.0 GA)

        • 1. Re: Create a proxy of proxy
          glammy

          The problem was (actually, it's the same with  CGLIB - I tried it using commons-proxy) that I should not try to create a  proxy class of the proxy class. The second proxy should again be of the  original class. So adding the following line resolves the problem:

          if (instance instanceof ProxyObject) {
              originalClass = originalClass.getSuperclass();
          }