use of javassist.util.proxy.ProxyFactory in project che by eclipse.
the class JUnitTestRunner method create4xTestListener.
private Object create4xTestListener(ClassLoader loader, Class<?> listenerClass, AbstractTestListener delegate) throws Exception {
ProxyFactory f = new ProxyFactory();
f.setSuperclass(listenerClass);
f.setFilter(new MethodFilter() {
@Override
public boolean isHandled(Method m) {
String methodName = m.getName();
switch(methodName) {
case "testStarted":
case "testFinished":
case "testFailure":
case "testAssumptionFailure":
return true;
}
return false;
}
});
Class<?> c = f.createClass();
MethodHandler mi = new MethodHandler() {
@Override
public Object invoke(Object self, Method m, Method method, Object[] args) throws Throwable {
String methodName = m.getName();
Object description = null;
Throwable throwable = null;
switch(methodName) {
case "testStarted":
case "testFinished":
description = args[0];
throwable = null;
break;
case "testFailure":
case "testAssumptionFailure":
description = args[0].getClass().getMethod("getDescription", new Class<?>[0]).invoke(args[0]);
throwable = (Throwable) args[0].getClass().getMethod("getException", new Class<?>[0]).invoke(args[0]);
break;
default:
return null;
}
String testKey = (String) description.getClass().getMethod("getDisplayName", new Class<?>[0]).invoke(description);
String testName = testKey;
switch(methodName) {
case "testStarted":
delegate.startTest(testKey, testName);
break;
case "testFinished":
delegate.endTest(testKey, testName);
break;
case "testFailure":
delegate.addFailure(testKey, throwable);
break;
case "testAssumptionFailure":
delegate.addError(testKey, throwable);
break;
}
return null;
}
};
Object listener = c.getConstructor().newInstance();
((javassist.util.proxy.Proxy) listener).setHandler(mi);
return listener;
}
use of javassist.util.proxy.ProxyFactory in project orientdb by orientechnologies.
the class OObjectEntityEnhancer method getProxiedInstance.
@SuppressWarnings("unchecked")
public <T> T getProxiedInstance(final Class<T> iClass, Object iEnclosingInstance, final ODocument doc, final ProxyObject parent, Object... iArgs) {
if (iClass == null) {
throw new OSerializationException("Type '" + doc.getClassName() + "' cannot be serialized because is not part of registered entities. To fix this error register this class");
}
final Class<T> c;
boolean isInnerClass = OObjectEntitySerializer.getEnclosingClass(iClass) != null;
if (Proxy.class.isAssignableFrom(iClass)) {
c = iClass;
} else {
ProxyFactory f = new ProxyFactory();
f.setSuperclass(iClass);
if (customMethodFilters.get(iClass) != null) {
f.setFilter(customMethodFilters.get(iClass));
} else {
f.setFilter(defaultMethodFilter);
}
c = f.createClass();
}
MethodHandler mi = new OObjectProxyMethodHandler(doc);
((OObjectProxyMethodHandler) mi).setParentObject(parent);
try {
T newEntity;
if (iArgs != null && iArgs.length > 0) {
if (isInnerClass) {
if (iEnclosingInstance == null) {
iEnclosingInstance = iClass.getEnclosingClass().newInstance();
}
Object[] newArgs = new Object[iArgs.length + 1];
newArgs[0] = iEnclosingInstance;
for (int i = 0; i < iArgs.length; i++) {
newArgs[i + 1] = iArgs[i];
}
iArgs = newArgs;
}
Constructor<T> constructor = null;
for (Constructor<?> constr : c.getConstructors()) {
boolean found = true;
if (constr.getParameterTypes().length == iArgs.length) {
for (int i = 0; i < constr.getParameterTypes().length; i++) {
Class<?> parameterType = constr.getParameterTypes()[i];
if (parameterType.isPrimitive()) {
if (!isPrimitiveParameterCorrect(parameterType, iArgs[i])) {
found = false;
break;
}
} else if (iArgs[i] != null && !parameterType.isAssignableFrom(iArgs[i].getClass())) {
found = false;
break;
}
}
} else {
continue;
}
if (found) {
constructor = (Constructor<T>) constr;
break;
}
}
if (constructor != null) {
newEntity = (T) constructor.newInstance(iArgs);
initDocument(iClass, newEntity, doc, (ODatabaseObject) ODatabaseRecordThreadLocal.INSTANCE.get().getDatabaseOwner());
} else {
if (iEnclosingInstance != null)
newEntity = createInstanceNoParameters(c, iEnclosingInstance);
else
newEntity = createInstanceNoParameters(c, iClass);
}
} else {
if (iEnclosingInstance != null)
newEntity = createInstanceNoParameters(c, iEnclosingInstance);
else
newEntity = createInstanceNoParameters(c, iClass);
}
((Proxy) newEntity).setHandler(mi);
if (OObjectEntitySerializer.hasBoundedDocumentField(iClass))
OObjectEntitySerializer.setFieldValue(OObjectEntitySerializer.getBoundedDocumentField(iClass), newEntity, doc);
OObjectEntitySerializer.setVersionField(iClass, newEntity, doc.getVersion());
return newEntity;
} catch (InstantiationException ie) {
OLogManager.instance().error(this, "Error creating proxied instance for class " + iClass.getName(), ie);
} catch (IllegalAccessException iae) {
OLogManager.instance().error(this, "Error creating proxied instance for class " + iClass.getName(), iae);
} catch (IllegalArgumentException iae) {
OLogManager.instance().error(this, "Error creating proxied instance for class " + iClass.getName(), iae);
} catch (SecurityException se) {
OLogManager.instance().error(this, "Error creating proxied instance for class " + iClass.getName(), se);
} catch (InvocationTargetException ite) {
OLogManager.instance().error(this, "Error creating proxied instance for class " + iClass.getName(), ite);
} catch (NoSuchMethodException nsme) {
OLogManager.instance().error(this, "Error creating proxied instance for class " + iClass.getName(), nsme);
}
return null;
}
use of javassist.util.proxy.ProxyFactory in project byte-buddy by raphw.
the class ClassByExtensionBenchmark method benchmarkJavassist.
/**
* Performs a benchmark of a class extension using javassist proxies.
*
* @return The created instance, in order to avoid JIT removal.
* @throws java.lang.Exception If the invocation causes an exception.
*/
@Benchmark
public ExampleClass benchmarkJavassist() throws Exception {
ProxyFactory proxyFactory = new ProxyFactory() {
@Override
protected ClassLoader getClassLoader() {
return newClassLoader();
}
};
proxyFactory.setUseCache(false);
proxyFactory.setSuperclass(baseClass);
proxyFactory.setFilter(new MethodFilter() {
public boolean isHandled(Method method) {
return method.getDeclaringClass() == baseClass;
}
});
@SuppressWarnings("unchecked") Object instance = proxyFactory.createClass().getDeclaredConstructor().newInstance();
((javassist.util.proxy.Proxy) instance).setHandler(new MethodHandler() {
public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable {
return proceed.invoke(self, args);
}
});
return (ExampleClass) instance;
}
use of javassist.util.proxy.ProxyFactory in project mybatis-3 by mybatis.
the class JavassistProxyFactory method crateProxy.
static Object crateProxy(Class<?> type, MethodHandler callback, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
ProxyFactory enhancer = new ProxyFactory();
enhancer.setSuperclass(type);
try {
type.getDeclaredMethod(WRITE_REPLACE_METHOD);
// ObjectOutputStream will call writeReplace of objects returned by writeReplace
if (log.isDebugEnabled()) {
log.debug(WRITE_REPLACE_METHOD + " method was found on bean " + type + ", make sure it returns this");
}
} catch (NoSuchMethodException e) {
enhancer.setInterfaces(new Class[] { WriteReplaceInterface.class });
} catch (SecurityException e) {
// nothing to do here
}
Object enhanced;
Class<?>[] typesArray = constructorArgTypes.toArray(new Class[constructorArgTypes.size()]);
Object[] valuesArray = constructorArgs.toArray(new Object[constructorArgs.size()]);
try {
enhanced = enhancer.create(typesArray, valuesArray);
} catch (Exception e) {
throw new ExecutorException("Error creating lazy proxy. Cause: " + e, e);
}
((Proxy) enhanced).setHandler(callback);
return enhanced;
}
Aggregations