Search in sources :

Example 1 with Proxy

use of javassist.util.proxy.Proxy in project mybatis-3 by mybatis.

the class BaseExecutorTest method shouldFetchPostsForBlog.

@Test
public void shouldFetchPostsForBlog() throws Exception {
    Executor executor = createExecutor(new JdbcTransaction(ds, null, false));
    try {
        MappedStatement selectBlog = ExecutorTestHelper.prepareComplexSelectBlogMappedStatement(config);
        MappedStatement selectPosts = ExecutorTestHelper.prepareSelectPostsForBlogMappedStatement(config);
        config.addMappedStatement(selectBlog);
        config.addMappedStatement(selectPosts);
        List<Post> posts = executor.query(selectPosts, 1, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER);
        executor.flushStatements();
        assertEquals(2, posts.size());
        assertTrue(posts.get(1) instanceof Proxy);
        assertNotNull(posts.get(1).getBlog());
        assertEquals(1, posts.get(1).getBlog().getId());
        executor.rollback(true);
    } finally {
        executor.rollback(true);
        executor.close(false);
    }
}
Also used : Proxy(javassist.util.proxy.Proxy) JdbcTransaction(org.apache.ibatis.transaction.jdbc.JdbcTransaction) Post(org.apache.ibatis.domain.blog.Post) MappedStatement(org.apache.ibatis.mapping.MappedStatement) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.Test)

Example 2 with Proxy

use of javassist.util.proxy.Proxy in project mybatis-3 by mybatis.

the class BindingTest method shouldGetBlogsWithAuthorsAndPosts.

@Test
public void shouldGetBlogsWithAuthorsAndPosts() {
    SqlSession session = sqlSessionFactory.openSession();
    try {
        BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
        List<Blog> blogs = mapper.selectBlogsWithAutorAndPosts();
        assertEquals(2, blogs.size());
        assertTrue(blogs.get(0) instanceof Proxy);
        assertEquals(101, blogs.get(0).getAuthor().getId());
        assertEquals(1, blogs.get(0).getPosts().size());
        assertEquals(1, blogs.get(0).getPosts().get(0).getId());
        assertTrue(blogs.get(1) instanceof Proxy);
        assertEquals(102, blogs.get(1).getAuthor().getId());
        assertEquals(1, blogs.get(1).getPosts().size());
        assertEquals(2, blogs.get(1).getPosts().get(0).getId());
    } finally {
        session.close();
    }
}
Also used : Proxy(javassist.util.proxy.Proxy) SqlSession(org.apache.ibatis.session.SqlSession) Blog(org.apache.ibatis.domain.blog.Blog) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.Test)

Example 3 with Proxy

use of javassist.util.proxy.Proxy in project mybatis-3 by mybatis.

the class JavassistTest method shouldGetAUserAndGroups.

@Test
public void shouldGetAUserAndGroups() {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        Mapper mapper = sqlSession.getMapper(Mapper.class);
        User user = mapper.getUser(1);
        Assert.assertEquals("User1", user.getName());
        assertTrue(user instanceof Proxy);
        Assert.assertEquals(1, user.getGroups().size());
    } finally {
        sqlSession.close();
    }
}
Also used : Proxy(javassist.util.proxy.Proxy) SqlSession(org.apache.ibatis.session.SqlSession) Test(org.junit.Test)

Example 4 with Proxy

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

the class OObjectProxyMethodHandler method manageArrayLoad.

@SuppressWarnings({ "rawtypes", "unchecked" })
protected Object manageArrayLoad(Object value, Field f) {
    if (((Object[]) value).length > 0) {
        Object o = ((Object[]) value)[0];
        if (o instanceof OIdentifiable) {
            Object[] newValue = new Object[((Object[]) value).length];
            for (int i = 0; i < ((Object[]) value).length; i++) {
                ODocument doc = ((OIdentifiable) ((Object[]) value)[i]).getRecord();
                newValue[i] = OObjectEntitySerializer.getDocument((Proxy) doc);
            }
            value = newValue;
        } else {
            final Class genericType = OReflectionHelper.getGenericMultivalueType(f);
            if (genericType != null && genericType.isEnum()) {
                Object newValue = Array.newInstance(genericType, ((Object[]) value).length);
                for (int i = 0; i < ((Object[]) value).length; i++) {
                    o = ((Object[]) value)[i];
                    if (o instanceof Number)
                        o = genericType.getEnumConstants()[((Number) o).intValue()];
                    else
                        o = Enum.valueOf(genericType, o.toString());
                    ((Enum[]) newValue)[i] = (Enum) o;
                }
                value = newValue;
            }
        }
    }
    return value;
}
Also used : Proxy(javassist.util.proxy.Proxy) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) ProxyObject(javassist.util.proxy.ProxyObject) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 5 with Proxy

use of javassist.util.proxy.Proxy 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)

Aggregations

Proxy (javassist.util.proxy.Proxy)26 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)8 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)5 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 ProxyObject (javassist.util.proxy.ProxyObject)5 Test (org.junit.Test)5 ORecordId (com.orientechnologies.orient.core.id.ORecordId)4 OObjectDatabaseTx (com.orientechnologies.orient.object.db.OObjectDatabaseTx)4 OObjectProxyMethodHandler (com.orientechnologies.orient.object.enhancement.OObjectProxyMethodHandler)4 EnumTest (com.orientechnologies.orient.test.domain.base.EnumTest)4 JavaAttachDetachTestClass (com.orientechnologies.orient.test.domain.base.JavaAttachDetachTestClass)4 Child (com.orientechnologies.orient.test.domain.business.Child)4 HashSet (java.util.HashSet)4 Set (java.util.Set)4 ProxyFactory (javassist.util.proxy.ProxyFactory)3 BaseDataTest (org.apache.ibatis.BaseDataTest)3 Test (org.testng.annotations.Test)3 OObjectLazyMultivalueElement (com.orientechnologies.orient.core.db.object.OObjectLazyMultivalueElement)2