langtools/test/tools/javac/4241573/T4241573.java
author sogoel
Thu, 05 Jun 2014 10:57:10 -0700
changeset 24797 850ebd4d80a7
parent 9744 3e48977e539d
child 30730 d3ce7619db2c
permissions -rw-r--r--
8044072: Group 2: create .out files for OverrideChecks tests in tools/javac dir Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3995
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
     1
/*
9744
3e48977e539d 7044486: open jdk repos have files with incorrect copyright headers, which can end up in src bundles
katleman
parents: 8432
diff changeset
     2
 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
3995
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
     4
 *
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
     8
 *
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    13
 * accompanied this code).
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    14
 *
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3995
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3995
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3995
diff changeset
    21
 * questions.
3995
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    22
 */
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    23
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    24
/*
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    25
 * @test
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    26
 * @bug 4241573
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    27
 * @summary SourceFile attribute includes full path
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    28
 */
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    29
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    30
import com.sun.tools.classfile.Attribute;
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    31
import com.sun.tools.classfile.ClassFile;
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    32
import com.sun.tools.classfile.SourceFile_attribute;
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    33
import java.io.*;
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    34
import java.util.*;
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    35
import java.util.jar.*;
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    36
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    37
public class T4241573 {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    38
    public static void main(String... args) throws Exception {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    39
        new T4241573().run();
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    40
    }
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    41
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    42
    public void run() throws Exception {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    43
        // Selection of files to be compiled
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    44
        File absJar = createJar(new File("abs.jar").getAbsoluteFile(), "j.A");
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    45
        File relJar = createJar(new File("rel.jar"), "j.R");
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    46
        File absDir = createDir(new File("abs.dir").getAbsoluteFile(), "d.A");
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    47
        File relDir = createDir(new File("rel.dir"), "d.R");
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    48
        File absTestFile = writeFile(new File("AbsTest.java").getAbsoluteFile(), "class AbsTest { class Inner { } }");
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    49
        File relTestFile = writeFile(new File("RelTest.java"), "class RelTest { class Inner { } }");
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    50
        File relTest2File = writeFile(new File("p/RelTest2.java"), "package p; class RelTest2 { class Inner { } }");
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    51
        // This next class references other classes that will be found on the source path
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    52
        // and which will therefore need to be compiled as well.
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    53
        File mainFile = writeFile(new File("Main.java"),
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    54
                "class Main { j.A ja; j.R jr; d.A da; d.R dr; }" +
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    55
                "");
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    56
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    57
        String sourcePath = createPath(absJar, relJar, absDir, relDir);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    58
        File outDir = new File("classes");
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    59
        outDir.mkdirs();
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    60
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    61
        String[] args = {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    62
            "-sourcepath", sourcePath,
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    63
            "-d", outDir.getPath(),
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    64
            absTestFile.getPath(),
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    65
            relTestFile.getPath(),
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    66
            relTest2File.getPath(),
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    67
            mainFile.getPath(),
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    68
        };
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    69
        System.err.println("compile: " + Arrays.asList(args));
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    70
        StringWriter sw = new StringWriter();
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    71
        PrintWriter pw = new PrintWriter(sw);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    72
        int rc = com.sun.tools.javac.Main.compile(args, pw);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    73
        pw.close();
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    74
        if (rc != 0) {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    75
            System.err.println(sw.toString());
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    76
            throw new Exception("unexpected exit from javac: " + rc);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    77
        }
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    78
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    79
        Set<File> expect = getFiles(outDir,
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    80
            "d/A.class",        "d/A$Inner.class",
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    81
            "d/R.class",        "d/R$Inner.class",
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    82
            "j/A.class",        "j/A$Inner.class",
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    83
            "j/R.class",        "j/R$Inner.class",
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    84
            "AbsTest.class",    "AbsTest$Inner.class",
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    85
            "RelTest.class",    "RelTest$Inner.class",
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    86
            "p/RelTest2.class", "p/RelTest2$Inner.class",
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    87
            "Main.class" );
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    88
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    89
        Set<File> found = findFiles(outDir);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    90
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    91
        if (!found.equals(expect)) {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    92
            if (found.containsAll(expect))
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    93
                throw new Exception("unexpected files found: " + diff(found, expect));
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    94
            else if (expect.containsAll(found))
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    95
                throw new Exception("expected files not found: " + diff(expect, found));
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    96
        }
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    97
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    98
        for (File f: found)
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
    99
            verifySourceFileAttribute(f);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   100
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   101
        if (errors > 0)
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   102
            throw new Exception(errors + " errors occurred");
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   103
    }
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   104
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   105
    /** Check the SourceFileAttribute is the simple name of the original source file. */
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   106
    void verifySourceFileAttribute(File f) {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   107
        System.err.println("verify: " + f);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   108
        try {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   109
            ClassFile cf = ClassFile.read(f);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   110
            SourceFile_attribute sfa = (SourceFile_attribute) cf.getAttribute(Attribute.SourceFile);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   111
            String found = sfa.getSourceFile(cf.constant_pool);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   112
            String expect = f.getName().replaceAll("([$.].*)?\\.class", ".java");
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   113
            if (!expect.equals(found)) {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   114
                error("bad value found: " + found + ", expected: " + expect);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   115
            }
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   116
        } catch (Exception e) {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   117
            error("error reading " + f +": " + e);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   118
        }
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   119
    }
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   120
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   121
    /** Create a directory containing one or more files. */
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   122
    File createDir(File dir, String... entries) throws Exception {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   123
        if (!dir.mkdirs())
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   124
            throw new Exception("cannot create directories " + dir);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   125
        for (String e: entries) {
8432
d3380b1e4779 7018859: javac turn off the Zip optimization by default
ksrini
parents: 5520
diff changeset
   126
            writeFile(new File(dir, getPathForDirEntry(e)), getBodyForEntry(e));
3995
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   127
        }
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   128
        return dir;
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   129
    }
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   130
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   131
    /** Create a jar file containing one or more entries. */
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   132
    File createJar(File jar, String... entries) throws IOException {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   133
        OutputStream out = new FileOutputStream(jar);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   134
        try {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   135
            JarOutputStream jos = new JarOutputStream(out);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   136
            for (String e: entries) {
8432
d3380b1e4779 7018859: javac turn off the Zip optimization by default
ksrini
parents: 5520
diff changeset
   137
                jos.putNextEntry(new JarEntry(getPathForZipEntry(e)));
3995
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   138
                jos.write(getBodyForEntry(e).getBytes());
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   139
            }
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   140
            jos.close();
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   141
        } finally {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   142
            out.close();
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   143
        }
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   144
        return jar;
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   145
    }
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   146
8432
d3380b1e4779 7018859: javac turn off the Zip optimization by default
ksrini
parents: 5520
diff changeset
   147
    /** Return the path for an entry given to createDir */
d3380b1e4779 7018859: javac turn off the Zip optimization by default
ksrini
parents: 5520
diff changeset
   148
    String getPathForDirEntry(String e) {
3995
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   149
        return e.replace(".", File.separator) + ".java";
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   150
    }
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   151
8432
d3380b1e4779 7018859: javac turn off the Zip optimization by default
ksrini
parents: 5520
diff changeset
   152
    /** Return the path for an entry given to createJar. */
d3380b1e4779 7018859: javac turn off the Zip optimization by default
ksrini
parents: 5520
diff changeset
   153
    String getPathForZipEntry(String e) {
d3380b1e4779 7018859: javac turn off the Zip optimization by default
ksrini
parents: 5520
diff changeset
   154
        return e.replace(".", "/") + ".java";
d3380b1e4779 7018859: javac turn off the Zip optimization by default
ksrini
parents: 5520
diff changeset
   155
    }
d3380b1e4779 7018859: javac turn off the Zip optimization by default
ksrini
parents: 5520
diff changeset
   156
3995
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   157
    /** Return the body text for an entry given to createDir or createJar. */
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   158
    String getBodyForEntry(String e) {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   159
        int sep = e.lastIndexOf(".");
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   160
        String pkgName = e.substring(0, sep);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   161
        String className = e.substring(sep + 1);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   162
        return "package " + pkgName + "; public class " + className + "{ class Inner { } }";
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   163
    }
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   164
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   165
    /** Write a file containing the given string. Parent directories are
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   166
     * created as needed. */
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   167
    File writeFile(File f, String s) throws IOException {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   168
        if (f.getParentFile() != null)
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   169
            f.getParentFile().mkdirs();
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   170
        FileWriter out = new FileWriter(f);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   171
        try {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   172
            out.write(s);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   173
        } finally {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   174
            out.close();
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   175
        }
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   176
        return f;
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   177
    }
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   178
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   179
    /** Create a path value from a list of directories and jar files. */
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   180
    String createPath(File... files) {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   181
        StringBuilder sb = new StringBuilder();
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   182
        for (File f: files) {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   183
            if (sb.length() > 0)
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   184
                sb.append(File.pathSeparatorChar);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   185
            sb.append(f.getPath());
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   186
        }
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   187
        return sb.toString();
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   188
    }
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   189
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   190
    /** Create a set of files from a base directory and a set of relative paths. */
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   191
    Set<File> getFiles(File dir, String... paths) {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   192
        Set<File> files = new LinkedHashSet<File>();
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   193
        for (String p: paths)
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   194
            files.add(new File(dir, p));
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   195
        return files;
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   196
    }
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   197
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   198
    /** Find all the files in a directory and its subdirectories. */
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   199
    Set<File> findFiles(File dir) {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   200
        Set<File> files = new LinkedHashSet<File>();
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   201
        findFiles(dir, files);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   202
        return files;
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   203
    }
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   204
    // where
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   205
    void findFiles(File dir, Set<File> files) {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   206
        for (File f: dir.listFiles()) {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   207
            if (f.isDirectory())
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   208
                findFiles(f, files);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   209
            else
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   210
                files.add(f);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   211
        }
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   212
    }
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   213
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   214
    /** Return the difference of two sets, a - b. */
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   215
    <T> Set<T> diff(Set<T> a, Set<T> b) {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   216
        if (b.isEmpty())
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   217
            return a;
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   218
        Set<T> result = new LinkedHashSet<T>(a);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   219
        result.removeAll(b);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   220
        return result;
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   221
    }
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   222
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   223
    /** Report an error. */
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   224
    void error(String msg) {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   225
        System.err.println(msg);
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   226
        errors++;
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   227
    }
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   228
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   229
    int errors;
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents:
diff changeset
   230
}