You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2016/12/03 16:38:12 UTC

[46/50] tomee git commit: preparing bval upgrade

preparing bval upgrade


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/f1eacc39
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/f1eacc39
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/f1eacc39

Branch: refs/heads/tomee-1.7.x
Commit: f1eacc39db8606b1fef2e8761243d9ded59158a5
Parents: a36aa12
Author: rmannibucau <rm...@apache.org>
Authored: Thu Oct 27 17:46:22 2016 +0200
Committer: rmannibucau <rm...@apache.org>
Committed: Thu Oct 27 17:46:22 2016 +0200

----------------------------------------------------------------------
 .../apache/openejb/util/proxy/QueryProxy.java   | 39 ++++++++++++++------
 pom.xml                                         |  2 +-
 2 files changed, 28 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/f1eacc39/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/QueryProxy.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/QueryProxy.java b/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/QueryProxy.java
index 042b039..ae3b673 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/QueryProxy.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/QueryProxy.java
@@ -17,7 +17,6 @@
 
 package org.apache.openejb.util.proxy;
 
-import org.apache.commons.beanutils.BeanUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.openejb.util.LogCategory;
 import org.apache.openejb.util.Logger;
@@ -33,6 +32,7 @@ import javax.persistence.criteria.Predicate;
 import javax.persistence.criteria.Root;
 import javax.persistence.metamodel.EntityType;
 import javax.persistence.metamodel.SingularAttribute;
+import java.lang.reflect.Field;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
@@ -44,6 +44,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
+import static org.apache.commons.lang3.StringUtils.capitalize;
+
 public class QueryProxy implements InvocationHandler {
     private static final Logger LOGGER = Logger.getInstance(LogCategory.OPENEJB, QueryProxy.class);
 
@@ -242,17 +244,7 @@ public class QueryProxy implements InvocationHandler {
                 }
                 final String idName = id.getName();
 
-                final Object idValue;
-                try {
-                    idValue = BeanUtils.getProperty(entity, idName);
-                } catch (final InvocationTargetException e) {
-                    throw new IllegalArgumentException("can't invoke to get entity id");
-                } catch (final NoSuchMethodException e) {
-                    throw new IllegalArgumentException("can't find the method to get entity id");
-                } catch (final IllegalAccessException e) {
-                    throw new IllegalArgumentException("can't access field/method to get entity id");
-                }
-
+                final Object idValue = getProperty(entity, idName);
                 entity = em.getReference(et.getJavaType(), idValue);
                 if (entity == null) {
                     throw new IllegalArgumentException("entity " + entity + " is not managed and can't be found.");
@@ -264,6 +256,29 @@ public class QueryProxy implements InvocationHandler {
         }
     }
 
+    // can be optimized for runtime, since there are today nice alternative we probably don't need
+    private Object getProperty(final Object entity, final String idName) {
+        try {
+            final Method method = entity.getClass().getMethod("get" + capitalize(idName));
+            if (!method.isAccessible()) {
+                method.setAccessible(true);
+            }
+            return method.invoke(entity);
+        } catch (final NoSuchMethodException | IllegalAccessException e) {
+            try {
+                final Field declaredField = entity.getClass().getDeclaredField(idName);
+                if (!declaredField.isAccessible()) {
+                    declaredField.setAccessible(true);
+                }
+                return declaredField.get(entity);
+            } catch (final NoSuchFieldException | IllegalAccessException e1) {
+                throw new IllegalArgumentException("Bad id: " + idName + " for " + entity);
+            }
+        } catch (final InvocationTargetException e) {
+            throw new IllegalStateException(e.getCause());
+        }
+    }
+
     private Object merge(final Object[] args, final Class<?> returnType) {
         if (args != null && args.length == 1 && returnType.equals(args[0].getClass())) {
             return em.merge(args[0]);

http://git-wip-us.apache.org/repos/asf/tomee/blob/f1eacc39/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index c06eaae..0ece54c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -161,7 +161,7 @@
     <commons-io.version>2.4</commons-io.version>
     <commons-net.version>3.3</commons-net.version>
 
-    <bval.version>1.1.1</bval.version>
+    <bval.version>1.1.2-SNAPSHOT</bval.version>
     <org.apache.activemq.version>5.14.1</org.apache.activemq.version>
     <org.springframework.version>3.1.4.RELEASE</org.springframework.version>
     <junit.version>4.12</junit.version>