Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31328

Re: Dynamic proxy causes downstream code to not find annotations. How to solve?

From Steven Simpson <ss@domain.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Dynamic proxy causes downstream code to not find annotations. How to solve?
Date 2015-03-17 14:24 +0000
Organization A noiseless patient Spider
Message-ID <o1qltb-jt8.ln1@s.simpson148.btinternet.com> (permalink)
References <9a31318b-4de7-42cb-9d29-202a973ae557@googlegroups.com> <4f429c73-7025-44e2-aadb-f971fbeba006@googlegroups.com>

Show all headers | View raw


On 16/03/15 14:44, rupertlssmith@googlemail.com wrote:
> class Test implements ITest<Integer> {
>      public Integer someMethod() {
>          return 1;
>      }
> }
>
> interface ITest<T> {
>      T someMethod();
> }
>
> And got this output:
>
> public java.lang.Integer org.mygovscot.stars.client.Test.someMethod()
> public java.lang.Object org.mygovscot.stars.client.Test.someMethod()
> (... other methods on Object)

(I'm confused about your problem now, because I thought you didn't have 
anything explicitly implementing your interface...)

In the invocation handler, can you not look up the method on your target 
object?  The simple way would be:

   obj.getClass().getMethod(method.getName(), method.getParameterTypes())

However, it might not find a method if your interface declares:

   void myMethod(String s);

...while the class declares:

   void myMethod(T s); // in MyClass<T>

...and the instance is a MyClass<String>.

So, more generally, can you do your own look-up along these lines?:

     private static Method findMethod(Class<?> clazz,
                                      String name, Class<?>[] types)
         throws NoSuchMethodException {
         next_method:
         for (Method meth : clazz.getMethods()) {
             if (!meth.getName().equals(name)) continue;
             if (meth.getParameterCount() != types.length) continue;
             Class<?>[] params = meth.getParameterTypes();
             for (int i = 0; i < types.length; i++) {
                 if (!params[i].isAssignableFrom(types[i]))
                     continue next_method;
             }
             return meth;
         }
         throw new NoSuchMethodException();
     }



-- 
ss at comp dot lancs dot ac dot uk

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Dynamic proxy causes downstream code to not find annotations. How to solve? rupertlssmith@googlemail.com - 2015-03-16 05:10 -0700
  Re: Dynamic proxy causes downstream code to not find annotations. How to solve? rupertlssmith@googlemail.com - 2015-03-16 07:44 -0700
    Re: Dynamic proxy causes downstream code to not find annotations. How to solve? Robbie Brown <rob@example.com> - 2015-03-16 16:33 +0000
    Re: Dynamic proxy causes downstream code to not find annotations. How to solve? Steven Simpson <ss@domain.invalid> - 2015-03-17 14:24 +0000

csiph-web