forked from apache/camel
/
BeanInfo.java
1038 lines (915 loc) · 44.2 KB
/
BeanInfo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* 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.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.camel.Attachments;
import org.apache.camel.Body;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.ExchangeException;
import org.apache.camel.Expression;
import org.apache.camel.Handler;
import org.apache.camel.Header;
import org.apache.camel.Headers;
import org.apache.camel.Message;
import org.apache.camel.OutHeaders;
import org.apache.camel.Properties;
import org.apache.camel.Property;
import org.apache.camel.builder.ExpressionBuilder;
import org.apache.camel.language.LanguageAnnotation;
import org.apache.camel.spi.Registry;
import org.apache.camel.util.CastUtils;
import org.apache.camel.util.IntrospectionSupport;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.StringQuoteHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Represents the metadata about a bean type created via a combination of
* introspection and annotations together with some useful sensible defaults
*/
public class BeanInfo {
private static final transient Logger LOG = LoggerFactory.getLogger(BeanInfo.class);
private static final String CGLIB_CLASS_SEPARATOR = "$$";
private static final List<Method> EXCLUDED_METHODS = new ArrayList<Method>();
private final CamelContext camelContext;
private final BeanComponent component;
private final Class<?> type;
private final ParameterMappingStrategy strategy;
private final MethodInfo defaultMethod;
// shared state with details of operations introspected from the bean, created during the constructor
private Map<String, List<MethodInfo>> operations = new HashMap<String, List<MethodInfo>>();
private List<MethodInfo> operationsWithBody = new ArrayList<MethodInfo>();
private List<MethodInfo> operationsWithNoBody = new ArrayList<MethodInfo>();
private List<MethodInfo> operationsWithCustomAnnotation = new ArrayList<MethodInfo>();
private List<MethodInfo> operationsWithHandlerAnnotation = new ArrayList<MethodInfo>();
private Map<Method, MethodInfo> methodMap = new HashMap<Method, MethodInfo>();
static {
// exclude all java.lang.Object methods as we dont want to invoke them
EXCLUDED_METHODS.addAll(Arrays.asList(Object.class.getMethods()));
// exclude all java.lang.reflect.Proxy methods as we dont want to invoke them
EXCLUDED_METHODS.addAll(Arrays.asList(Proxy.class.getMethods()));
try {
// but keep toString as this method is okay
EXCLUDED_METHODS.remove(Object.class.getMethod("toString"));
EXCLUDED_METHODS.remove(Proxy.class.getMethod("toString"));
} catch (Throwable e) {
// ignore
}
}
public BeanInfo(CamelContext camelContext, Class<?> type) {
this(camelContext, type, createParameterMappingStrategy(camelContext));
}
public BeanInfo(CamelContext camelContext, Method explicitMethod) {
this(camelContext, explicitMethod.getDeclaringClass(), explicitMethod, createParameterMappingStrategy(camelContext));
}
public BeanInfo(CamelContext camelContext, Class<?> type, ParameterMappingStrategy strategy) {
this(camelContext, type, null, strategy);
}
public BeanInfo(CamelContext camelContext, Class<?> type, Method explicitMethod, ParameterMappingStrategy strategy) {
this.camelContext = camelContext;
this.type = type;
this.strategy = strategy;
this.component = camelContext.getComponent("bean", BeanComponent.class);
final BeanInfoCacheKey key = new BeanInfoCacheKey(type, explicitMethod);
// lookup if we have a bean info cache
BeanInfo beanInfo = component.getBeanInfoFromCache(key);
if (beanInfo != null) {
// copy the values from the cache we need
defaultMethod = beanInfo.defaultMethod;
operations = beanInfo.operations;
operationsWithBody = beanInfo.operationsWithBody;
operationsWithNoBody = beanInfo.operationsWithNoBody;
operationsWithCustomAnnotation = beanInfo.operationsWithCustomAnnotation;
operationsWithHandlerAnnotation = beanInfo.operationsWithHandlerAnnotation;
methodMap = beanInfo.methodMap;
return;
}
if (explicitMethod != null) {
// must be a valid method
if (!isValidMethod(type, explicitMethod)) {
throw new IllegalArgumentException("The method " + explicitMethod + " is not valid (for example the method must be public)");
}
introspect(getType(), explicitMethod);
} else {
introspect(getType());
}
// if there are only 1 method with 1 operation then select it as a default/fallback method
MethodInfo method = null;
if (operations.size() == 1) {
List<MethodInfo> methods = operations.values().iterator().next();
if (methods.size() == 1) {
method = methods.get(0);
}
}
defaultMethod = method;
// mark the operations lists as unmodifiable, as they should not change during runtime
// to keep this code thread safe
operations = Collections.unmodifiableMap(operations);
operationsWithBody = Collections.unmodifiableList(operationsWithBody);
operationsWithNoBody = Collections.unmodifiableList(operationsWithNoBody);
operationsWithCustomAnnotation = Collections.unmodifiableList(operationsWithCustomAnnotation);
operationsWithHandlerAnnotation = Collections.unmodifiableList(operationsWithHandlerAnnotation);
methodMap = Collections.unmodifiableMap(methodMap);
// add new bean info to cache
component.addBeanInfoToCache(key, this);
}
public Class<?> getType() {
return type;
}
public CamelContext getCamelContext() {
return camelContext;
}
public static ParameterMappingStrategy createParameterMappingStrategy(CamelContext camelContext) {
// lookup in registry first if there is a user define strategy
Registry registry = camelContext.getRegistry();
ParameterMappingStrategy answer = registry.lookupByNameAndType(BeanConstants.BEAN_PARAMETER_MAPPING_STRATEGY, ParameterMappingStrategy.class);
if (answer == null) {
// no then use the default one
answer = new DefaultParameterMappingStrategy();
}
return answer;
}
public MethodInvocation createInvocation(Object pojo, Exchange exchange)
throws AmbiguousMethodCallException, MethodNotFoundException {
return createInvocation(pojo, exchange, null);
}
private MethodInvocation createInvocation(Object pojo, Exchange exchange, Method explicitMethod)
throws AmbiguousMethodCallException, MethodNotFoundException {
MethodInfo methodInfo = null;
// find the explicit method to invoke
if (explicitMethod != null) {
Iterator<List<MethodInfo>> it = operations.values().iterator();
while (it.hasNext()) {
List<MethodInfo> infos = it.next();
for (MethodInfo info : infos) {
if (explicitMethod.equals(info.getMethod())) {
return info.createMethodInvocation(pojo, exchange);
}
}
}
throw new MethodNotFoundException(exchange, pojo, explicitMethod.getName());
}
String methodName = exchange.getIn().getHeader(Exchange.BEAN_METHOD_NAME, String.class);
if (methodName != null) {
// do not use qualifier for name
String name = methodName;
if (methodName.contains("(")) {
name = ObjectHelper.before(methodName, "(");
}
// 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");
}
} else {
List<MethodInfo> methods = getOperations(name);
if (methods != null && methods.size() == 1) {
// only one method then choose it
methodInfo = methods.get(0);
} 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);
if (methodInfo == null || !name.equals(methodInfo.getMethod().getName())) {
throw new AmbiguousMethodCallException(exchange, methods);
}
} else {
// a specific method was given to invoke but not found
throw new MethodNotFoundException(exchange, pojo, methodName);
}
}
}
if (methodInfo == null) {
// no name or type
methodInfo = chooseMethod(pojo, exchange, null);
}
if (methodInfo == null) {
methodInfo = defaultMethod;
}
if (methodInfo != null) {
LOG.trace("Chosen method to invoke: {} on bean: {}", methodInfo, pojo);
return methodInfo.createMethodInvocation(pojo, exchange);
}
LOG.debug("Cannot find suitable method to invoke on bean: {}", pojo);
return null;
}
/**
* Introspects the given class
*
* @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);
// if the class is not public then fallback and use interface methods if possible
// this allow Camel to invoke private beans which implements interfaces
List<Method> methods = Arrays.asList(clazz.getDeclaredMethods());
if (!Modifier.isPublic(clazz.getModifiers())) {
LOG.trace("Preferring interface methods as class: {} is not public accessible", clazz);
List<Method> interfaceMethods = getInterfaceMethods(clazz);
// still keep non-accessible class methods to provide more specific Exception if method is non-accessible
interfaceMethods.addAll(methods);
methods = interfaceMethods;
}
for (Method method : methods) {
boolean valid = isValidMethod(clazz, method);
LOG.trace("Method: {} is valid: {}", method, valid);
if (valid) {
introspect(clazz, method);
}
}
Class<?> superclass = clazz.getSuperclass();
if (superclass != null && !superclass.equals(Object.class)) {
introspect(superclass);
}
}
/**
* Introspects the given method
*
* @param clazz the class
* @param method the method
* @return the method info, is newer <tt>null</tt>
*/
private MethodInfo introspect(Class<?> clazz, Method method) {
LOG.trace("Introspecting class: {}, method: {}", clazz, method);
String opName = method.getName();
MethodInfo methodInfo = createMethodInfo(clazz, method);
// methods already registered should be preferred to use instead of super classes of existing methods
// we want to us the method from the sub class over super classes, so if we have already registered
// the method then use it (we are traversing upwards: sub (child) -> super (farther) )
MethodInfo existingMethodInfo = overridesExistingMethod(methodInfo);
if (existingMethodInfo != null) {
LOG.trace("This method is already overridden in a subclass, so the method from the sub class is preferred: {}", existingMethodInfo);
return existingMethodInfo;
}
LOG.trace("Adding operation: {} for method: {}", opName, methodInfo);
if (hasMethod(opName)) {
// we have an overloaded method so add the method info to the same key
List<MethodInfo> existing = getOperations(opName);
existing.add(methodInfo);
} else {
// its a new method we have not seen before so wrap it in a list and add it
List<MethodInfo> methods = new ArrayList<MethodInfo>();
methods.add(methodInfo);
operations.put(opName, methods);
}
if (methodInfo.hasCustomAnnotation()) {
operationsWithCustomAnnotation.add(methodInfo);
} else if (methodInfo.hasBodyParameter()) {
operationsWithBody.add(methodInfo);
} else {
operationsWithNoBody.add(methodInfo);
}
if (methodInfo.hasHandlerAnnotation()) {
operationsWithHandlerAnnotation.add(methodInfo);
}
// must add to method map last otherwise we break stuff
methodMap.put(method, methodInfo);
return methodInfo;
}
/**
* Returns the {@link MethodInfo} for the given method if it exists or null
* if there is no metadata available for the given method
*/
public MethodInfo getMethodInfo(Method method) {
MethodInfo answer = methodMap.get(method);
if (answer == null) {
// maybe the method is defined on a base class?
if (type != Object.class) {
Class<?> superclass = type.getSuperclass();
if (superclass != null && superclass != Object.class) {
BeanInfo superBeanInfo = new BeanInfo(camelContext, superclass, strategy);
return superBeanInfo.getMethodInfo(method);
}
}
}
return answer;
}
protected MethodInfo createMethodInfo(Class<?> clazz, Method method) {
Class<?>[] parameterTypes = method.getParameterTypes();
List<Annotation>[] parametersAnnotations = collectParameterAnnotations(clazz, method);
List<ParameterInfo> parameters = new ArrayList<ParameterInfo>();
List<ParameterInfo> bodyParameters = new ArrayList<ParameterInfo>();
boolean hasCustomAnnotation = false;
boolean hasHandlerAnnotation = ObjectHelper.hasAnnotation(method.getAnnotations(), Handler.class);
int size = parameterTypes.length;
if (LOG.isTraceEnabled()) {
LOG.trace("Creating MethodInfo for class: {} method: {} having {} parameters", new Object[]{clazz, method, size});
}
for (int i = 0; i < size; i++) {
Class<?> parameterType = parameterTypes[i];
Annotation[] parameterAnnotations = parametersAnnotations[i].toArray(new Annotation[parametersAnnotations[i].size()]);
Expression expression = createParameterUnmarshalExpression(clazz, method, parameterType, parameterAnnotations);
hasCustomAnnotation |= expression != null;
ParameterInfo parameterInfo = new ParameterInfo(i, parameterType, parameterAnnotations, expression);
LOG.trace("Parameter #{}: {}", i, parameterInfo);
parameters.add(parameterInfo);
if (expression == null) {
boolean bodyAnnotation = ObjectHelper.hasAnnotation(parameterAnnotations, Body.class);
LOG.trace("Parameter #{} has @Body annotation", i);
hasCustomAnnotation |= bodyAnnotation;
if (bodyParameters.isEmpty()) {
// okay we have not yet set the body parameter and we have found
// the candidate now to use as body parameter
if (Exchange.class.isAssignableFrom(parameterType)) {
// use exchange
expression = ExpressionBuilder.exchangeExpression();
} else {
// assume it's the body and it must be mandatory convertible to the parameter type
// but we allow null bodies in case the message really contains a null body
expression = ExpressionBuilder.mandatoryBodyExpression(parameterType, true);
}
LOG.trace("Parameter #{} is the body parameter using expression {}", i, expression);
parameterInfo.setExpression(expression);
bodyParameters.add(parameterInfo);
} else {
// will ignore the expression for parameter evaluation
}
}
LOG.trace("Parameter #{} has parameter info: ", i, parameterInfo);
}
// now let's add the method to the repository
return new MethodInfo(camelContext, clazz, method, parameters, bodyParameters, hasCustomAnnotation, hasHandlerAnnotation);
}
protected List<Annotation>[] collectParameterAnnotations(Class<?> c, Method m) {
@SuppressWarnings("unchecked")
List<Annotation>[] annotations = new List[m.getParameterTypes().length];
for (int i = 0; i < annotations.length; i++) {
annotations[i] = new ArrayList<Annotation>();
}
collectParameterAnnotations(c, m, annotations);
return annotations;
}
protected void collectParameterAnnotations(Class<?> c, Method m, List<Annotation>[] a) {
try {
Annotation[][] pa = c.getDeclaredMethod(m.getName(), m.getParameterTypes()).getParameterAnnotations();
for (int i = 0; i < pa.length; i++) {
a[i].addAll(Arrays.asList(pa[i]));
}
} catch (NoSuchMethodException e) {
// no method with signature of m declared on c
}
for (Class<?> i : c.getInterfaces()) {
collectParameterAnnotations(i, m, a);
}
if (!c.isInterface() && c.getSuperclass() != null) {
collectParameterAnnotations(c.getSuperclass(), m, a);
}
}
/**
* Choose one of the available methods to invoke if we can match
* the message body to the body parameter
*
* @param pojo the bean to invoke a method on
* @param exchange the message exchange
* @param name an optional name of the method that must match, use <tt>null</tt> to indicate all methods
* @return the method to invoke or null if no definitive method could be matched
* @throws AmbiguousMethodCallException is thrown if cannot choose method due to ambiguity
*/
protected MethodInfo chooseMethod(Object pojo, Exchange exchange, String name) throws AmbiguousMethodCallException {
// @Handler should be select first
// then any single method that has a custom @annotation
// or any single method that has a match parameter type that matches the Exchange payload
// and last then try to select the best among the rest
// must use defensive copy, to avoid altering the shared lists
// and we want to remove unwanted operations from these local lists
final List<MethodInfo> localOperationsWithBody = new ArrayList<MethodInfo>(operationsWithBody);
final List<MethodInfo> localOperationsWithNoBody = new ArrayList<MethodInfo>(operationsWithNoBody);
final List<MethodInfo> localOperationsWithCustomAnnotation = new ArrayList<MethodInfo>(operationsWithCustomAnnotation);
final List<MethodInfo> localOperationsWithHandlerAnnotation = new ArrayList<MethodInfo>(operationsWithHandlerAnnotation);
if (name != null) {
// filter all lists to only include methods with this name
removeNonMatchingMethods(localOperationsWithHandlerAnnotation, name);
removeNonMatchingMethods(localOperationsWithCustomAnnotation, name);
removeNonMatchingMethods(localOperationsWithBody, name);
removeNonMatchingMethods(localOperationsWithNoBody, name);
} else {
// remove all getter/setter as we do not want to consider these methods
removeAllSetterOrGetterMethods(localOperationsWithHandlerAnnotation);
removeAllSetterOrGetterMethods(localOperationsWithCustomAnnotation);
removeAllSetterOrGetterMethods(localOperationsWithBody);
removeAllSetterOrGetterMethods(localOperationsWithNoBody);
}
if (localOperationsWithHandlerAnnotation.size() > 1) {
// if we have more than 1 @Handler then its ambiguous
throw new AmbiguousMethodCallException(exchange, localOperationsWithHandlerAnnotation);
}
if (localOperationsWithHandlerAnnotation.size() == 1) {
// methods with handler should be preferred
return localOperationsWithHandlerAnnotation.get(0);
} else if (localOperationsWithCustomAnnotation.size() == 1) {
// if there is one method with an annotation then use that one
return localOperationsWithCustomAnnotation.get(0);
}
// named method and with no parameters
boolean noParameters = name != null && name.endsWith("()");
if (noParameters && localOperationsWithNoBody.size() == 1) {
// if there was a method name configured and it has no parameters, then use the method with no body (eg no parameters)
return localOperationsWithNoBody.get(0);
} else if (!noParameters && localOperationsWithBody.size() == 1) {
// if there is one method with body then use that one
return localOperationsWithBody.get(0);
}
Collection<MethodInfo> possibleOperations = new ArrayList<MethodInfo>();
possibleOperations.addAll(localOperationsWithBody);
possibleOperations.addAll(localOperationsWithCustomAnnotation);
if (!possibleOperations.isEmpty()) {
// multiple possible operations so find the best suited if possible
MethodInfo answer = chooseMethodWithMatchingBody(exchange, possibleOperations, localOperationsWithCustomAnnotation);
if (answer == null && name != null) {
// do we have hardcoded parameters values provided from the method name then fallback and try that
String parameters = ObjectHelper.between(name, "(", ")");
if (parameters != null) {
// special as we have hardcoded parameters, so we need to choose method that matches those parameters the best
answer = chooseMethodWithMatchingParameters(exchange, parameters, possibleOperations);
}
}
if (answer == null) {
throw new AmbiguousMethodCallException(exchange, possibleOperations);
} else {
return answer;
}
}
// not possible to determine
return null;
}
private MethodInfo chooseMethodWithMatchingParameters(Exchange exchange, String parameters, Collection<MethodInfo> operationList)
throws AmbiguousMethodCallException {
// we have hardcoded parameters so need to match that with the given operations
Iterator<?> it = ObjectHelper.createIterator(parameters);
int count = 0;
while (it.hasNext()) {
it.next();
count++;
}
List<MethodInfo> operations = new ArrayList<MethodInfo>();
for (MethodInfo info : operationList) {
if (info.getParameters().size() == count) {
operations.add(info);
}
}
if (operations.isEmpty()) {
return null;
} else if (operations.size() == 1) {
return operations.get(0);
}
// okay we still got multiple operations, so need to match the best one
List<MethodInfo> candidates = new ArrayList<MethodInfo>();
for (MethodInfo info : operations) {
it = ObjectHelper.createIterator(parameters);
int index = 0;
boolean matches = true;
while (it.hasNext()) {
String parameter = (String) it.next();
Class<?> parameterType = BeanHelper.getValidParameterType(parameter);
Class<?> expectedType = info.getParameters().get(index).getType();
if (parameterType != null && expectedType != null) {
if (!parameterType.isAssignableFrom(expectedType)) {
matches = false;
break;
}
}
index++;
}
if (matches) {
candidates.add(info);
}
}
if (candidates.size() > 1) {
throw new AmbiguousMethodCallException(exchange, candidates);
}
return candidates.size() == 1 ? candidates.get(0) : null;
}
private MethodInfo chooseMethodWithMatchingBody(Exchange exchange, Collection<MethodInfo> operationList,
List<MethodInfo> operationsWithCustomAnnotation)
throws AmbiguousMethodCallException {
// see if we can find a method whose body param type matches the message body
Message in = exchange.getIn();
Object body = in.getBody();
if (body != null) {
Class<?> bodyType = body.getClass();
if (LOG.isTraceEnabled()) {
LOG.trace("Matching for method with a single parameter that matches type: {}", bodyType.getCanonicalName());
}
List<MethodInfo> possibles = new ArrayList<MethodInfo>();
List<MethodInfo> possiblesWithException = new ArrayList<MethodInfo>();
for (MethodInfo methodInfo : operationList) {
// test for MEP pattern matching
boolean out = exchange.getPattern().isOutCapable();
if (out && methodInfo.isReturnTypeVoid()) {
// skip this method as the MEP is Out so the method must return something
continue;
}
// try to match the arguments
if (methodInfo.bodyParameterMatches(bodyType)) {
LOG.trace("Found a possible method: {}", methodInfo);
if (methodInfo.hasExceptionParameter()) {
// methods with accepts exceptions
possiblesWithException.add(methodInfo);
} else {
// regular methods with no exceptions
possibles.add(methodInfo);
}
}
}
// find best suited method to use
return chooseBestPossibleMethodInfo(exchange, operationList, body, possibles, possiblesWithException, operationsWithCustomAnnotation);
}
// no match so return null
return null;
}
private MethodInfo chooseBestPossibleMethodInfo(Exchange exchange, Collection<MethodInfo> operationList, Object body,
List<MethodInfo> possibles, List<MethodInfo> possiblesWithException,
List<MethodInfo> possibleWithCustomAnnotation)
throws AmbiguousMethodCallException {
Exception exception = ExpressionBuilder.exchangeExceptionExpression().evaluate(exchange, Exception.class);
if (exception != null && possiblesWithException.size() == 1) {
LOG.trace("Exchange has exception set so we prefer method that also has exception as parameter");
// prefer the method that accepts exception in case we have an exception also
return possiblesWithException.get(0);
} else if (possibles.size() == 1) {
return possibles.get(0);
} else if (possibles.isEmpty()) {
LOG.trace("No possible methods so now trying to convert body to parameter types");
// let's try converting
Object newBody = null;
MethodInfo matched = null;
int matchCounter = 0;
for (MethodInfo methodInfo : operationList) {
if (methodInfo.getBodyParameterType().isInstance(body)) {
return methodInfo;
}
// we should only try to convert, as we are looking for best match
Object value = exchange.getContext().getTypeConverter().tryConvertTo(methodInfo.getBodyParameterType(), exchange, body);
if (value != null) {
if (LOG.isTraceEnabled()) {
LOG.trace("Converted body from: {} to: {}",
body.getClass().getCanonicalName(), methodInfo.getBodyParameterType().getCanonicalName());
}
matchCounter++;
newBody = value;
matched = methodInfo;
}
}
if (matchCounter > 1) {
throw new AmbiguousMethodCallException(exchange, Arrays.asList(matched, matched));
}
if (matched != null) {
LOG.trace("Setting converted body: {}", body);
Message in = exchange.getIn();
in.setBody(newBody);
return matched;
}
} else {
// if we only have a single method with custom annotations, let's use that one
if (possibleWithCustomAnnotation.size() == 1) {
MethodInfo answer = possibleWithCustomAnnotation.get(0);
LOG.trace("There are only one method with annotations so we choose it: {}", answer);
return answer;
}
// phew try to choose among multiple methods with annotations
return chooseMethodWithCustomAnnotations(exchange, possibles);
}
// cannot find a good method to use
return null;
}
/**
* Validates whether the given method is a valid candidate for Camel Bean Binding.
*
* @param clazz the class
* @param method the method
* @return true if valid, false to skip the method
*/
protected boolean isValidMethod(Class<?> clazz, Method method) {
// must not be in the excluded list
for (Method excluded : EXCLUDED_METHODS) {
if (ObjectHelper.isOverridingMethod(excluded, method)) {
// the method is overriding an excluded method so its not valid
return false;
}
}
// must be a public method
if (!Modifier.isPublic(method.getModifiers())) {
return false;
}
// return type must not be an Exchange and it should not be a bridge method
if ((method.getReturnType() != null && Exchange.class.isAssignableFrom(method.getReturnType())) || method.isBridge()) {
return false;
}
return true;
}
/**
* Does the given method info override an existing method registered before (from a subclass)
*
* @param methodInfo the method to test
* @return the already registered method to use, null if not overriding any
*/
private MethodInfo overridesExistingMethod(MethodInfo methodInfo) {
for (MethodInfo info : methodMap.values()) {
Method source = info.getMethod();
Method target = methodInfo.getMethod();
boolean override = ObjectHelper.isOverridingMethod(source, target);
if (override) {
// same name, same parameters, then its overrides an existing class
return info;
}
}
return null;
}
private MethodInfo chooseMethodWithCustomAnnotations(Exchange exchange, Collection<MethodInfo> possibles)
throws AmbiguousMethodCallException {
// if we have only one method with custom annotations let's choose that
MethodInfo chosen = null;
for (MethodInfo possible : possibles) {
if (possible.hasCustomAnnotation()) {
if (chosen != null) {
chosen = null;
break;
} else {
chosen = possible;
}
}
}
if (chosen != null) {
return chosen;
}
throw new AmbiguousMethodCallException(exchange, possibles);
}
/**
* Creates an expression for the given parameter type if the parameter can
* be mapped automatically or null if the parameter cannot be mapped due to
* insufficient annotations or not fitting with the default type
* conventions.
*/
private Expression createParameterUnmarshalExpression(Class<?> clazz, Method method,
Class<?> parameterType, Annotation[] parameterAnnotation) {
// look for a parameter annotation that converts into an expression
for (Annotation annotation : parameterAnnotation) {
Expression answer = createParameterUnmarshalExpressionForAnnotation(clazz, method, parameterType, annotation);
if (answer != null) {
return answer;
}
}
// no annotations then try the default parameter mappings
return strategy.getDefaultParameterTypeExpression(parameterType);
}
private Expression createParameterUnmarshalExpressionForAnnotation(Class<?> clazz, Method method,
Class<?> parameterType, Annotation annotation) {
if (annotation instanceof Attachments) {
return ExpressionBuilder.attachmentsExpression();
} else if (annotation instanceof Property) {
Property propertyAnnotation = (Property)annotation;
return ExpressionBuilder.propertyExpression(propertyAnnotation.value());
} else if (annotation instanceof Properties) {
return ExpressionBuilder.propertiesExpression();
} else if (annotation instanceof Header) {
Header headerAnnotation = (Header)annotation;
return ExpressionBuilder.headerExpression(headerAnnotation.value());
} else if (annotation instanceof Headers) {
return ExpressionBuilder.headersExpression();
} else if (annotation instanceof OutHeaders) {
return ExpressionBuilder.outHeadersExpression();
} else if (annotation instanceof ExchangeException) {
return ExpressionBuilder.exchangeExceptionExpression(CastUtils.cast(parameterType, Exception.class));
} else {
LanguageAnnotation languageAnnotation = annotation.annotationType().getAnnotation(LanguageAnnotation.class);
if (languageAnnotation != null) {
Class<?> type = languageAnnotation.factory();
Object object = camelContext.getInjector().newInstance(type);
if (object instanceof AnnotationExpressionFactory) {
AnnotationExpressionFactory expressionFactory = (AnnotationExpressionFactory) object;
return expressionFactory.createExpression(camelContext, annotation, languageAnnotation, parameterType);
} else {
LOG.warn("Ignoring bad annotation: " + languageAnnotation + "on method: " + method
+ " which declares a factory: " + type.getName()
+ " which does not implement " + AnnotationExpressionFactory.class.getName());
}
}
}
return null;
}
private static List<Method> getInterfaceMethods(Class<?> clazz) {
final List<Method> answer = new ArrayList<Method>();
for (Class<?> interfaceClazz : clazz.getInterfaces()) {
for (Method interfaceMethod : interfaceClazz.getDeclaredMethods()) {
answer.add(interfaceMethod);
}
}
return answer;
}
private static void removeAllSetterOrGetterMethods(List<MethodInfo> methods) {
Iterator<MethodInfo> it = methods.iterator();
while (it.hasNext()) {
MethodInfo info = it.next();
if (IntrospectionSupport.isGetter(info.getMethod())) {
// skip getters
it.remove();
} else if (IntrospectionSupport.isSetter(info.getMethod())) {
// skip setters
it.remove();
}
}
}
private void removeNonMatchingMethods(List<MethodInfo> methods, String name) {
Iterator<MethodInfo> it = methods.iterator();
while (it.hasNext()) {
MethodInfo info = it.next();
if (!matchMethod(info.getMethod(), name)) {
// method does not match so remove it
it.remove();
}
}
}
private boolean matchMethod(Method method, String methodName) {
if (methodName == null) {
return true;
}
if (methodName.contains("(") && !methodName.endsWith(")")) {
throw new IllegalArgumentException("Name must have both starting and ending parenthesis, was: " + methodName);
}
// do not use qualifier for name matching
String name = methodName;
if (name.contains("(")) {
name = ObjectHelper.before(name, "(");
}
// must match name
if (!name.equals(method.getName())) {
return false;
}
// is it a method with no parameters
boolean noParameters = methodName.endsWith("()");
if (noParameters) {
return method.getParameterTypes().length == 0;
}
// match qualifier types which is used to select among overloaded methods
String types = ObjectHelper.between(methodName, "(", ")");
if (ObjectHelper.isNotEmpty(types)) {
// we must qualify based on types to match method
String[] parameters = StringQuoteHelper.splitSafeQuote(types, ',');
Iterator<?> it = ObjectHelper.createIterator(parameters);
for (int i = 0; i < method.getParameterTypes().length; i++) {
if (it.hasNext()) {
Class<?> parameterType = method.getParameterTypes()[i];
String qualifyType = (String) it.next();
if (ObjectHelper.isEmpty(qualifyType)) {
continue;
}
// trim the type
qualifyType = qualifyType.trim();
if ("*".equals(qualifyType)) {
// * is a wildcard so we accept and match that parameter type
continue;
}
if (BeanHelper.isValidParameterValue(qualifyType)) {
// its a parameter value, so continue to next parameter
// as we should only check for FQN/type parameters
continue;
}
// if qualify type indeed is a class, then it must be assignable with the parameter type
Boolean assignable = BeanHelper.isAssignableToExpectedType(getCamelContext().getClassResolver(), qualifyType, parameterType);
// the method will return null if the qualifyType is not a class
if (assignable != null && !assignable) {
return false;
}
} else {
// there method has more parameters than was specified in the method name qualifiers
return false;
}
}
// if the method has no more types then we can only regard it as matched
// if there are no more qualifiers
if (it.hasNext()) {
return false;
}
}
// the method matched
return true;
}
private static Class<?> getTargetClass(Class<?> clazz) {
if (clazz != null && clazz.getName().contains(CGLIB_CLASS_SEPARATOR)) {
Class<?> superClass = clazz.getSuperclass();
if (superClass != null && !Object.class.equals(superClass)) {
return superClass;
}
}
return clazz;
}
/**
* Do we have a method with the given name.
* <p/>
* Shorthand method names for getters is supported, so you can pass in eg 'name' and Camel
* will can find the real 'getName' method instead.
*
* @param methodName the method name
* @return <tt>true</tt> if we have such a method.
*/
public boolean hasMethod(String methodName) {
return getOperations(methodName) != null;
}
/**
* Do we have a static method with the given name.
* <p/>
* Shorthand method names for getters is supported, so you can pass in eg 'name' and Camel
* will can find the real 'getName' method instead.
*
* @param methodName the method name
* @return <tt>true</tt> if we have such a static method.
*/
public boolean hasStaticMethod(String methodName) {
List<MethodInfo> methods = getOperations(methodName);
if (methods == null || methods.isEmpty()) {
return false;
}
for (MethodInfo method : methods) {
if (method.isStaticMethod()) {
return true;
}
}
return false;
}
/**
* Gets the list of methods sorted by A..Z method name.
*
* @return the methods.
*/
public List<MethodInfo> getMethods() {
if (operations.isEmpty()) {
return Collections.emptyList();
}
List<MethodInfo> methods = new ArrayList<MethodInfo>();
for (Collection<MethodInfo> col : operations.values()) {
methods.addAll(col);
}
// sort the methods by name A..Z
Collections.sort(methods, new Comparator<MethodInfo>() {
public int compare(MethodInfo o1, MethodInfo o2) {
return o1.getMethod().getName().compareTo(o2.getMethod().getName());
}
});