mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-19 10:34:38 +02:00
6927061: Refactor apt implemenation to use code from JSR 269
Reviewed-by: jjg
This commit is contained in:
parent
b8574a9e3b
commit
e985558459
6 changed files with 117 additions and 264 deletions
|
@ -102,7 +102,7 @@ class SourceOrderDeclScanner extends DeclarationScanner {
|
||||||
@SuppressWarnings("cast")
|
@SuppressWarnings("cast")
|
||||||
private int compareEqualPosition(Declaration d1, Declaration d2) {
|
private int compareEqualPosition(Declaration d1, Declaration d2) {
|
||||||
assert
|
assert
|
||||||
(d1.getPosition() == d2.getPosition()) || // Handles d1 == d2 == null
|
(d1.getPosition() == d2.getPosition()) || // Handles two null positions.
|
||||||
(d1.getPosition().file().compareTo(d2.getPosition().file()) == 0 &&
|
(d1.getPosition().file().compareTo(d2.getPosition().file()) == 0 &&
|
||||||
d1.getPosition().line() == d2.getPosition().line() &&
|
d1.getPosition().line() == d2.getPosition().line() &&
|
||||||
d1.getPosition().column() == d2.getPosition().column());
|
d1.getPosition().column() == d2.getPosition().column());
|
||||||
|
|
|
@ -496,57 +496,12 @@ public class Apt extends ListBuffer<Env<AttrContext>> {
|
||||||
* won't match anything.
|
* won't match anything.
|
||||||
*/
|
*/
|
||||||
Pattern importStringToPattern(String s) {
|
Pattern importStringToPattern(String s) {
|
||||||
if (s.equals("*")) {
|
if (com.sun.tools.javac.processing.JavacProcessingEnvironment.isValidImportString(s)) {
|
||||||
return allMatches;
|
return com.sun.tools.javac.processing.JavacProcessingEnvironment.validImportStringToPattern(s);
|
||||||
} else {
|
} else {
|
||||||
String t = s;
|
Bark bark = Bark.instance(context);
|
||||||
boolean star = false;
|
bark.aptWarning("MalformedSupportedString", s);
|
||||||
|
return com.sun.tools.javac.processing.JavacProcessingEnvironment.noMatches;
|
||||||
/*
|
|
||||||
* Validate string from factory is legal. If the string
|
|
||||||
* has more than one asterisks or the asterisks does not
|
|
||||||
* appear as the last character (preceded by a period),
|
|
||||||
* the string is not legal.
|
|
||||||
*/
|
|
||||||
|
|
||||||
boolean valid = true;
|
|
||||||
int index = t.indexOf('*');
|
|
||||||
if (index != -1) {
|
|
||||||
// '*' must be last character...
|
|
||||||
if (index == t.length() -1) {
|
|
||||||
// ... and preceeding character must be '.'
|
|
||||||
if ( index-1 >= 0 ) {
|
|
||||||
valid = t.charAt(index-1) == '.';
|
|
||||||
// Strip off ".*$" for identifier checks
|
|
||||||
t = t.substring(0, t.length()-2);
|
|
||||||
}
|
|
||||||
} else
|
|
||||||
valid = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify string is off the form (javaId \.)+ or javaId
|
|
||||||
if (valid) {
|
|
||||||
String[] javaIds = t.split("\\.", t.length()+2);
|
|
||||||
for(String javaId: javaIds)
|
|
||||||
valid &= isJavaIdentifier(javaId);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!valid) {
|
|
||||||
Bark bark = Bark.instance(context);
|
|
||||||
bark.aptWarning("MalformedSupportedString", s);
|
|
||||||
return noMatches; // won't match any valid identifier
|
|
||||||
}
|
|
||||||
|
|
||||||
String s_prime = s.replaceAll("\\.", "\\\\.");
|
|
||||||
|
|
||||||
if (s_prime.endsWith("*")) {
|
|
||||||
s_prime = s_prime.substring(0, s_prime.length() - 1) + ".+";
|
|
||||||
}
|
|
||||||
|
|
||||||
return Pattern.compile(s_prime);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final Pattern allMatches = Pattern.compile(".*");
|
|
||||||
private static final Pattern noMatches = Pattern.compile("(\\P{all})+");
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,6 +56,8 @@ import com.sun.tools.apt.comp.UsageMessageNeededException;
|
||||||
import com.sun.tools.apt.util.Bark;
|
import com.sun.tools.apt.util.Bark;
|
||||||
import com.sun.mirror.apt.AnnotationProcessorFactory;
|
import com.sun.mirror.apt.AnnotationProcessorFactory;
|
||||||
|
|
||||||
|
import static com.sun.tools.javac.file.Paths.pathToURLs;
|
||||||
|
|
||||||
/** This class provides a commandline interface to the apt build-time
|
/** This class provides a commandline interface to the apt build-time
|
||||||
* tool.
|
* tool.
|
||||||
*
|
*
|
||||||
|
@ -1276,59 +1278,4 @@ public class Main {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Borrowed from DocletInvoker
|
|
||||||
/**
|
|
||||||
* Utility method for converting a search path string to an array
|
|
||||||
* of directory and JAR file URLs.
|
|
||||||
*
|
|
||||||
* @param path the search path string
|
|
||||||
* @return the resulting array of directory and JAR file URLs
|
|
||||||
*/
|
|
||||||
static URL[] pathToURLs(String path) {
|
|
||||||
StringTokenizer st = new StringTokenizer(path, File.pathSeparator);
|
|
||||||
URL[] urls = new URL[st.countTokens()];
|
|
||||||
int count = 0;
|
|
||||||
while (st.hasMoreTokens()) {
|
|
||||||
URL url = fileToURL(new File(st.nextToken()));
|
|
||||||
if (url != null) {
|
|
||||||
urls[count++] = url;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (urls.length != count) {
|
|
||||||
URL[] tmp = new URL[count];
|
|
||||||
System.arraycopy(urls, 0, tmp, 0, count);
|
|
||||||
urls = tmp;
|
|
||||||
}
|
|
||||||
return urls;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the directory or JAR file URL corresponding to the specified
|
|
||||||
* local file name.
|
|
||||||
*
|
|
||||||
* @param file the File object
|
|
||||||
* @return the resulting directory or JAR file URL, or null if unknown
|
|
||||||
*/
|
|
||||||
static URL fileToURL(File file) {
|
|
||||||
String name;
|
|
||||||
try {
|
|
||||||
name = file.getCanonicalPath();
|
|
||||||
} catch (IOException e) {
|
|
||||||
name = file.getAbsolutePath();
|
|
||||||
}
|
|
||||||
name = name.replace(File.separatorChar, '/');
|
|
||||||
if (!name.startsWith("/")) {
|
|
||||||
name = "/" + name;
|
|
||||||
}
|
|
||||||
// If the file does not exist, then assume that it's a directory
|
|
||||||
if (!file.isFile()) {
|
|
||||||
name = name + "/";
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
return new URL("file", "", name);
|
|
||||||
} catch (MalformedURLException e) {
|
|
||||||
throw new IllegalArgumentException("file");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,8 @@ package com.sun.tools.javac.file;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URL;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -34,6 +36,7 @@ import java.util.Set;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
|
import java.util.StringTokenizer;
|
||||||
import java.util.zip.ZipFile;
|
import java.util.zip.ZipFile;
|
||||||
import javax.tools.JavaFileManager.Location;
|
import javax.tools.JavaFileManager.Location;
|
||||||
|
|
||||||
|
@ -449,4 +452,60 @@ public class Paths {
|
||||||
return fsInfo.isFile(file)
|
return fsInfo.isFile(file)
|
||||||
&& (n.endsWith(".jar") || n.endsWith(".zip"));
|
&& (n.endsWith(".jar") || n.endsWith(".zip"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utility method for converting a search path string to an array
|
||||||
|
* of directory and JAR file URLs.
|
||||||
|
*
|
||||||
|
* Note that this method is called by apt and the DocletInvoker.
|
||||||
|
*
|
||||||
|
* @param path the search path string
|
||||||
|
* @return the resulting array of directory and JAR file URLs
|
||||||
|
*/
|
||||||
|
public static URL[] pathToURLs(String path) {
|
||||||
|
StringTokenizer st = new StringTokenizer(path, File.pathSeparator);
|
||||||
|
URL[] urls = new URL[st.countTokens()];
|
||||||
|
int count = 0;
|
||||||
|
while (st.hasMoreTokens()) {
|
||||||
|
URL url = fileToURL(new File(st.nextToken()));
|
||||||
|
if (url != null) {
|
||||||
|
urls[count++] = url;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (urls.length != count) {
|
||||||
|
URL[] tmp = new URL[count];
|
||||||
|
System.arraycopy(urls, 0, tmp, 0, count);
|
||||||
|
urls = tmp;
|
||||||
|
}
|
||||||
|
return urls;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the directory or JAR file URL corresponding to the specified
|
||||||
|
* local file name.
|
||||||
|
*
|
||||||
|
* @param file the File object
|
||||||
|
* @return the resulting directory or JAR file URL, or null if unknown
|
||||||
|
*/
|
||||||
|
private static URL fileToURL(File file) {
|
||||||
|
String name;
|
||||||
|
try {
|
||||||
|
name = file.getCanonicalPath();
|
||||||
|
} catch (IOException e) {
|
||||||
|
name = file.getAbsolutePath();
|
||||||
|
}
|
||||||
|
name = name.replace(File.separatorChar, '/');
|
||||||
|
if (!name.startsWith("/")) {
|
||||||
|
name = "/" + name;
|
||||||
|
}
|
||||||
|
// If the file does not exist, then assume that it's a directory
|
||||||
|
if (!file.isFile()) {
|
||||||
|
name = name + "/";
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return new URL("file", "", name);
|
||||||
|
} catch (MalformedURLException e) {
|
||||||
|
throw new IllegalArgumentException(file.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,6 @@
|
||||||
|
|
||||||
package com.sun.tools.javac.processing;
|
package com.sun.tools.javac.processing;
|
||||||
|
|
||||||
|
|
||||||
import java.lang.reflect.*;
|
import java.lang.reflect.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.regex.*;
|
import java.util.regex.*;
|
||||||
|
@ -1355,115 +1354,62 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea
|
||||||
return specifiedPackages;
|
return specifiedPackages;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Borrowed from DocletInvoker and apt
|
|
||||||
// TODO: remove from apt's Main
|
|
||||||
/**
|
|
||||||
* Utility method for converting a search path string to an array
|
|
||||||
* of directory and JAR file URLs.
|
|
||||||
*
|
|
||||||
* @param path the search path string
|
|
||||||
* @return the resulting array of directory and JAR file URLs
|
|
||||||
*/
|
|
||||||
public static URL[] pathToURLs(String path) {
|
|
||||||
StringTokenizer st = new StringTokenizer(path, File.pathSeparator);
|
|
||||||
URL[] urls = new URL[st.countTokens()];
|
|
||||||
int count = 0;
|
|
||||||
while (st.hasMoreTokens()) {
|
|
||||||
URL url = fileToURL(new File(st.nextToken()));
|
|
||||||
if (url != null) {
|
|
||||||
urls[count++] = url;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (urls.length != count) {
|
|
||||||
URL[] tmp = new URL[count];
|
|
||||||
System.arraycopy(urls, 0, tmp, 0, count);
|
|
||||||
urls = tmp;
|
|
||||||
}
|
|
||||||
return urls;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the directory or JAR file URL corresponding to the specified
|
|
||||||
* local file name.
|
|
||||||
*
|
|
||||||
* @param file the File object
|
|
||||||
* @return the resulting directory or JAR file URL, or null if unknown
|
|
||||||
*/
|
|
||||||
private static URL fileToURL(File file) {
|
|
||||||
String name;
|
|
||||||
try {
|
|
||||||
name = file.getCanonicalPath();
|
|
||||||
} catch (IOException e) {
|
|
||||||
name = file.getAbsolutePath();
|
|
||||||
}
|
|
||||||
name = name.replace(File.separatorChar, '/');
|
|
||||||
if (!name.startsWith("/")) {
|
|
||||||
name = "/" + name;
|
|
||||||
}
|
|
||||||
// If the file does not exist, then assume that it's a directory
|
|
||||||
if (!file.isFile()) {
|
|
||||||
name = name + "/";
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
return new URL("file", "", name);
|
|
||||||
} catch (MalformedURLException e) {
|
|
||||||
throw new IllegalArgumentException("file");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private static final Pattern allMatches = Pattern.compile(".*");
|
private static final Pattern allMatches = Pattern.compile(".*");
|
||||||
|
public static final Pattern noMatches = Pattern.compile("(\\P{all})+");
|
||||||
|
|
||||||
private static final Pattern noMatches = Pattern.compile("(\\P{all})+");
|
|
||||||
/**
|
/**
|
||||||
* Convert import-style string to regex matching that string. If
|
* Convert import-style string for supported annotations into a
|
||||||
* the string is a valid import-style string, return a regex that
|
* regex matching that string. If the string is a valid
|
||||||
* won't match anything.
|
* import-style string, return a regex that won't match anything.
|
||||||
*/
|
*/
|
||||||
// TODO: remove version in Apt.java
|
private static Pattern importStringToPattern(String s, Processor p, Log log) {
|
||||||
public static Pattern importStringToPattern(String s, Processor p, Log log) {
|
if (isValidImportString(s)) {
|
||||||
|
return validImportStringToPattern(s);
|
||||||
|
} else {
|
||||||
|
log.warning("proc.malformed.supported.string", s, p.getClass().getName());
|
||||||
|
return noMatches; // won't match any valid identifier
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return true if the argument string is a valid import-style
|
||||||
|
* string specifying claimed annotations; return false otherwise.
|
||||||
|
*/
|
||||||
|
public static boolean isValidImportString(String s) {
|
||||||
|
if (s.equals("*"))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
boolean valid = true;
|
||||||
|
String t = s;
|
||||||
|
int index = t.indexOf('*');
|
||||||
|
|
||||||
|
if (index != -1) {
|
||||||
|
// '*' must be last character...
|
||||||
|
if (index == t.length() -1) {
|
||||||
|
// ... any and preceding character must be '.'
|
||||||
|
if ( index-1 >= 0 ) {
|
||||||
|
valid = t.charAt(index-1) == '.';
|
||||||
|
// Strip off ".*$" for identifier checks
|
||||||
|
t = t.substring(0, t.length()-2);
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify string is off the form (javaId \.)+ or javaId
|
||||||
|
if (valid) {
|
||||||
|
String[] javaIds = t.split("\\.", t.length()+2);
|
||||||
|
for(String javaId: javaIds)
|
||||||
|
valid &= SourceVersion.isIdentifier(javaId);
|
||||||
|
}
|
||||||
|
return valid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Pattern validImportStringToPattern(String s) {
|
||||||
if (s.equals("*")) {
|
if (s.equals("*")) {
|
||||||
return allMatches;
|
return allMatches;
|
||||||
} else {
|
} else {
|
||||||
String t = s;
|
String s_prime = s.replace(".", "\\.");
|
||||||
boolean star = false;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Validate string from factory is legal. If the string
|
|
||||||
* has more than one asterisks or the asterisks does not
|
|
||||||
* appear as the last character (preceded by a period),
|
|
||||||
* the string is not legal.
|
|
||||||
*/
|
|
||||||
|
|
||||||
boolean valid = true;
|
|
||||||
int index = t.indexOf('*');
|
|
||||||
if (index != -1) {
|
|
||||||
// '*' must be last character...
|
|
||||||
if (index == t.length() -1) {
|
|
||||||
// ... and preceeding character must be '.'
|
|
||||||
if ( index-1 >= 0 ) {
|
|
||||||
valid = t.charAt(index-1) == '.';
|
|
||||||
// Strip off ".*$" for identifier checks
|
|
||||||
t = t.substring(0, t.length()-2);
|
|
||||||
}
|
|
||||||
} else
|
|
||||||
valid = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify string is off the form (javaId \.)+ or javaId
|
|
||||||
if (valid) {
|
|
||||||
String[] javaIds = t.split("\\.", t.length()+2);
|
|
||||||
for(String javaId: javaIds)
|
|
||||||
valid &= SourceVersion.isIdentifier(javaId);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!valid) {
|
|
||||||
log.warning("proc.malformed.supported.string", s, p.getClass().getName());
|
|
||||||
return noMatches; // won't match any valid identifier
|
|
||||||
}
|
|
||||||
|
|
||||||
String s_prime = s.replaceAll("\\.", "\\\\.");
|
|
||||||
|
|
||||||
if (s_prime.endsWith("*")) {
|
if (s_prime.endsWith("*")) {
|
||||||
s_prime = s_prime.substring(0, s_prime.length() - 1) + ".+";
|
s_prime = s_prime.substring(0, s_prime.length() - 1) + ".+";
|
||||||
|
|
|
@ -81,7 +81,7 @@ public class DocletInvoker {
|
||||||
cpString = appendPath(System.getProperty("env.class.path"), cpString);
|
cpString = appendPath(System.getProperty("env.class.path"), cpString);
|
||||||
cpString = appendPath(System.getProperty("java.class.path"), cpString);
|
cpString = appendPath(System.getProperty("java.class.path"), cpString);
|
||||||
cpString = appendPath(docletPath, cpString);
|
cpString = appendPath(docletPath, cpString);
|
||||||
URL[] urls = pathToURLs(cpString);
|
URL[] urls = com.sun.tools.javac.file.Paths.pathToURLs(cpString);
|
||||||
if (docletParentClassLoader == null)
|
if (docletParentClassLoader == null)
|
||||||
appClassLoader = new URLClassLoader(urls, getDelegationClassLoader(docletClassName));
|
appClassLoader = new URLClassLoader(urls, getDelegationClassLoader(docletClassName));
|
||||||
else
|
else
|
||||||
|
@ -313,58 +313,4 @@ public class DocletInvoker {
|
||||||
Thread.currentThread().setContextClassLoader(savedCCL);
|
Thread.currentThread().setContextClassLoader(savedCCL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Utility method for converting a search path string to an array
|
|
||||||
* of directory and JAR file URLs.
|
|
||||||
*
|
|
||||||
* @param path the search path string
|
|
||||||
* @return the resulting array of directory and JAR file URLs
|
|
||||||
*/
|
|
||||||
static URL[] pathToURLs(String path) {
|
|
||||||
StringTokenizer st = new StringTokenizer(path, File.pathSeparator);
|
|
||||||
URL[] urls = new URL[st.countTokens()];
|
|
||||||
int count = 0;
|
|
||||||
while (st.hasMoreTokens()) {
|
|
||||||
URL url = fileToURL(new File(st.nextToken()));
|
|
||||||
if (url != null) {
|
|
||||||
urls[count++] = url;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (urls.length != count) {
|
|
||||||
URL[] tmp = new URL[count];
|
|
||||||
System.arraycopy(urls, 0, tmp, 0, count);
|
|
||||||
urls = tmp;
|
|
||||||
}
|
|
||||||
return urls;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the directory or JAR file URL corresponding to the specified
|
|
||||||
* local file name.
|
|
||||||
*
|
|
||||||
* @param file the File object
|
|
||||||
* @return the resulting directory or JAR file URL, or null if unknown
|
|
||||||
*/
|
|
||||||
static URL fileToURL(File file) {
|
|
||||||
String name;
|
|
||||||
try {
|
|
||||||
name = file.getCanonicalPath();
|
|
||||||
} catch (IOException e) {
|
|
||||||
name = file.getAbsolutePath();
|
|
||||||
}
|
|
||||||
name = name.replace(File.separatorChar, '/');
|
|
||||||
if (!name.startsWith("/")) {
|
|
||||||
name = "/" + name;
|
|
||||||
}
|
|
||||||
// If the file does not exist, then assume that it's a directory
|
|
||||||
if (!file.isFile()) {
|
|
||||||
name = name + "/";
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
return new URL("file", "", name);
|
|
||||||
} catch (MalformedURLException e) {
|
|
||||||
throw new IllegalArgumentException("file");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue