View Javadoc

1   /*
2    * $Id: LookupAnnotationParser.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.config.expression;
11  
12  import org.mule.api.annotations.expressions.Lookup;
13  import org.mule.api.annotations.meta.Evaluator;
14  import org.mule.api.expression.ExpressionAnnotationParser;
15  import org.mule.expression.ExpressionConfig;
16  import org.mule.expression.transformers.ExpressionArgument;
17  import org.mule.util.StringUtils;
18  
19  import java.lang.annotation.Annotation;
20  
21  /**
22   * Used to parse Expr parameter annotations
23   *
24   * @see org.mule.expression.StringExpressionEvaluator
25   * @see org.mule.api.annotations.expressions.Expr
26   *
27   * @since 3.0
28   */
29  public class LookupAnnotationParser implements ExpressionAnnotationParser
30  {
31      public ExpressionArgument parse(Annotation annotation, Class<?> parameterType)
32      {
33          Evaluator evaluator = annotation.annotationType().getAnnotation(Evaluator.class);
34          if (evaluator != null)
35          {
36              String expression = ((Lookup) annotation).value();
37              if(StringUtils.isEmpty(expression))
38              {
39                  expression  = "type:" + parameterType.getName();
40              }
41              return new ExpressionArgument(null, new ExpressionConfig(expression,
42                      evaluator.value(), null), ((Lookup) annotation).optional(), parameterType);
43          }
44          else
45          {
46              throw new IllegalArgumentException("The @Evaluator annotation must be set on an Expression Annotation");
47          }
48  
49      }
50  
51      public boolean supports(Annotation annotation)
52      {
53          return annotation instanceof Lookup;
54      }
55  }