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