Search in sources :

Example 26 with ProxyObject

use of javassist.util.proxy.ProxyObject 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();
}
Also used : ProxyObject(javassist.util.proxy.ProxyObject) MethodHandler(javassist.util.proxy.MethodHandler) AbstractArtifactCollectorFacade(org.jboss.tools.hibernate.runtime.common.AbstractArtifactCollectorFacade) ProxyFactory(javassist.util.proxy.ProxyFactory) ArtifactCollector(org.hibernate.tool.hbm2x.ArtifactCollector) IArtifactCollector(org.jboss.tools.hibernate.runtime.spi.IArtifactCollector) ProxyObject(javassist.util.proxy.ProxyObject) Method(java.lang.reflect.Method) Before(org.junit.Before)

Example 27 with ProxyObject

use of javassist.util.proxy.ProxyObject 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();
}
Also used : ProxyObject(javassist.util.proxy.ProxyObject) MethodHandler(javassist.util.proxy.MethodHandler) AbstractArtifactCollectorFacade(org.jboss.tools.hibernate.runtime.common.AbstractArtifactCollectorFacade) ProxyFactory(javassist.util.proxy.ProxyFactory) ArtifactCollector(org.hibernate.tool.hbm2x.ArtifactCollector) IArtifactCollector(org.jboss.tools.hibernate.runtime.spi.IArtifactCollector) ProxyObject(javassist.util.proxy.ProxyObject) Method(java.lang.reflect.Method) Before(org.junit.Before)

Example 28 with ProxyObject

use of javassist.util.proxy.ProxyObject 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;
}
Also used : OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) ProxyFactory(javassist.util.proxy.ProxyFactory) InvocationTargetException(java.lang.reflect.InvocationTargetException) MethodHandler(javassist.util.proxy.MethodHandler) Proxy(javassist.util.proxy.Proxy) ProxyObject(javassist.util.proxy.ProxyObject) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject)

Example 29 with ProxyObject

use of javassist.util.proxy.ProxyObject in project orientdb by orientechnologies.

the class OObjectLazyIterator method next.

public TYPE next(final String iFetchPlan) {
    final Object value = underlying.next();
    if (value == null)
        return null;
    if (value instanceof ORID && autoConvert2Object) {
        currentElement = (OIdentifiable) value;
        ORecord record = ((ODatabaseDocument) database.getUnderlying()).load((ORID) value, iFetchPlan);
        if (record == null) {
            OLogManager.instance().warn(this, "Record " + ((OObjectProxyMethodHandler) sourceRecord.getHandler()).getDoc().getIdentity() + " references a deleted instance");
            return null;
        }
        TYPE o = database.getUserObjectByRecord(record, iFetchPlan);
        ((OObjectProxyMethodHandler) (((ProxyObject) o)).getHandler()).setParentObject(sourceRecord);
        return o;
    } else if (value instanceof ODocument && autoConvert2Object) {
        currentElement = (OIdentifiable) value;
        TYPE o = database.getUserObjectByRecord((ODocument) value, iFetchPlan);
        ((OObjectProxyMethodHandler) (((ProxyObject) o)).getHandler()).setParentObject(sourceRecord);
        return o;
    } else {
        currentElement = database.getRecordByUserObject(value, false);
    }
    if (OObjectEntitySerializer.isToSerialize(value.getClass()))
        return (TYPE) OObjectEntitySerializer.deserializeFieldValue(value.getClass(), value);
    return (TYPE) value;
}
Also used : ProxyObject(javassist.util.proxy.ProxyObject) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ORecord(com.orientechnologies.orient.core.record.ORecord) ProxyObject(javassist.util.proxy.ProxyObject) ORID(com.orientechnologies.orient.core.id.ORID) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OObjectProxyMethodHandler(com.orientechnologies.orient.object.enhancement.OObjectProxyMethodHandler) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 30 with ProxyObject

use of javassist.util.proxy.ProxyObject in project latke by b3log.

the class BeanImpl method instantiateReference.

/**
 * Constructs the bean object with dependencies resolved.
 *
 * @return bean object
 * @throws Exception exception
 */
private T instantiateReference() throws Exception {
    T ret;
    if (constructorParameterInjectionPoints.size() == 1) {
        // only one constructor allow to be annotated with @Inject
        // instantiate an instance by the constructor annotated with @Inject
        final AnnotatedConstructor<T> annotatedConstructor = constructorParameterInjectionPoints.keySet().iterator().next();
        final List<ParameterInjectionPoint> paraInjectionPoints = constructorParameterInjectionPoints.get(annotatedConstructor);
        final Object[] args = new Object[paraInjectionPoints.size()];
        int i = 0;
        for (final ParameterInjectionPoint paraInjectionPoint : paraInjectionPoints) {
            Object arg = beanManager.getInjectableReference(paraInjectionPoint, null);
            if (arg == null) {
                for (final ParameterProvider<?> provider : constructorParameterProviders) {
                    if (provider.getAnnotated().equals(paraInjectionPoint.getAnnotated())) {
                        arg = provider;
                        break;
                    }
                }
            }
            args[i++] = arg;
        }
        final Constructor<T> oriBeanConstructor = annotatedConstructor.getJavaMember();
        final Constructor<T> constructor = proxyClass.getConstructor(oriBeanConstructor.getParameterTypes());
        ret = constructor.newInstance(args);
    } else {
        ret = proxyClass.newInstance();
    }
    ((ProxyObject) ret).setHandler(javassistMethodHandler);
    LOGGER.log(Level.TRACE, "Uses Javassist method handler for bean[class={0}]", beanClass.getName());
    return ret;
}
Also used : ProxyObject(javassist.util.proxy.ProxyObject) ProxyObject(javassist.util.proxy.ProxyObject) ParameterInjectionPoint(org.b3log.latke.ioc.point.ParameterInjectionPoint) FieldInjectionPoint(org.b3log.latke.ioc.point.FieldInjectionPoint) ParameterInjectionPoint(org.b3log.latke.ioc.point.ParameterInjectionPoint) InjectionPoint(org.b3log.latke.ioc.point.InjectionPoint)

Aggregations

ProxyObject (javassist.util.proxy.ProxyObject)30 MethodHandler (javassist.util.proxy.MethodHandler)26 ProxyFactory (javassist.util.proxy.ProxyFactory)26 Method (java.lang.reflect.Method)25 Before (org.junit.Before)24 ArtifactCollector (org.hibernate.tool.hbm2x.ArtifactCollector)6 AbstractArtifactCollectorFacade (org.jboss.tools.hibernate.runtime.common.AbstractArtifactCollectorFacade)6 AbstractForeignKeyFacade (org.jboss.tools.hibernate.runtime.common.AbstractForeignKeyFacade)6 AbstractGenericExporterFacade (org.jboss.tools.hibernate.runtime.common.AbstractGenericExporterFacade)6 AbstractHbm2DDLExporterFacade (org.jboss.tools.hibernate.runtime.common.AbstractHbm2DDLExporterFacade)6 IArtifactCollector (org.jboss.tools.hibernate.runtime.spi.IArtifactCollector)6 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)2 ORID (com.orientechnologies.orient.core.id.ORID)2 ORecord (com.orientechnologies.orient.core.record.ORecord)2 OObjectProxyMethodHandler (com.orientechnologies.orient.object.enhancement.OObjectProxyMethodHandler)2 Proxy (javassist.util.proxy.Proxy)2 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 OObjectLazyMultivalueElement (com.orientechnologies.orient.core.db.object.OObjectLazyMultivalueElement)1 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)1 OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)1