langtools/test/tools/javac/TestPkgInfo.java
author hseigel
Wed, 02 Mar 2016 23:48:41 +0000
changeset 36397 c487ced7231c
parent 30730 d3ce7619db2c
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6575
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 19488
diff changeset
     2
 * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
6575
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
     4
 *
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
     8
 *
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    13
 * accompanied this code).
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    14
 *
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    18
 *
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    21
 * questions.
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    22
 */
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    23
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    24
/*
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    25
 * @test
19488
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    26
 * @bug 6960424 8022161
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    27
 * @summary new option -Xpkginfo for better control of when package-info.class
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    28
 *          is generated, also ensures no failures if package-info.java is
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    29
 *          not available.
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 19488
diff changeset
    30
 * @modules jdk.compiler
6575
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    31
 */
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    32
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    33
import java.io.*;
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    34
import java.util.*;
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    35
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    36
public class TestPkgInfo {
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    37
    enum OptKind {
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    38
        NONE(null),
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    39
        ALWAYS("-Xpkginfo:always"),
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    40
        NONEMPTY("-Xpkginfo:nonempty"),
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    41
        LEGACY("-Xpkginfo:legacy");
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    42
        OptKind(String opt) { this.opt = opt; }
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    43
        final String opt;
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    44
    };
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    45
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    46
    public static void main(String... args) throws Exception {
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    47
        new TestPkgInfo().run(args);
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    48
    }
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    49
    public void run(String... args) throws Exception {
19488
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    50
        testPositive();
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    51
        testNoExceptions();
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    52
    }
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    53
    public void testPositive(String... args) throws Exception {
6575
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    54
        boolean[] booleanValues = { false, true };
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    55
        for (OptKind ok: OptKind.values()) {
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    56
            for (boolean sr: booleanValues) {
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    57
                for (boolean cr: booleanValues) {
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    58
                    for (boolean rr: booleanValues) {
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    59
                        try {
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    60
                            test(ok, sr, cr, rr);
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    61
                        } catch (Exception e) {
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    62
                            error("Exception: " + e);
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    63
                        }
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    64
                        if (errors > 0) throw new AssertionError();
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    65
                    }
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    66
                }
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    67
            }
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    68
        }
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    69
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    70
        if (errors > 0)
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    71
            throw new Exception(errors + " errors occurred");
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    72
    }
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
    73
19488
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    74
    /** this should throw no exceptions **/
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    75
    void testNoExceptions() throws Exception {
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    76
        count++;
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    77
        System.err.println("Test " + count + ": ALWAYS nofile");
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    78
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    79
        StringBuilder sb = new StringBuilder();
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    80
        sb.append("package test; class Hello{}");
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    81
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    82
        // test specific tmp directory
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    83
        File tmpDir = new File("tmp.test" + count);
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    84
        File classesDir = new File(tmpDir, "classes");
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    85
        classesDir.mkdirs();
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    86
        File javafile = new File(new File(tmpDir, "src"), "Hello.java");
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    87
        writeFile(javafile, sb.toString());
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    88
        // build up list of options and files to be compiled
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    89
        List<String> opts = new ArrayList<>();
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    90
        List<File> files = new ArrayList<>();
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    91
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    92
        opts.add("-d");
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    93
        opts.add(classesDir.getPath());
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    94
        opts.add("-Xpkginfo:always");
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    95
        files.add(javafile);
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    96
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    97
        compile(opts, files);
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    98
    }
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
    99
6575
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   100
    void test(OptKind ok, boolean sr, boolean cr, boolean rr) throws Exception {
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   101
        count++;
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   102
        System.err.println("Test " + count + ": ok:" + ok + " sr:" + sr + " cr:" + cr + " rr:" + rr);
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   103
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   104
        StringBuilder sb = new StringBuilder();
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   105
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   106
        // create annotated package statement with all combinations of retention policy
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   107
        if (sr) sb.append("@SR\n");
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   108
        if (cr) sb.append("@CR\n");
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   109
        if (rr) sb.append("@RR\n");
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   110
        sb.append("package p;\n");
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   111
        sb.append("\n");
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   112
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   113
        sb.append("import java.lang.annotation.*;\n");
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   114
        sb.append("@Retention(RetentionPolicy.SOURCE)  @interface SR { }\n");
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   115
        sb.append("@Retention(RetentionPolicy.CLASS)   @interface CR { }\n");
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   116
        sb.append("@Retention(RetentionPolicy.RUNTIME) @interface RR { }\n");
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   117
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   118
        // test specific tmp directory
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   119
        File tmpDir = new File("tmp.test" + count);
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   120
        File classesDir = new File(tmpDir, "classes");
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   121
        classesDir.mkdirs();
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   122
        File pkginfo_java = new File(new File(tmpDir, "src"), "package-info.java");
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   123
        writeFile(pkginfo_java, sb.toString());
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   124
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   125
        // build up list of options and files to be compiled
19488
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
   126
        List<String> opts = new ArrayList<>();
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
   127
        List<File> files = new ArrayList<>();
6575
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   128
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   129
        opts.add("-d");
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   130
        opts.add(classesDir.getPath());
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   131
        if (ok.opt != null)
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   132
            opts.add(ok.opt);
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   133
        //opts.add("-verbose");
19488
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
   134
            files.add(pkginfo_java);
6575
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   135
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   136
        compile(opts, files);
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   137
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   138
        File pkginfo_class = new File(new File(classesDir, "p"), "package-info.class");
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   139
        boolean exists = pkginfo_class.exists();
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   140
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   141
        boolean expected;
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   142
        switch (ok) {
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   143
            case ALWAYS:
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   144
                expected = true;
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   145
                break;
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   146
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   147
            case LEGACY:
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   148
            case NONE:
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   149
                expected = (sr || cr || rr ); // any annotation
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   150
                break;
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   151
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   152
            case NONEMPTY:
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   153
                expected = (cr || rr ); // any annotation in class file
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   154
                break;
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   155
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   156
            default:
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   157
                throw new IllegalStateException();
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   158
        }
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   159
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   160
        if (exists && !expected)
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   161
            error("package-info.class found but not expected");
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   162
        if (!exists && expected)
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   163
            error("package-info.class expected but not found");
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   164
    }
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   165
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   166
    /** Compile files with options provided. */
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   167
    void compile(List<String> opts, List<File> files) throws Exception {
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   168
        System.err.println("javac: " + opts + " " + files);
19488
3d9b2d67f71f 8022161: javac Null Pointer Exception in Enter.visitTopLevel
ksrini
parents: 6575
diff changeset
   169
        List<String> args = new ArrayList<>();
6575
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   170
        args.addAll(opts);
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   171
        for (File f: files)
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   172
            args.add(f.getPath());
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   173
        StringWriter sw = new StringWriter();
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   174
        PrintWriter pw = new PrintWriter(sw);
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   175
        int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw);
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   176
        pw.flush();
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   177
        if (sw.getBuffer().length() > 0)
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   178
            System.err.println(sw.toString());
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   179
        if (rc != 0)
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   180
            throw new Exception("compilation failed: rc=" + rc);
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   181
    }
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   182
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   183
    /** Write a file with a given body. */
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   184
    void writeFile(File f, String body) throws Exception {
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   185
        if (f.getParentFile() != null)
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   186
            f.getParentFile().mkdirs();
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   187
        Writer out = new FileWriter(f);
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   188
        try {
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   189
            out.write(body);
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   190
        } finally {
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   191
            out.close();
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   192
        }
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   193
    }
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   194
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   195
    /** Report an error. */
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   196
    void error(String msg) {
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   197
        System.err.println("Error: " + msg);
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   198
        errors++;
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   199
    }
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   200
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   201
    /** Test case counter. */
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   202
    int count;
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   203
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   204
    /** Number of errors found. */
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   205
    int errors;
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
diff changeset
   206
}