1/*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test 8167643 8129559
26 * @summary Tests for modifiers
27 * @build KullaTesting TestingInputStream ExpectedDiagnostic
28 * @run testng ModifiersTest
29 */
30
31import java.util.ArrayList;
32import java.util.List;
33
34import java.util.function.Consumer;
35import javax.tools.Diagnostic;
36
37import org.testng.annotations.DataProvider;
38import org.testng.annotations.Test;
39
40@Test
41public class ModifiersTest extends KullaTesting {
42
43    @DataProvider(name = "ignoredModifiers")
44    public Object[][] getTestCases() {
45        List<Object[]> testCases = new ArrayList<>();
46        String[] ignoredModifiers = new String[] {
47            "static", "final"
48        };
49        String[] silentlyIgnoredModifiers = new String[] {
50            "public", "protected", "private"
51        };
52        String[] before = new String[] {
53            "strictfp", "abstract", "@X", "@X(value=9)"
54        };
55        String context = "@interface X { int value() default 0; }";
56        Consumer<String> eval = this::assertEval;
57        Consumer<String> evalWarn = s -> assertDeclareWarn1(s, "jdk.eval.warn.illegal.modifiers");
58        for (String ignoredModifier : ignoredModifiers) {
59            for (ClassType classType : ClassType.values()) {
60                testCases.add(new Object[] { ignoredModifier, classType, evalWarn, "", null });
61            }
62        }
63        for (String ignoredModifier : ignoredModifiers) {
64            for (String preface : before) {
65                testCases.add(new Object[] { ignoredModifier, ClassType.CLASS, evalWarn, preface, context});
66            }
67        }
68        for (String ignoredModifier : silentlyIgnoredModifiers) {
69            for (ClassType classType : ClassType.values()) {
70                testCases.add(new Object[] { ignoredModifier, classType, eval, "", null });
71            }
72        }
73        for (String ignoredModifier : silentlyIgnoredModifiers) {
74            for (String preface : before) {
75                testCases.add(new Object[] { ignoredModifier, ClassType.CLASS, eval, preface, context});
76            }
77        }
78        return testCases.toArray(new Object[testCases.size()][]);
79    }
80
81    @Test(dataProvider = "ignoredModifiers")
82    public void ignoredModifiers(String modifier, ClassType classType,
83            Consumer<String> eval, String preface, String context) {
84        if (context != null) {
85            assertEval(context);
86        }
87        String decl = String.format("%s %s %s A {}", preface, modifier, classType);
88        eval.accept(decl);
89        if (context != null) {
90            assertNumberOfActiveClasses(2);
91            assertClasses(clazz(ClassType.ANNOTATION, "X"), clazz(classType, "A"));
92        } else {
93            assertNumberOfActiveClasses(1);
94            assertClasses(clazz(classType, "A"));
95        }
96        assertActiveKeys();
97    }
98
99    public void accessToStaticFieldsOfClass() {
100        assertEval("class A {" +
101                "int x = 14;" +
102                "static int y = 18;" +
103                " }");
104        assertDeclareFail("A.x;",
105                new ExpectedDiagnostic("compiler.err.non-static.cant.be.ref", 0, 3, 1, -1, -1, Diagnostic.Kind.ERROR));
106        assertEval("A.y;", "18");
107        assertEval("new A().x;", "14");
108        assertEval("A.y = 88;", "88");
109        assertActiveKeys();
110    }
111
112    public void accessToStaticMethodsOfClass() {
113        assertEval("class A {" +
114                "void x() {}" +
115                "static void y() {}" +
116                " }");
117        assertDeclareFail("A.x();",
118                new ExpectedDiagnostic("compiler.err.non-static.cant.be.ref", 0, 3, 1, -1, -1, Diagnostic.Kind.ERROR));
119        assertEval("A.y();");
120        assertActiveKeys();
121    }
122
123    public void accessToStaticFieldsOfInterface() {
124        assertEval("interface A {" +
125                "int x = 14;" +
126                "static int y = 18;" +
127                " }");
128        assertEval("A.x;", "14");
129        assertEval("A.y;", "18");
130
131        assertDeclareFail("A.x = 18;",
132                new ExpectedDiagnostic("compiler.err.cant.assign.val.to.final.var", 0, 3, 1, -1, -1, Diagnostic.Kind.ERROR));
133        assertDeclareFail("A.y = 88;",
134                new ExpectedDiagnostic("compiler.err.cant.assign.val.to.final.var", 0, 3, 1, -1, -1, Diagnostic.Kind.ERROR));
135        assertActiveKeys();
136    }
137
138    public void accessToStaticMethodsOfInterface() {
139        assertEval("interface A { static void x() {} }");
140        assertEval("A.x();");
141        assertActiveKeys();
142    }
143
144    public void finalMethod() {
145        assertEval("class A { final void f() {} }");
146        assertDeclareFail("class B extends A { void f() {} }",
147                new ExpectedDiagnostic("compiler.err.override.meth", 20, 31, 25, -1, -1, Diagnostic.Kind.ERROR));
148        assertActiveKeys();
149    }
150
151    //TODO: is this the right semantics?
152    public void finalConstructor() {
153        assertDeclareFail("class A { final A() {} }",
154                new ExpectedDiagnostic("compiler.err.mod.not.allowed.here", 10, 22, 16, -1, -1, Diagnostic.Kind.ERROR));
155        assertActiveKeys();
156    }
157
158    //TODO: is this the right semantics?
159    public void finalDefaultMethod() {
160        assertDeclareFail("interface A { final default void a() {} }",
161                new ExpectedDiagnostic("compiler.err.mod.not.allowed.here", 14, 39, 33, -1, -1, Diagnostic.Kind.ERROR));
162        assertActiveKeys();
163    }
164}
165