author | mcimadamore |
Tue, 13 Jan 2009 13:27:14 +0000 | |
changeset 1789 | 7ac8c0815000 |
parent 10 | 06bc494ca11e |
child 1870 | 57a1138dffc8 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
2 |
* Copyright 2004-2005 Sun Microsystems, Inc. All Rights Reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.tools.apt.comp; |
|
27 |
||
28 |
import com.sun.tools.javac.code.*; |
|
29 |
import com.sun.tools.javac.comp.*; |
|
30 |
import com.sun.tools.javac.tree.*; |
|
31 |
import com.sun.tools.javac.util.*; |
|
32 |
import com.sun.tools.javac.tree.TreeScanner; |
|
33 |
import com.sun.tools.javac.util.Context; |
|
34 |
import com.sun.tools.apt.util.Bark; |
|
35 |
import com.sun.tools.javac.util.Position; |
|
36 |
||
37 |
import java.util.*; |
|
38 |
import java.util.regex.*; |
|
39 |
import java.lang.reflect.*; |
|
40 |
import java.lang.reflect.InvocationTargetException; |
|
41 |
import java.io.IOException; |
|
42 |
||
43 |
import com.sun.tools.apt.*; |
|
44 |
import com.sun.tools.apt.comp.*; |
|
45 |
import com.sun.tools.javac.code.Symbol.*; |
|
46 |
||
47 |
import com.sun.mirror.declaration.TypeDeclaration; |
|
48 |
import com.sun.mirror.declaration.AnnotationTypeDeclaration; |
|
49 |
import com.sun.mirror.apt.*; |
|
50 |
// import com.sun.mirror.apt.AnnotationProcessorFactory; |
|
51 |
import com.sun.mirror.apt.AnnotationProcessors; |
|
52 |
||
53 |
import com.sun.tools.apt.mirror.AptEnv; |
|
54 |
import com.sun.tools.apt.mirror.apt.FilerImpl; |
|
55 |
import com.sun.tools.apt.mirror.apt.AnnotationProcessorEnvironmentImpl; |
|
56 |
||
57 |
||
58 |
import static com.sun.tools.apt.mirror.declaration.DeclarationMaker.isJavaIdentifier; |
|
59 |
||
60 |
/** |
|
61 |
* Apt compiler phase. |
|
62 |
* |
|
63 |
* <p><b>This is NOT part of any API supported by Sun Microsystems. |
|
64 |
* If you write code that depends on this, you do so at your own |
|
65 |
* risk. This code and its internal interfaces are subject to change |
|
66 |
* or deletion without notice.</b> |
|
67 |
*/ |
|
68 |
public class Apt extends ListBuffer<Env<AttrContext>> { |
|
69 |
java.util.Set<String> genSourceFileNames = new java.util.LinkedHashSet<String>(); |
|
70 |
public java.util.Set<String> getSourceFileNames() { |
|
71 |
return genSourceFileNames; |
|
72 |
} |
|
73 |
||
74 |
/** List of names of generated class files. |
|
75 |
*/ |
|
76 |
java.util.Set<String> genClassFileNames = new java.util.LinkedHashSet<String>(); |
|
77 |
public java.util.Set<String> getClassFileNames() { |
|
78 |
return genClassFileNames; |
|
79 |
} |
|
80 |
||
81 |
/* AptEnvironment */ |
|
82 |
AptEnv aptenv; |
|
83 |
||
84 |
private Context context; |
|
85 |
||
86 |
/** The context key for the todo list. */ |
|
87 |
||
88 |
protected static final Context.Key<Apt> aptKey = |
|
89 |
new Context.Key<Apt>(); |
|
90 |
||
91 |
/** Get the Apt instance for this context. */ |
|
92 |
public static Apt instance(Context context) { |
|
93 |
Apt instance = context.get(aptKey); |
|
94 |
if (instance == null) |
|
95 |
instance = new Apt(context); |
|
96 |
return instance; |
|
97 |
} |
|
98 |
||
99 |
/** Create a new apt list. */ |
|
100 |
protected Apt(Context context) { |
|
101 |
this.context = context; |
|
102 |
||
103 |
context.put(aptKey, this); |
|
104 |
aptenv = AptEnv.instance(context); |
|
105 |
} |
|
106 |
||
107 |
/** |
|
108 |
* Used to scan javac trees to build data structures needed for |
|
109 |
* bootstrapping the apt environment. In particular: |
|
110 |
* |
|
111 |
* <ul> |
|
112 |
* |
|
113 |
* <li> Generate list of canonical names of annotation types that |
|
114 |
* appear in source files given on the command line |
|
115 |
* |
|
116 |
* <li> Collect list of javac symbols representing source files |
|
117 |
* given on the command line |
|
118 |
* |
|
119 |
* </ul> |
|
120 |
*/ |
|
121 |
static class AptTreeScanner extends TreeScanner { |
|
122 |
||
123 |
// Set of fully qualified names of annotation types present in |
|
124 |
// examined source |
|
125 |
private Set<String> annotationSet; |
|
126 |
||
127 |
// Symbols to build bootstrapping declaration list |
|
128 |
private Collection<ClassSymbol> specifiedDeclCollection; |
|
129 |
private Collection<ClassSymbol> declCollection; |
|
130 |
||
131 |
public Set<String> getAnnotationSet() { |
|
132 |
return annotationSet; |
|
133 |
} |
|
134 |
||
135 |
public AptTreeScanner() { |
|
136 |
annotationSet = new LinkedHashSet<String>(); |
|
137 |
specifiedDeclCollection = new LinkedHashSet<ClassSymbol>(); |
|
138 |
declCollection = new LinkedHashSet<ClassSymbol>(); |
|
139 |
} |
|
140 |
||
141 |
public void visitTopLevel(JCTree.JCCompilationUnit tree) { |
|
142 |
super.visitTopLevel(tree); |
|
143 |
// Print out contents -- what are we dealing with? |
|
144 |
||
145 |
for(JCTree d: tree.defs) { |
|
146 |
if (d instanceof JCTree.JCClassDecl) |
|
147 |
specifiedDeclCollection.add(((JCTree.JCClassDecl) d).sym); |
|
148 |
} |
|
149 |
||
150 |
} |
|
151 |
||
152 |
public void visitBlock(JCTree.JCBlock tree) { |
|
153 |
; // Do nothing. |
|
154 |
} |
|
155 |
||
156 |
||
157 |
// should add nested classes to packages, etc. |
|
158 |
public void visitClassDef(JCTree.JCClassDecl tree) { |
|
159 |
if (tree.sym == null) { |
|
160 |
// could be an anon class w/in an initializer |
|
161 |
return; |
|
162 |
} |
|
163 |
||
164 |
super.visitClassDef(tree); |
|
165 |
||
166 |
declCollection.add(tree.sym); |
|
167 |
} |
|
168 |
||
169 |
public void visitMethodDef(JCTree.JCMethodDecl tree) { |
|
170 |
super.visitMethodDef(tree); |
|
171 |
} |
|
172 |
||
173 |
public void visitVarDef(JCTree.JCVariableDecl tree) { |
|
174 |
super.visitVarDef(tree); |
|
175 |
} |
|
176 |
||
177 |
public void visitAnnotation(JCTree.JCAnnotation tree) { |
|
178 |
super.visitAnnotation(tree); |
|
179 |
annotationSet.add(tree.type.tsym.toString()); |
|
180 |
} |
|
181 |
} |
|
182 |
||
183 |
Set<String> computeAnnotationSet(Collection<ClassSymbol> classSymbols) { |
|
184 |
Set<String> annotationSet = new HashSet<String>(); |
|
185 |
||
186 |
for(ClassSymbol classSymbol: classSymbols) { |
|
187 |
computeAnnotationSet(classSymbol, annotationSet); |
|
188 |
} |
|
189 |
return annotationSet; |
|
190 |
} |
|
191 |
||
192 |
void computeAnnotationSet(Symbol symbol, Set<String> annotationSet) { |
|
193 |
if (symbol != null ) { |
|
194 |
if (symbol.getAnnotationMirrors() != null) |
|
195 |
for(Attribute.Compound compound: symbol.getAnnotationMirrors()) |
|
196 |
annotationSet.add(compound.type.tsym.toString()); // should fullName be used instead of toString? |
|
197 |
||
198 |
if (symbol instanceof Symbol.MethodSymbol) // add parameter annotations |
|
199 |
for(Symbol param: ((MethodSymbol) symbol).params()) |
|
200 |
computeAnnotationSet(param, annotationSet); |
|
201 |
||
202 |
if (symbol.members() != null) { |
|
203 |
for(Scope.Entry e: symbol.members().table) |
|
204 |
computeAnnotationSet(e.sym, annotationSet); |
|
205 |
} |
|
206 |
} |
|
207 |
} |
|
208 |
||
209 |
public void main(com.sun.tools.javac.util.List<JCTree.JCCompilationUnit> treeList, |
|
210 |
ListBuffer<ClassSymbol> classes, |
|
211 |
Map<String, String> origOptions, |
|
212 |
ClassLoader aptCL, |
|
213 |
AnnotationProcessorFactory providedFactory, |
|
214 |
java.util.Set<Class<? extends AnnotationProcessorFactory> > productiveFactories) { |
|
215 |
Bark bark = Bark.instance(context); |
|
216 |
java.io.PrintWriter out = bark.warnWriter; |
|
217 |
Options options = Options.instance(context); |
|
218 |
||
219 |
Collection<TypeDeclaration> spectypedecls = new LinkedHashSet<TypeDeclaration>(); |
|
220 |
Collection<TypeDeclaration> typedecls = new LinkedHashSet<TypeDeclaration>(); |
|
221 |
Set<String> unmatchedAnnotations = new LinkedHashSet<String>(); |
|
222 |
Set<AnnotationTypeDeclaration> emptyATDS = Collections.emptySet(); |
|
223 |
Set<Class<? extends AnnotationProcessorFactory> > currentRoundFactories = |
|
224 |
new LinkedHashSet<Class<? extends AnnotationProcessorFactory> >(); |
|
225 |
||
226 |
// Determine what annotations are present on the input source |
|
227 |
// files, create collections of specified type declarations, |
|
228 |
// and type declarations. |
|
229 |
AptTreeScanner ats = new AptTreeScanner(); |
|
230 |
for(JCTree t: treeList) { |
|
231 |
t.accept(ats); |
|
232 |
} |
|
233 |
||
234 |
// Turn collection of ClassSymbols into Collection of apt decls |
|
235 |
for (ClassSymbol cs : ats.specifiedDeclCollection) { |
|
236 |
TypeDeclaration decl = aptenv.declMaker.getTypeDeclaration(cs); |
|
237 |
spectypedecls.add(decl); |
|
238 |
} |
|
239 |
||
240 |
for (ClassSymbol cs : ats.declCollection) { |
|
241 |
TypeDeclaration decl = aptenv.declMaker.getTypeDeclaration(cs); |
|
242 |
typedecls.add(decl); |
|
243 |
} |
|
244 |
||
245 |
unmatchedAnnotations.addAll(ats.getAnnotationSet()); |
|
246 |
||
247 |
// Process input class files |
|
248 |
for(ClassSymbol cs : classes) { |
|
249 |
TypeDeclaration decl = aptenv.declMaker.getTypeDeclaration(cs); |
|
250 |
// System.out.println("Adding a class to spectypedecls"); |
|
251 |
spectypedecls.add(decl); |
|
252 |
typedecls.add(decl); |
|
253 |
computeAnnotationSet(cs, unmatchedAnnotations); |
|
254 |
} |
|
255 |
||
256 |
if (options.get("-XListAnnotationTypes") != null) { |
|
257 |
out.println("Set of annotations found:" + |
|
258 |
(new TreeSet<String>(unmatchedAnnotations)).toString()); |
|
259 |
} |
|
260 |
||
261 |
AnnotationProcessorEnvironmentImpl trivAPE = |
|
262 |
new AnnotationProcessorEnvironmentImpl(spectypedecls, typedecls, origOptions, context); |
|
263 |
||
264 |
if (options.get("-XListDeclarations") != null) { |
|
265 |
out.println("Set of Specified Declarations:" + |
|
266 |
spectypedecls); |
|
267 |
||
268 |
out.println("Set of Included Declarations: " + |
|
269 |
typedecls); |
|
270 |
} |
|
271 |
||
272 |
if (options.get("-print") != null) { |
|
273 |
if (spectypedecls.size() == 0 ) |
|
274 |
throw new UsageMessageNeededException(); |
|
275 |
||
276 |
// Run the printing processor |
|
277 |
AnnotationProcessor proc = (new BootstrapAPF()).getProcessorFor(new HashSet<AnnotationTypeDeclaration>(), |
|
278 |
trivAPE); |
|
279 |
proc.process(); |
|
280 |
} else { |
|
281 |
// Discovery process |
|
282 |
||
283 |
// List of annotation processory factory instances |
|
1789
7ac8c0815000
6765045: Remove rawtypes warnings from langtools
mcimadamore
parents:
10
diff
changeset
|
284 |
java.util.Iterator<AnnotationProcessorFactory> providers = null; |
10 | 285 |
{ |
286 |
/* |
|
287 |
* If a factory is provided by the user, the |
|
288 |
* "-factory" and "-factorypath" options are not used. |
|
289 |
* |
|
290 |
* Otherwise, if the "-factory" option is used, search |
|
291 |
* the appropriate path for the named class. |
|
292 |
* Otherwise, use sun.misc.Service to implement the |
|
293 |
* default discovery policy. |
|
294 |
*/ |
|
295 |
||
296 |
java.util.List<AnnotationProcessorFactory> list = |
|
297 |
new LinkedList<AnnotationProcessorFactory>(); |
|
298 |
String factoryName = options.get("-factory"); |
|
299 |
||
300 |
if (providedFactory != null) { |
|
301 |
list.add(providedFactory); |
|
302 |
providers = list.iterator(); |
|
303 |
} else if (factoryName != null) { |
|
304 |
try { |
|
305 |
AnnotationProcessorFactory factory = |
|
306 |
(AnnotationProcessorFactory) (aptCL.loadClass(factoryName).newInstance()); |
|
307 |
list.add(factory); |
|
308 |
} catch (ClassNotFoundException cnfe) { |
|
309 |
bark.aptWarning("FactoryNotFound", factoryName); |
|
310 |
} catch (ClassCastException cce) { |
|
311 |
bark.aptWarning("FactoryWrongType", factoryName); |
|
312 |
} catch (Exception e ) { |
|
313 |
bark.aptWarning("FactoryCantInstantiate", factoryName); |
|
314 |
} catch(Throwable t) { |
|
315 |
throw new AnnotationProcessingError(t); |
|
316 |
} |
|
317 |
||
318 |
providers = list.iterator(); |
|
319 |
} else |
|
320 |
providers = sun.misc.Service.providers(AnnotationProcessorFactory.class, aptCL); |
|
321 |
} |
|
322 |
||
323 |
java.util.Map<AnnotationProcessorFactory, Set<AnnotationTypeDeclaration>> factoryToAnnotation = |
|
324 |
new LinkedHashMap<AnnotationProcessorFactory, Set<AnnotationTypeDeclaration>>(); |
|
325 |
||
326 |
if (!providers.hasNext() && productiveFactories.size() == 0) { |
|
327 |
if (unmatchedAnnotations.size() > 0) |
|
328 |
bark.aptWarning("NoAnnotationProcessors"); |
|
329 |
if (spectypedecls.size() == 0) |
|
330 |
throw new UsageMessageNeededException(); |
|
331 |
return; // no processors; nothing else to do |
|
332 |
} else { |
|
333 |
// If there are no annotations, still give |
|
334 |
// processors that match everything a chance to |
|
335 |
// run. |
|
336 |
||
337 |
if(unmatchedAnnotations.size() == 0) |
|
338 |
unmatchedAnnotations.add(""); |
|
339 |
||
340 |
Set<String> emptyStringSet = new HashSet<String>(); |
|
341 |
emptyStringSet.add(""); |
|
342 |
emptyStringSet = Collections.unmodifiableSet(emptyStringSet); |
|
343 |
||
344 |
while (providers.hasNext() ) { |
|
345 |
Object provider = providers.next(); |
|
346 |
try { |
|
347 |
Set<String> matchedStrings = new HashSet<String>(); |
|
348 |
||
349 |
AnnotationProcessorFactory apf = (AnnotationProcessorFactory) provider; |
|
350 |
Collection<String> supportedTypes = apf.supportedAnnotationTypes(); |
|
351 |
||
352 |
Collection<Pattern> supportedTypePatterns = new LinkedList<Pattern>(); |
|
353 |
for(String s: supportedTypes) |
|
354 |
supportedTypePatterns.add(importStringToPattern(s)); |
|
355 |
||
356 |
for(String s: unmatchedAnnotations) { |
|
357 |
for(Pattern p: supportedTypePatterns) { |
|
358 |
if (p.matcher(s).matches()) { |
|
359 |
matchedStrings.add(s); |
|
360 |
break; |
|
361 |
} |
|
362 |
} |
|
363 |
} |
|
364 |
||
365 |
unmatchedAnnotations.removeAll(matchedStrings); |
|
366 |
||
367 |
if (options.get("-XPrintFactoryInfo") != null) { |
|
368 |
out.println("Factory " + apf.getClass().getName() + |
|
369 |
" matches " + |
|
370 |
((matchedStrings.size() == 0)? |
|
371 |
"nothing.": matchedStrings)); |
|
372 |
} |
|
373 |
||
374 |
if (matchedStrings.size() > 0) { |
|
375 |
// convert annotation names to annotation |
|
376 |
// type decls |
|
377 |
Set<AnnotationTypeDeclaration> atds = new HashSet<AnnotationTypeDeclaration>(); |
|
378 |
||
379 |
// If a "*" processor is called on the |
|
380 |
// empty string, pass in an empty set of |
|
381 |
// annotation type declarations. |
|
382 |
if (!matchedStrings.equals(emptyStringSet)) { |
|
383 |
for(String s: matchedStrings) { |
|
384 |
TypeDeclaration decl = aptenv.declMaker.getTypeDeclaration(s); |
|
385 |
AnnotationTypeDeclaration annotdecl; |
|
386 |
if (decl == null) { |
|
387 |
bark.aptError("DeclarationCreation", s); |
|
388 |
} else { |
|
389 |
try { |
|
390 |
annotdecl = (AnnotationTypeDeclaration)decl; |
|
391 |
atds.add(annotdecl); |
|
392 |
||
393 |
} catch (ClassCastException cce) { |
|
394 |
bark.aptError("BadDeclaration", s); |
|
395 |
} |
|
396 |
} |
|
397 |
} |
|
398 |
} |
|
399 |
||
400 |
currentRoundFactories.add(apf.getClass()); |
|
401 |
productiveFactories.add(apf.getClass()); |
|
402 |
factoryToAnnotation.put(apf, atds); |
|
403 |
} else if (productiveFactories.contains(apf.getClass())) { |
|
404 |
// If a factory provided a processor in a |
|
405 |
// previous round but doesn't match any |
|
406 |
// annotations this round, call it with an |
|
407 |
// empty set of declarations. |
|
408 |
currentRoundFactories.add(apf.getClass()); |
|
409 |
factoryToAnnotation.put(apf, emptyATDS ); |
|
410 |
} |
|
411 |
||
412 |
if (unmatchedAnnotations.size() == 0) |
|
413 |
break; |
|
414 |
||
415 |
} catch (ClassCastException cce) { |
|
416 |
bark.aptWarning("BadFactory", cce); |
|
417 |
} |
|
418 |
} |
|
419 |
||
420 |
unmatchedAnnotations.remove(""); |
|
421 |
} |
|
422 |
||
423 |
// If the set difference of productiveFactories and |
|
424 |
// currentRoundFactories is non-empty, call the remaining |
|
425 |
// productive factories with an empty set of declarations. |
|
426 |
{ |
|
427 |
java.util.Set<Class<? extends AnnotationProcessorFactory> > neglectedFactories = |
|
428 |
new LinkedHashSet<Class<? extends AnnotationProcessorFactory>>(productiveFactories); |
|
429 |
neglectedFactories.removeAll(currentRoundFactories); |
|
430 |
for(Class<? extends AnnotationProcessorFactory> working : neglectedFactories) { |
|
431 |
try { |
|
432 |
AnnotationProcessorFactory factory = working.newInstance(); |
|
433 |
factoryToAnnotation.put(factory, emptyATDS); |
|
434 |
} catch (Exception e ) { |
|
435 |
bark.aptWarning("FactoryCantInstantiate", working.getName()); |
|
436 |
} catch(Throwable t) { |
|
437 |
throw new AnnotationProcessingError(t); |
|
438 |
} |
|
439 |
} |
|
440 |
} |
|
441 |
||
442 |
if (unmatchedAnnotations.size() > 0) |
|
443 |
bark.aptWarning("AnnotationsWithoutProcessors", unmatchedAnnotations); |
|
444 |
||
445 |
Set<AnnotationProcessor> processors = new LinkedHashSet<AnnotationProcessor>(); |
|
446 |
||
447 |
// If there were no source files AND no factory matching "*", |
|
448 |
// make sure the usage message is printed |
|
449 |
if (spectypedecls.size() == 0 && |
|
450 |
factoryToAnnotation.keySet().size() == 0 ) |
|
451 |
throw new UsageMessageNeededException(); |
|
452 |
||
453 |
try { |
|
454 |
for(AnnotationProcessorFactory apFactory: factoryToAnnotation.keySet()) { |
|
455 |
AnnotationProcessor processor = apFactory.getProcessorFor(factoryToAnnotation.get(apFactory), |
|
456 |
trivAPE); |
|
457 |
if (processor != null) |
|
458 |
processors.add(processor); |
|
459 |
else |
|
460 |
bark.aptWarning("NullProcessor", apFactory.getClass().getName()); |
|
461 |
} |
|
462 |
} catch(Throwable t) { |
|
463 |
throw new AnnotationProcessingError(t); |
|
464 |
} |
|
465 |
||
466 |
LinkedList<AnnotationProcessor> temp = new LinkedList<AnnotationProcessor>(); |
|
467 |
temp.addAll(processors); |
|
468 |
||
469 |
AnnotationProcessor proc = AnnotationProcessors.getCompositeAnnotationProcessor(temp); |
|
470 |
||
471 |
try { |
|
472 |
proc.process(); |
|
473 |
} catch (Throwable t) { |
|
474 |
throw new AnnotationProcessingError(t); |
|
475 |
} |
|
476 |
||
477 |
// Invoke listener callback mechanism |
|
478 |
trivAPE.roundComplete(); |
|
479 |
||
480 |
FilerImpl filerimpl = (FilerImpl)trivAPE.getFiler(); |
|
481 |
genSourceFileNames = filerimpl.getSourceFileNames(); |
|
482 |
genClassFileNames = filerimpl.getClassFileNames(); |
|
483 |
filerimpl.flush(); // Make sure new files are written out |
|
484 |
} |
|
485 |
} |
|
486 |
||
487 |
/** |
|
488 |
* Convert import-style string to regex matching that string. If |
|
489 |
* the string is a valid import-style string, return a regex that |
|
490 |
* won't match anything. |
|
491 |
*/ |
|
492 |
Pattern importStringToPattern(String s) { |
|
493 |
if (s.equals("*")) { |
|
494 |
return allMatches; |
|
495 |
} else { |
|
496 |
String t = s; |
|
497 |
boolean star = false; |
|
498 |
||
499 |
/* |
|
500 |
* Validate string from factory is legal. If the string |
|
501 |
* has more than one asterisks or the asterisks does not |
|
502 |
* appear as the last character (preceded by a period), |
|
503 |
* the string is not legal. |
|
504 |
*/ |
|
505 |
||
506 |
boolean valid = true; |
|
507 |
int index = t.indexOf('*'); |
|
508 |
if (index != -1) { |
|
509 |
// '*' must be last character... |
|
510 |
if (index == t.length() -1) { |
|
511 |
// ... and preceeding character must be '.' |
|
512 |
if ( index-1 >= 0 ) { |
|
513 |
valid = t.charAt(index-1) == '.'; |
|
514 |
// Strip off ".*$" for identifier checks |
|
515 |
t = t.substring(0, t.length()-2); |
|
516 |
} |
|
517 |
} else |
|
518 |
valid = false; |
|
519 |
} |
|
520 |
||
521 |
// Verify string is off the form (javaId \.)+ or javaId |
|
522 |
if (valid) { |
|
523 |
String[] javaIds = t.split("\\.", t.length()+2); |
|
524 |
for(String javaId: javaIds) |
|
525 |
valid &= isJavaIdentifier(javaId); |
|
526 |
} |
|
527 |
||
528 |
if (!valid) { |
|
529 |
Bark bark = Bark.instance(context); |
|
530 |
bark.aptWarning("MalformedSupportedString", s); |
|
531 |
return noMatches; // won't match any valid identifier |
|
532 |
} |
|
533 |
||
534 |
String s_prime = s.replaceAll("\\.", "\\\\."); |
|
535 |
||
536 |
if (s_prime.endsWith("*")) { |
|
537 |
s_prime = s_prime.substring(0, s_prime.length() - 1) + ".+"; |
|
538 |
} |
|
539 |
||
540 |
return Pattern.compile(s_prime); |
|
541 |
} |
|
542 |
} |
|
543 |
||
544 |
private static final Pattern allMatches = Pattern.compile(".*"); |
|
545 |
private static final Pattern noMatches = Pattern.compile("(\\P{all})+"); |
|
546 |
} |