use of java.lang.reflect.InvocationHandler in project querydsl by querydsl.
the class JPAProviderTest method hibernate_for_proxy.
@Test
public void hibernate_for_proxy() {
factory = Persistence.createEntityManagerFactory("h2");
em = factory.createEntityManager();
InvocationHandler handler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return method.invoke(em, args);
}
};
EntityManager proxy = (EntityManager) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { EntityManager.class }, handler);
assertEquals(HQLTemplates.DEFAULT, JPAProvider.getTemplates(proxy));
}
use of java.lang.reflect.InvocationHandler in project querydsl by querydsl.
the class JPAProviderTest method eclipseLink_for_proxy.
@Test
public void eclipseLink_for_proxy() {
factory = Persistence.createEntityManagerFactory("h2-eclipselink");
em = factory.createEntityManager();
InvocationHandler handler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return method.invoke(em, args);
}
};
EntityManager proxy = (EntityManager) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { EntityManager.class }, handler);
assertEquals(EclipseLinkTemplates.DEFAULT, JPAProvider.getTemplates(proxy));
}
use of java.lang.reflect.InvocationHandler in project ratpack by ratpack.
the class RatpackServerProxy method capture.
public static RatpackServerProxy capture(ClassLoader classLoader, final String mainClassName, final String[] appArgs) {
Class<?> mainClass;
try {
mainClass = classLoader.loadClass(mainClassName);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Did not find specified main class: " + mainClassName, e);
}
Method main;
try {
main = mainClass.getMethod("main", String[].class);
} catch (NoSuchMethodException e) {
throw new RuntimeException("Did not find main(String...) method on main class: " + mainClassName, e);
}
if (!Modifier.isStatic(main.getModifiers())) {
throw new RuntimeException("main(String...) is not static on class: " + mainClassName);
}
Class<?> capturer;
try {
capturer = classLoader.loadClass(CAPTURER_CLASS_NAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException("could not load " + CAPTURER_CLASS_NAME, e);
}
Class<?> blockType;
try {
blockType = classLoader.loadClass(BLOCK_CLASS_NAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException("could not load " + BLOCK_CLASS_NAME, e);
}
Method captureMethod;
try {
captureMethod = capturer.getMethod("capture", blockType);
} catch (NoSuchMethodException e) {
throw new RuntimeException("Could not find capture() on ServerCapturer");
}
final Method finalMain = main;
Object block = Proxy.newProxyInstance(classLoader, new Class<?>[] { blockType }, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
finalMain.invoke(null, new Object[] { appArgs });
} catch (Exception e) {
throw new RuntimeException("failed to invoke main(String...) on class: " + mainClassName, e);
}
return null;
}
});
Object server;
try {
server = captureMethod.invoke(null, block);
} catch (Exception e) {
throw new RuntimeException("Failed to invoke get() on ServerCapturer");
}
if (server == null) {
throw new RuntimeException("main(String...) of " + mainClassName + " did not start a Ratpack server");
}
if (!server.getClass().getName().equals(SERVER_CLASS_NAME)) {
throw new RuntimeException("Captured " + server.getClass().getName() + ", not " + SERVER_CLASS_NAME);
}
return new RatpackServerProxy(server);
}
use of java.lang.reflect.InvocationHandler in project roboguice by roboguice.
the class FactoryProvider method get.
public F get() {
InvocationHandler invocationHandler = new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] creationArgs) throws Throwable {
// pass methods from Object.class to the proxy
if (method.getDeclaringClass().equals(Object.class)) {
if ("equals".equals(method.getName())) {
return proxy == creationArgs[0];
} else if ("hashCode".equals(method.getName())) {
return System.identityHashCode(proxy);
} else {
return method.invoke(this, creationArgs);
}
}
AssistedConstructor<?> constructor = factoryMethodToConstructor.get(method);
Object[] constructorArgs = gatherArgsForConstructor(constructor, creationArgs);
Object objectToReturn = constructor.newInstance(constructorArgs);
injector.injectMembers(objectToReturn);
return objectToReturn;
}
public Object[] gatherArgsForConstructor(AssistedConstructor<?> constructor, Object[] factoryArgs) {
int numParams = constructor.getAllParameters().size();
int argPosition = 0;
Object[] result = new Object[numParams];
for (int i = 0; i < numParams; i++) {
Parameter parameter = constructor.getAllParameters().get(i);
if (parameter.isProvidedByFactory()) {
result[i] = factoryArgs[argPosition];
argPosition++;
} else {
result[i] = parameter.getValue(injector);
}
}
return result;
}
};
// we imprecisely treat the class literal of T as a Class<T>
@SuppressWarnings("unchecked") Class<F> factoryRawType = (Class<F>) (Class<?>) factoryType.getRawType();
return factoryRawType.cast(Proxy.newProxyInstance(BytecodeGen.getClassLoader(factoryRawType), new Class[] { factoryRawType }, invocationHandler));
}
use of java.lang.reflect.InvocationHandler in project robolectric by robolectric.
the class ShadowWebView method ensureProviderCreated.
@HiddenApi
@Implementation
public void ensureProviderCreated() {
final ClassLoader classLoader = getClass().getClassLoader();
Class<?> webViewProviderClass = getClassNamed("android.webkit.WebViewProvider");
Field mProvider;
try {
mProvider = WebView.class.getDeclaredField("mProvider");
mProvider.setAccessible(true);
if (mProvider.get(realView) == null) {
Object provider = Proxy.newProxyInstance(classLoader, new Class[] { webViewProviderClass }, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("getViewDelegate") || method.getName().equals("getScrollDelegate")) {
return Proxy.newProxyInstance(classLoader, new Class[] { getClassNamed("android.webkit.WebViewProvider$ViewDelegate"), getClassNamed("android.webkit.WebViewProvider$ScrollDelegate") }, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return nullish(method);
}
});
}
return nullish(method);
}
});
mProvider.set(realView, provider);
}
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
Aggregations