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);
}
}
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));
}
}
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"));
}
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());
}
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);
}
Aggregations