Search in sources :

Example 16 with InvocationHandler

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));
}
Also used : Method(java.lang.reflect.Method) OracleConnection(oracle.jdbc.OracleConnection) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 17 with InvocationHandler

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();
    }
}
Also used : MethodInterceptor(net.sf.cglib.proxy.MethodInterceptor) Enhancer(net.sf.cglib.proxy.Enhancer) MethodProxy(net.sf.cglib.proxy.MethodProxy) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) Test(org.junit.Test)

Example 18 with InvocationHandler

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;
}
Also used : DatabaseFieldConfig(com.j256.ormlite.field.DatabaseFieldConfig) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 19 with InvocationHandler

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);
}
Also used : UncaughtExceptionHandler(com.sun.jna.Callback.UncaughtExceptionHandler) InvocationHandler(java.lang.reflect.InvocationHandler) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 20 with InvocationHandler

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);
}
Also used : AccessibleObject(java.lang.reflect.AccessibleObject) Method(java.lang.reflect.Method) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) 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