Fri, 24 Apr 2020 03:58:51 +0100
Merge
1 /*
2 * Copyright (c) 2014, 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 */
24 /*
25 * @test
26 * @bug 8029240 8030855
27 * @summary Default methods not always visible under -source 7
28 * Default methods should be visible under source previous to 8
29 * @library /tools/javac/lib
30 * @build ToolBox
31 * @run main DefaultMethodsNotVisibleForSourceLessThan8Test
32 */
34 import java.nio.file.Files;
35 import java.nio.file.Paths;
37 public class DefaultMethodsNotVisibleForSourceLessThan8Test {
38 // common definitions
40 // this one should be compiled with source 8, the rest with source < 8
41 static final String ISrc =
42 "interface I {\n" +
43 " default void m() {}\n" +
44 "}";
46 static final String JSrc =
47 "interface J extends I {}";
49 static final String ASrc =
50 "abstract class A implements I {}";
52 static final String BSrc =
53 "class B implements I {}";
55 // test legacy implementations
56 static final String C1Src =
57 "class C1 implements I {\n" +
58 " public void m() {}\n" +
59 "}";
61 static final String C2Src =
62 "class C2 implements J {\n" +
63 " public void m() {}\n" +
64 "}";
66 static final String C3Src =
67 "class C3 extends A {\n" +
68 " public void m() {}\n" +
69 "}";
71 static final String C4Src =
72 "class C4 extends B {\n" +
73 " public void m() {}\n" +
74 "}";
76 //test legacy invocations
77 static final String LegacyInvocationSrc =
78 "class LegacyInvocation {\n" +
79 " public static void test(I i, J j, A a, B b) {\n" +
80 " i.m();\n" +
81 " j.m();\n" +
82 " a.m();\n" +
83 " b.m();\n" +
84 " }\n" +
85 "}";
87 //test case super invocations
88 static final String SubASrc =
89 "class SubA extends A {\n" +
90 " public void test() {\n" +
91 " super.m();\n" +
92 " }\n" +
93 "}";
95 static final String SubBSrc =
96 "class SubB extends B {\n" +
97 " public void test() {\n" +
98 " super.m();\n" +
99 " }\n" +
100 "}";
102 public static void main(String[] args) throws Exception {
103 String[] sources = new String[] {
104 "1.2",
105 "1.3",
106 "1.4",
107 "1.5",
108 "1.6",
109 "1.7",
110 };
111 for (String source : sources) {
112 new DefaultMethodsNotVisibleForSourceLessThan8Test().run(source);
113 }
114 }
116 String outDir;
117 String source;
119 void run(String source) throws Exception {
120 this.source = source;
121 outDir = "out" + source.replace('.', '_');
122 testsPreparation();
123 testLegacyImplementations();
124 testLegacyInvocations();
125 testSuperInvocations();
126 }
128 void testsPreparation() throws Exception {
129 Files.createDirectory(Paths.get(outDir));
131 /* as an extra check let's make sure that interface 'I' can't be compiled
132 * with source < 8
133 */
134 ToolBox.JavaToolArgs javacArgs =
135 new ToolBox.JavaToolArgs(ToolBox.Expect.FAIL)
136 .setOptions("-d", outDir, "-source", source)
137 .setSources(ISrc);
138 ToolBox.javac(javacArgs);
140 //but it should compile with source >= 8
141 javacArgs =
142 new ToolBox.JavaToolArgs()
143 .setOptions("-d", outDir)
144 .setSources(ISrc);
145 ToolBox.javac(javacArgs);
147 javacArgs =
148 new ToolBox.JavaToolArgs()
149 .setOptions("-cp", outDir, "-d", outDir, "-source", source)
150 .setSources(JSrc, ASrc, BSrc);
151 ToolBox.javac(javacArgs);
152 }
154 void testLegacyImplementations() throws Exception {
155 //compile C1-4
156 ToolBox.JavaToolArgs javacArgs =
157 new ToolBox.JavaToolArgs()
158 .setOptions("-cp", outDir, "-d", outDir, "-source", source)
159 .setSources(C1Src, C2Src, C3Src, C4Src);
160 ToolBox.javac(javacArgs);
161 }
163 void testLegacyInvocations() throws Exception {
164 //compile LegacyInvocation
165 ToolBox.JavaToolArgs javacArgs =
166 new ToolBox.JavaToolArgs()
167 .setOptions("-cp", outDir, "-d", outDir, "-source", source)
168 .setSources(LegacyInvocationSrc);
169 ToolBox.javac(javacArgs);
170 }
172 void testSuperInvocations() throws Exception {
173 //compile SubA, SubB
174 ToolBox.JavaToolArgs javacArgs =
175 new ToolBox.JavaToolArgs()
176 .setOptions("-cp", outDir, "-d", outDir, "-source", source)
177 .setSources(SubASrc, SubBSrc);
178 ToolBox.javac(javacArgs);
179 }
180 }