use of javassist.util.proxy.MethodHandler in project che by eclipse.
the class JUnitTestRunner method create3xTestListener.
private Object create3xTestListener(ClassLoader loader, Class<?> listenerClass, AbstractTestListener delegate) throws Exception {
ProxyFactory f = new ProxyFactory();
f.setSuperclass(Object.class);
f.setInterfaces(new Class<?>[] { listenerClass });
f.setFilter(new MethodFilter() {
@Override
public boolean isHandled(Method m) {
String methodName = m.getName();
switch(methodName) {
case "startTest":
case "endTest":
case "addError":
case "addFailure":
return true;
}
return false;
}
});
Class<?> c = f.createClass();
MethodHandler mi = new MethodHandler() {
public Object invoke(Object self, Method method, Method proceed, Object[] args) throws Throwable {
String testKey = args[0].getClass().toString();
String testName = args[0].getClass().getName();
String methodName = method.getName();
switch(methodName) {
case "startTest":
delegate.startTest(testKey, testName);
break;
case "endTest":
delegate.endTest(testKey, testName);
break;
case "addError":
delegate.addError(testKey, (Throwable) args[1]);
break;
case "addFailure":
delegate.addFailure(testKey, (Throwable) args[1]);
break;
}
return null;
}
};
Object listener = c.getConstructor().newInstance();
((javassist.util.proxy.Proxy) listener).setHandler(mi);
return listener;
}
use of javassist.util.proxy.MethodHandler in project byte-buddy by raphw.
the class ClassByImplementationBenchmark method benchmarkJavassist.
/**
* Performs a benchmark of an interface implementation using javassist proxies.
*
* @return The created instance, in order to avoid JIT removal.
* @throws java.lang.Exception If the reflective invocation causes an exception.
*/
@Benchmark
public ExampleInterface benchmarkJavassist() throws Exception {
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setUseCache(false);
ProxyFactory.classLoaderProvider = new ProxyFactory.ClassLoaderProvider() {
@Override
public ClassLoader get(ProxyFactory proxyFactory) {
return newClassLoader();
}
};
proxyFactory.setSuperclass(Object.class);
proxyFactory.setInterfaces(new Class<?>[] { baseClass });
proxyFactory.setFilter(new MethodFilter() {
public boolean isHandled(Method method) {
return true;
}
});
@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 {
Class<?> returnType = thisMethod.getReturnType();
if (returnType.isPrimitive()) {
if (returnType == boolean.class) {
return defaultBooleanValue;
} else if (returnType == byte.class) {
return defaultByteValue;
} else if (returnType == short.class) {
return defaultShortValue;
} else if (returnType == char.class) {
return defaultCharValue;
} else if (returnType == int.class) {
return defaultIntValue;
} else if (returnType == long.class) {
return defaultLongValue;
} else if (returnType == float.class) {
return defaultFloatValue;
} else {
return defaultDoubleValue;
}
} else {
return defaultReferenceValue;
}
}
});
return (ExampleInterface) instance;
}
use of javassist.util.proxy.MethodHandler in project jbosstools-hibernate by jbosstools.
the class ForeignKeyFacadeTest method setUp.
@Before
public void setUp() throws Exception {
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setSuperclass(ForeignKey.class);
Class<?> proxyClass = proxyFactory.createClass();
foreignKey = (ForeignKey) proxyClass.newInstance();
((ProxyObject) foreignKey).setHandler(new MethodHandler() {
@Override
public Object invoke(Object self, Method m, Method proceed, Object[] args) throws Throwable {
if (methodName == null) {
methodName = m.getName();
}
if (arguments == null) {
arguments = args;
}
return proceed.invoke(self, args);
}
});
foreignKeyFacade = new AbstractForeignKeyFacade(FACADE_FACTORY, foreignKey) {
};
reset();
}
use of javassist.util.proxy.MethodHandler in project jbosstools-hibernate by jbosstools.
the class GenericExporterFacadeTest method setUp.
@Before
public void setUp() throws Exception {
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setSuperclass(GenericExporter.class);
Class<?> proxyClass = proxyFactory.createClass();
genericExporter = (GenericExporter) proxyClass.newInstance();
((ProxyObject) genericExporter).setHandler(new MethodHandler() {
@Override
public Object invoke(Object self, Method m, Method proceed, Object[] args) throws Throwable {
if (methodName == null) {
methodName = m.getName();
}
if (arguments == null) {
arguments = args;
}
return proceed.invoke(self, args);
}
});
genericExporterFacade = new AbstractGenericExporterFacade(FACADE_FACTORY, genericExporter) {
};
reset();
}
use of javassist.util.proxy.MethodHandler in project jbosstools-hibernate by jbosstools.
the class Hbm2DDLExporterFacadeTest method setUp.
@Before
public void setUp() throws Exception {
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setSuperclass(Hbm2DDLExporter.class);
Class<?> proxyClass = proxyFactory.createClass();
hbm2ddlExporter = (Hbm2DDLExporter) proxyClass.newInstance();
((ProxyObject) hbm2ddlExporter).setHandler(new MethodHandler() {
@Override
public Object invoke(Object self, Method m, Method proceed, Object[] args) throws Throwable {
if (methodName == null) {
methodName = m.getName();
}
if (arguments == null) {
arguments = args;
}
return proceed.invoke(self, args);
}
});
hbm2DDLExporterFacade = new AbstractHbm2DDLExporterFacade(FACADE_FACTORY, hbm2ddlExporter) {
};
reset();
}
Aggregations