You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2017/07/05 08:34:20 UTC
[1/3] camel git commit: Corrected issues in BeanInfo introspection
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 <al...@gmail.com>
Authored: Tue Jul 4 22:14:35 2017 +0200
Committer: Claus Ibsen <da...@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;
[2/3] camel git commit: Implemented 2 tests showing issues in BeanInfo
Posted by da...@apache.org.
Implemented 2 tests showing issues in BeanInfo
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/7376cc40
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/7376cc40
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/7376cc40
Branch: refs/heads/master
Commit: 7376cc40a72da1b0574e149b8e6cd44e5805aa6e
Parents: 12f9de9
Author: aldettinger <al...@gmail.com>
Authored: Thu Jun 29 18:26:48 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Jul 5 09:41:43 2017 +0200
----------------------------------------------------------------------
.../camel/component/bean/BeanInfoTest.java | 55 +++++++++++++++++++-
1 file changed, 54 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/7376cc40/camel-core/src/test/java/org/apache/camel/component/bean/BeanInfoTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/bean/BeanInfoTest.java b/camel-core/src/test/java/org/apache/camel/component/bean/BeanInfoTest.java
index b541345..84d3fde 100644
--- a/camel-core/src/test/java/org/apache/camel/component/bean/BeanInfoTest.java
+++ b/camel-core/src/test/java/org/apache/camel/component/bean/BeanInfoTest.java
@@ -17,6 +17,7 @@
package org.apache.camel.component.bean;
import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
import java.util.List;
import junit.framework.TestCase;
@@ -26,11 +27,12 @@ import org.apache.camel.InOnly;
import org.apache.camel.InOut;
import org.apache.camel.Pattern;
import org.apache.camel.impl.DefaultCamelContext;
+import org.junit.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- * @version
+ * @version
*/
public class BeanInfoTest extends TestCase {
private static final Logger LOG = LoggerFactory.getLogger(BeanInfoTest.class);
@@ -112,6 +114,30 @@ public class BeanInfoTest extends TestCase {
assertMethodPattern(info, "inOutMethod", ExchangePattern.InOut);
}
+ public void testImplementLevel2InterfaceMethodInPackagePrivateClass() {
+ BeanInfo info = createBeanInfo(PackagePrivateClassImplementingLevel2InterfaceMethod.class);
+ List<MethodInfo> mis = info.getMethods();
+ Assert.assertNotNull(mis);
+ Assert.assertEquals(1, mis.size());
+ MethodInfo mi = mis.get(0);
+ Assert.assertNotNull(mi);
+ Method m = mi.getMethod();
+ Assert.assertEquals("method", m.getName());
+ Assert.assertTrue(Modifier.isPublic(m.getDeclaringClass().getModifiers()));
+ }
+
+ public void testPublicClassImplementingInterfaceMethodBySuperPackagePrivateClass() {
+ BeanInfo info = createBeanInfo(PublicClassImplementingBySuperPackagePrivateClass.class);
+ List<MethodInfo> mis = info.getMethods();
+ Assert.assertNotNull(mis);
+ Assert.assertEquals(1, mis.size());
+ MethodInfo mi = mis.get(0);
+ Assert.assertNotNull(mi);
+ Method m = mi.getMethod();
+ Assert.assertEquals("method", m.getName());
+ Assert.assertTrue(Modifier.isPublic(m.getDeclaringClass().getModifiers()));
+ }
+
protected BeanInfo createBeanInfo(Class<?> type) {
BeanInfo info = new BeanInfo(camelContext, type);
return info;
@@ -185,4 +211,31 @@ public class BeanInfoTest extends TestCase {
}
}
+ public interface ILevel2Interface {
+ String method();
+ }
+
+ public interface ILevel1Interface extends ILevel2Interface {
+ }
+
+ class PackagePrivateClassImplementingLevel2InterfaceMethod implements ILevel1Interface {
+ @Override
+ public String method() {
+ return "PackagePrivateClassImplementingLevel2InterfaceMethod.method() has been called";
+ }
+ }
+
+ public interface IMethodInterface {
+ String method();
+ }
+
+ class PackagePrivateClassDefiningMethod {
+ public String method() {
+ return "PackagePrivateClassDefiningMethod.method() has been called";
+ }
+ }
+
+ public class PublicClassImplementingBySuperPackagePrivateClass extends PackagePrivateClassDefiningMethod implements IMethodInterface {
+ }
+
}
[3/3] camel git commit: Fixed CS
Posted by da...@apache.org.
Fixed CS
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/e6bcdba1
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/e6bcdba1
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/e6bcdba1
Branch: refs/heads/master
Commit: e6bcdba111d267bd90e29ce2261dde0aae3a196c
Parents: 7376cc4
Author: Claus Ibsen <da...@apache.org>
Authored: Wed Jul 5 10:14:19 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Jul 5 10:14:19 2017 +0200
----------------------------------------------------------------------
.../java/org/apache/camel/support/ReloadStrategySupport.java | 3 +--
.../src/main/java/org/apache/camel/util/LRUCacheFactory.java | 7 +++++--
2 files changed, 6 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/e6bcdba1/camel-core/src/main/java/org/apache/camel/support/ReloadStrategySupport.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/support/ReloadStrategySupport.java b/camel-core/src/main/java/org/apache/camel/support/ReloadStrategySupport.java
index ecc040c..41d2600 100644
--- a/camel-core/src/main/java/org/apache/camel/support/ReloadStrategySupport.java
+++ b/camel-core/src/main/java/org/apache/camel/support/ReloadStrategySupport.java
@@ -22,7 +22,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
-import org.apache.camel.util.LRUCacheFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@@ -35,7 +34,7 @@ import org.apache.camel.model.RouteDefinition;
import org.apache.camel.model.RoutesDefinition;
import org.apache.camel.spi.ReloadStrategy;
import org.apache.camel.util.CollectionStringBuffer;
-import org.apache.camel.util.LRUCache;
+import org.apache.camel.util.LRUCacheFactory;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.StringHelper;
import org.apache.camel.util.XmlLineNumberParser;
http://git-wip-us.apache.org/repos/asf/camel/blob/e6bcdba1/camel-core/src/main/java/org/apache/camel/util/LRUCacheFactory.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/LRUCacheFactory.java b/camel-core/src/main/java/org/apache/camel/util/LRUCacheFactory.java
index 71363b3..97d5018 100644
--- a/camel-core/src/main/java/org/apache/camel/util/LRUCacheFactory.java
+++ b/camel-core/src/main/java/org/apache/camel/util/LRUCacheFactory.java
@@ -29,17 +29,20 @@ public final class LRUCacheFactory {
private static final Logger LOG = LoggerFactory.getLogger(LRUCacheFactory.class);
- private static final AtomicBoolean init = new AtomicBoolean();
+ private static final AtomicBoolean INIT = new AtomicBoolean();
private LRUCacheFactory() {
}
+ /**
+ * Warm-up the LRUCache to startup Apache Camel faster.
+ */
@SuppressWarnings("unchecked")
public static void warmUp() {
// create a dummy map in a separate thread to warm-up the Caffeine cache concurrently
// while Camel is starting up. This allows us to overall startup Camel a bit faster
// as Caffeine takes 150+ millis to initialize.
- if (init.compareAndSet(false, true)) {
+ if (INIT.compareAndSet(false, true)) {
// only need to init Caffeine once in the JVM/classloader
Runnable task = () -> {
StopWatch watch = new StopWatch();