⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 javacfiler.java

📁 是一款用JAVA 编写的编译器 具有很强的编译功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Sun designates this * particular file as subject to the "Classpath" exception as provided * by Sun in the LICENSE file that accompanied this code. * * This code 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 * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * CA 95054 USA or visit www.sun.com if you need additional information or * have any questions. */package com.sun.tools.javac.processing;import com.sun.tools.javac.util.*;import javax.annotation.processing.*;import javax.lang.model.SourceVersion;import javax.lang.model.element.NestingKind;import javax.lang.model.element.Modifier;import javax.lang.model.element.Element;import java.util.*;import java.io.Closeable;import java.io.File;import java.io.InputStream;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.FilterOutputStream;import java.io.Reader;import java.io.Writer;import java.io.FilterWriter;import java.io.PrintWriter;import java.io.IOException;import java.net.URI;import javax.tools.FileObject;import javax.tools.*;import static java.util.Collections.*;import javax.tools.JavaFileManager.Location;import static javax.tools.StandardLocation.SOURCE_OUTPUT;import static javax.tools.StandardLocation.CLASS_OUTPUT;/** * The FilerImplementation class must maintain a number of * constraints.  First, multiple attempts to open the same path within * the same invocation of the tool results in an IOException being * thrown.  For example, trying to open the same source file twice: * * <pre> * createSourceFile("foo.Bar") * ... * createSourceFile("foo.Bar") * </pre> * * is disallowed as is opening a text file that happens to have * the same name as a source file: * * <pre> * createSourceFile("foo.Bar") * ... * createTextFile(SOURCE_TREE, "foo", new File("Bar"), null) * </pre> * * <p>Additionally, creating a source file that corresponds to an * already created class file (or vice versa) also results in an * IOException since each type can only be created once.  However, if * the Filer is used to create a text file named *.java that happens * to correspond to an existing class file, a warning is *not* * generated.  Similarly, a warning is not generated for a binary file * named *.class and an existing source file. * * <p>The reason for this difference is that source files and class * files are registered with the tool and can get passed on as * declarations to the next round of processing.  Files that are just * named *.java and *.class are not processed in that manner; although * having extra source files and class files on the source path and * class path can alter the behavior of the tool and any final * compile. * * <p><b>This is NOT part of any API supported by Sun Microsystems. * If you write code that depends on this, you do so at your own risk. * This code and its internal interfaces are subject to change or * deletion without notice.</b> */public class JavacFiler implements Filer, Closeable {    // TODO: Implement different transaction model for updating the    // Filer's record keeping on file close.    private static final String ALREADY_OPENED =	"Output stream or writer has already been opened.";    private static final String NOT_FOR_READING =	"FileObject was not opened for reading.";    private static final String NOT_FOR_WRITING =	"FileObject was not opened for writing.";    /**     * Wrap a JavaFileObject to manage writing by the Filer.     */    private class FilerOutputFileObject extends ForwardingFileObject<FileObject> {	private boolean opened = false;	private String name;	FilerOutputFileObject(String name, FileObject fileObject) {	    super(fileObject);	    this.name = name;	}	@Override	public synchronized OutputStream openOutputStream() throws IOException {	    if (opened)		throw new IOException(ALREADY_OPENED);	    opened = true;	    return new FilerOutputStream(name, fileObject);	}	@Override	public synchronized Writer openWriter() throws IOException {	    if (opened)		throw new IOException(ALREADY_OPENED);	    opened = true;	    return new FilerWriter(name, fileObject);	}	// Three anti-literacy methods	@Override	public InputStream openInputStream() throws IOException {	    throw new IllegalStateException(NOT_FOR_READING);	}	@Override	public Reader openReader(boolean ignoreEncodingErrors) throws IOException {	    throw new IllegalStateException(NOT_FOR_READING);	}	@Override	public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {	    throw new IllegalStateException(NOT_FOR_READING);	}	@Override	public boolean delete() {	    return false;	}    }    private class FilerOutputJavaFileObject extends FilerOutputFileObject implements JavaFileObject {	private final JavaFileObject javaFileObject;	FilerOutputJavaFileObject(String name, JavaFileObject javaFileObject) {	    super(name, javaFileObject);	    this.javaFileObject = javaFileObject;	}	public JavaFileObject.Kind getKind() {	    return javaFileObject.getKind();	}	public boolean isNameCompatible(String simpleName,					JavaFileObject.Kind kind) {	    return javaFileObject.isNameCompatible(simpleName, kind);	}		public NestingKind getNestingKind() {	    return javaFileObject.getNestingKind();	}	public Modifier getAccessLevel() {	    return javaFileObject.getAccessLevel();	}    }    /**     * Wrap a JavaFileObject to manage reading by the Filer.     */    private class FilerInputFileObject extends ForwardingFileObject<FileObject> {	FilerInputFileObject(FileObject fileObject) {	    super(fileObject);	}	@Override	public OutputStream openOutputStream() throws IOException {	    throw new IllegalStateException(NOT_FOR_WRITING);	}	@Override	public Writer openWriter() throws IOException {	    throw new IllegalStateException(NOT_FOR_WRITING);	}	@Override	public boolean delete() {	    return false;	}    }    private class FilerInputJavaFileObject extends FilerInputFileObject implements JavaFileObject {	private final JavaFileObject javaFileObject;	FilerInputJavaFileObject(JavaFileObject javaFileObject) {	    super(javaFileObject);	    this.javaFileObject = javaFileObject;	}	public JavaFileObject.Kind getKind() {	    return javaFileObject.getKind();	}	public boolean isNameCompatible(String simpleName,					JavaFileObject.Kind kind) {	    return javaFileObject.isNameCompatible(simpleName, kind);	}		public NestingKind getNestingKind() {	    return javaFileObject.getNestingKind();	}	public Modifier getAccessLevel() {	    return javaFileObject.getAccessLevel();	}    }    /**     * Wrap a {@code OutputStream} returned from the {@code     * JavaFileManager} to properly register source or class files     * when they are closed.     */    private class FilerOutputStream extends FilterOutputStream {        String typeName;        FileObject fileObject;	boolean closed = false;	        /**         * @param typeName name of class or {@code null} if just a         * binary file         */        FilerOutputStream(String typeName, FileObject fileObject) throws IOException {            super(fileObject.openOutputStream());            this.typeName = typeName;            this.fileObject = fileObject;        }        public synchronized void close() throws IOException {	    if (!closed) {		closed = true;		/*		 * If an IOException occurs when closing the underlying		 * stream, still try to process the file.		 */		closeFileObject(typeName, fileObject);		out.close();	    }        }    }    /**     * Wrap a {@code Writer} returned from the {@code JavaFileManager}     * to properly register source or class files when they are     * closed.     */    private class FilerWriter extends FilterWriter {	String typeName;        FileObject fileObject;	boolean closed = false;        /**         * @param fileObject the fileObject to be written to         * @param typeName name of source file or {@code null} if just a         * text file         */        FilerWriter(String typeName, FileObject fileObject) throws IOException {            super(fileObject.openWriter());            this.typeName = typeName;            this.fileObject = fileObject;        }        public synchronized void close() throws IOException {	    if (!closed) {		closed = true;		/*		 * If an IOException occurs when closing the underlying		 * Writer, still try to process the file.		 */		closeFileObject(typeName, fileObject);		out.close();	    }        }    }    JavaFileManager fileManager;    Log log;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -