Search in sources :

Example 56 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project jdk8u_jdk by JetBrains.

the class Util method createProxy.

/**
     * Returns a proxy for the specified implClass.
     *
     * If both of the following criteria is satisfied, a dynamic proxy for
     * the specified implClass is returned (otherwise a RemoteStub instance
     * for the specified implClass is returned):
     *
     *    a) either the property java.rmi.server.ignoreStubClasses is true or
     *       a pregenerated stub class does not exist for the impl class, and
     *    b) forceStubUse is false.
     *
     * If the above criteria are satisfied, this method constructs a
     * dynamic proxy instance (that implements the remote interfaces of
     * implClass) constructed with a RemoteObjectInvocationHandler instance
     * constructed with the clientRef.
     *
     * Otherwise, this method loads the pregenerated stub class (which
     * extends RemoteStub and implements the remote interfaces of
     * implClass) and constructs an instance of the pregenerated stub
     * class with the clientRef.
     *
     * @param implClass the class to obtain remote interfaces from
     * @param clientRef the remote ref to use in the invocation handler
     * @param forceStubUse if true, forces creation of a RemoteStub
     * @throws IllegalArgumentException if implClass implements illegal
     * remote interfaces
     * @throws StubNotFoundException if problem locating/creating stub or
     * creating the dynamic proxy instance
     **/
public static Remote createProxy(Class<?> implClass, RemoteRef clientRef, boolean forceStubUse) throws StubNotFoundException {
    Class<?> remoteClass;
    try {
        remoteClass = getRemoteClass(implClass);
    } catch (ClassNotFoundException ex) {
        throw new StubNotFoundException("object does not implement a remote interface: " + implClass.getName());
    }
    if (forceStubUse || !(ignoreStubClasses || !stubClassExists(remoteClass))) {
        return createStub(remoteClass, clientRef);
    }
    final ClassLoader loader = implClass.getClassLoader();
    final Class<?>[] interfaces = getRemoteInterfaces(implClass);
    final InvocationHandler handler = new RemoteObjectInvocationHandler(clientRef);
    try {
        return AccessController.doPrivileged(new PrivilegedAction<Remote>() {

            public Remote run() {
                return (Remote) Proxy.newProxyInstance(loader, interfaces, handler);
            }
        });
    } catch (IllegalArgumentException e) {
        throw new StubNotFoundException("unable to create proxy", e);
    }
}
Also used : RemoteObjectInvocationHandler(java.rmi.server.RemoteObjectInvocationHandler) Remote(java.rmi.Remote) RemoteObjectInvocationHandler(java.rmi.server.RemoteObjectInvocationHandler) InvocationHandler(java.lang.reflect.InvocationHandler) StubNotFoundException(java.rmi.StubNotFoundException)

Example 57 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project jdk8u_jdk by JetBrains.

the class RMIConnector method checkStub.

//--------------------------------------------------------------------
// Private stuff - Check if stub can be trusted.
//--------------------------------------------------------------------
private static void checkStub(Remote stub, Class<?> stubClass) {
    //
    if (stub.getClass() != stubClass) {
        if (!Proxy.isProxyClass(stub.getClass())) {
            throw new SecurityException("Expecting a " + stubClass.getName() + " stub!");
        } else {
            InvocationHandler handler = Proxy.getInvocationHandler(stub);
            if (handler.getClass() != RemoteObjectInvocationHandler.class)
                throw new SecurityException("Expecting a dynamic proxy instance with a " + RemoteObjectInvocationHandler.class.getName() + " invocation handler!");
            else
                stub = (Remote) handler;
        }
    }
    // Check RemoteRef in stub is from the expected class
    // "sun.rmi.server.UnicastRef2".
    //
    RemoteRef ref = ((RemoteObject) stub).getRef();
    if (ref.getClass() != UnicastRef2.class)
        throw new SecurityException("Expecting a " + UnicastRef2.class.getName() + " remote reference in stub!");
    // Check RMIClientSocketFactory in stub is from the expected class
    // "javax.rmi.ssl.SslRMIClientSocketFactory".
    //
    LiveRef liveRef = ((UnicastRef2) ref).getLiveRef();
    RMIClientSocketFactory csf = liveRef.getClientSocketFactory();
    if (csf == null || csf.getClass() != SslRMIClientSocketFactory.class)
        throw new SecurityException("Expecting a " + SslRMIClientSocketFactory.class.getName() + " RMI client socket factory in stub!");
}
Also used : SslRMIClientSocketFactory(javax.rmi.ssl.SslRMIClientSocketFactory) RemoteObject(java.rmi.server.RemoteObject) LiveRef(sun.rmi.transport.LiveRef) RemoteRef(java.rmi.server.RemoteRef) Remote(java.rmi.Remote) UnicastRef2(sun.rmi.server.UnicastRef2) RemoteObjectInvocationHandler(java.rmi.server.RemoteObjectInvocationHandler) InvocationHandler(java.lang.reflect.InvocationHandler) SslRMIClientSocketFactory(javax.rmi.ssl.SslRMIClientSocketFactory) RMIClientSocketFactory(java.rmi.server.RMIClientSocketFactory)

Example 58 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project bnd by bndtools.

the class GogoRedirector method proxy.

/*
	 * Create a proxy on a class. This is to prevent class cast exceptions. We
	 * get our Gogo likely from another class loader since the agent can reside
	 * on the framework side and we can't force Gogo to import our classes (nor
	 * should we).
	 */
@SuppressWarnings("unchecked")
<T> T proxy(final Class<T> clazz, final Object target) {
    final Class<?> targetClass = target.getClass();
    if (targetClass == clazz)
        return clazz.cast(target);
    return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class<?>[] { clazz }, new InvocationHandler() {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            Method targetMethod = targetClass.getMethod(method.getName(), method.getParameterTypes());
            Object result = targetMethod.invoke(target, args);
            if (result != null && method.getReturnType().isInterface() && targetMethod.getReturnType() != method.getReturnType())
                try {
                    return proxy(method.getReturnType(), result);
                } catch (Exception e) {
                }
            return result;
        }
    });
}
Also used : Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) IOException(java.io.IOException)

Example 59 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project deltaspike by apache.

the class ProxyUtilsTest method testIsIntefaceProxy.

@Test
public void testIsIntefaceProxy() {
    Object proxy = Proxy.newProxyInstance(myDependentScopedBean.getClass().getClassLoader(), new Class[] { MyInterface.class }, new InvocationHandler() {

        @Override
        public Object invoke(Object arg0, Method arg1, Object[] arg2) throws Throwable {
            return null;
        }
    });
    Assert.assertTrue(ProxyUtils.isInterfaceProxy(proxy.getClass()));
}
Also used : Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) Test(org.junit.Test)

Example 60 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project deltaspike by apache.

the class QueryStringExtractorFactoryTest method should_unwrap_query_even_proxied.

@Test
public void should_unwrap_query_even_proxied() {
    // when
    String extracted = factory.extract((Query) newProxyInstance(currentThread().getContextClassLoader(), new Class[] { Query.class }, new InvocationHandler() {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if (method.getName().equals("toString")) {
                return "Unknown provider wrapper for tests.";
            }
            if (method.getName().equals("unwrap")) {
                Class<?> clazz = (Class<?>) args[0];
                if (clazz.getName().contains("hibernate") || clazz.getName().contains("openjpa") || clazz.getName().contains("eclipse")) {
                    return createProxy(clazz);
                } else {
                    throw new PersistenceException("Unable to unwrap for " + clazz);
                }
            }
            return null;
        }
    }));
    // then
    assertEquals(QUERY_STRING, extracted);
}
Also used : PersistenceException(javax.persistence.PersistenceException) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) Test(org.junit.Test)

Aggregations

InvocationHandler (java.lang.reflect.InvocationHandler)411 Method (java.lang.reflect.Method)232 Test (org.junit.Test)70 InvocationTargetException (java.lang.reflect.InvocationTargetException)54 Proxy (java.lang.reflect.Proxy)28 ArrayList (java.util.ArrayList)25 Map (java.util.Map)23 IOException (java.io.IOException)19 Field (java.lang.reflect.Field)19 HashMap (java.util.HashMap)18 AccessibleObject (java.lang.reflect.AccessibleObject)16 List (java.util.List)16 BindingProvider (javax.xml.ws.BindingProvider)14 PersistentClass (org.hibernate.mapping.PersistentClass)12 RootClass (org.hibernate.mapping.RootClass)12 Before (org.junit.Before)10 Connection (java.sql.Connection)9 LinkedHashMap (java.util.LinkedHashMap)9 AbstractQueryFacade (org.jboss.tools.hibernate.runtime.common.AbstractQueryFacade)8 DexMakerTest (com.android.dx.DexMakerTest)7