diff options
Diffstat (limited to 'javax/tools')
-rw-r--r-- | javax/tools/FileObject.java | 2 | ||||
-rw-r--r-- | javax/tools/ForwardingFileObject.java | 161 | ||||
-rw-r--r-- | javax/tools/ForwardingJavaFileObject.java | 102 | ||||
-rw-r--r-- | javax/tools/JavaCompiler.java | 213 | ||||
-rw-r--r-- | javax/tools/JavaFileManager.java | 351 | ||||
-rw-r--r-- | javax/tools/JavaFileObject.java | 125 | ||||
-rw-r--r-- | javax/tools/OptionChecker.java | 59 | ||||
-rw-r--r-- | javax/tools/SimpleJavaFileObject.java | 281 | ||||
-rw-r--r-- | javax/tools/StandardJavaFileManager.java | 166 | ||||
-rw-r--r-- | javax/tools/StandardLocation.java | 119 | ||||
-rw-r--r-- | javax/tools/Tool.java | 84 |
11 files changed, 1662 insertions, 1 deletions
diff --git a/javax/tools/FileObject.java b/javax/tools/FileObject.java index e04c056c7..bde59eab3 100644 --- a/javax/tools/FileObject.java +++ b/javax/tools/FileObject.java @@ -1,4 +1,4 @@ -/* FileObject.java -- +/* FileObject.java -- File object abstraction. Copyright (C) 2008 Free Software Foundation, Inc. This file is part of GNU Classpath. diff --git a/javax/tools/ForwardingFileObject.java b/javax/tools/ForwardingFileObject.java new file mode 100644 index 000000000..20eb75e84 --- /dev/null +++ b/javax/tools/ForwardingFileObject.java @@ -0,0 +1,161 @@ +/* ForwardingFileObject.java -- File object that forwards to another. + Copyright (C) 2012 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package javax.tools; + +import java.io.InputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.Reader; +import java.io.Writer; + +import java.net.URI; + +/** + * Forwards calls to a specified file object. + * + * @param <F> the kind of object calls are forwarded to. + * @author Andrew John Hughes (gnu_andrew@member.fsf.org) + * @since 1.6 + */ +public class ForwardingFileObject<F extends FileObject> + implements FileObject +{ + + /** + * The file object to delegate to. + */ + protected F fileObject; + + /** + * Creates a new forwarder, delegating calls to the + * specified object. + * + * @param obj the object to forward calls to. + */ + protected ForwardingFileObject(F obj) + { + fileObject = obj; + } + + /** + * @inheritDoc + */ + @Override + public boolean delete() + { + return fileObject.delete(); + } + + /** + * @inheritDoc + */ + @Override + public CharSequence getCharContent(boolean ignoreEncodingErrors) + throws IOException + { + return fileObject.getCharContent(ignoreEncodingErrors); + } + + /** + * @inheritDoc + */ + @Override + public long getLastModified() + { + return fileObject.getLastModified(); + } + + /** + * @inheritDoc + */ + @Override + public String getName() + { + return fileObject.getName(); + } + + /** + * @inheritDoc + */ + @Override + public InputStream openInputStream() + throws IOException + { + return fileObject.openInputStream(); + } + + /** + * @inheritDoc + */ + @Override + public OutputStream openOutputStream() + throws IOException + { + return fileObject.openOutputStream(); + } + + /** + * @inheritDoc + */ + @Override + public Reader openReader(boolean ignoreEncodingErrors) + throws IOException + { + return fileObject.openReader(ignoreEncodingErrors); + } + + /** + * @inheritDoc + */ + @Override + public Writer openWriter() + throws IOException + { + return fileObject.openWriter(); + } + + /** + * @inheritDoc + */ + @Override + public URI toUri() + { + return fileObject.toUri(); + } + +} diff --git a/javax/tools/ForwardingJavaFileObject.java b/javax/tools/ForwardingJavaFileObject.java new file mode 100644 index 000000000..2ed9cb359 --- /dev/null +++ b/javax/tools/ForwardingJavaFileObject.java @@ -0,0 +1,102 @@ +/* ForwardingJavaFileObject.java -- Java file object that forwards to another. + Copyright (C) 2012 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package javax.tools; + +import javax.lang.model.element.Modifier; +import javax.lang.model.element.NestingKind; + +/** + * Forwards calls to a specified {@link JavaFileObject}. + * + * @param <F> the kind of object calls are forwarded to. + * @author Andrew John Hughes (gnu_andrew@member.fsf.org) + * @since 1.6 + */ +public class ForwardingJavaFileObject<F extends JavaFileObject> + extends ForwardingFileObject<F> + implements JavaFileObject +{ + + /** + * Creates a new forwarder, delegating calls to the + * specified object. + * + * @param obj the object to forward calls to. + */ + protected ForwardingJavaFileObject(F obj) + { + super(obj); + } + + /** + * @inheritDoc + */ + @Override + public JavaFileObject.Kind getKind() + { + return ((JavaFileObject) fileObject).getKind(); + } + + /** + * @inheritDoc + */ + @Override + public boolean isNameCompatible(String simpleName, JavaFileObject.Kind kind) + { + return ((JavaFileObject) fileObject).isNameCompatible(simpleName, kind); + } + + /** + * @inheritDoc + */ + @Override + public Modifier getAccessLevel() + { + return ((JavaFileObject) fileObject).getAccessLevel(); + } + + /** + * @inheritDoc + */ + @Override + public NestingKind getNestingKind() + { + return ((JavaFileObject) fileObject).getNestingKind(); + } + +} diff --git a/javax/tools/JavaCompiler.java b/javax/tools/JavaCompiler.java new file mode 100644 index 000000000..e1a8430c4 --- /dev/null +++ b/javax/tools/JavaCompiler.java @@ -0,0 +1,213 @@ +/* JavaCompiler.java -- Programmatic interface for a compiler. + Copyright (C) 2012 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package javax.tools; + +import java.io.Writer; + +import java.nio.charset.Charset; + +import java.util.Locale; + +import java.util.concurrent.Callable; + +import javax.annotation.processing.Processor; + +/** + * <p> + * Provides a programmatic interface to a compiler for the Java + * programming language. The compiler relies on two services; + * a diagnostic listener for producing textual output including + * warnings or errors from the code, and a file manager for providing + * access to the files to be compiled and the output location. + * More detail on these services and various helper classes related + * to them is provided below. The classes {@link DiagnosticListener}, + * {@link JavaFileManager}, {@link FileObject} and {@link JavaFileObject} + * provide the service provider interface for these services and are not + * intended for user consumption.</p> + * <p>Compilers implementing {@link JavaCompiler} must conform to the Java + * Language Specification and produce class files conforming to the + * Java Virtual Machine Specification. In addition, those compilers + * which claim to support {@link SourceVersion#RELEASE_6} or later + * must support annotation processing.</p> + * <h2>Diagnostics</h2> + * <p>Where possible, the compiler provides diagnostic output to + * a {@link DiagnosticListener} if provided. If a listener is + * not provided, diagnostics are instead written to the default + * output ({@code{System.err} by default). This may also happen + * where the diagnostics are inappropriate for a {@link Diagnostic} + * object. The class {@link DiagnosticCollector} provides a means + * collate together multiple {@link Diagnostic}s in a list.</p> + * <h2>The File Manager</h2> + * <p>A compiler tool has an associated standard file manager which + * is native to it. An instance of it can be obtained by calling + * {@link #getStandardFileManager(DiagnosticListener,Locale,Charset)} + * and this is automatically called if no file manager is supplied to + * the compiler. However, the compiler must work with other file + * managers, provided they meet the requirements detailed in the methods + * below. Such file managers allow the user to customise how the compiler + * reads and writes files, and can be shared between multiple compilation + * tasks. Such reuse between different tasks can potentially provide + * a performance improvement.</p> + * <p>The standard file manager returned by this interface is an instance + * of the subinterface, {@link StandardJavaFileManager}, which is intended + * for operating on regular files and provides additional methods to support + * this.</p> + * <p>All file managers return a {@link FileObject} which provides an + * abstraction from the underlying file. The class {@link SimpleJavaFileObject} + * is provided as a means to simplifying implementing this interface.</p> + * <h2>Forwarding</h2> + * <p>As file managers and file objects are provided as return values from + * the methods of {@link JavaCompiler} and the file manager respectively, + * there is no means to override their behaviour by subclassing. Instead, + * it is necessary to wrap the returned instance in another implementation + * and forward method calls to it as appropriate. The classes + * {@link ForwardingJavaFileManager}, {@link ForwardingFileObject} and + * {@link ForwardingJavaFileObject} facilitate this by providing an + * implementation that simply forwards to another, which can then be subclassed + * to provide additional behaviour.</p> + * + * @author Andrew John Hughes (gnu_andrew@member.fsf.org) + * @since 1.6 + */ +public interface JavaCompiler + extends Tool, OptionChecker +{ + + /** + * Interface representing a compilation task as an asynchronous + * event or {@link java.util.concurrent.Future}. The task may be + * started by invoking {@link #call()}. Prior to this, the task + * may be configured by the user using the other methods of this + * interface. + * + * @author Andrew John Hughes (gnu_andrew@member.fsf.org) + * @since 1.6 + */ + public static interface CompilationTask + extends Callable<Boolean> + { + + /** + * Performs the compilation. The compilation may only + * be performed once. Subsequent calls will throw + * an {@link IllegalStateException}. + * + * @return true if all files were successfully compiled. + * @throws RuntimeException if an unrecoverable error occurred + * in a user-supplied component of + * the compilation process. The user + * exception will be provided as the + * cause of this exception. + * @throws IllegalStateException if the compilation has already run. + */ + Boolean call(); + + /** + * Sets the locale to use for outputting diagnostics. + * + * @param locale the locale to use. A value of {@code null} + * means no locale is applied. + * @throws IllegalStateException if the compilation has started. + */ + void setLocale(Locale locale); + + /** + * Explicitly set the annotation processors to be used, + * overriding the usual discovery process. + * + * @param processors the processors to use. + * @throws IllegalStateException if the compilation has started. + */ + void setProcessors(Iterable<? extends Processor> processors); + + } + + /** + * Returns a new instance of the standard file manager implementation + * used by this tool. Any non-fatal diagnostics produced by the tool + * will be passed to the specified {@link DiagnosticListener}, if + * supplied, using the given locale. Calls to the file manager after + * a {@link JavaFileManager#flush()} or {@link JavaFileManager#close()} + * will cause the file manager to be reopened. The file manager must + * be usable with other tools. + * + * @param listener the diagnostic listener to use or {@code null} if + * the compiler should use its own methods for producing + * diagnostics. + * @param locale the locale to use to format the diagnostics or {@code null} + * to use the default locale. + * @param charset the character set to use for decoding bytes or {@code null} + * to use the platform default. + * @return the standard file manager implementation. + */ + StandardJavaFileManager getStandardFileManager(DiagnosticListener<? super JavaFileObject> listener, + Locale locale, Charset charset); + + /** + * Returns an asynchrononous task for performing the specified compilation. + * The compilation may not have completed upon return of this method. + * If a file manager is specified, it must supported all locations specified + * in {@link StandardLocation}. + * + * @param out the output stream for compiler output beyond that provided by + * diagnostics; {@code System.err} is used if this is {@code null}. + * @param fileManager the file manager to use or {@code null} to use a new + * instance from + * {@link #getStandardFileManaager(DiagnosticListener,Locale,Charset)} + * @param listener the listener to pass diagnostic output to, or + * {@code null} if the compiler's default method should + * be used instead. + * @param options the options to supply to the compiler or {@code null} if there are none. + * @param classes the names of classes to use for annotation processing or {@code null} + * if there are none. + * @param objects the file objects to compile, or {@code null} if there are none. + * @return a task representing the proposed compilation. + * @throws RuntimeException if an unrecoverable error occurred + * in a user-supplied component of + * the compilation process. The user + * exception will be provided as the + * cause of this exception. + * @throws IllegalArgumentException if the kind of any of the objects is something + * other than {@link JavaFileObject.Kind#SOURCE}. + */ + CompilationTask getTask(Writer out, JavaFileManager fileManager, + DiagnosticListener<? super JavaFileObject> listener, + Iterable<String> options, Iterable<String> classes, + Iterable<? extends JavaFileObject> objects); + +} diff --git a/javax/tools/JavaFileManager.java b/javax/tools/JavaFileManager.java new file mode 100644 index 000000000..990962c95 --- /dev/null +++ b/javax/tools/JavaFileManager.java @@ -0,0 +1,351 @@ +/* JavaFileManager.java -- File manager for source & class file tools. + Copyright (C) 2012 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package javax.tools; + +import java.io.Closeable; +import java.io.Flushable; +import java.io.IOException; + +import java.util.Iterator; +import java.util.Set; + +/** + * <p>File manager for tools which operate on source and class files. + * In this context, the term {@code file} is used to refer to the abstract + * concept of a data source, rather than specifically a regular file on + * a filesystem.</p> + * <p>The file manager determines where to create new {@link FileObject}s. + * It may have a default directory where files are created. The user may + * provide hints as to how to perform file creation, but the file manager + * may choose to ignore these.</p> + * <p>For methods in this interface which use class names, these must + * be given in the internal virtual machine form of fully qualified + * class and interface names. The use of '/' and '.' are interchangeable, + * making {@code java.lang.Object}, {@code java/lang.Object} and + * {@code java/lang/Object} all equivalent.</p> + * <p>All names should be treated as being case-sensitive. If the underlying + * system is not case-aware, steps should be taken to handle this in the + * implementation, such as the use of {@link java.io.File#getCanonicalFile()} + * to preserve case.</p> + * <p>For methods in this interface which use relative names, these are + * path separated by {@code '/'} and do not include the + * segments {@code '.'} and {@code '..'}, so that they may only + * refer to subdirectories. A valid relative name must match + * the "path-rootless" rule of RFC 3986, section 3.3. The construction + * {@code URI.create(name).normalize().getPath().equals(name)} should hold.</p> + * <p>All methods may throw a {@link SecurityException}. All methods may + * throw a {@link NullPointerException} if an argument is null, unless + * otherwise specified. It is not required that the implementation support + * concurrent access to the file manager, but the file objects created by + * it should be thread-safe.</p> + * + * @author Andrew John Hughes (gnu_andrew@member.fsf.org) + * @since 1.6 + */ +public interface JavaFileManager + extends Closeable, Flushable, OptionChecker +{ + + /** + * Interface for obtaining the location of {@link FileObject}s. + * Used by the file manager to know where to create or locate them. + * + * @author Andrew John Hughes (gnu_andrew@member.fsf.org) + * @since 1.6 + */ + public static interface Location + { + /** + * Returns the name of this location. + * + * @return the name. + */ + String getName(); + + /** + * Returns true if this location is used for output. + * + * @return true if the location is used for output. + */ + boolean isOutputLocation(); + } + + /** + * Closes the file manager and releases any resources in + * use. Calling {@code close()} on an already closed + * file manager has no effect. The effect of calling + * other methods on a closed file manager is undefined, + * unless specified in the documentation of that method. + * + * @throws IOException if an I/O error occurs. + * @see #flush() + */ + void close() throws IOException; + + /** + * Flushes data to any resources controlled by this file + * manager. Flushing a closed file manager has no effect. + * + * @throws IOException if an I/O error occurs. + * @see #close() + */ + void flush() throws IOException; + + /** + * Returns the class loader that is responsible for loading + * class files from the specified location. For example, + * {@code getClassLoader(StandardLocation.ANNOTATION_PROCESSOR_PATH)} + * will return the class loader which is used to load + * annotation processors. + * + * @param location the location to retrieve the class loader for. + * @return the class loader for class files in that location, or + * {@code null} if the location is unknown or class loading + * from that location is disabled. + * @throws SecurityException if the class loader is not retrievable + * under the current security manager. + * @throws IllegalStateException if {@link #close()} has been called + * and the file manager can't be reopened. + */ + ClassLoader getClassLoader(Location location); + + /** + * <p>Returns a file object for reading a file. If this + * is a source or class file, the returned instance + * must be a {@link JavaFileObject}. The file name is + * constructed as a concatenation of the location, the + * package name and the relative file name.</p> + * <p>For example, for the call + * {@ocode getFileForInput(StandardLocation.SOURCE_PATH, + * "gnu.classpath.util", "CoolCode.java")}, assuming + * that the source path is set to + * {@code "/home/bob/sources"}, the returned file object + * would represent the file + * {@code "/home/bob/sources/gnu/classpath/util/CoolCode.java"}.</p> + * + * @param location the base location for the file. + * @param pkg the package the file will be read from. + * @param relName the path to the file, relative to {@code location+pkg}. + * @return a file object or may return {@code null} if the file does not exist. + * @throws IllegalArgumentException if the location is unknown and unknown locations + * are not supported, or if the relative name is invalid. + * @throws IOException if an I/O error occurs, or the file manager + * has been closed and can't be reopened. + * @throws IllegalStateException if the file manager has been closed and can't be reopened. + */ + FileObject getFileForInput(Location location, String pkg, String relName) + throws IOException; + + /** + * <p>Returns a file object for writing a file. If this + * is a source or class file, the returned instance + * must be a {@link JavaFileObject}. The file name is + * constructed as a concatenation of the location, the + * package name and the relative file name.</p> + * <p>For example, for the call + * {@ocode getFileForOutput(StandardLocation.SOURCE_OUTPUT, + * "gnu.classpath.util", "GenSyms.java")}, assuming + * that the source output is set to + * {@code "/home/bob/generated"}, the returned file object + * would represent the file + * {@code "/home/bob/generated/gnu/classpath/util/GenSyms.java"}.</p> + * <p>The file manager may optionally use the supplied + * sibling as a hint as to where to place the output file. + * The exact semantics of this hint are left to the implementation. + * As an example, the compiler places class files in the same location + * as the corresponding source file, if no destination is specified. + * To facilitate this, the source file location may be passed as + * a hint to the file manager.</p> + * + * @param location the base location for the file. + * @param pkg the package in which the file will be stored. + * @param relName the path to the file, relative to {@code location+pkg}. + * @param sibling an optional hint as to where to place the output file. + * May be {@code null}. + * @return a file object. + * @throws IllegalArgumentException if the location is unknown and unknown locations + * are not supported, if the relative name is invalid or + * if the sibling is not known to this file manager. + * @throws IOException if an I/O error occurs, or the file manager + * has been closed and can't be reopened. + * @throws IllegalStateException if the file manager has been closed and can't be reopened. + */ + FileObject getFileForOutput(Location location, String pkg, String relName, + FileObject sibling) + throws IOException; + + /** + * <p>Returns a {@link JavaFileObject} for reading + * a source file or class file. The file name is + * constructed as a concatenation of the location + * and the information provided by the type name + * and the kind of the file.</p> + * <p>For example, for the call + * {@ocode getJavaFileForInput(StandardLocation.SOURCE_PATH, + * "gnu.classpath.util.CoolCode", JavaFileObject.Kind.SOURCE)}, + * assuming that the source path is set to + * {@code "/home/bob/sources"}, the returned file object + * would represent the file + * {@code "/home/bob/sources/gnu/classpath/util/CoolCode.java"}.</p> + * + * @param location the base location for the file. + * @param className the name of the class. + * @param kind the kind of file, either {@link JavaFileObject.Kind.SOURCE} or + * {@link JavaFileObject.Kind.CLASS}. + * @return a file object or may return {@code null} if the file does not exist. + * @throws IllegalArgumentException if the location is unknown and unknown locations + * are not supported, or if the kind is invalid. + * @throws IOException if an I/O error occurs, or the file manager + * has been closed and can't be reopened. + * @throws IllegalStateException if the file manager has been closed and can't be reopened. + */ + JavaFileObject getJavaFileForInput(Location location, String className, JavaFileObject.Kind kind) + throws IOException; + + /** + * <p>Returns a {@link JavaFileObject} for writing a + * source or class file. The file name is + * constructed as a concatenation of the location and + * the information provided by the type name and the + * kind of the file.</p> + * <p>For example, for the call + * {@ocode getJavaFileForOutput(StandardLocation.CLASS_OUTPUT, + * "gnu.classpath.util.CoolCode", JavaFileObject.Kind.CLASS)}, assuming + * that the class output is set to + * {@code "/home/bob/build"}, the returned file object + * would represent the file + * {@code "/home/bob/build/gnu/classpath/util/GenSyms.class"}.</p> + * <p>The file manager may optionally use the supplied + * sibling as a hint as to where to place the output file. + * The exact semantics of this hint are left to the implementation. + * As an example, the compiler places class files in the same location + * as the corresponding source file, if no destination is specified. + * To facilitate this, the source file location may be passed as + * a hint to the file manager.</p> + * + * @param location the base location for the file. + * @param className the name of the class. + * @param kind the kind of file, either {@link JavaFileObject.Kind.SOURCE} or + * {@link JavaFileObject.Kind.CLASS}. + * @param sibling an optional hint as to where to place the output file. + * May be {@code null}. + * @return a file object. + * @throws IllegalArgumentException if the location is unknown and unknown locations + * are not supported, if the kind is invalid or + * if the sibling is not known to this file manager. + * @throws IOException if an I/O error occurs, or the file manager + * has been closed and can't be reopened. + * @throws IllegalStateException if the file manager has been closed and can't be reopened. + */ + JavaFileObject getJavaFileForOutput(Location location, String className, + JavaFileObject.Kind kind, FileObject sibling) + throws IOException; + + /** + * Processes one option. If the specified option + * is supported by this file manager, it will read + * any necessary arguments to the option from the + * iterator and then return {@code true}. Otherwise, it + * will return {@code false}. + * + * @param option the option to process. + * @param remaining the remaining arguments/options. + * @return true if the option was handled. + * @throws IllegalArgumentException if the option is used incorrectly. + * @throws IllegalStateException if the file manager has been closed and can't be reopened. + */ + boolean handleOption(String option, Iterator<String> remaining); + + /** + * Returns {@code true} if the specified location + * is known to this file manager. + * + * @param location the location to check. + * @return true if the location is known. + */ + boolean hasLocation(Location location); + + /** + * Infers a binary name for the given file object, + * based on its location. The returned name may + * not be a valid binary name, according to the + * Java language specification. + * + * @param location the location to use as a basis. + * @param file the file object. + * @return a binary name or {@code null} if the file + * object is not found in the given location. + * @throws IllegalStateException if the file manager has been closed and can't be reopened. + */ + String inferBinaryName(Location location, JavaFileObject file); + + /** + * Returns true if the two given file objects represent + * the same file. + * + * @param x the first object. + * @param y the second object. + * @return true if {@code x} and {@code y} represent the + * same file. + * @throws IllegalArgumentException if either {@code x} or + * {@code y} were created by a foreign file manager and + * this file manager does not support such comparisons. + */ + boolean isSameFile(FileObject x, FileObject y); + + /** + * Lists all file objects matching the given criteria in the + * specified location. If {@code recurse} is true, the list + * will include those found in subpackages. Note that a file + * manager may not return a {@code null} list or throw an + * exception if the location is unknown. + * + * @param location the location to search. + * @param pkg the name of the package to search. + * @param kinds the kinds of object to return. + * @param recurse {@code true} if subpackages should be searched. + * @return an {@link Iterable} over the matching file object. + * @throws IOException if an I/O error occurs, or the file manager + * has been closed and can't be reopened. + * @throws IllegalStateException if the file manager has been closed and can't be reopened. + */ + Iterable<JavaFileObject> list(Location location, String pkg, + Set<JavaFileObject.Kind> kinds, boolean recurse) + throws IOException; + +} diff --git a/javax/tools/JavaFileObject.java b/javax/tools/JavaFileObject.java new file mode 100644 index 000000000..e8293316a --- /dev/null +++ b/javax/tools/JavaFileObject.java @@ -0,0 +1,125 @@ +/* JavaFileObject.java -- Abstraction for working with class & source files. + Copyright (C) 2012 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package javax.tools; + +import javax.lang.model.element.Modifier; +import javax.lang.model.element.NestingKind; + +/** + * Abstraction for tools working with source and class files. + * All methods may throw a {@link SecurityException}. All + * methods may throw a {@link NullPointerException} if supplied + * with a {@code null} argument, unless otherwise specified. + * + * @author Andrew John Hughes (gnu_andrew@member.fsf.org) + * @since 1.6 + */ +public interface JavaFileObject + extends FileObject +{ + + /** + * Kinds of Java files. + * + * @author Andrew John Hughes (gnu_andrew@member.fsf.org) + * @since 1.6 + */ + public enum Kind + { + /** Java class files, which end in {@code ".class"} */ CLASS (".class"), + /** Web pages, which end in {@code "*.html"} */ HTML (".html"), + /** Any other kind of file. */ OTHER (""), + /** Java source files, which end in {@code ".java"} */ SOURCE (".java"); + + /** + * The file extension usually used for files of this + * type. If there is no conventional extension for + * this type, this field is set to the empty string, + * {@code ""}. + */ + public final String extension; + + Kind(String extension) + { + this.extension = extension; + } + + } + + /** + * Returns the kind of this file object. + * + * @return the kind. + */ + Kind getKind(); + + /** + * Checks if this file object represents the specified + * kind of file for the supplied type. The class name is + * a simple name i.e. not qualified. + * + * @param simpleName the simple name of the class. + * @param kind the kind of file. + * @return true if this object matches the type specified. + */ + boolean isNameCompatible(String simpleName, Kind kind); + + /** + * Provides a hint about the access level of this class, based + * on its modifiers. If the access level is unknown, or this + * file object does not represent a class file, {@code null} + * is returned. + * + * @return the access level. + */ + Modifier getAccessLevel(); + + /** + * Provides a hint about the nesting level of this class. + * It may return {@link NestingKind#MEMBER} instead of + * {@link NestingKind#LOCAL} or {@link NestingKind#ANONYMOUS} + * if it can not determine the exact type of nesting. If + * the nesting level is completely unknown, or this file + * object does not represent a class file, {@code null} + * is returned. + * + * @return the nesting level. + */ + NestingKind getNestingKind(); + +} diff --git a/javax/tools/OptionChecker.java b/javax/tools/OptionChecker.java new file mode 100644 index 000000000..13475d01f --- /dev/null +++ b/javax/tools/OptionChecker.java @@ -0,0 +1,59 @@ +/* OptionChecker.java -- Interface for recognising options. + Copyright (C) 2012 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package javax.tools; + +/** + * Interface for recognising options. + * + * @author Andrew John Hughes (gnu_andrew@member.fsf.org) + * @since 1.6 + */ +public interface OptionChecker +{ + + /** + * Determines if the specified option is supported or not, + * and, if so, how many arguments it takes. + * + * @param option the name of the option. + * @return the number of arguments the option takes, or + * -1 if the option is not supported. + */ + int isSupportedOption(String option); + +} diff --git a/javax/tools/SimpleJavaFileObject.java b/javax/tools/SimpleJavaFileObject.java new file mode 100644 index 000000000..452858f12 --- /dev/null +++ b/javax/tools/SimpleJavaFileObject.java @@ -0,0 +1,281 @@ +/* SimpleJavaFileObject.java -- Simple implementation of JavaFileObject. + Copyright (C) 2012 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package javax.tools; + +import java.io.InputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Reader; +import java.io.StringReader; +import java.io.Writer; + +import java.net.URI; + +import javax.lang.model.element.Modifier; +import javax.lang.model.element.NestingKind; + +/** + * Provides a simple implementation of many of the + * {@link JavaFileObject} methods, thus giving a useful basis + * for a subclass to complete the work. + * + * @author Andrew John Hughes (gnu_andrew@member.fsf.org) + * @since 1.6 + */ +public class SimpleJavaFileObject + implements JavaFileObject +{ + + /** + * The kind of this file object. + */ + protected Kind kind; + + /** + * The URI of this file object. + */ + protected URI uri; + + /** + * Constructs a new {@link SimpleJavaFileObject} + * of the specified kind using the supplied URI. + * + * @param uri the URI of this file object. + * @param kind the kind of this file object. + */ + protected SimpleJavaFileObject(URI uri, Kind kind) + { + this.uri = uri; + this.kind = kind; + } + + /** + * This implementation does nothing and always + * returns {@code false}. + * + * @return false. + */ + @Override + public boolean delete() + { + return false; + } + + /** + * This implementation throws {@link UnsupportedOperationException} + * and should be overridden in the subclass. + * + * @param ignoreEncodingErrors whether or not to ignore + * any encoding errors that + * occur. + * @return a character sequence if available. Otherwise {@code null}. + * @throws IOException if an I/O error occurs. + */ + @Override + public CharSequence getCharContent(boolean ignoreEncodingErrors) + throws IOException + { + throw new UnsupportedOperationException("getCharContent not implemented."); + } + + /** + * Returns a user-friendly name for this object. + * This implementation simply uses the path of the + * URI. + * + * @return a user-friendly name. + */ + @Override + public String getName() + { + return uri.getPath(); + } + + /** + * Returns the kind of this file object. + * + * @return {@code this.kind} + */ + @Override + public Kind getKind() + { + return kind; + } + + /** + * This implementation does nothing and always + * returns {@code 0L}. + * + * @return 0L. + */ + @Override + public long getLastModified() + { + return 0L; + } + + /** + * <p>Returns true if the given kind is the same as the + * kind of this file object, and the path of this + * object's URI is equal to the supplied name followed + * by the extension specified by its kind.</p> + * <p>This method uses {@link #getKind()} and + * {@link #toUri()} to retrieve the kind and URI + * respectively, so subclasses may override the values + * used.</p> + * + * @param simpleName the simple name to compare. + * @param kind the kind to compare. + * @return true if the above criteria are met. + */ + @Override + public boolean isNameCompatible(String simpleName, Kind kind) + { + return getKind().equals(kind) && + toUri().getPath().equals(simpleName + kind.extension); + } + + /** + * This implementation throws {@link UnsupportedOperationException} + * and should be overridden in the subclass. + * + * @return an input stream. + * @throws IOException if an I/O error occurs. + */ + @Override + public InputStream openInputStream() + throws IOException + { + throw new UnsupportedOperationException("openInputStream not implemented."); + } + + /** + * This implementation throws {@link UnsupportedOperationException} + * and should be overridden in the subclass. + * + * @return an output stream. + * @throws IOException if an I/O error occurs. + */ + @Override + public OutputStream openOutputStream() + throws IOException + { + throw new UnsupportedOperationException("openOutputStream not implemented."); + } + + /** + * Wraps the result of {@link #getCharContent(boolean)} + * in a {@link Reader}. + * + * @param ignoreEncodingErrors whether or not to ignore + * any encoding errors that + * occur. + * @return a Reader instance wrapping the result of + * {@code getCharContent(ignoreEncodingErrors)} + * @throws IllegalStateException if this file was opened for writing and + * does not support reading. + * @throws UnsupportedOperationException if this kind of file does not support + * character access. + * @throws IOException if an I/O error occurs. + */ + @Override + public Reader openReader(boolean ignoreEncodingErrors) + throws IOException + { + CharSequence content = getCharContent(ignoreEncodingErrors); + if (content == null) + throw new IllegalStateException("No character content available."); + return new StringReader(content.toString()); + } + + /** + * Wraps the result of {@link #openOutputStream()} + * in a {@link Writer}. + * + * @return a Writer instance wrapping the result + * of {@code openOutputStream()}. + * @throws IllegalStateException if this file was opened for reading and + * does not support writing. + * @throws UnsupportedOperationException if this kind of file does not support + * character access. + * @throws IOException if an I/O error occurs. + */ + @Override + public Writer openWriter() + throws IOException + { + return new OutputStreamWriter(openOutputStream()); + } + + /** + * Returns a {@link URI} identifying this object. + * + * @return a URI. + */ + @Override + public URI toUri() + { + return uri; + } + + /** + * This implementation does nothing and always + * returns {@code null}. + * + * @return null. + */ + @Override + public Modifier getAccessLevel() + { + return null; + } + + /** + * This implementation does nothing and always + * returns {@code null}. + * + * @return null. + */ + @Override + public NestingKind getNestingKind() + { + return null; + } + +} diff --git a/javax/tools/StandardJavaFileManager.java b/javax/tools/StandardJavaFileManager.java new file mode 100644 index 000000000..f938469a5 --- /dev/null +++ b/javax/tools/StandardJavaFileManager.java @@ -0,0 +1,166 @@ +/* StandardJavaFileManager.java -- File manager for regular files. + Copyright (C) 2012 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package javax.tools; + +import java.io.File; +import java.io.IOException; + +/** + * <p>File manager for working with regular files or entries + * within file system containers, such as zip files. An + * implementation of this interface is usually retrieved using the + * {@link javax.tools.JavaCompiler#getStandardFileManager(DiagnosticListener,Locale,Charset)} + * of {@link javax.tools.JavaCompiler}, an instance of which + * is, in turn, returned from + * {@link javax.tools.ToolProvider#getSystemJavaCompiler()}.</p> + * <p>Regular file objects returned from a file manager implementing this + * interface must meet the following criteria:</p> + * <ul> + * <li>{@link FileObject#delete()} is equivalent to + * {@link java.io.File#delete()}.</li> + * <li>{@link FileObject#getLastModified} is equivalent to + * {@link java.io.File#lastModified}.</li> + * <li>Methods for reading input ({@link FileObject#getCharContent(boolean)}, + * {@link FileObject#openInputStream()}, + * {@link FileObject#openReader(boolean)}) must succeed, + * ignoring character encoding issues, if + * {@code new FileInputStream(new File(fileObj.toUri()))}.</li> + * <li>Methods for writing output ({@link FileObject#openOutputStream()}, + * {@link FileObject.openWriter()}) must succeed, + * ignoring character encoding issues, if + * {@code new FileOutputStream(new File(fileObj.toUri()))}.</li> + * <li><p>The URI returned from {@link FileObject#toUri()} must + * be have a schema and a normalised path component, which can + * be resolved without regard to context (i.e. it must be absolute, + * not relative to a particular directory). For example, the + * URI {@code file:///home/bob/sources} would be valid, while + * {@code file:sources} (not absolute) or + * {@code file:///home/bob/lib/../sources} (not normalised) would + * not.</li> + * </ul> + * <p>The file names used by the file objects need not be canonical.</p> + * + * @author Andrew John Hughes (gnu_andrew@member.fsf.org) + * @since 1.6 + */ +public interface StandardJavaFileManager + extends JavaFileManager +{ + + /** + * Returns file objects representing the given files. This is + * a convenience method equivalent to calling + * {@code getJavaFileObjectsFromFiles(Arrays.asList(files))}. + * + * @param files the files to retrieve objects for. + * @return a list of file objects matching the specified array. + * @throws IllegalArgumentException if the array contains a directory. + * @throws NullPointerException if one of the array elements is {@code null}. + */ + Iterable<? extends JavaFileObject> getJavaFileObjects(File... files); + + /** + * Returns file objects representing the given file names. This is + * a convenience method equivalent to calling + * {@code getJavaFileObjectsFromString(Arrays.asList(fileNames))}. + * + * @param fileNames the file names to retrieve objects for. + * @return a list of file objects matching the specified array. + * @throws IllegalArgumentException if the array contains a directory. + * @throws NullPointerException if one of the array elements is {@code null}. + */ + Iterable<? extends JavaFileObject> getJavaFileObjects(String... files); + + /** + * Returns file objects representing the given files. + * + * @param files the files to retrieve objects for. + * @return a list of file objects. + * @throws IllegalArgumentException if the array contains a directory. + */ + Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles(Iterable<? extends File> files); + + /** + * Returns file objects representing the given file names. + * + * @param fileNames the file names to retrieve objects for. + * @return a list of file objects. + * @throws IllegalArgumentException if the array contains a directory. + */ + Iterable<? extends JavaFileObject> getJavaFileObjectsFromStrings(Iterable<String> files); + + /** + * Returns the list of paths associated with the specified location or + * {@code null} if there is no such mapping. Output locations only + * return a single path, representing the output directory. + * + * @param location the location whose path should be retrieved. + * @return the associated list of paths. + * @see #setLocation(Location,Iterable) + */ + Iterable<? extends File> getLocation(Location location); + + /** + * Returns true if the two file objects represent the same + * file. + * + * @param objA the first file object to compare. + * @param objB the second file object to compare. + * @return true if the two objects represent the same file. + * @throws IllegalArgumentException if either object was created + * by a different implementation. + */ + boolean isSameFile(FileObject objA, FileObject objB); + + /** + * Associates the given list of paths with the specified location, + * discarding any previous mapping. If the list is {@code null}, + * the mapping is reset to the default. + * + * @param location the location which will refer to this list of paths. + * @param paths a list of paths to associate with the location, or + * {@code null} to trigger a reset to the default list. + * @throws IllegalArgumentException if the location is an output location + * and the list contains more than one path. + * @throws IOException if the location is an output location but + * the list contains a path that isn't a directory. + * @see #getLocation(Location) + */ + void setLocation(Location location, Iterable<? extends File> paths) + throws IOException; +} diff --git a/javax/tools/StandardLocation.java b/javax/tools/StandardLocation.java new file mode 100644 index 000000000..bb4b70299 --- /dev/null +++ b/javax/tools/StandardLocation.java @@ -0,0 +1,119 @@ +/* StandardLocation.java -- Enumeration of standard file locations. + Copyright (C) 2012 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package javax.tools; + +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.ConcurrentHashMap; + +import static javax.tools.JavaFileManager.Location; + +/** + * Enumeration of standard locations for storing + * {@link FileObject}s. + * + * @author Andrew John Hughes (gnu_andrew@member.fsf.org) + * @since 1.6 + */ +public enum StandardLocation + implements Location +{ + /** Location where annotation processors are found. */ + ANNOTATION_PROCESSOR_PATH { @Override public boolean isOutputLocation() { return false; } }, + /** Location to write class files to. */ + CLASS_OUTPUT { @Override public boolean isOutputLocation() { return true; } }, + /** Location where class files are found. */ + CLASS_PATH { @Override public boolean isOutputLocation() { return false; } }, + /** Location where platform class files are found. */ + PLATFORM_CLASS_PATH { @Override public boolean isOutputLocation() { return false; } }, + /** Location to write source files to. */ + SOURCE_OUTPUT { @Override public boolean isOutputLocation() { return true; } }, + /** Location where source files are found. */ + SOURCE_PATH { @Override public boolean isOutputLocation() { return false; } }; + + private static final ConcurrentMap<String,Location> locCache = + new ConcurrentHashMap<String,Location>(); + + static + { + for (StandardLocation loc : values()) + locCache.put(loc.name(), loc); + } + + /** + * Returns the name of the location. This is simply + * the enum constant. + * + * @return the name of the location. + */ + @Override + public String getName() + { + return name(); + } + + /** + * Returns an instance of {@link JavaFileManager.Location} + * for the given name. If the name is one of the standard + * names, the enumeration constant is returned. Otherwise, + * a new instance is generated. For location names {@code x} + * and {@code y}, {@code locationFor(x) == locationFor(y)} + * if, and only if, {@code x.equals(y)}. The returned location + * will only be an output location if the name ends with + * the suffix {@code "_OUTPUT"}. + * + * @param name the name of the location. + * @return the location. + */ + public static Location locationFor(String name) + { + final String locName = name; + Location loc = locCache.get(name); + if (loc == null) + { + loc = new Location() { + public String getName() { return locName; } + public boolean isOutputLocation() { return locName.endsWith("_OUTPUT"); } + }; + Location mapLoc = locCache.putIfAbsent(name, loc); + if (mapLoc != null) // Someone got there first + loc = mapLoc; + } + return loc; + } + +} diff --git a/javax/tools/Tool.java b/javax/tools/Tool.java new file mode 100644 index 000000000..a1b707029 --- /dev/null +++ b/javax/tools/Tool.java @@ -0,0 +1,84 @@ +/* Tool.java -- Interface for programatically-invokable tools. + Copyright (C) 2012 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + +package javax.tools; + +import java.io.InputStream; +import java.io.OutputStream; + +import java.util.Set; + +import javax.lang.model.SourceVersion; + +/** + * Interface for tools than be invoked from a program. Tools + * can be located using {@link java.util.ServiceLoader#load(Class)}. + * + * @author Andrew John Hughes (gnu_andrew@member.fsf.org) + * @since 1.6 + */ +public interface Tool +{ + + /** + * Returns the set of source language versions that are supported + * by this tool. + * + * @return the set of supported source code versions. + */ + Set<SourceVersion> getSourceVersions(); + + /** + * Invokes the tool using the specified input/output streams and + * arguments, returning the same exit code it would if called from + * the command line. Traditionally, zero indicates success and non-zero + * is used for errors. Diagnostics from the tool will be written + * to {@code out} or {@code err} in a way specific to the tool. + * + * @param in the standard input for the tool or {@code null} to use + * {@code System.in}. + * @param out the standard output for the tool or {@code null} to use + * {@code System.out} + * @param err the standard error stream for the tool or {@code null} + * to use {@code System.err}. + * @param args the arguments to pass to the tool. + * @return the return code from the tool. + * @throws NullPointerException if any argument is {@code null}. + */ + int run(InputStream in, OutputStream out, OutputStream err, + String... arguments); +} |