File: T6996914a.java

package info (click to toggle)
libnb-javaparser-java 7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 35,940 kB
  • ctags: 55,696
  • sloc: java: 264,137; xml: 2,781; sh: 1,135; makefile: 425
file content (157 lines) | stat: -rw-r--r-- 5,078 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (c) 2010, 2011, 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 6996914 7020044
 * @summary  Diamond inference: problem when accessing protected constructor
 * @run main T6996914a
 */

import com.sun.source.util.JavacTask;
import java.net.URI;
import java.util.Arrays;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.ToolProvider;

public class T6996914a {

    enum PackageKind {
        DEFAULT("", ""),
        A("package a;", "import a.*;");

        String pkgDecl;
        String importDecl;

        PackageKind(String pkgDecl, String importDecl) {
            this.pkgDecl = pkgDecl;
            this.importDecl = importDecl;
        }
    }

    enum ConstructorKind {
        PACKAGE(""),
        PROTECTED("protected"),
        PRIVATE("private"),
        PUBLIC("public");

        String mod;

        ConstructorKind(String mod) {
            this.mod = mod;
        }
    }

    static class FooClass extends SimpleJavaFileObject {

        final static String sourceStub =
                        "#P\n" +
                        "public class Foo<X> {\n" +
                        "  #M Foo() {}\n" +
                        "}\n";

        String source;

        public FooClass(PackageKind pk, ConstructorKind ck) {
            super(URI.create("myfo:/" + (pk != PackageKind.DEFAULT ? "a/Foo.java" : "Foo.java")),
                    JavaFileObject.Kind.SOURCE);
            source = sourceStub.replace("#P", pk.pkgDecl).replace("#M", ck.mod);
        }

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

    static class ClientClass extends SimpleJavaFileObject {

        final static String sourceStub =
                        "#I\n" +
                        "class Test {\n" +
                        "  Foo<String> fs = new Foo<>();\n" +
                        "}\n";

        String source;

        public ClientClass(PackageKind pk) {
            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
            source = sourceStub.replace("#I", pk.importDecl);
        }

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

    public static void main(String... args) throws Exception {
        for (PackageKind pk : PackageKind.values()) {
            for (ConstructorKind ck : ConstructorKind.values()) {
                    compileAndCheck(pk, ck);
            }
        }
    }

    static void compileAndCheck(PackageKind pk, ConstructorKind ck) throws Exception {
        FooClass foo = new FooClass(pk, ck);
        ClientClass client = new ClientClass(pk);
        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
        ErrorListener el = new ErrorListener();
        JavacTask ct = (JavacTask)tool.getTask(null, null, el,
                null, null, Arrays.asList(foo, client));
        ct.analyze();
        if (el.errors > 0 == check(pk, ck)) {
            String msg = el.errors > 0 ?
                "Error compiling files" :
                "No error when compiling files";
            throw new AssertionError(msg + ": \n" + foo.source + "\n" + client.source);
        }
    }

    static boolean check(PackageKind pk, ConstructorKind ck) {
        switch (pk) {
            case A: return ck == ConstructorKind.PUBLIC;
            case DEFAULT: return ck != ConstructorKind.PRIVATE;
            default: throw new AssertionError("Unknown package kind");
        }
    }

    /**
     * DiagnosticListener to count any errors that occur
     */
    private static class ErrorListener implements DiagnosticListener<JavaFileObject> {

        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
            switch (diagnostic.getKind()) {
                case ERROR:
                    errors++;
            }
        }
        int errors;
    }
}