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);
}
}
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();
}
}
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();
}
}
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;
}
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());
}
Aggregations