duke@1: /* ohair@798: * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.apt.mirror.apt; duke@1: duke@1: duke@1: import java.io.*; duke@1: import java.util.Collection; duke@1: import java.util.EnumMap; duke@1: import java.util.HashSet; duke@1: import java.util.Set; duke@1: duke@1: import com.sun.mirror.apt.Filer; duke@1: import com.sun.tools.apt.mirror.declaration.DeclarationMaker; duke@1: import com.sun.tools.javac.util.Context; duke@1: import com.sun.tools.javac.util.Options; duke@1: import com.sun.tools.javac.util.Position; duke@1: import com.sun.tools.apt.util.Bark; duke@1: duke@1: import static com.sun.mirror.apt.Filer.Location.*; duke@1: duke@1: duke@1: /** duke@1: * Implementation of Filer. duke@1: */ darcy@331: @SuppressWarnings("deprecation") duke@1: public class FilerImpl implements Filer { duke@1: /* duke@1: * The Filer class must maintain a number of constraints. First, duke@1: * multiple attempts to open the same path within the same duke@1: * invocation of apt results in an IOException being thrown. For duke@1: * example, trying to open the same source file twice: duke@1: * duke@1: * createSourceFile("foo.Bar") duke@1: * ... duke@1: * createSourceFile("foo.Bar") duke@1: * duke@1: * is disallowed as is opening a text file that happens to have duke@1: * the same name as a source file: duke@1: * duke@1: * createSourceFile("foo.Bar") duke@1: * ... duke@1: * createTextFile(SOURCE_TREE, "foo", new File("Bar"), null) duke@1: * duke@1: * Additionally, creating a source file that corresponds to an duke@1: * already created class file (or vice versa) generates at least a duke@1: * warning. This is an error if -XclassesAsDecls is being used duke@1: * since you can't create the same type twice. However, if the duke@1: * Filer is used to create a text file named *.java that happens duke@1: * to correspond to an existing class file, a warning is *not* duke@1: * generated. Similarly, a warning is not generated for a binary duke@1: * file named *.class and an existing source file. duke@1: * duke@1: * The reason for this difference is that source files and class duke@1: * files are registered with apt and can get passed on as duke@1: * declarations to the next round of processing. Files that are duke@1: * just named *.java and *.class are not processed in that manner; duke@1: * although having extra source files and class files on the duke@1: * source path and class path can alter the behavior of the tool duke@1: * and any final compile. duke@1: */ duke@1: duke@1: private enum FileKind { duke@1: SOURCE { duke@1: void register(File file, String name, FilerImpl that) throws IOException { duke@1: // Check for corresponding class file duke@1: if (that.filesCreated.contains(new File(that.locations.get(CLASS_TREE), duke@1: that.nameToPath(name, ".class")))) { duke@1: duke@1: that.bark.aptWarning("CorrespondingClassFile", name); duke@1: if (that.opts.get("-XclassesAsDecls") != null) duke@1: throw new IOException(); duke@1: } duke@1: that.sourceFileNames.add(file.getPath()); duke@1: } duke@1: }, duke@1: duke@1: CLASS { duke@1: void register(File file, String name, FilerImpl that) throws IOException { duke@1: if (that.filesCreated.contains(new File(that.locations.get(SOURCE_TREE), duke@1: that.nameToPath(name, ".java")))) { duke@1: that.bark.aptWarning("CorrespondingSourceFile", name); duke@1: if (that.opts.get("-XclassesAsDecls") != null) duke@1: throw new IOException(); duke@1: } duke@1: // Track the binary name instead of the filesystem location duke@1: that.classFileNames.add(name); duke@1: } duke@1: }, duke@1: duke@1: OTHER { duke@1: // Nothing special to do duke@1: void register(File file, String name, FilerImpl that) throws IOException {} duke@1: }; duke@1: duke@1: abstract void register(File file, String name, FilerImpl that) throws IOException; duke@1: } duke@1: duke@1: private final Options opts; duke@1: private final DeclarationMaker declMaker; jjg@789: private final com.sun.tools.apt.main.AptJavaCompiler comp; duke@1: duke@1: // Platform's default encoding duke@1: private final static String DEFAULT_ENCODING = duke@1: new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding(); duke@1: duke@1: private String encoding; // name of charset used for source files duke@1: duke@1: private final EnumMap locations; // where new files go duke@1: duke@1: duke@1: private static final Context.Key filerKey = duke@1: new Context.Key(); duke@1: duke@1: // Set of files opened. duke@1: private Collection wc; duke@1: duke@1: private Bark bark; duke@1: duke@1: // All created files. duke@1: private final Set filesCreated; duke@1: duke@1: // Names of newly created source files duke@1: private HashSet sourceFileNames = new HashSet(); duke@1: duke@1: // Names of newly created class files duke@1: private HashSet classFileNames = new HashSet(); duke@1: duke@1: private boolean roundOver; duke@1: duke@1: public static FilerImpl instance(Context context) { duke@1: FilerImpl instance = context.get(filerKey); duke@1: if (instance == null) { duke@1: instance = new FilerImpl(context); duke@1: } duke@1: return instance; duke@1: } duke@1: duke@1: // flush all output streams; duke@1: public void flush() { duke@1: for(Flushable opendedFile: wc) { duke@1: try { duke@1: opendedFile.flush(); duke@1: if (opendedFile instanceof FileOutputStream) { duke@1: try { duke@1: ((FileOutputStream) opendedFile).getFD().sync() ; duke@1: } catch (java.io.SyncFailedException sfe) {} duke@1: } duke@1: } catch (IOException e) { } duke@1: } duke@1: } duke@1: duke@1: private FilerImpl(Context context) { duke@1: context.put(filerKey, this); duke@1: opts = Options.instance(context); duke@1: declMaker = DeclarationMaker.instance(context); duke@1: bark = Bark.instance(context); jjg@789: comp = com.sun.tools.apt.main.AptJavaCompiler.instance(context); duke@1: roundOver = false; duke@1: this.filesCreated = comp.getAggregateGenFiles(); duke@1: duke@1: // Encoding duke@1: encoding = opts.get("-encoding"); duke@1: if (encoding == null) { duke@1: encoding = DEFAULT_ENCODING; duke@1: } duke@1: duke@1: wc = new HashSet(); duke@1: duke@1: // Locations duke@1: locations = new EnumMap(Location.class); duke@1: String s = opts.get("-s"); // location for new source files duke@1: String d = opts.get("-d"); // location for new class files duke@1: locations.put(SOURCE_TREE, new File(s != null ? s : ".")); duke@1: locations.put(CLASS_TREE, new File(d != null ? d : ".")); duke@1: } duke@1: duke@1: duke@1: /** duke@1: * {@inheritDoc} duke@1: */ duke@1: public PrintWriter createSourceFile(String name) throws IOException { duke@1: String pathname = nameToPath(name, ".java"); duke@1: File file = new File(locations.get(SOURCE_TREE), duke@1: pathname); duke@1: PrintWriter pw = getPrintWriter(file, encoding, name, FileKind.SOURCE); duke@1: return pw; duke@1: } duke@1: duke@1: /** duke@1: * {@inheritDoc} duke@1: */ duke@1: public OutputStream createClassFile(String name) throws IOException { duke@1: String pathname = nameToPath(name, ".class"); duke@1: File file = new File(locations.get(CLASS_TREE), duke@1: pathname); duke@1: OutputStream os = getOutputStream(file, name, FileKind.CLASS); duke@1: return os; duke@1: } duke@1: duke@1: /** duke@1: * {@inheritDoc} duke@1: */ duke@1: public PrintWriter createTextFile(Location loc, duke@1: String pkg, duke@1: File relPath, duke@1: String charsetName) throws IOException { duke@1: File file = (pkg.length() == 0) duke@1: ? relPath duke@1: : new File(nameToPath(pkg), relPath.getPath()); duke@1: if (charsetName == null) { duke@1: charsetName = encoding; duke@1: } duke@1: return getPrintWriter(loc, file.getPath(), charsetName, null, FileKind.OTHER); duke@1: } duke@1: duke@1: /** duke@1: * {@inheritDoc} duke@1: */ duke@1: public OutputStream createBinaryFile(Location loc, duke@1: String pkg, duke@1: File relPath) throws IOException { duke@1: File file = (pkg.length() == 0) duke@1: ? relPath duke@1: : new File(nameToPath(pkg), relPath.getPath()); duke@1: return getOutputStream(loc, file.getPath(), null, FileKind.OTHER); duke@1: } duke@1: duke@1: duke@1: /** duke@1: * Converts the canonical name of a top-level type or package to a duke@1: * pathname. Suffix is ".java" or ".class" or "". duke@1: */ duke@1: private String nameToPath(String name, String suffix) throws IOException { duke@1: if (!DeclarationMaker.isJavaIdentifier(name.replace('.', '_'))) { duke@1: bark.aptWarning("IllegalFileName", name); duke@1: throw new IOException(); duke@1: } duke@1: return name.replace('.', File.separatorChar) + suffix; duke@1: } duke@1: duke@1: private String nameToPath(String name) throws IOException { duke@1: return nameToPath(name, ""); duke@1: } duke@1: duke@1: /** duke@1: * Returns a writer for a text file given its location, its duke@1: * pathname relative to that location, and its encoding. duke@1: */ duke@1: private PrintWriter getPrintWriter(Location loc, String pathname, duke@1: String encoding, String name, FileKind kind) throws IOException { duke@1: File file = new File(locations.get(loc), pathname); duke@1: return getPrintWriter(file, encoding, name, kind); duke@1: } duke@1: duke@1: /** duke@1: * Returns a writer for a text file given its encoding. duke@1: */ duke@1: private PrintWriter getPrintWriter(File file, duke@1: String encoding, String name, FileKind kind) throws IOException { duke@1: prepareFile(file, name, kind); duke@1: PrintWriter pw = duke@1: new PrintWriter( duke@1: new BufferedWriter( duke@1: new OutputStreamWriter(new FileOutputStream(file), duke@1: encoding))); duke@1: wc.add(pw); duke@1: return pw; duke@1: } duke@1: duke@1: /** duke@1: * Returns an output stream for a binary file given its location duke@1: * and its pathname relative to that location. duke@1: */ duke@1: private OutputStream getOutputStream(Location loc, String pathname, String name, FileKind kind) duke@1: throws IOException { duke@1: File file = new File(locations.get(loc), pathname); duke@1: return getOutputStream(file, name, kind); duke@1: } duke@1: duke@1: private OutputStream getOutputStream(File file, String name, FileKind kind) throws IOException { duke@1: prepareFile(file, name, kind); duke@1: OutputStream os = new FileOutputStream(file); duke@1: wc.add(os); duke@1: return os; duke@1: duke@1: } duke@1: duke@1: public Set getSourceFileNames() { duke@1: return sourceFileNames; duke@1: } duke@1: duke@1: public Set getClassFileNames() { duke@1: return classFileNames; duke@1: } duke@1: duke@1: public void roundOver() { duke@1: roundOver = true; duke@1: } duke@1: duke@1: /** duke@1: * Checks that the file has not already been created during this duke@1: * invocation. If not, creates intermediate directories, and duke@1: * deletes the file if it already exists. duke@1: */ duke@1: private void prepareFile(File file, String name, FileKind kind) throws IOException { duke@1: if (roundOver) { duke@1: bark.aptWarning("NoNewFilesAfterRound", file.toString()); duke@1: throw new IOException(); duke@1: } duke@1: duke@1: if (filesCreated.contains(file)) { duke@1: bark.aptWarning("FileReopening", file.toString()); duke@1: throw new IOException(); duke@1: } else { duke@1: if (file.exists()) { duke@1: file.delete(); duke@1: } else { duke@1: File parent = file.getParentFile(); duke@1: if (parent != null && !parent.exists()) { duke@1: if(!parent.mkdirs()) { duke@1: bark.aptWarning("BadParentDirectory", file.toString()); duke@1: throw new IOException(); duke@1: } duke@1: } duke@1: } duke@1: duke@1: kind.register(file, name, this); duke@1: filesCreated.add(file); duke@1: } duke@1: } duke@1: }