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 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 8029240 |
|
27 * @summary Default methods not always visible under -source 7 |
|
28 * @library /tools/javac/lib |
|
29 * @build ToolBox |
|
30 * @run main DefaultMethodsNotVisibileForSource7Test |
|
31 */ |
|
32 |
|
33 import java.nio.file.Files; |
|
34 import java.nio.file.Paths; |
|
35 |
|
36 public class DefaultMethodsNotVisibileForSource7Test { |
|
37 // common definitions |
|
38 |
|
39 // this one should be compiled with source 8, the rest with source 7 |
|
40 static final String ISrc = |
|
41 "interface I {\n" + |
|
42 " default void m() {}\n" + |
|
43 "}"; |
|
44 |
|
45 static final String JSrc = |
|
46 "interface J extends I {}"; |
|
47 |
|
48 static final String ASrc = |
|
49 "abstract class A implements I {}"; |
|
50 |
|
51 static final String BSrc = |
|
52 "class B implements I {}"; |
|
53 |
|
54 // test legacy implementations |
|
55 static final String C1Src = |
|
56 "class C1 implements I {\n" + |
|
57 " @Override public void m() {}\n" + |
|
58 "}"; |
|
59 |
|
60 static final String C2Src = |
|
61 "class C2 implements J {\n" + |
|
62 " @Override public void m() {}\n" + |
|
63 "}"; |
|
64 |
|
65 static final String C3Src = |
|
66 "class C3 extends A {\n" + |
|
67 " @Override public void m() {}\n" + |
|
68 "}"; |
|
69 |
|
70 static final String C4Src = |
|
71 "class C4 extends B {\n" + |
|
72 " @Override public void m() {}\n" + |
|
73 "}"; |
|
74 |
|
75 //test legacy invocations |
|
76 static final String LegacyInvocationSrc = |
|
77 "class LegacyInvocation {\n" + |
|
78 " public static void test(I i, J j, A a, B b) {\n" + |
|
79 " i.m();\n" + |
|
80 " j.m();\n" + |
|
81 " a.m();\n" + |
|
82 " b.m();\n" + |
|
83 " }\n" + |
|
84 "}"; |
|
85 |
|
86 //test case super invocations |
|
87 static final String SubASrc = |
|
88 "class SubA extends A {\n" + |
|
89 " public void test() {\n" + |
|
90 " super.m();\n" + |
|
91 " }\n" + |
|
92 "}"; |
|
93 |
|
94 static final String SubBSrc = |
|
95 "class SubB extends B {\n" + |
|
96 " public void test() {\n" + |
|
97 " super.m();\n" + |
|
98 " }\n" + |
|
99 "}"; |
|
100 |
|
101 public static void main(String[] args) throws Exception { |
|
102 new DefaultMethodsNotVisibileForSource7Test().run(); |
|
103 } |
|
104 |
|
105 void run() throws Exception { |
|
106 testsPreparation(); |
|
107 testLegacyImplementations(); |
|
108 testLegacyInvocations(); |
|
109 testSuperInvocations(); |
|
110 } |
|
111 |
|
112 void testsPreparation() throws Exception { |
|
113 Files.createDirectory(Paths.get("out")); |
|
114 |
|
115 /* as an extra check let's make sure that interface 'I' can't be compiled |
|
116 * with source 7 |
|
117 */ |
|
118 ToolBox.JavaToolArgs javacArgs = |
|
119 new ToolBox.JavaToolArgs(ToolBox.Expect.FAIL) |
|
120 .setOptions("-d", "out", "-source", "7") |
|
121 .setSources(ISrc); |
|
122 ToolBox.javac(javacArgs); |
|
123 |
|
124 //but it should compile with source >= 8 |
|
125 javacArgs = |
|
126 new ToolBox.JavaToolArgs() |
|
127 .setOptions("-d", "out") |
|
128 .setSources(ISrc); |
|
129 ToolBox.javac(javacArgs); |
|
130 |
|
131 javacArgs = |
|
132 new ToolBox.JavaToolArgs() |
|
133 .setOptions("-cp", "out", "-d", "out", "-source", "7") |
|
134 .setSources(JSrc, ASrc, BSrc); |
|
135 ToolBox.javac(javacArgs); |
|
136 } |
|
137 |
|
138 void testLegacyImplementations() throws Exception { |
|
139 //compile C1-4 |
|
140 ToolBox.JavaToolArgs javacArgs = |
|
141 new ToolBox.JavaToolArgs() |
|
142 .setOptions("-cp", "out", "-d", "out", "-source", "7") |
|
143 .setSources(C1Src, C2Src, C3Src, C4Src); |
|
144 ToolBox.javac(javacArgs); |
|
145 } |
|
146 |
|
147 void testLegacyInvocations() throws Exception { |
|
148 //compile LegacyInvocation |
|
149 ToolBox.JavaToolArgs javacArgs = |
|
150 new ToolBox.JavaToolArgs() |
|
151 .setOptions("-cp", "out", "-d", "out", "-source", "7") |
|
152 .setSources(LegacyInvocationSrc); |
|
153 ToolBox.javac(javacArgs); |
|
154 } |
|
155 |
|
156 void testSuperInvocations() throws Exception { |
|
157 //compile SubA, SubB |
|
158 ToolBox.JavaToolArgs javacArgs = |
|
159 new ToolBox.JavaToolArgs() |
|
160 .setOptions("-cp", "out", "-d", "out", "-source", "7") |
|
161 .setSources(SubASrc, SubBSrc); |
|
162 ToolBox.javac(javacArgs); |
|
163 } |
|
164 } |
|