Search in sources :

Example 11 with ProxyObject

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

Example 12 with ProxyObject

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

the class OObjectProxyMethodHandler method detachAll.

/**
 * Method that detaches all fields contained in the document to the given object
 *
 * @param self
 *          :- The object containing this handler instance
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 * @throws NoSuchMethodException
 */
public void detachAll(final Object self, final boolean nonProxiedInstance, final Map<Object, Object> alreadyDetached, final Map<Object, Object> lazyObjects) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    final Class<?> selfClass = self.getClass();
    for (String fieldName : doc.fieldNames()) {
        final Field field = OObjectEntitySerializer.getField(fieldName, selfClass);
        if (field != null) {
            Object value = getValue(self, fieldName, false, null, true);
            if (value instanceof OObjectLazyMultivalueElement) {
                ((OObjectLazyMultivalueElement<?>) value).detachAll(nonProxiedInstance, alreadyDetached, lazyObjects);
                if (nonProxiedInstance)
                    value = ((OObjectLazyMultivalueElement<?>) value).getNonOrientInstance();
            } else if (value instanceof Proxy) {
                OObjectProxyMethodHandler handler = (OObjectProxyMethodHandler) ((ProxyObject) value).getHandler();
                if (nonProxiedInstance) {
                    value = OObjectEntitySerializer.getNonProxiedInstance(value);
                }
                if (OObjectEntitySerializer.isFetchLazyField(self.getClass(), fieldName)) {
                    // just make a placeholder with only the id, so it can be fetched later (but not by orient internally)
                    // do not use the already detached map for this, that might mix up lazy and non-lazy objects
                    Object lazyValue = lazyObjects.get(handler.doc.getIdentity());
                    if (lazyValue != null) {
                        value = lazyValue;
                    } else {
                        OObjectEntitySerializer.setIdField(field.getType(), value, handler.doc.getIdentity());
                        lazyObjects.put(handler.doc.getIdentity(), value);
                    }
                } else {
                    Object detachedValue = alreadyDetached.get(handler.doc.getIdentity());
                    if (detachedValue != null) {
                        value = detachedValue;
                    } else {
                        ORID identity = handler.doc.getIdentity();
                        if (identity.isValid())
                            alreadyDetached.put(identity, value);
                        handler.detachAll(value, nonProxiedInstance, alreadyDetached, lazyObjects);
                    }
                }
            }
            OObjectEntitySerializer.setFieldValue(field, self, value);
        }
    }
    OObjectEntitySerializer.setIdField(selfClass, self, doc.getIdentity());
    OObjectEntitySerializer.setVersionField(selfClass, self, doc.getVersion());
}
Also used : Field(java.lang.reflect.Field) ProxyObject(javassist.util.proxy.ProxyObject) Proxy(javassist.util.proxy.Proxy) OObjectLazyMultivalueElement(com.orientechnologies.orient.core.db.object.OObjectLazyMultivalueElement) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) ProxyObject(javassist.util.proxy.ProxyObject) ORID(com.orientechnologies.orient.core.id.ORID)

Example 13 with ProxyObject

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

the class OObjectLazyMap method convert.

/**
 * Assure that the requested key is converted.
 */
private void convert(final String iKey) {
    if (converted || !convertToRecord)
        return;
    if (super.containsKey(iKey))
        return;
    final ORecord record = (ORecord) underlying.get(iKey);
    if (record == null)
        return;
    TYPE o = getDatabase().getUserObjectByRecord(record, null);
    ((OObjectProxyMethodHandler) (((ProxyObject) o)).getHandler()).setParentObject(sourceRecord);
    super.put(iKey, o);
}
Also used : ProxyObject(javassist.util.proxy.ProxyObject) ORecord(com.orientechnologies.orient.core.record.ORecord) OObjectProxyMethodHandler(com.orientechnologies.orient.object.enhancement.OObjectProxyMethodHandler)

Example 14 with ProxyObject

use of javassist.util.proxy.ProxyObject in project fastjson by alibaba.

the class ProxyTest method create.

public static <T> T create(Class<T> classs) throws Exception {
    ProxyFactory factory = new ProxyFactory();
    factory.setSuperclass(classs);
    Class clazz = factory.createClass();
    MethodHandler handler = new MethodHandler() {

        public Object invoke(Object self, Method overridden, Method forwarder, Object[] args) throws Throwable {
            return forwarder.invoke(self, args);
        }
    };
    Object instance = clazz.newInstance();
    ((ProxyObject) instance).setHandler(handler);
    return (T) instance;
}
Also used : ProxyObject(javassist.util.proxy.ProxyObject) MethodHandler(javassist.util.proxy.MethodHandler) ProxyFactory(javassist.util.proxy.ProxyFactory) ProxyObject(javassist.util.proxy.ProxyObject) Method(java.lang.reflect.Method)

Example 15 with ProxyObject

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

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