This is an automated email from the ASF dual-hosted git repository. orpiske pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit 42b81db7d4da70f6685dbae57f4ba263abb1b6d6 Author: Otavio Rodolfo Piske <angusyo...@gmail.com> AuthorDate: Fri Jan 31 11:48:45 2025 +0100 (chores) camel-bean: refactor large methods to help inlining --- .../org/apache/camel/component/bean/BeanInfo.java | 120 +++++++++++++-------- .../apache/camel/component/bean/MethodInfo.java | 50 +++++---- 2 files changed, 104 insertions(+), 66 deletions(-) diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java index 99b646dcaa3..2d831de3617 100644 --- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java +++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java @@ -209,59 +209,16 @@ public class BeanInfo { // special for getClass, as we want the user to be able to invoke this method // for example to log the class type or the likes if ("class".equals(name) || "getClass".equals(name)) { - try { - Method method = pojo.getClass().getMethod("getClass"); - methodInfo = new MethodInfo( - exchange.getContext(), pojo.getClass(), method, Collections.<ParameterInfo> emptyList(), - Collections.<ParameterInfo> emptyList(), false, false); - } catch (NoSuchMethodException e) { - throw new MethodNotFoundException(exchange, pojo, "getClass"); - } + methodInfo = createGetClassInvocation(pojo, exchange); // special for length on an array type } else if ("length".equals(name) && pojo.getClass().isArray()) { - try { - // need to use arrayLength method from ObjectHelper as Camel's bean OGNL support is method invocation based - // and not for accessing fields. And hence we need to create a MethodInfo instance with a method to call - // and therefore use arrayLength from ObjectHelper to return the array length field. - Method method = org.apache.camel.util.ObjectHelper.class.getMethod("arrayLength", Object[].class); - ParameterInfo pi = new ParameterInfo( - 0, Object[].class, null, ExpressionBuilder.mandatoryBodyExpression(Object[].class, true)); - List<ParameterInfo> lpi = new ArrayList<>(1); - lpi.add(pi); - methodInfo = new MethodInfo(exchange.getContext(), pojo.getClass(), method, lpi, lpi, false, false); - // Need to update the message body to be pojo for the invocation - exchange.getIn().setBody(pojo); - } catch (NoSuchMethodException e) { - throw new MethodNotFoundException(exchange, pojo, "getClass"); - } + methodInfo = createLengthInvocation(pojo, exchange); } else { List<MethodInfo> methods = getOperations(name); if (methods != null && methods.size() == 1) { - // only one method then choose it - methodInfo = methods.get(0); - - // validate that if we want an explicit no-arg method, then that's what we get - if (emptyParameters && methodInfo.hasParameters()) { - throw new MethodNotFoundException(exchange, pojo, methodName, "(with no parameters)"); - } + methodInfo = createSingleMethodInvocation(pojo, exchange, methods, emptyParameters, methodName); } else if (methods != null) { - // there are more methods with that name so we cannot decide which to use - - // but first let's try to choose a method and see if that complies with the name - // must use the method name which may have qualifiers - methodInfo = chooseMethod(pojo, exchange, methodName); - - // validate that if we want an explicit no-arg method, then that's what we get - if (emptyParameters) { - if (methodInfo == null || methodInfo.hasParameters()) { - // we could not find a no-arg method with that name - throw new MethodNotFoundException(exchange, pojo, methodName, "(with no parameters)"); - } - } - - if (methodInfo == null || !name.equals(methodInfo.getMethod().getName())) { - throw new AmbiguousMethodCallException(exchange, methods); - } + methodInfo = evalMethods(pojo, exchange, methodName, emptyParameters, name, methods); } else { // a specific method was given to invoke but not found throw new MethodNotFoundException(exchange, pojo, methodName); @@ -285,6 +242,75 @@ public class BeanInfo { return null; } + private MethodInfo evalMethods( + Object pojo, Exchange exchange, String methodName, boolean emptyParameters, String name, List<MethodInfo> methods) { + MethodInfo methodInfo; + // there are more methods with that name so we cannot decide which to use + + // but first let's try to choose a method and see if that complies with the name + // must use the method name which may have qualifiers + methodInfo = chooseMethod(pojo, exchange, methodName); + + // validate that if we want an explicit no-arg method, then that's what we get + if (emptyParameters) { + if (methodInfo == null || methodInfo.hasParameters()) { + // we could not find a no-arg method with that name + throw new MethodNotFoundException(exchange, pojo, methodName, "(with no parameters)"); + } + } + + if (methodInfo == null || !name.equals(methodInfo.getMethod().getName())) { + throw new AmbiguousMethodCallException(exchange, methods); + } + return methodInfo; + } + + private static MethodInfo createSingleMethodInvocation( + Object pojo, Exchange exchange, List<MethodInfo> methods, boolean emptyParameters, String methodName) { + MethodInfo methodInfo; + // only one method then choose it + methodInfo = methods.get(0); + + // validate that if we want an explicit no-arg method, then that's what we get + if (emptyParameters && methodInfo.hasParameters()) { + throw new MethodNotFoundException(exchange, pojo, methodName, "(with no parameters)"); + } + return methodInfo; + } + + private static MethodInfo createLengthInvocation(Object pojo, Exchange exchange) { + MethodInfo methodInfo; + try { + // need to use arrayLength method from ObjectHelper as Camel's bean OGNL support is method invocation based + // and not for accessing fields. And hence we need to create a MethodInfo instance with a method to call + // and therefore use arrayLength from ObjectHelper to return the array length field. + Method method = org.apache.camel.util.ObjectHelper.class.getMethod("arrayLength", Object[].class); + ParameterInfo pi = new ParameterInfo( + 0, Object[].class, null, ExpressionBuilder.mandatoryBodyExpression(Object[].class, true)); + List<ParameterInfo> lpi = new ArrayList<>(1); + lpi.add(pi); + methodInfo = new MethodInfo(exchange.getContext(), pojo.getClass(), method, lpi, lpi, false, false); + // Need to update the message body to be pojo for the invocation + exchange.getIn().setBody(pojo); + } catch (NoSuchMethodException e) { + throw new MethodNotFoundException(exchange, pojo, "getClass"); + } + return methodInfo; + } + + private static MethodInfo createGetClassInvocation(Object pojo, Exchange exchange) { + MethodInfo methodInfo; + try { + Method method = pojo.getClass().getMethod("getClass"); + methodInfo = new MethodInfo( + exchange.getContext(), pojo.getClass(), method, Collections.<ParameterInfo> emptyList(), + Collections.<ParameterInfo> emptyList(), false, false); + } catch (NoSuchMethodException e) { + throw new MethodNotFoundException(exchange, pojo, "getClass"); + } + return methodInfo; + } + /** * Introspects the given class * diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/MethodInfo.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/MethodInfo.java index 5e6d0ac34de..1d601e5cec6 100644 --- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/MethodInfo.java +++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/MethodInfo.java @@ -297,14 +297,7 @@ public class MethodInfo { private boolean doProceed(AsyncCallback callback) throws Exception { // dynamic router should be invoked beforehand if (dynamicRouter != null) { - if (!ServiceHelper.isStarted(dynamicRouter)) { - ServiceHelper.startService(dynamicRouter); - } - // use an expression which invokes the method to be used by dynamic router - Expression expression = new DynamicRouterExpression(pojo); - expression.init(camelContext); - exchange.setProperty(ExchangePropertyKey.EVALUATE_EXPRESSION_RESULT, expression); - return dynamicRouter.process(exchange, callback); + return dynamicRouterInvocation(callback); } // invoke pojo @@ -327,19 +320,10 @@ public class MethodInfo { } if (recipientList != null) { - // ensure its started - if (!ServiceHelper.isStarted(recipientList)) { - ServiceHelper.startService(recipientList); - } - exchange.setProperty(ExchangePropertyKey.EVALUATE_EXPRESSION_RESULT, result); - return recipientList.process(exchange, callback); + return recipientListInvocation(callback, result); } if (routingSlip != null) { - if (!ServiceHelper.isStarted(routingSlip)) { - ServiceHelper.startService(routingSlip); - } - exchange.setProperty(ExchangePropertyKey.EVALUATE_EXPRESSION_RESULT, result); - return routingSlip.process(exchange, callback); + return routingSlipInvocation(callback, result); } //If it's Java 8 async result @@ -369,6 +353,34 @@ public class MethodInfo { return true; } + private boolean routingSlipInvocation(AsyncCallback callback, Object result) { + if (!ServiceHelper.isStarted(routingSlip)) { + ServiceHelper.startService(routingSlip); + } + exchange.setProperty(ExchangePropertyKey.EVALUATE_EXPRESSION_RESULT, result); + return routingSlip.process(exchange, callback); + } + + private boolean recipientListInvocation(AsyncCallback callback, Object result) { + // ensure its started + if (!ServiceHelper.isStarted(recipientList)) { + ServiceHelper.startService(recipientList); + } + exchange.setProperty(ExchangePropertyKey.EVALUATE_EXPRESSION_RESULT, result); + return recipientList.process(exchange, callback); + } + + private boolean dynamicRouterInvocation(AsyncCallback callback) { + if (!ServiceHelper.isStarted(dynamicRouter)) { + ServiceHelper.startService(dynamicRouter); + } + // use an expression which invokes the method to be used by dynamic router + Expression expression = new DynamicRouterExpression(pojo); + expression.init(camelContext); + exchange.setProperty(ExchangePropertyKey.EVALUATE_EXPRESSION_RESULT, expression); + return dynamicRouter.process(exchange, callback); + } + public Object getThis() { return pojo; }