Method from com.sun.tools.javac.file.JavacFileManager Detail: |
public void close() {
for (Iterator< Archive > i = archives.values().iterator(); i.hasNext(); ) {
Archive a = i.next();
i.remove();
try {
a.close();
} catch (IOException e) {
}
}
}
Close the JavaFileManager, releasing resources. |
public void flush() {
contentCache.clear();
}
Flush any output resources. |
public ClassLoader getClassLoader(Location location) {
nullCheck(location);
Iterable< ? extends File > path = getLocation(location);
if (path == null)
return null;
ListBuffer< URL > lb = new ListBuffer< URL >();
for (File f: path) {
try {
lb.append(f.toURI().toURL());
} catch (MalformedURLException e) {
throw new AssertionError(e);
}
}
return getClassLoader(lb.toArray(new URL[lb.size()]));
}
|
public JavaFileObject getFileForInput(String name) {
return getRegularFile(new File(name));
}
|
public FileObject getFileForInput(Location location,
String packageName,
String relativeName) throws IOException {
nullCheck(location);
// validatePackageName(packageName);
nullCheck(packageName);
if (!isRelativeUri(relativeName))
throw new IllegalArgumentException("Invalid relative name: " + relativeName);
RelativeFile name = packageName.length() == 0
? new RelativeFile(relativeName)
: new RelativeFile(RelativeDirectory.forPackage(packageName), relativeName);
return getFileForInput(location, name);
}
|
public JavaFileObject getFileForOutput(String classname,
Kind kind,
JavaFileObject sibling) throws IOException {
return getJavaFileForOutput(CLASS_OUTPUT, classname, kind, sibling);
}
|
public FileObject getFileForOutput(Location location,
String packageName,
String relativeName,
FileObject sibling) throws IOException {
nullCheck(location);
// validatePackageName(packageName);
nullCheck(packageName);
if (!isRelativeUri(relativeName))
throw new IllegalArgumentException("Invalid relative name: " + relativeName);
RelativeFile name = packageName.length() == 0
? new RelativeFile(relativeName)
: new RelativeFile(RelativeDirectory.forPackage(packageName), relativeName);
return getFileForOutput(location, name, sibling);
}
|
public JavaFileObject getJavaFileForInput(Location location,
String className,
Kind kind) throws IOException {
nullCheck(location);
// validateClassName(className);
nullCheck(className);
nullCheck(kind);
if (!sourceOrClass.contains(kind))
throw new IllegalArgumentException("Invalid kind: " + kind);
return getFileForInput(location, RelativeFile.forClass(className, kind));
}
|
public JavaFileObject getJavaFileForOutput(Location location,
String className,
Kind kind,
FileObject sibling) throws IOException {
nullCheck(location);
// validateClassName(className);
nullCheck(className);
nullCheck(kind);
if (!sourceOrClass.contains(kind))
throw new IllegalArgumentException("Invalid kind: " + kind);
return getFileForOutput(location, RelativeFile.forClass(className, kind), sibling);
}
|
public Iterable<JavaFileObject> getJavaFileObjects(String names) {
return getJavaFileObjectsFromStrings(Arrays.asList(nullCheck(names)));
}
|
public Iterable<JavaFileObject> getJavaFileObjects(File files) {
return getJavaFileObjectsFromFiles(Arrays.asList(nullCheck(files)));
}
|
public Iterable<JavaFileObject> getJavaFileObjectsFromFiles(Iterable<File> files) {
ArrayList< RegularFileObject > result;
if (files instanceof Collection< ? >)
result = new ArrayList< RegularFileObject >(((Collection< ? >)files).size());
else
result = new ArrayList< RegularFileObject >();
for (File f: files)
result.add(new RegularFileObject(this, nullCheck(f)));
return result;
}
|
public Iterable<JavaFileObject> getJavaFileObjectsFromStrings(Iterable<String> names) {
ListBuffer< File > files = new ListBuffer< File >();
for (String name : names)
files.append(new File(nullCheck(name)));
return getJavaFileObjectsFromFiles(files.toList());
}
|
public Iterable<File> getLocation(Location location) {
nullCheck(location);
paths.lazy();
if (location == CLASS_OUTPUT) {
return (getClassOutDir() == null ? null : List.of(getClassOutDir()));
} else if (location == SOURCE_OUTPUT) {
return (getSourceOutDir() == null ? null : List.of(getSourceOutDir()));
} else
return paths.getPathForLocation(location);
}
|
public static String getMessage(IOException e) {
String s = e.getLocalizedMessage();
if (s != null)
return s;
s = e.getMessage();
if (s != null)
return s;
return e.toString();
}
Get a detail message from an IOException.
Most, but not all, instances of IOException provide a non-null result
for getLocalizedMessage(). But some instances return null: in these
cases, fallover to getMessage(), and if even that is null, return the
name of the exception itself. |
public JavaFileObject getRegularFile(File file) {
return new RegularFileObject(this, file);
}
|
public static String getRelativeName(File file) {
if (!file.isAbsolute()) {
String result = file.getPath().replace(File.separatorChar, '/');
if (isRelativeUri(result))
return result;
}
throw new IllegalArgumentException("Invalid relative path: " + file);
}
Converts a relative file name to a relative URI. This is
different from File.toURI as this method does not canonicalize
the file before creating the URI. Furthermore, no schema is
used. |
public boolean hasLocation(Location location) {
return getLocation(location) != null;
}
|
public String inferBinaryName(Location location,
JavaFileObject file) {
file.getClass(); // null check
location.getClass(); // null check
// Need to match the path semantics of list(location, ...)
Iterable< ? extends File > path = getLocation(location);
if (path == null) {
return null;
}
if (file instanceof BaseFileObject) {
return ((BaseFileObject) file).inferBinaryName(path);
} else
throw new IllegalArgumentException(file.getClass().getName());
}
|
public boolean isDefaultBootClassPath() {
return paths.isDefaultBootClassPath();
}
|
protected static boolean isRelativeUri(URI uri) {
if (uri.isAbsolute())
return false;
String path = uri.normalize().getPath();
if (path.length() == 0 /* isEmpty() is mustang API */)
return false;
if (!path.equals(uri.getPath())) // implicitly checks for embedded . and ..
return false;
if (path.startsWith("/") || path.startsWith("./") || path.startsWith("../"))
return false;
return true;
}
Enforces the specification of a "relative" URI as used in
{@linkplain #getFileForInput(Location,String,URI)
getFileForInput}. This method must follow the rules defined in
that method, do not make any changes without consulting the
specification. |
protected static boolean isRelativeUri(String u) {
try {
return isRelativeUri(new URI(u));
} catch (URISyntaxException e) {
return false;
}
}
|
public boolean isSameFile(FileObject a,
FileObject b) {
nullCheck(a);
nullCheck(b);
if (!(a instanceof BaseFileObject))
throw new IllegalArgumentException("Not supported: " + a);
if (!(b instanceof BaseFileObject))
throw new IllegalArgumentException("Not supported: " + b);
return a.equals(b);
}
|
public Iterable<JavaFileObject> list(Location location,
String packageName,
Set<Kind> kinds,
boolean recurse) throws IOException {
// validatePackageName(packageName);
nullCheck(packageName);
nullCheck(kinds);
Iterable< ? extends File > path = getLocation(location);
if (path == null)
return List.nil();
RelativeDirectory subdirectory = RelativeDirectory.forPackage(packageName);
ListBuffer< JavaFileObject > results = new ListBuffer< JavaFileObject >();
for (File directory : path)
listContainer(directory, subdirectory, kinds, recurse, results);
return results.toList();
}
|
protected Archive openArchive(File zipFilename) throws IOException {
try {
return openArchive(zipFilename, contextUseOptimizedZip);
} catch (IOException ioe) {
if (ioe instanceof ZipFileIndex.ZipFormatException) {
return openArchive(zipFilename, false);
} else {
throw ioe;
}
}
}
|
public static void preRegister(Context context) {
context.put(JavaFileManager.class, new Context.Factory< JavaFileManager >() {
public JavaFileManager make(Context c) {
return new JavacFileManager(c, true, null);
}
});
}
Register a Context.Factory to create a JavacFileManager. |
public void setContext(Context context) {
super.setContext(context);
if (paths == null) {
paths = Paths.instance(context);
} else {
// Reuse the Paths object as it stores the locations that
// have been set with setLocation, etc.
paths.setContext(context);
}
fsInfo = FSInfo.instance(context);
contextUseOptimizedZip = options.getBoolean("useOptimizedZip", true);
if (contextUseOptimizedZip)
zipFileIndexCache = ZipFileIndexCache.getSharedInstance();
mmappedIO = options.isSet("mmappedIO");
ignoreSymbolFile = options.isSet("ignore.symbol.file");
String sf = options.get("sortFiles");
if (sf != null) {
sortFiles = (sf.equals("reverse") ? SortFiles.REVERSE : SortFiles.FORWARD);
}
}
Set the context for JavacFileManager. |
public void setLocation(Location location,
Iterable<File> path) throws IOException {
nullCheck(location);
paths.lazy();
final File dir = location.isOutputLocation() ? getOutputDirectory(path) : null;
if (location == CLASS_OUTPUT)
classOutDir = getOutputLocation(dir, D);
else if (location == SOURCE_OUTPUT)
sourceOutDir = getOutputLocation(dir, S);
else
paths.setPathForLocation(location, path);
}
|
public static void testName(String name,
boolean isValidPackageName,
boolean isValidClassName) {
try {
validatePackageName(name);
if (!isValidPackageName)
throw new AssertionError("Invalid package name accepted: " + name);
printAscii("Valid package name: \"%s\"", name);
} catch (IllegalArgumentException e) {
if (isValidPackageName)
throw new AssertionError("Valid package name rejected: " + name);
printAscii("Invalid package name: \"%s\"", name);
}
try {
validateClassName(name);
if (!isValidClassName)
throw new AssertionError("Invalid class name accepted: " + name);
printAscii("Valid class name: \"%s\"", name);
} catch (IllegalArgumentException e) {
if (isValidClassName)
throw new AssertionError("Valid class name rejected: " + name);
printAscii("Invalid class name: \"%s\"", name);
}
}
|
public static char[] toArray(CharBuffer buffer) {
if (buffer.hasArray())
return ((CharBuffer)buffer.compact().flip()).array();
else
return buffer.toString().toCharArray();
}
|