langtools/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java
changeset 4937 2fc03fb01efa
parent 4934 c6a2cf3b6b82
child 5001 71ed0cc22974
equal deleted inserted replaced
4936:abf1a22d9bcf 4937:2fc03fb01efa
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    23  * have any questions.
    24  */
    24  */
    25 
    25 
    26 package com.sun.tools.javac.processing;
    26 package com.sun.tools.javac.processing;
    27 
       
    28 
    27 
    29 import java.lang.reflect.*;
    28 import java.lang.reflect.*;
    30 import java.util.*;
    29 import java.util.*;
    31 import java.util.regex.*;
    30 import java.util.regex.*;
    32 
    31 
  1353 
  1352 
  1354     public Set<Symbol.PackageSymbol> getSpecifiedPackages() {
  1353     public Set<Symbol.PackageSymbol> getSpecifiedPackages() {
  1355         return specifiedPackages;
  1354         return specifiedPackages;
  1356     }
  1355     }
  1357 
  1356 
  1358     // Borrowed from DocletInvoker and apt
       
  1359     // TODO: remove from apt's Main
       
  1360     /**
       
  1361      * Utility method for converting a search path string to an array
       
  1362      * of directory and JAR file URLs.
       
  1363      *
       
  1364      * @param path the search path string
       
  1365      * @return the resulting array of directory and JAR file URLs
       
  1366      */
       
  1367     public static URL[] pathToURLs(String path) {
       
  1368         StringTokenizer st = new StringTokenizer(path, File.pathSeparator);
       
  1369         URL[] urls = new URL[st.countTokens()];
       
  1370         int count = 0;
       
  1371         while (st.hasMoreTokens()) {
       
  1372             URL url = fileToURL(new File(st.nextToken()));
       
  1373             if (url != null) {
       
  1374                 urls[count++] = url;
       
  1375             }
       
  1376         }
       
  1377         if (urls.length != count) {
       
  1378             URL[] tmp = new URL[count];
       
  1379             System.arraycopy(urls, 0, tmp, 0, count);
       
  1380             urls = tmp;
       
  1381         }
       
  1382         return urls;
       
  1383     }
       
  1384 
       
  1385     /**
       
  1386      * Returns the directory or JAR file URL corresponding to the specified
       
  1387      * local file name.
       
  1388      *
       
  1389      * @param file the File object
       
  1390      * @return the resulting directory or JAR file URL, or null if unknown
       
  1391      */
       
  1392     private static URL fileToURL(File file) {
       
  1393         String name;
       
  1394         try {
       
  1395             name = file.getCanonicalPath();
       
  1396         } catch (IOException e) {
       
  1397             name = file.getAbsolutePath();
       
  1398         }
       
  1399         name = name.replace(File.separatorChar, '/');
       
  1400         if (!name.startsWith("/")) {
       
  1401             name = "/" + name;
       
  1402         }
       
  1403         // If the file does not exist, then assume that it's a directory
       
  1404         if (!file.isFile()) {
       
  1405             name = name + "/";
       
  1406         }
       
  1407         try {
       
  1408             return new URL("file", "", name);
       
  1409         } catch (MalformedURLException e) {
       
  1410             throw new IllegalArgumentException("file");
       
  1411         }
       
  1412     }
       
  1413 
       
  1414 
       
  1415 
       
  1416     private static final Pattern allMatches = Pattern.compile(".*");
  1357     private static final Pattern allMatches = Pattern.compile(".*");
  1417 
  1358     public static final Pattern noMatches  = Pattern.compile("(\\P{all})+");
  1418     private static final Pattern noMatches  = Pattern.compile("(\\P{all})+");
  1359 
  1419     /**
  1360     /**
  1420      * Convert import-style string to regex matching that string.  If
  1361      * Convert import-style string for supported annotations into a
  1421      * the string is a valid import-style string, return a regex that
  1362      * regex matching that string.  If the string is a valid
  1422      * won't match anything.
  1363      * import-style string, return a regex that won't match anything.
  1423      */
  1364      */
  1424     // TODO: remove version in Apt.java
  1365     private static Pattern importStringToPattern(String s, Processor p, Log log) {
  1425     public static Pattern importStringToPattern(String s, Processor p, Log log) {
  1366         if (isValidImportString(s)) {
       
  1367             return validImportStringToPattern(s);
       
  1368         } else {
       
  1369             log.warning("proc.malformed.supported.string", s, p.getClass().getName());
       
  1370             return noMatches; // won't match any valid identifier
       
  1371         }
       
  1372     }
       
  1373 
       
  1374     /**
       
  1375      * Return true if the argument string is a valid import-style
       
  1376      * string specifying claimed annotations; return false otherwise.
       
  1377      */
       
  1378     public static boolean isValidImportString(String s) {
       
  1379         if (s.equals("*"))
       
  1380             return true;
       
  1381 
       
  1382         boolean valid = true;
       
  1383         String t = s;
       
  1384         int index = t.indexOf('*');
       
  1385 
       
  1386         if (index != -1) {
       
  1387             // '*' must be last character...
       
  1388             if (index == t.length() -1) {
       
  1389                 // ... any and preceding character must be '.'
       
  1390                 if ( index-1 >= 0 ) {
       
  1391                     valid = t.charAt(index-1) == '.';
       
  1392                     // Strip off ".*$" for identifier checks
       
  1393                     t = t.substring(0, t.length()-2);
       
  1394                 }
       
  1395             } else
       
  1396                 return false;
       
  1397         }
       
  1398 
       
  1399         // Verify string is off the form (javaId \.)+ or javaId
       
  1400         if (valid) {
       
  1401             String[] javaIds = t.split("\\.", t.length()+2);
       
  1402             for(String javaId: javaIds)
       
  1403                 valid &= SourceVersion.isIdentifier(javaId);
       
  1404         }
       
  1405         return valid;
       
  1406     }
       
  1407 
       
  1408     public static Pattern validImportStringToPattern(String s) {
  1426         if (s.equals("*")) {
  1409         if (s.equals("*")) {
  1427             return allMatches;
  1410             return allMatches;
  1428         } else {
  1411         } else {
  1429             String t = s;
  1412             String s_prime = s.replace(".", "\\.");
  1430             boolean star = false;
       
  1431 
       
  1432             /*
       
  1433              * Validate string from factory is legal.  If the string
       
  1434              * has more than one asterisks or the asterisks does not
       
  1435              * appear as the last character (preceded by a period),
       
  1436              * the string is not legal.
       
  1437              */
       
  1438 
       
  1439             boolean valid = true;
       
  1440             int index = t.indexOf('*');
       
  1441             if (index != -1) {
       
  1442                 // '*' must be last character...
       
  1443                 if (index == t.length() -1) {
       
  1444                      // ... and preceeding character must be '.'
       
  1445                     if ( index-1 >= 0 ) {
       
  1446                         valid = t.charAt(index-1) == '.';
       
  1447                         // Strip off ".*$" for identifier checks
       
  1448                         t = t.substring(0, t.length()-2);
       
  1449                     }
       
  1450                 } else
       
  1451                     valid = false;
       
  1452             }
       
  1453 
       
  1454             // Verify string is off the form (javaId \.)+ or javaId
       
  1455             if (valid) {
       
  1456                 String[] javaIds = t.split("\\.", t.length()+2);
       
  1457                 for(String javaId: javaIds)
       
  1458                     valid &= SourceVersion.isIdentifier(javaId);
       
  1459             }
       
  1460 
       
  1461             if (!valid) {
       
  1462                 log.warning("proc.malformed.supported.string", s, p.getClass().getName());
       
  1463                 return noMatches; // won't match any valid identifier
       
  1464             }
       
  1465 
       
  1466             String s_prime = s.replaceAll("\\.", "\\\\.");
       
  1467 
  1413 
  1468             if (s_prime.endsWith("*")) {
  1414             if (s_prime.endsWith("*")) {
  1469                 s_prime =  s_prime.substring(0, s_prime.length() - 1) + ".+";
  1415                 s_prime =  s_prime.substring(0, s_prime.length() - 1) + ".+";
  1470             }
  1416             }
  1471 
  1417