author | mchung |
Sat, 24 Feb 2018 09:41:42 -0800 | |
changeset 48984 | b5d1fb0701d4 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
33362 | 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 |
/* |
|
41940
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
25 |
* @test 8167643 8129559 |
33362 | 26 |
* @summary Tests for modifiers |
27 |
* @build KullaTesting TestingInputStream ExpectedDiagnostic |
|
28 |
* @run testng ModifiersTest |
|
29 |
*/ |
|
30 |
||
31 |
import java.util.ArrayList; |
|
32 |
import java.util.List; |
|
33 |
||
41940
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
34 |
import java.util.function.Consumer; |
33362 | 35 |
import javax.tools.Diagnostic; |
36 |
||
37 |
import org.testng.annotations.DataProvider; |
|
38 |
import org.testng.annotations.Test; |
|
39 |
||
40 |
@Test |
|
41 |
public class ModifiersTest extends KullaTesting { |
|
42 |
||
43 |
@DataProvider(name = "ignoredModifiers") |
|
44 |
public Object[][] getTestCases() { |
|
45 |
List<Object[]> testCases = new ArrayList<>(); |
|
46 |
String[] ignoredModifiers = new String[] { |
|
41858
5843b57ce3a6
8167643: JShell: silently ignore access modifiers (as semantically irrelevant)
rfield
parents:
33362
diff
changeset
|
47 |
"static", "final" |
33362 | 48 |
}; |
41940
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
49 |
String[] silentlyIgnoredModifiers = new String[] { |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
50 |
"public", "protected", "private" |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
51 |
}; |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
52 |
String[] before = new String[] { |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
53 |
"strictfp", "abstract", "@X", "@X(value=9)" |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
54 |
}; |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
55 |
String context = "@interface X { int value() default 0; }"; |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
56 |
Consumer<String> eval = this::assertEval; |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
57 |
Consumer<String> evalWarn = s -> assertDeclareWarn1(s, "jdk.eval.warn.illegal.modifiers"); |
33362 | 58 |
for (String ignoredModifier : ignoredModifiers) { |
59 |
for (ClassType classType : ClassType.values()) { |
|
41940
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
60 |
testCases.add(new Object[] { ignoredModifier, classType, evalWarn, "", null }); |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
61 |
} |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
62 |
} |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
63 |
for (String ignoredModifier : ignoredModifiers) { |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
64 |
for (String preface : before) { |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
65 |
testCases.add(new Object[] { ignoredModifier, ClassType.CLASS, evalWarn, preface, context}); |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
66 |
} |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
67 |
} |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
68 |
for (String ignoredModifier : silentlyIgnoredModifiers) { |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
69 |
for (ClassType classType : ClassType.values()) { |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
70 |
testCases.add(new Object[] { ignoredModifier, classType, eval, "", null }); |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
71 |
} |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
72 |
} |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
73 |
for (String ignoredModifier : silentlyIgnoredModifiers) { |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
74 |
for (String preface : before) { |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
75 |
testCases.add(new Object[] { ignoredModifier, ClassType.CLASS, eval, preface, context}); |
33362 | 76 |
} |
77 |
} |
|
78 |
return testCases.toArray(new Object[testCases.size()][]); |
|
79 |
} |
|
80 |
||
81 |
@Test(dataProvider = "ignoredModifiers") |
|
41940
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
82 |
public void ignoredModifiers(String modifier, ClassType classType, |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
83 |
Consumer<String> eval, String preface, String context) { |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
84 |
if (context != null) { |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
85 |
assertEval(context); |
41858
5843b57ce3a6
8167643: JShell: silently ignore access modifiers (as semantically irrelevant)
rfield
parents:
33362
diff
changeset
|
86 |
} |
41940
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
87 |
String decl = String.format("%s %s %s A {}", preface, modifier, classType); |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
88 |
eval.accept(decl); |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
89 |
if (context != null) { |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
90 |
assertNumberOfActiveClasses(2); |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
91 |
assertClasses(clazz(ClassType.ANNOTATION, "X"), clazz(classType, "A")); |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
92 |
} else { |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
93 |
assertNumberOfActiveClasses(1); |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
94 |
assertClasses(clazz(classType, "A")); |
048d559e9da7
8129559: JShell: compilation fails if class, method or field is annotated and has modifiers
rfield
parents:
41858
diff
changeset
|
95 |
} |
41858
5843b57ce3a6
8167643: JShell: silently ignore access modifiers (as semantically irrelevant)
rfield
parents:
33362
diff
changeset
|
96 |
assertActiveKeys(); |
5843b57ce3a6
8167643: JShell: silently ignore access modifiers (as semantically irrelevant)
rfield
parents:
33362
diff
changeset
|
97 |
} |
5843b57ce3a6
8167643: JShell: silently ignore access modifiers (as semantically irrelevant)
rfield
parents:
33362
diff
changeset
|
98 |
|
33362 | 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 |
} |