use of java.lang.reflect.InvocationHandler in project druid by alibaba.
the class OracleUtilsTest method test_oracle.
public void test_oracle() throws Exception {
InvocationHandler handler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("pingDatabase")) {
return 1;
}
return null;
}
};
OracleConnection conn = (OracleConnection) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { OracleConnection.class }, handler);
Assert.assertNotNull(OracleUtils.unwrap(conn));
Assert.assertEquals(1, OracleUtils.pingDatabase(conn));
}
use of java.lang.reflect.InvocationHandler in project dubbo by alibaba.
the class ProxyTest method testCglibProxy.
@Test
public void testCglibProxy() throws Exception {
ITest test = (ITest) Proxy.getProxy(ITest.class).newInstance(new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println(method.getName());
return null;
}
});
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(test.getClass());
enhancer.setCallback(new MethodInterceptor() {
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
return null;
}
});
try {
enhancer.create();
} catch (IllegalArgumentException e) {
e.printStackTrace();
Assert.fail();
}
}
use of java.lang.reflect.InvocationHandler in project ormlite-android by j256.
the class DatabaseTableConfigUtil method buildConfig.
/**
* Instead of calling the annotation methods directly, we peer inside the proxy and investigate the array of
* AnnotationMember objects stored by the AnnotationFactory.
*/
private static DatabaseFieldConfig buildConfig(DatabaseField databaseField, String tableName, Field field) throws Exception {
InvocationHandler proxy = Proxy.getInvocationHandler(databaseField);
if (proxy.getClass() != annotationFactoryClazz) {
return null;
}
// this should be an array of AnnotationMember objects
Object elementsObject = elementsField.get(proxy);
if (elementsObject == null) {
return null;
}
DatabaseFieldConfig config = new DatabaseFieldConfig(field.getName());
Object[] objs = (Object[]) elementsObject;
for (int i = 0; i < configFieldNums.length; i++) {
Object value = valueField.get(objs[i]);
if (value != null) {
assignConfigField(configFieldNums[i], config, field, value);
}
}
return config;
}
use of java.lang.reflect.InvocationHandler in project jna by java-native-access.
the class Native method synchronizedLibrary.
/**
* Returns a synchronized (thread-safe) library backed by the specified
* library. This wrapping will prevent simultaneous invocations of any
* functions mapped to a given {@link NativeLibrary}. Note that the
* native library may still be sensitive to being called from different
* threads.
* <p>
* @param library the library to be "wrapped" in a synchronized library.
* @return a synchronized view of the specified library.
*/
public static Library synchronizedLibrary(final Library library) {
Class<?> cls = library.getClass();
if (!Proxy.isProxyClass(cls)) {
throw new IllegalArgumentException("Library must be a proxy class");
}
InvocationHandler ih = Proxy.getInvocationHandler(library);
if (!(ih instanceof Library.Handler)) {
throw new IllegalArgumentException("Unrecognized proxy handler: " + ih);
}
final Library.Handler handler = (Library.Handler) ih;
InvocationHandler newHandler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
synchronized (handler.getNativeLibrary()) {
return handler.invoke(library, method, args);
}
}
};
return (Library) Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), newHandler);
}
use of java.lang.reflect.InvocationHandler in project jOOQ by jOOQ.
the class Reflect method as.
/**
* Create a proxy for the wrapped object allowing to typesafely invoke
* methods on it using a custom interface
*
* @param proxyType The interface type that is implemented by the proxy
* @return A proxy for the wrapped object
*/
@SuppressWarnings("unchecked")
public <P> P as(Class<P> proxyType) {
final boolean isMap = (object instanceof Map);
final InvocationHandler handler = new InvocationHandler() {
@SuppressWarnings("null")
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String name = method.getName();
// Actual method name matches always come first
try {
return on(object).call(name, args).get();
}// [#14] Emulate POJO behaviour on wrapped map objects
catch (ReflectException e) {
if (isMap) {
Map<String, Object> map = (Map<String, Object>) object;
int length = (args == null ? 0 : args.length);
if (length == 0 && name.startsWith("get")) {
return map.get(property(name.substring(3)));
} else if (length == 0 && name.startsWith("is")) {
return map.get(property(name.substring(2)));
} else if (length == 1 && name.startsWith("set")) {
map.put(property(name.substring(3)), args[0]);
return null;
}
}
throw e;
}
}
};
return (P) Proxy.newProxyInstance(proxyType.getClassLoader(), new Class[] { proxyType }, handler);
}
Aggregations