KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > expressions > ParameterExpression


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.expressions;
23
24 import javassist.CtClass;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28 import java.util.regex.Matcher JavaDoc;
29 import java.util.regex.Pattern JavaDoc;
30
31
32 /**
33  * Comment
34  *
35  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
36  * @version $Revision: 37406 $
37  *
38  **/

39 public class ParameterExpression
40 {
41    protected static final Pattern JavaDoc ALL;
42    protected static final Pattern JavaDoc PARAMS;
43
44    static
45    {
46       ALL = Pattern.compile("\\s*\\.\\.\\s*");
47       PARAMS = Pattern.compile("\\s*(.*)\\s*");
48    }
49
50
51    public String JavaDoc originalExpression;
52    public Pattern JavaDoc[] parameterPatterns;
53    public boolean isAll = false;
54
55    protected static String JavaDoc simpleType(Class JavaDoc type)
56    {
57       Class JavaDoc ret = type;
58       if (ret.isArray())
59       {
60          Class JavaDoc arr = ret;
61          String JavaDoc array = "";
62          while (arr.isArray())
63          {
64             array += "[]";
65             arr = arr.getComponentType();
66          }
67          return arr.getName() + array;
68       }
69       return ret.getName();
70    }
71    /**
72     *
73     * @param expression should be (..) or comma delimited (int, .*, java.lang.String)
74     */

75    public ParameterExpression(String JavaDoc expression)
76    {
77       originalExpression = expression;
78       Matcher JavaDoc all = ALL.matcher(expression);
79       isAll = all.matches();
80       if (!isAll)
81       {
82          Matcher JavaDoc params = PARAMS.matcher(expression);
83          if (!params.matches())
84          {
85             throw new RuntimeException JavaDoc("Illegal parameter list expression: " + expression);
86          }
87          StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(expression, ",");
88          ArrayList JavaDoc paramList = new ArrayList JavaDoc();
89          while (tokenizer.hasMoreTokens())
90          {
91             String JavaDoc token = tokenizer.nextToken().trim();
92             token = token.replaceAll("\\.", "\\\\.");
93             token = token.replaceAll("\\*", ".*");
94             token = token.replaceAll("\\[", "\\\\[");
95             token = token.replaceAll("\\]", "\\\\]");
96             paramList.add(Pattern.compile(token));
97          }
98          parameterPatterns = (Pattern JavaDoc[])paramList.toArray(new Pattern JavaDoc[paramList.size()]);
99       }
100    }
101
102    public boolean matches(Class JavaDoc[] params)
103    {
104       if (isAll)
105       {
106          return true;
107       }
108
109       if (params.length != parameterPatterns.length) return false;
110
111       for (int i = 0; i < params.length; i++)
112       {
113          String JavaDoc asString = simpleType(params[i]);
114          Matcher JavaDoc m = parameterPatterns[i].matcher(asString);
115          if (!m.matches()) return false;
116       }
117       return true;
118    }
119
120    public boolean matches(CtClass[] params)
121    {
122       if (isAll) return true;
123
124       if (params.length != parameterPatterns.length) return false;
125
126       for (int i = 0; i < params.length; i++)
127       {
128          String JavaDoc asString = params[i].getName();
129          Matcher JavaDoc m = parameterPatterns[i].matcher(asString);
130          if (!m.matches()) return false;
131       }
132       return true;
133    }
134 }
135
Popular Tags