langtools/src/share/classes/com/sun/tools/apt/mirror/apt/FilerImpl.java
author darcy
Sun, 26 Jul 2009 21:27:11 -0700
changeset 3378 22011d9a9398
parent 10 06bc494ca11e
child 5520 86e4b9a9da40
permissions -rw-r--r--
6381698: Warn of decommissioning of apt Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2004-2005 Sun Microsystems, Inc.  All Rights Reserved.
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.apt.mirror.apt;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.util.Collection;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.util.EnumMap;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.util.HashSet;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.util.Set;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import com.sun.mirror.apt.Filer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import com.sun.tools.apt.mirror.declaration.DeclarationMaker;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import com.sun.tools.javac.util.Context;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import com.sun.tools.javac.util.Options;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import com.sun.tools.javac.util.Position;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import com.sun.tools.apt.util.Bark;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import static com.sun.mirror.apt.Filer.Location.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 * Implementation of Filer.
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 */
3378
22011d9a9398 6381698: Warn of decommissioning of apt
darcy
parents: 10
diff changeset
    48
@SuppressWarnings("deprecation")
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
public class FilerImpl implements Filer {
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    /*
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
     * The Filer class must maintain a number of constraints.  First,
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
     * multiple attempts to open the same path within the same
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
     * invocation of apt results in an IOException being thrown.  For
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
     * example, trying to open the same source file twice:
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
     * createSourceFile("foo.Bar")
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
     * ...
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
     * createSourceFile("foo.Bar")
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
     * is disallowed as is opening a text file that happens to have
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
     * the same name as a source file:
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     * createSourceFile("foo.Bar")
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     * ...
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
     * createTextFile(SOURCE_TREE, "foo", new File("Bar"), null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
     * Additionally, creating a source file that corresponds to an
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
     * already created class file (or vice versa) generates at least a
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
     * warning.  This is an error if -XclassesAsDecls is being used
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
     * since you can't create the same type twice.  However, if the
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     * Filer is used to create a text file named *.java that happens
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
     * to correspond to an existing class file, a warning is *not*
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     * generated.  Similarly, a warning is not generated for a binary
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     * file named *.class and an existing source file.
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
     * The reason for this difference is that source files and class
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
     * files are registered with apt and can get passed on as
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     * declarations to the next round of processing.  Files that are
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
     * just named *.java and *.class are not processed in that manner;
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
     * although having extra source files and class files on the
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
     * source path and class path can alter the behavior of the tool
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
     * and any final compile.
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    private enum FileKind {
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        SOURCE {
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
            void register(File file, String name, FilerImpl that) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
                // Check for corresponding class file
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
                if (that.filesCreated.contains(new File(that.locations.get(CLASS_TREE),
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
                                                        that.nameToPath(name, ".class")))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
                    that.bark.aptWarning("CorrespondingClassFile", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
                    if (that.opts.get("-XclassesAsDecls") != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
                        throw new IOException();
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
                that.sourceFileNames.add(file.getPath());
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        },
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        CLASS  {
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
            void register(File file, String name, FilerImpl that) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
                if (that.filesCreated.contains(new File(that.locations.get(SOURCE_TREE),
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
                                                        that.nameToPath(name, ".java")))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
                    that.bark.aptWarning("CorrespondingSourceFile", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
                    if (that.opts.get("-XclassesAsDecls") != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
                        throw new IOException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
                // Track the binary name instead of the filesystem location
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
                that.classFileNames.add(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        },
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        OTHER  {
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
            // Nothing special to do
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            void register(File file, String name, FilerImpl that) throws IOException {}
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        abstract void register(File file, String name, FilerImpl that) throws IOException;
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    private final Options opts;
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    private final DeclarationMaker declMaker;
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    private final com.sun.tools.apt.main.JavaCompiler comp;
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
    // Platform's default encoding
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    private final static String DEFAULT_ENCODING =
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
            new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding();
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    private String encoding;    // name of charset used for source files
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    private final EnumMap<Location, File> locations;    // where new files go
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    private static final Context.Key<FilerImpl> filerKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
            new Context.Key<FilerImpl>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
    // Set of files opened.
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
    private Collection<Flushable> wc;
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
    private Bark bark;
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
    // All created files.
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    private final Set<File> filesCreated;
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
    // Names of newly created source files
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
    private HashSet<String> sourceFileNames = new HashSet<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
    // Names of newly created class files
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
    private HashSet<String> classFileNames  = new HashSet<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
    private boolean roundOver;
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
    public static FilerImpl instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        FilerImpl instance = context.get(filerKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
        if (instance == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
            instance = new FilerImpl(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
    // flush all output streams;
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
    public void flush() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        for(Flushable opendedFile: wc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
                opendedFile.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
                if (opendedFile instanceof FileOutputStream) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
                    try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
                        ((FileOutputStream) opendedFile).getFD().sync() ;
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
                    } catch (java.io.SyncFailedException sfe) {}
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
            } catch (IOException e) { }
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
    private FilerImpl(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        context.put(filerKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
        opts = Options.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
        declMaker = DeclarationMaker.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
        bark = Bark.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        comp = com.sun.tools.apt.main.JavaCompiler.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        roundOver = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
        this.filesCreated = comp.getAggregateGenFiles();
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
        // Encoding
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        encoding = opts.get("-encoding");
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
        if (encoding == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
            encoding = DEFAULT_ENCODING;
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
        wc = new HashSet<Flushable>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
        // Locations
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
        locations = new EnumMap<Location, File>(Location.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
        String s = opts.get("-s");      // location for new source files
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
        String d = opts.get("-d");      // location for new class files
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
        locations.put(SOURCE_TREE, new File(s != null ? s : "."));
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
        locations.put(CLASS_TREE,  new File(d != null ? d : "."));
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
     * {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
    public PrintWriter createSourceFile(String name) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
        String pathname = nameToPath(name, ".java");
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
        File file = new File(locations.get(SOURCE_TREE),
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
                             pathname);
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
        PrintWriter pw = getPrintWriter(file, encoding, name, FileKind.SOURCE);
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
        return pw;
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
     * {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
    public OutputStream createClassFile(String name) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
        String pathname = nameToPath(name, ".class");
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
        File file = new File(locations.get(CLASS_TREE),
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
                             pathname);
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
        OutputStream os = getOutputStream(file, name, FileKind.CLASS);
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
        return os;
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
     * {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
    public PrintWriter createTextFile(Location loc,
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
                                      String pkg,
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
                                      File relPath,
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
                                      String charsetName) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
        File file = (pkg.length() == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
                        ? relPath
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
                        : new File(nameToPath(pkg), relPath.getPath());
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
        if (charsetName == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
            charsetName = encoding;
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
        return getPrintWriter(loc, file.getPath(), charsetName, null, FileKind.OTHER);
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
     * {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
    public OutputStream createBinaryFile(Location loc,
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
                                         String pkg,
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
                                         File relPath) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
        File file = (pkg.length() == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
                        ? relPath
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
                        : new File(nameToPath(pkg), relPath.getPath());
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
        return getOutputStream(loc, file.getPath(), null, FileKind.OTHER);
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
     * Converts the canonical name of a top-level type or package to a
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
     * pathname.  Suffix is ".java" or ".class" or "".
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
    private String nameToPath(String name, String suffix) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
        if (!DeclarationMaker.isJavaIdentifier(name.replace('.', '_'))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
            bark.aptWarning("IllegalFileName", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
            throw new IOException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
        return name.replace('.', File.separatorChar) + suffix;
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
    private String nameToPath(String name) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
        return nameToPath(name, "");
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
     * Returns a writer for a text file given its location, its
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
     * pathname relative to that location, and its encoding.
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
    private PrintWriter getPrintWriter(Location loc, String pathname,
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
                                       String encoding, String name, FileKind kind) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
        File file = new File(locations.get(loc), pathname);
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
        return getPrintWriter(file, encoding, name, kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
     * Returns a writer for a text file given its encoding.
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
    private PrintWriter getPrintWriter(File file,
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
                                       String encoding, String name, FileKind kind) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
        prepareFile(file, name, kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
        PrintWriter pw =
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
            new PrintWriter(
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
                    new BufferedWriter(
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
                         new OutputStreamWriter(new FileOutputStream(file),
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
                                                encoding)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
        wc.add(pw);
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
        return pw;
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
     * Returns an output stream for a binary file given its location
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
     * and its pathname relative to that location.
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
    private OutputStream getOutputStream(Location loc, String pathname, String name, FileKind kind)
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
                                                        throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
        File file = new File(locations.get(loc), pathname);
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
        return getOutputStream(file, name, kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
    private OutputStream getOutputStream(File file, String name, FileKind kind) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
        prepareFile(file, name, kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
        OutputStream os = new FileOutputStream(file);
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
        wc.add(os);
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
        return os;
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
    public Set<String> getSourceFileNames() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
        return sourceFileNames;
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
    public Set<String> getClassFileNames() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
        return classFileNames;
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
    public void roundOver() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
        roundOver = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
     * Checks that the file has not already been created during this
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
     * invocation.  If not, creates intermediate directories, and
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
     * deletes the file if it already exists.
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
    private void prepareFile(File file, String name, FileKind kind) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
        if (roundOver) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
            bark.aptWarning("NoNewFilesAfterRound", file.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
            throw new IOException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
        if (filesCreated.contains(file)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
            bark.aptWarning("FileReopening", file.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
            throw new IOException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
            if (file.exists()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
                file.delete();
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
                File parent = file.getParentFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
                if (parent != null && !parent.exists()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
                    if(!parent.mkdirs()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
                        bark.aptWarning("BadParentDirectory", file.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
                        throw new IOException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
            kind.register(file, name, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
            filesCreated.add(file);
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
}