1
2
3
4
5
6
7
8
9
10 package org.mule.config.expression;
11
12 import org.mule.api.MuleContext;
13 import org.mule.api.expression.ExpressionAnnotationParser;
14 import org.mule.api.lifecycle.InitialisationException;
15 import org.mule.api.registry.RegistrationException;
16 import org.mule.api.transformer.TransformerException;
17 import org.mule.config.AnnotationsParserFactory;
18 import org.mule.config.i18n.AnnotationsMessages;
19 import org.mule.expression.transformers.ExpressionArgument;
20 import org.mule.expression.transformers.ExpressionTransformer;
21
22 import java.lang.annotation.Annotation;
23 import java.lang.reflect.Method;
24
25
26
27
28 public class ExpressionAnnotationsHelper
29 {
30 public static ExpressionTransformer getTransformerForMethodWithAnnotations(Method method, MuleContext context) throws TransformerException, InitialisationException
31 {
32 ExpressionTransformer trans = new ExpressionTransformer();
33 trans.setMuleContext(context);
34
35 Annotation[][] annotations = method.getParameterAnnotations();
36
37 for (int i = 0; i < annotations.length; i++)
38 {
39 Annotation[] annotation = annotations[i];
40 for (int j = 0; j < annotation.length; j++)
41 {
42 Annotation ann = annotation[j];
43 ExpressionArgument arg = parseAnnotation(ann, method.getParameterTypes()[i], context);
44
45 trans.addArgument(arg);
46 }
47 }
48 trans.initialise();
49 return trans;
50 }
51
52 static synchronized ExpressionArgument parseAnnotation(Annotation annotation,
53 Class<?> paramType, MuleContext muleContext)
54 {
55 AnnotationsParserFactory factory;
56 try
57 {
58 factory = muleContext.getRegistry().lookupObject(AnnotationsParserFactory.class);
59 }
60 catch (RegistrationException e)
61 {
62
63 throw new IllegalArgumentException(AnnotationsMessages.noParserFoundForAnnotation(annotation).getMessage());
64 }
65
66 ExpressionAnnotationParser parser = factory.getExpressionParser(annotation);
67 if (parser == null)
68 {
69 throw new IllegalArgumentException(AnnotationsMessages.noParserFoundForAnnotation(annotation).getMessage());
70 }
71 return parser.parse(annotation, paramType);
72 }
73 }