Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I suspect, annotations are not added dynamically to the proxy instances. (probably because it is not straightforward). However it is possible to get the annotations from actual method instance during invocation (or filtering). For example,</p> <pre><code>import static java.lang.annotation.ElementType.PARAMETER; import static java.lang.annotation.RetentionPolicy.RUNTIME; import java.lang.annotation.Annotation; import java.lang.annotation.Retention; import java.lang.annotation.Target; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import javassist.util.proxy.MethodHandler; import javassist.util.proxy.ProxyFactory; import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; public class ProxyTester { @Target({ PARAMETER }) @Retention(RUNTIME) public static @interface TestAnnotation {} public static interface IProxyMe { void aMethod(@TestAnnotation int param); } public static class ProxyMe implements IProxyMe { public void aMethod(@TestAnnotation int param) { System.out.println("Invoked " + param); System.out.println("-----------------"); } } static void dumpAnnotations(String type, Object proxy, Object forObject, String forMethod) { String className = forObject.getClass().getName(); System.out.println(type + " proxy for Class: " + className); for(Method method : proxy.getClass().getMethods()) { if(method.getName().equals(forMethod)) { printAnnotations(method); } } } static void printAnnotations(Method method) { int paramCount = method.getParameterTypes().length; System.out.println("Method: " + method.getName() + " has " + paramCount + " parameters"); for(Annotation[] paramAnnotations : method.getParameterAnnotations()) { System.out.println("Annotations: " + paramAnnotations.length); for(Annotation annotation : paramAnnotations) { System.out.println(" Annotation " + annotation.toString()); } } } static Object javassistProxy(IProxyMe in) throws Exception { ProxyFactory pf = new ProxyFactory(); pf.setSuperclass(in.getClass()); MethodHandler handler = new MethodHandler() { public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable { if(thisMethod.getName().endsWith("aMethod")) printAnnotations(thisMethod); return proceed.invoke(self, args); } }; return pf.create(new Class&lt;?&gt;[0], new Object[0], handler); } static Object cglibProxy(IProxyMe in) throws Exception { Object p2 = Enhancer.create(in.getClass(), in.getClass().getInterfaces(), new MethodInterceptor() { public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable { printAnnotations(arg1); return arg3.invokeSuper(arg0, arg2); } }); return p2; } static Object jdkProxy(final IProxyMe in) throws Exception { return java.lang.reflect.Proxy.newProxyInstance(in.getClass().getClassLoader(), in.getClass().getInterfaces(), new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { printAnnotations(method); return method.invoke(in, args); } }); } public static void main(String[] args) throws Exception { IProxyMe proxyMe = new ProxyMe(); IProxyMe x = (IProxyMe) javassistProxy(proxyMe); IProxyMe y = (IProxyMe) cglibProxy(proxyMe); IProxyMe z = (IProxyMe) jdkProxy(proxyMe); dumpAnnotations("no", proxyMe, IProxyMe.class, "aMethod"); dumpAnnotations("javassist", x, IProxyMe.class, "aMethod"); dumpAnnotations("cglib", y, IProxyMe.class, "aMethod"); dumpAnnotations("jdk", z, IProxyMe.class, "aMethod"); System.out.println("&lt;&lt;&lt;&lt;&lt; ---- Invoking methods ----- &gt;&gt;&gt;&gt;&gt;"); x.aMethod(1); y.aMethod(2); z.aMethod(3); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload