Search in sources :

Example 36 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project robovm by robovm.

the class WTInvocationHandler method main.

public static void main(String[] args) {
    WTMix mix = new WTMix();
    InvocationHandler handler = new WTInvocationHandler(mix);
    Object proxy;
    try {
        proxy = Proxy.newProxyInstance(WrappedThrow.class.getClassLoader(), new Class[] { InterfaceW1.class, InterfaceW2.class }, handler);
    } catch (IllegalArgumentException iae) {
        System.out.println("WT init failed");
        return;
    }
    InterfaceW1 if1 = (InterfaceW1) proxy;
    InterfaceW2 if2 = (InterfaceW2) proxy;
    try {
        if1.throwFunky();
        System.err.println("No exception thrown");
    } catch (UndeclaredThrowableException ute) {
        System.out.println("Got expected UTE");
    } catch (Throwable t) {
        System.err.println("Got unexpected exception: " + t);
    }
    try {
        if1.throwFunky2();
        System.err.println("No exception thrown");
    } catch (IOException ioe) {
        System.out.println("Got expected IOE");
    } catch (Throwable t) {
        System.err.println("Got unexpected exception: " + t);
    }
    try {
        if2.throwFunky2();
        System.err.println("No exception thrown");
    } catch (IOException ioe) {
        System.out.println("Got expected IOE");
    } catch (Throwable t) {
        System.err.println("Got unexpected exception: " + t);
    }
    /*
         * Throw exceptions, walking down the hierarchy.
         */
    try {
        if1.throwException();
        System.err.println("No exception thrown");
    } catch (UndeclaredThrowableException ute) {
        System.out.println("Got expected UTE");
    } catch (Throwable t) {
        System.err.println("Got unexpected exception: " + t);
    }
    try {
        if1.throwBase();
        System.err.println("No exception thrown");
    } catch (UndeclaredThrowableException ute) {
        System.out.println("Got expected UTE");
    } catch (Throwable t) {
        System.err.println("Got unexpected exception: " + t);
    }
    try {
        if2.throwSub();
        System.err.println("No exception thrown");
    } catch (SubException se) {
        System.out.println("Got expected exception");
    } catch (Throwable t) {
        System.err.println("Got unexpected exception: " + t);
    }
    try {
        if2.throwSubSub();
        System.err.println("No exception thrown");
    } catch (SubException se) {
        System.out.println("Got expected exception");
    } catch (Throwable t) {
        System.err.println("Got unexpected exception: " + t);
    }
    /*
         * Make sure that, if the class explicitly allows the base
         * class of an exception, that we still allow it.
         */
    try {
        if1.bothThrowBase();
        System.err.println("No exception thrown");
    } catch (BaseException se) {
        System.out.println("Got expected exception");
    } catch (Throwable t) {
        System.err.println("Got unexpected exception: " + t);
    }
}
Also used : IOException(java.io.IOException) InvocationHandler(java.lang.reflect.InvocationHandler) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException)

Example 37 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project robovm by robovm.

the class ProxyTest method test24846.

// https://code.google.com/p/android/issues/detail?id=24846
public void test24846() throws Exception {
    ClassLoader cl = getClass().getClassLoader();
    Class[] interfaces = { java.beans.PropertyChangeListener.class };
    Object proxy = Proxy.newProxyInstance(cl, interfaces, new InvocationHandler() {

        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            return null;
        }
    });
    for (Field field : proxy.getClass().getDeclaredFields()) {
        field.setAccessible(true);
        assertFalse(field.isAnnotationPresent(Deprecated.class));
    }
}
Also used : Field(java.lang.reflect.Field) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 38 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project spring-framework by spring-projects.

the class ModelMapTests method testRawJdkProxy.

@Test
public void testRawJdkProxy() throws Exception {
    ModelMap map = new ModelMap();
    Object proxy = Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] { Map.class }, new InvocationHandler() {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) {
            return "proxy";
        }
    });
    map.addAttribute(proxy);
    assertSame(proxy, map.get("map"));
}
Also used : Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) Test(org.junit.Test)

Example 39 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project spring-framework by spring-projects.

the class SerializableTypeWrapper method forTypeProvider.

/**
	 * Return a {@link Serializable} {@link Type} backed by a {@link TypeProvider} .
	 */
static Type forTypeProvider(final TypeProvider provider) {
    Assert.notNull(provider, "Provider must not be null");
    if (provider.getType() instanceof Serializable || provider.getType() == null) {
        return provider.getType();
    }
    Type cached = cache.get(provider.getType());
    if (cached != null) {
        return cached;
    }
    for (Class<?> type : SUPPORTED_SERIALIZABLE_TYPES) {
        if (type.isAssignableFrom(provider.getType().getClass())) {
            ClassLoader classLoader = provider.getClass().getClassLoader();
            Class<?>[] interfaces = new Class<?>[] { type, SerializableTypeProxy.class, Serializable.class };
            InvocationHandler handler = new TypeProxyInvocationHandler(provider);
            cached = (Type) Proxy.newProxyInstance(classLoader, interfaces, handler);
            cache.put(provider.getType(), cached);
            return cached;
        }
    }
    throw new IllegalArgumentException("Unsupported Type class: " + provider.getType().getClass().getName());
}
Also used : Serializable(java.io.Serializable) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 40 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project spring-framework by spring-projects.

the class AnnotationUtils method synthesizeAnnotation.

@SuppressWarnings("unchecked")
static <A extends Annotation> A synthesizeAnnotation(A annotation, Object annotatedElement) {
    if (annotation == null) {
        return null;
    }
    if (annotation instanceof SynthesizedAnnotation) {
        return annotation;
    }
    Class<? extends Annotation> annotationType = annotation.annotationType();
    if (!isSynthesizable(annotationType)) {
        return annotation;
    }
    DefaultAnnotationAttributeExtractor attributeExtractor = new DefaultAnnotationAttributeExtractor(annotation, annotatedElement);
    InvocationHandler handler = new SynthesizedAnnotationInvocationHandler(attributeExtractor);
    // Can always expose Spring's SynthesizedAnnotation marker since we explicitly check for a
    // synthesizable annotation before (which needs to declare @AliasFor from the same package)
    Class<?>[] exposedInterfaces = new Class<?>[] { annotationType, SynthesizedAnnotation.class };
    return (A) Proxy.newProxyInstance(annotation.getClass().getClassLoader(), exposedInterfaces, handler);
}
Also used : InvocationHandler(java.lang.reflect.InvocationHandler)

Aggregations

InvocationHandler (java.lang.reflect.InvocationHandler)179 Method (java.lang.reflect.Method)97 InvocationTargetException (java.lang.reflect.InvocationTargetException)24 Test (org.junit.Test)19 IOException (java.io.IOException)11 Proxy (java.lang.reflect.Proxy)10 Field (java.lang.reflect.Field)9 AccessibleObject (java.lang.reflect.AccessibleObject)7 ArrayList (java.util.ArrayList)6 NotNull (org.jetbrains.annotations.NotNull)6 Support_Proxy_I1 (tests.support.Support_Proxy_I1)6 Callable (java.util.concurrent.Callable)5 MBeanServerInvocationHandler (javax.management.MBeanServerInvocationHandler)5 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)3 RemoteObjectInvocationHandler (java.rmi.server.RemoteObjectInvocationHandler)3 Connection (java.sql.Connection)3 SQLException (java.sql.SQLException)3 HashMap (java.util.HashMap)3