View Javadoc

1   /***************************************************************************************
2    * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved.                 *
3    * http://aspectwerkz.codehaus.org                                                    *
4    * ---------------------------------------------------------------------------------- *
5    * The software in this package is published under the terms of the LGPL license      *
6    * a copy of which has been included with this distribution in the license.txt file.  *
7    **************************************************************************************/
8   package org.codehaus.aspectwerkz.annotation;
9   
10  import org.codehaus.aspectwerkz.annotation.expression.AnnotationVisitor;
11  import org.codehaus.aspectwerkz.annotation.expression.ast.AnnotationParser;
12  import org.codehaus.aspectwerkz.annotation.expression.ast.ParseException;
13  
14  import java.io.Serializable;
15  
16  /***
17   * The base class for the typed annotation proxies.
18   * 
19   * @author <a href="mailto:jboner@codehaus.org">Jonas Bonér </a>
20   */
21  public abstract class TypedAnnotationProxy implements Annotation, Serializable {
22      /***
23       * The one and only annotation parser.
24       */
25      protected static final AnnotationParser PARSER = new AnnotationParser(System.in);
26  
27      /***
28       * The name of the annotation.
29       */
30      protected String m_name;
31  
32      /***
33       * Returns the name.
34       * 
35       * @return the name
36       */
37      public String getName() {
38          return m_name;
39      }
40  
41      /***
42       * Sets the name of the annotation, the '@[name]'.
43       * 
44       * @param name
45       */
46      public void setName(final String name) {
47          m_name = name;
48      }
49  
50      /***
51       * Sets the full value of the annotation (including possible named parameters etc.)
52       * as @Foo(x=3 ...).
53       *
54       * @param name the name of the annotation FQN with package name etc
55       * @param value the key/value pairs separated with commas (key1=value1, key2=value2, ...)
56       */
57      public void initialize(final String name, String value) {
58          if (name == null) {
59              throw new IllegalArgumentException("name can not be null");
60          }
61          setName(name);
62  
63          // FIXME - Alex to Jonas: why not keeping the FQN ?
64          // if proxy was used, the real "shortName" should be extracted back from the properties file anywaay
65  
66          String shortName = name;
67          int index = name.lastIndexOf('$');
68          if (index > 0) {
69              shortName = name.substring(index + 1, name.length());
70          } else {
71              index = name.lastIndexOf('.');
72              if (index > 0) {
73                  shortName = name.substring(index + 1, name.length());
74              }
75          }
76  
77          StringBuffer representation = new StringBuffer("@");
78          representation.append(shortName).append('(');
79          if (value!=null) {
80              representation.append(value);
81          }
82          representation.append(')');
83  
84          try {
85              AnnotationVisitor.parse(this, PARSER.parse(representation.toString()));
86          } catch (ParseException e) {
87              e.printStackTrace();
88              throw new RuntimeException("could not parse annotation [" + m_name + " " + representation.toString() + "]");
89          }
90      }
91  
92      /***
93       * Checks if the annotation is typed or not.
94       * 
95       * @return boolean
96       */
97      public boolean isTyped() {
98          return true;
99      }
100 }