|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
MethodPatternParser.java | 100% | 100% | 100% | 100% |
|
1 |
// Copyright 2004 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
14 |
|
|
15 |
package org.apache.hivemind.methodmatch;
|
|
16 |
|
|
17 |
import java.util.ArrayList;
|
|
18 |
import java.util.List;
|
|
19 |
|
|
20 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
21 |
import org.apache.hivemind.util.StringUtils;
|
|
22 |
|
|
23 |
/**
|
|
24 |
* Parses a method pattern (consisting of a name pattern, followed by an optional
|
|
25 |
* parameters pattern) into a {@link hivemind.test.services.impl.MethodFilter}. In most cases,
|
|
26 |
* the patterns will require several checks (i.e., match against name, match against parameters)
|
|
27 |
* in which case a {@link org.apache.hivemind.methodmatch.CompositeFilter} is returned.
|
|
28 |
*
|
|
29 |
* @author Howard Lewis Ship
|
|
30 |
*/
|
|
31 |
|
|
32 |
public class MethodPatternParser |
|
33 |
{ |
|
34 |
private List _filters;
|
|
35 |
|
|
36 | 19 |
public MethodFilter parseMethodPattern(String pattern)
|
37 |
{ |
|
38 | 19 |
_filters = new ArrayList();
|
39 |
|
|
40 | 19 |
int parenx = pattern.indexOf('(');
|
41 |
|
|
42 | 19 |
String namePattern = parenx < 0 ? pattern : pattern.substring(0, parenx); |
43 |
|
|
44 | 19 |
parseNamePattern(pattern, namePattern); |
45 |
|
|
46 | 17 |
if (parenx >= 0)
|
47 | 9 |
parseParametersPattern(pattern, pattern.substring(parenx)); |
48 |
|
|
49 | 13 |
switch (_filters.size())
|
50 |
{ |
|
51 |
case 0 :
|
|
52 | 2 |
return new MatchAllFilter(); |
53 |
|
|
54 |
case 1 :
|
|
55 |
|
|
56 | 8 |
return (MethodFilter) _filters.get(0);
|
57 |
|
|
58 |
default :
|
|
59 | 3 |
return new CompositeFilter(_filters); |
60 |
} |
|
61 |
} |
|
62 |
|
|
63 | 19 |
private void parseNamePattern(String methodPattern, String namePattern) |
64 |
{ |
|
65 | 19 |
if (namePattern.equals("*")) |
66 | 8 |
return;
|
67 |
|
|
68 | 11 |
if (namePattern.length() == 0)
|
69 | 1 |
throw new ApplicationRuntimeException( |
70 |
MethodMatchMessages.missingNamePattern(methodPattern)); |
|
71 |
|
|
72 | 10 |
if (namePattern.startsWith("*") && namePattern.endsWith("*")) |
73 |
{ |
|
74 | 2 |
String substring = namePattern.substring(1, namePattern.length() - 1); |
75 |
|
|
76 | 2 |
validateNamePattern(methodPattern, substring); |
77 |
|
|
78 | 1 |
_filters.add(new InfixNameFilter(substring));
|
79 | 1 |
return;
|
80 |
} |
|
81 |
|
|
82 | 8 |
if (namePattern.startsWith("*")) |
83 |
{ |
|
84 | 2 |
String suffix = namePattern.substring(1); |
85 |
|
|
86 | 2 |
validateNamePattern(methodPattern, suffix); |
87 |
|
|
88 | 2 |
_filters.add(new NameSuffixFilter(suffix));
|
89 | 2 |
return;
|
90 |
} |
|
91 |
|
|
92 | 6 |
if (namePattern.endsWith("*")) |
93 |
{ |
|
94 | 3 |
String prefix = namePattern.substring(0, namePattern.length() - 1); |
95 |
|
|
96 | 3 |
validateNamePattern(methodPattern, prefix); |
97 |
|
|
98 | 3 |
_filters.add(new NamePrefixFilter(prefix));
|
99 | 3 |
return;
|
100 |
} |
|
101 |
|
|
102 | 3 |
validateNamePattern(methodPattern, namePattern); |
103 |
|
|
104 | 3 |
_filters.add(new ExactNameFilter(namePattern));
|
105 |
} |
|
106 |
|
|
107 | 9 |
private void parseParametersPattern(String methodPattern, String pattern) |
108 |
{ |
|
109 | 9 |
if (pattern.equals("()")) |
110 |
{ |
|
111 | 1 |
addParameterCountFilter(0); |
112 | 1 |
return;
|
113 |
} |
|
114 |
|
|
115 | 8 |
if (!pattern.endsWith(")")) |
116 | 3 |
throw new ApplicationRuntimeException( |
117 |
MethodMatchMessages.invalidParametersPattern(methodPattern)); |
|
118 |
|
|
119 |
// Trim off leading and trailing parens.
|
|
120 |
|
|
121 | 5 |
pattern = pattern.substring(1, pattern.length() - 1); |
122 |
|
|
123 | 5 |
char ch = pattern.charAt(0);
|
124 |
|
|
125 | 5 |
if (Character.isDigit(ch))
|
126 |
{ |
|
127 | 2 |
addParameterCountFilter(methodPattern, pattern); |
128 | 1 |
return;
|
129 |
} |
|
130 |
|
|
131 | 3 |
String[] names = StringUtils.split(pattern); |
132 |
|
|
133 |
// Would be nice to do some kind of validation here, to prove
|
|
134 |
// that the provided class names exist, and that
|
|
135 |
// primitive types names are valid.
|
|
136 |
|
|
137 | 3 |
addParameterCountFilter(names.length); |
138 | 3 |
for (int i = 0; i < names.length; i++) |
139 | 4 |
_filters.add(new ParameterFilter(i, names[i].trim()));
|
140 |
|
|
141 |
} |
|
142 |
|
|
143 | 2 |
private void addParameterCountFilter(String methodPattern, String pattern) |
144 |
{ |
|
145 | 2 |
try
|
146 |
{ |
|
147 | 2 |
int count = Integer.parseInt(pattern);
|
148 | 1 |
addParameterCountFilter(count); |
149 |
} |
|
150 |
catch (NumberFormatException ex)
|
|
151 |
{ |
|
152 | 1 |
throw new ApplicationRuntimeException( |
153 |
MethodMatchMessages.invalidParametersPattern(methodPattern)); |
|
154 |
} |
|
155 |
} |
|
156 |
|
|
157 | 5 |
private void addParameterCountFilter(int count) |
158 |
{ |
|
159 |
// Add the count filter first, since it is always the least expensive test.
|
|
160 | 5 |
_filters.add(0, new ParameterCountFilter(count));
|
161 |
} |
|
162 |
|
|
163 | 10 |
private void validateNamePattern(String methodPattern, String nameSubstring) |
164 |
{ |
|
165 | 10 |
if (nameSubstring.indexOf('*') >= 0)
|
166 | 1 |
throw new ApplicationRuntimeException( |
167 |
MethodMatchMessages.invalidNamePattern(methodPattern)); |
|
168 |
} |
|
169 |
} |
|
170 |
|
|