KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > pointcut > ast > ClassExpression


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.aop.pointcut.ast;
23
24 import java.util.regex.Matcher JavaDoc;
25 import java.util.regex.Pattern JavaDoc;
26
27 /**
28  * Comment
29  *
30  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
31  * @version $Revision: 40346 $
32  */

33 public class ClassExpression
34 {
35    private String JavaDoc original;
36    private Pattern JavaDoc classPattern;
37    private boolean isAnnotation = false;
38    private boolean isInstanceOf = false;
39    private boolean isTypedef = false;
40
41    private boolean isInstanceOfAnnotated = false;
42
43    public ClassExpression(String JavaDoc expr)
44    {
45       original = expr;
46       if (expr.startsWith("@"))
47       {
48          isAnnotation = true;
49       }
50       else
51       {
52          expr = original;
53          if (expr.startsWith("$instanceof{"))
54          {
55             isInstanceOf = true;
56             expr = expr.substring(12, expr.lastIndexOf("}"));
57             isInstanceOfAnnotated = (expr.startsWith("@"));
58          }
59          else if (expr.startsWith("$typedef{"))
60          {
61             isTypedef = true;
62             expr = expr.substring(9, expr.lastIndexOf("}"));
63          }
64          
65          if (!isAnnotation)
66          {
67              expr = expr.replaceAll("\\.", "\\\\.");
68              expr = expr.replaceAll("\\*", ".*");
69              expr = expr.replaceAll("\\[", "\\\\[");
70              expr = expr.replaceAll("]", "\\\\]");
71              expr = expr.replaceAll("\\$", "\\\\\\$");
72              classPattern = Pattern.compile(expr);
73          }
74       }
75
76    }
77
78    public boolean isSimple()
79    {
80       return !(isAnnotation || isInstanceOf || isTypedef || isInstanceOfAnnotated);
81    }
82
83
84    public boolean matches(String JavaDoc classname)
85    {
86       if (isAnnotation) return false;
87       Matcher JavaDoc m = classPattern.matcher(classname);
88       return m.matches();
89    }
90
91    public boolean matchesAnnotation(String JavaDoc annotation)
92    {
93       if (!isAnnotation) return false;
94       Matcher JavaDoc m = classPattern.matcher(annotation);
95       return m.matches();
96    }
97
98    public boolean isAnnotation()
99    {
100       return this.isAnnotation;
101    }
102
103    public boolean isInstanceOf()
104    {
105       return this.isInstanceOf;
106    }
107
108    public boolean isTypedef()
109    {
110       return this.isTypedef;
111    }
112
113    public boolean isInstanceOfAnnotated()
114    {
115       return this.isInstanceOfAnnotated;
116    }
117
118    public String JavaDoc getInstanceOfAnnotation()
119    {
120       if (!isInstanceOfAnnotated)
121       {
122          return null;
123       }
124       return original.substring(12, original.lastIndexOf("}"));
125    }
126
127    public String JavaDoc getOriginal()
128    {
129       return original;
130    }
131
132    public static String JavaDoc simpleType(Class JavaDoc type)
133    {
134       Class JavaDoc ret = type;
135       if (ret.isArray())
136       {
137          Class JavaDoc arr = ret;
138          String JavaDoc array = "";
139          while (arr.isArray())
140          {
141             array += "[]";
142             arr = arr.getComponentType();
143          }
144          return arr.getName() + array;
145       }
146       return ret.getName();
147    }
148    
149    public static void main(String JavaDoc[] args)
150    {
151       System.out.println(simpleType(Float.TYPE));
152    }
153 }
154
Popular Tags