File: T6550655.java

package info (click to toggle)
libnb-javaparser-java 9%2B2018-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 65,320 kB
  • sloc: java: 440,096; xml: 6,359; sh: 865; makefile: 314
file content (193 lines) | stat: -rw-r--r-- 6,520 bytes parent folder | download | duplicates (21)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/**
 * @test
 * @bug     6550655
 * @summary javac crashes when compiling against an annotated class
 * @modules java.compiler
 *          jdk.compiler
 */

import java.io.File;
import java.net.URI;
import java.util.Arrays;

import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.ToolProvider;

public class T6550655 {

    JavaCompiler javacTool;
    File testDir;
    TestKind testKind;
    EnumActionKind actionKind;

    String testSource = "enum E { NORTH, SOUTH, WEST, EAST; }\n" +
                        "@I(val = E.NORTH)class A {}\n" +
                        "@interface I { E val(); }";

    T6550655(JavaCompiler javacTool, File testDir, TestKind testKind, EnumActionKind actionKind) {
        this.javacTool = javacTool;
        this.testDir = testDir;
        this.testKind = testKind;
        this.actionKind = actionKind;
    }

    void test() {
        testDir.mkdirs();
        compile(null, new JavaSource("Test.java", testSource));
        actionKind.doAction(this);
        compile(new DiagnosticChecker(), testKind.source);
    }

    void compile(DiagnosticChecker dc, JavaSource... sources) {
        try {
            CompilationTask ct = javacTool.getTask(null, null, dc,
                    Arrays.asList("-d", testDir.getAbsolutePath(), "-cp", testDir.getAbsolutePath()),
                    null, Arrays.asList(sources));
            ct.call();
        }
        catch (Exception e) {
            error("Internal compilation error");
        }
    }

    void replaceEnum(String newSource) {
        compile(null, new JavaSource("Replace.java", newSource));
    };

    void removeEnum() {
        File enumClass = new File(testDir, "E.class");
        if (!enumClass.exists()) {
            error("Expected file E.class does not exists in folder " + testDir);
        }
        enumClass.delete();
    };

    void error(String msg) {
        System.err.println(msg);
        nerrors++;
    }

    class DiagnosticChecker implements DiagnosticListener<JavaFileObject> {

        String[][] expectedKeys = new String[][] {
         //             DIRECT,                                         INDIRECT
        {/*REPLACE1*/   "compiler.err.cant.resolve.location"     ,      "compiler.warn.unknown.enum.constant" },
        {/*REPLACE2*/   "compiler.err.cant.resolve.location.args",      "compiler.warn.annotation.method.not.found" },
        {/*REMOVE*/     "compiler.err.cant.resolve"              ,      "compiler.warn.unknown.enum.constant.reason" } };

        String keyToIgnore = "compiler.err.attribute.value.must.be.constant";

        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
            String expectedCode = expectedKeys[actionKind.ordinal()][testKind.ordinal()];
            if (!diagnostic.getCode().equals(keyToIgnore) &&
                    !diagnostic.getCode().equals(expectedCode)) {
                error("Unexpected diagnostic" +
                      "\nfound " + diagnostic.getCode() +
                      "\nexpected " + expectedCode +
                      "\ntestKind " + testKind +
                      "\nactionKind " + actionKind);
            }
        }
    }

    //global declarations

    enum EnumActionKind {
        REPLACE1("enum E { SOUTH, WEST, EAST; }") {
            @Override
            void doAction(T6550655 test) {
                test.replaceEnum(optionalSource);
            }
        },
        REPLACE2("@interface I { E valNew() default E.EAST; }") {
            @Override
            void doAction(T6550655 test) {
                test.replaceEnum(optionalSource);
            }
        },
        REMOVE(null) {
            @Override
            void doAction(T6550655 test) { test.removeEnum(); }
        };

        String optionalSource;

        private EnumActionKind(String optionalSource) {
            this.optionalSource = optionalSource;
        }

        abstract void doAction(T6550655 test);
    }

    enum TestKind {
        DIRECT("@I(val = E.NORTH)class C1 {}"),
        INDIRECT("class C2 { A a; }");

        JavaSource source;

        private TestKind(final String code) {
            this.source = new JavaSource("Test.java", code);
        }
    }

    public static void main(String[] args) throws Exception {
        String SCRATCH_DIR = System.getProperty("user.dir");
        JavaCompiler javacTool = ToolProvider.getSystemJavaCompiler();
        int n = 0;
        for (TestKind testKind : TestKind.values()) {
            for (EnumActionKind actionKind : EnumActionKind.values()) {
                File testDir = new File(SCRATCH_DIR, "test"+n);
                new T6550655(javacTool, testDir, testKind, actionKind).test();
                n++;
            }
        }
        if (nerrors > 0) {
            throw new AssertionError("Some errors have been detected");
        }
    }

    static class JavaSource extends SimpleJavaFileObject {

        String source;

        public JavaSource(String filename, String source) {
            super(URI.create("myfo:/" + filename), JavaFileObject.Kind.SOURCE);
            this.source = source;
        }

        @Override
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
            return source;
        }
    }

    static int nerrors = 0;
}