camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [1/3] camel git commit: Corrected issues in BeanInfo introspection
Date Wed, 05 Jul 2017 08:34:20 GMT
Repository: camel
Updated Branches:
  refs/heads/master 763c9e021 -> e6bcdba11


Corrected issues in BeanInfo introspection


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

Branch: refs/heads/master
Commit: 12f9de9f7a033e710b647bbf17adb7d4e31685d8
Parents: 763c9e0
Author: aldettinger <aldettinger@gmail.com>
Authored: Tue Jul 4 22:14:35 2017 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Wed Jul 5 09:41:42 2017 +0200

----------------------------------------------------------------------
 .../apache/camel/component/bean/BeanInfo.java   | 51 +++++-------
 .../camel/component/bean/MethodsFilter.java     | 84 ++++++++++++++++++++
 .../org/apache/camel/util/ObjectHelper.java     |  2 +-
 3 files changed, 103 insertions(+), 34 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/12f9de9f/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java b/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
index 62217c0..48eb177 100644
--- a/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
+++ b/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
@@ -304,43 +304,41 @@ public class BeanInfo {
      * @param clazz the class
      */
     private void introspect(Class<?> clazz) {
-        // get the target clazz as it could potentially have been enhanced by CGLIB etc.
-        clazz = getTargetClass(clazz);
-        ObjectHelper.notNull(clazz, "clazz", this);
-
-        LOG.trace("Introspecting class: {}", clazz);
 
         // does the class have any public constructors?
         publicConstructors = clazz.getConstructors().length > 0;
 
-        // favor declared methods, and then filter out duplicate interface methods
-        List<Method> methods;
-        if (Modifier.isPublic(clazz.getModifiers())) {
-            LOG.trace("Preferring class methods as class: {} is public accessible", clazz);
-            methods = new ArrayList<Method>(Arrays.asList(clazz.getDeclaredMethods()));
-        } else {
-            LOG.trace("Preferring interface methods as class: {} is not public accessible",
clazz);
-            methods = getInterfaceMethods(clazz);
-            // and then we must add its declared methods as well
-            List<Method> extraMethods = Arrays.asList(clazz.getDeclaredMethods());
-            methods.addAll(extraMethods);
-        }
+        MethodsFilter methods = new MethodsFilter(getType());
+        introspect(clazz, methods);
 
         // now introspect the methods and filter non valid methods
-        for (Method method : methods) {
+        for (Method method : methods.asReadOnlyList()) {
             boolean valid = isValidMethod(clazz, method);
             LOG.trace("Method: {} is valid: {}", method, valid);
             if (valid) {
                 introspect(clazz, method);
             }
         }
+    }
+
+    private void introspect(Class<?> clazz, MethodsFilter filteredMethods) {
+        // get the target clazz as it could potentially have been enhanced by
+        // CGLIB etc.
+        clazz = getTargetClass(clazz);
+        ObjectHelper.notNull(clazz, "clazz", this);
+
+        LOG.trace("Introspecting class: {}", clazz);
+
+        for (Method m : Arrays.asList(clazz.getDeclaredMethods())) {
+            filteredMethods.filterMethod(m);
+        }
 
         Class<?> superClass = clazz.getSuperclass();
         if (superClass != null && !superClass.equals(Object.class)) {
-            introspect(superClass);
+            introspect(superClass, filteredMethods);
         }
         for (Class<?> superInterface : clazz.getInterfaces()) {
-            introspect(superInterface);
+            introspect(superInterface, filteredMethods);
         }
     }
 
@@ -1006,19 +1004,6 @@ public class BeanInfo {
 
         return null;
     }
-    
-    private static List<Method> getInterfaceMethods(Class<?> clazz) {
-        final List<Method> answer = new ArrayList<Method>();
-
-        while (clazz != null && !clazz.equals(Object.class)) {
-            for (Class<?> interfaceClazz : clazz.getInterfaces()) {
-                Collections.addAll(answer, interfaceClazz.getDeclaredMethods());
-            }
-            clazz = clazz.getSuperclass();
-        }
-
-        return answer;
-    }
 
     private static void removeAllSetterOrGetterMethods(List<MethodInfo> methods) {
         Iterator<MethodInfo> it = methods.iterator();

http://git-wip-us.apache.org/repos/asf/camel/blob/12f9de9f/camel-core/src/main/java/org/apache/camel/component/bean/MethodsFilter.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/bean/MethodsFilter.java b/camel-core/src/main/java/org/apache/camel/component/bean/MethodsFilter.java
new file mode 100644
index 0000000..c4e3faf
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/component/bean/MethodsFilter.java
@@ -0,0 +1,84 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.bean;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.camel.util.ObjectHelper;
+
+/**
+ * This class aims at retaining the right methods while parsing a given
+ * {@link java.lang.Class}.
+ */
+class MethodsFilter {
+
+    private final List<Method> methods = new ArrayList<Method>();
+    private final Class<?> inheritingClass;
+
+    /**
+     * Creates a <code>MethodsFilter</code> for a given {@link java.lang.Class}.
+     *
+     * @param clazz The {@link java.lang.Class} whose methods are to be
+     *            filtered.
+     */
+    public MethodsFilter(Class<?> clazz) {
+        this.inheritingClass = clazz;
+    }
+
+    /**
+     * Retains methods, preferring those from public classes in case of
+     * overrides.
+     *
+     * @param proposedMethod The method proposed to the filter.
+     */
+    void filterMethod(Method proposedMethod) {
+        if (proposedMethod.isBridge()) {
+            return;
+        }
+
+        for (int i = 0; i < methods.size(); i++) {
+            Method alreadyRegistered = methods.get(i);
+
+            if (Modifier.isPublic(proposedMethod.getDeclaringClass().getModifiers())) {
+                boolean overridden = ObjectHelper.isOverridingMethod(inheritingClass, proposedMethod,
alreadyRegistered, false);
+                boolean overridding = ObjectHelper.isOverridingMethod(inheritingClass, alreadyRegistered,
proposedMethod, false);
+
+                boolean registeredMethodIsPublic = Modifier.isPublic(alreadyRegistered.getDeclaringClass().getModifiers());
+
+                if (overridden && !registeredMethodIsPublic) {
+                    // Retain the overridden method from a public class
+                    methods.set(i, proposedMethod);
+                    return;
+                } else if (overridding) {
+                    // Retain the override from a public class
+                    methods.set(i, proposedMethod);
+                    return;
+                }
+            }
+        }
+
+        methods.add(proposedMethod);
+    }
+
+    List<Method> asReadOnlyList() {
+        return Collections.unmodifiableList(methods);
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/12f9de9f/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java b/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
index 73b6fae..ba9b02f 100644
--- a/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
@@ -1438,7 +1438,7 @@ public final class ObjectHelper {
 
         if (source.equals(target)) {
             return true;
-        } else if (source.getDeclaringClass() == target.getDeclaringClass()) {
+        } else if (target.getDeclaringClass().isAssignableFrom(source.getDeclaringClass()))
{
             return false;
         } else if (!source.getDeclaringClass().isAssignableFrom(inheritingClass) || !target.getDeclaringClass().isAssignableFrom(inheritingClass))
{
             return false;


Mime
View raw message