use of javassist.util.proxy.MethodHandler in project jbosstools-hibernate by jbosstools.
the class ArtifactCollectorFacadeTest method setUp.
@Before
public void setUp() throws Exception {
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setSuperclass(ArtifactCollector.class);
Class<?> proxyClass = proxyFactory.createClass();
ProxyObject proxy = (ProxyObject) proxyClass.newInstance();
proxy.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);
}
});
artifactCollector = new AbstractArtifactCollectorFacade(FACADE_FACTORY, (ArtifactCollector) proxy) {
};
reset();
}
use of javassist.util.proxy.MethodHandler 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.MethodHandler in project scheduling by ow2-proactive.
the class ContextClassLoaderInjector method createInjectedObject.
/**
* Create an injected object which has been injected with specifying the thread context class loader during all its methods invocation.
*
* @param superClass The class whose object methods need to be injected
* @param contextClassLoader the thread context class loader to inject in all the methods of the class object
* @return the created injected object
*/
public static Object createInjectedObject(Class<?> superClass, ClassLoader contextClassLoader) throws Exception {
proxyFactory.setSuperclass(superClass);
// The thread context class loader also needs to be sepecified during the object instantiation.
Object object = switchContextClassLoader(contextClassLoader, () -> {
Class<?> proxyClass = proxyFactory.createClass();
return proxyClass.newInstance();
});
MethodHandler injectClassLoaderHandler = (self, method, proceed, args) -> {
logger.debug("Delegating method: " + self.getClass().getSimpleName() + "." + method.getName());
// inject setting of thread context classloader during execution of all the object original methods
return switchContextClassLoader(contextClassLoader, () -> proceed.invoke(self, args));
};
((Proxy) object).setHandler(injectClassLoaderHandler);
return object;
}
Aggregations