Sat, 22 Dec 2018 13:10:38 -0800
Added tag jdk8u202-b25 for changeset 15d8c732039b
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | */ |
aoqi@0 | 23 | |
aoqi@0 | 24 | /* |
aoqi@0 | 25 | * @test |
aoqi@0 | 26 | * @bug 8029240 8030855 |
aoqi@0 | 27 | * @summary Default methods not always visible under -source 7 |
aoqi@0 | 28 | * Default methods should be visible under source previous to 8 |
aoqi@0 | 29 | * @library /tools/javac/lib |
aoqi@0 | 30 | * @build ToolBox |
aoqi@0 | 31 | * @run main DefaultMethodsNotVisibleForSourceLessThan8Test |
aoqi@0 | 32 | */ |
aoqi@0 | 33 | |
aoqi@0 | 34 | import java.nio.file.Files; |
aoqi@0 | 35 | import java.nio.file.Paths; |
aoqi@0 | 36 | |
aoqi@0 | 37 | public class DefaultMethodsNotVisibleForSourceLessThan8Test { |
aoqi@0 | 38 | // common definitions |
aoqi@0 | 39 | |
aoqi@0 | 40 | // this one should be compiled with source 8, the rest with source < 8 |
aoqi@0 | 41 | static final String ISrc = |
aoqi@0 | 42 | "interface I {\n" + |
aoqi@0 | 43 | " default void m() {}\n" + |
aoqi@0 | 44 | "}"; |
aoqi@0 | 45 | |
aoqi@0 | 46 | static final String JSrc = |
aoqi@0 | 47 | "interface J extends I {}"; |
aoqi@0 | 48 | |
aoqi@0 | 49 | static final String ASrc = |
aoqi@0 | 50 | "abstract class A implements I {}"; |
aoqi@0 | 51 | |
aoqi@0 | 52 | static final String BSrc = |
aoqi@0 | 53 | "class B implements I {}"; |
aoqi@0 | 54 | |
aoqi@0 | 55 | // test legacy implementations |
aoqi@0 | 56 | static final String C1Src = |
aoqi@0 | 57 | "class C1 implements I {\n" + |
aoqi@0 | 58 | " public void m() {}\n" + |
aoqi@0 | 59 | "}"; |
aoqi@0 | 60 | |
aoqi@0 | 61 | static final String C2Src = |
aoqi@0 | 62 | "class C2 implements J {\n" + |
aoqi@0 | 63 | " public void m() {}\n" + |
aoqi@0 | 64 | "}"; |
aoqi@0 | 65 | |
aoqi@0 | 66 | static final String C3Src = |
aoqi@0 | 67 | "class C3 extends A {\n" + |
aoqi@0 | 68 | " public void m() {}\n" + |
aoqi@0 | 69 | "}"; |
aoqi@0 | 70 | |
aoqi@0 | 71 | static final String C4Src = |
aoqi@0 | 72 | "class C4 extends B {\n" + |
aoqi@0 | 73 | " public void m() {}\n" + |
aoqi@0 | 74 | "}"; |
aoqi@0 | 75 | |
aoqi@0 | 76 | //test legacy invocations |
aoqi@0 | 77 | static final String LegacyInvocationSrc = |
aoqi@0 | 78 | "class LegacyInvocation {\n" + |
aoqi@0 | 79 | " public static void test(I i, J j, A a, B b) {\n" + |
aoqi@0 | 80 | " i.m();\n" + |
aoqi@0 | 81 | " j.m();\n" + |
aoqi@0 | 82 | " a.m();\n" + |
aoqi@0 | 83 | " b.m();\n" + |
aoqi@0 | 84 | " }\n" + |
aoqi@0 | 85 | "}"; |
aoqi@0 | 86 | |
aoqi@0 | 87 | //test case super invocations |
aoqi@0 | 88 | static final String SubASrc = |
aoqi@0 | 89 | "class SubA extends A {\n" + |
aoqi@0 | 90 | " public void test() {\n" + |
aoqi@0 | 91 | " super.m();\n" + |
aoqi@0 | 92 | " }\n" + |
aoqi@0 | 93 | "}"; |
aoqi@0 | 94 | |
aoqi@0 | 95 | static final String SubBSrc = |
aoqi@0 | 96 | "class SubB extends B {\n" + |
aoqi@0 | 97 | " public void test() {\n" + |
aoqi@0 | 98 | " super.m();\n" + |
aoqi@0 | 99 | " }\n" + |
aoqi@0 | 100 | "}"; |
aoqi@0 | 101 | |
aoqi@0 | 102 | public static void main(String[] args) throws Exception { |
aoqi@0 | 103 | String[] sources = new String[] { |
aoqi@0 | 104 | "1.2", |
aoqi@0 | 105 | "1.3", |
aoqi@0 | 106 | "1.4", |
aoqi@0 | 107 | "1.5", |
aoqi@0 | 108 | "1.6", |
aoqi@0 | 109 | "1.7", |
aoqi@0 | 110 | }; |
aoqi@0 | 111 | for (String source : sources) { |
aoqi@0 | 112 | new DefaultMethodsNotVisibleForSourceLessThan8Test().run(source); |
aoqi@0 | 113 | } |
aoqi@0 | 114 | } |
aoqi@0 | 115 | |
aoqi@0 | 116 | String outDir; |
aoqi@0 | 117 | String source; |
aoqi@0 | 118 | |
aoqi@0 | 119 | void run(String source) throws Exception { |
aoqi@0 | 120 | this.source = source; |
aoqi@0 | 121 | outDir = "out" + source.replace('.', '_'); |
aoqi@0 | 122 | testsPreparation(); |
aoqi@0 | 123 | testLegacyImplementations(); |
aoqi@0 | 124 | testLegacyInvocations(); |
aoqi@0 | 125 | testSuperInvocations(); |
aoqi@0 | 126 | } |
aoqi@0 | 127 | |
aoqi@0 | 128 | void testsPreparation() throws Exception { |
aoqi@0 | 129 | Files.createDirectory(Paths.get(outDir)); |
aoqi@0 | 130 | |
aoqi@0 | 131 | /* as an extra check let's make sure that interface 'I' can't be compiled |
aoqi@0 | 132 | * with source < 8 |
aoqi@0 | 133 | */ |
aoqi@0 | 134 | ToolBox.JavaToolArgs javacArgs = |
aoqi@0 | 135 | new ToolBox.JavaToolArgs(ToolBox.Expect.FAIL) |
aoqi@0 | 136 | .setOptions("-d", outDir, "-source", source) |
aoqi@0 | 137 | .setSources(ISrc); |
aoqi@0 | 138 | ToolBox.javac(javacArgs); |
aoqi@0 | 139 | |
aoqi@0 | 140 | //but it should compile with source >= 8 |
aoqi@0 | 141 | javacArgs = |
aoqi@0 | 142 | new ToolBox.JavaToolArgs() |
aoqi@0 | 143 | .setOptions("-d", outDir) |
aoqi@0 | 144 | .setSources(ISrc); |
aoqi@0 | 145 | ToolBox.javac(javacArgs); |
aoqi@0 | 146 | |
aoqi@0 | 147 | javacArgs = |
aoqi@0 | 148 | new ToolBox.JavaToolArgs() |
aoqi@0 | 149 | .setOptions("-cp", outDir, "-d", outDir, "-source", source) |
aoqi@0 | 150 | .setSources(JSrc, ASrc, BSrc); |
aoqi@0 | 151 | ToolBox.javac(javacArgs); |
aoqi@0 | 152 | } |
aoqi@0 | 153 | |
aoqi@0 | 154 | void testLegacyImplementations() throws Exception { |
aoqi@0 | 155 | //compile C1-4 |
aoqi@0 | 156 | ToolBox.JavaToolArgs javacArgs = |
aoqi@0 | 157 | new ToolBox.JavaToolArgs() |
aoqi@0 | 158 | .setOptions("-cp", outDir, "-d", outDir, "-source", source) |
aoqi@0 | 159 | .setSources(C1Src, C2Src, C3Src, C4Src); |
aoqi@0 | 160 | ToolBox.javac(javacArgs); |
aoqi@0 | 161 | } |
aoqi@0 | 162 | |
aoqi@0 | 163 | void testLegacyInvocations() throws Exception { |
aoqi@0 | 164 | //compile LegacyInvocation |
aoqi@0 | 165 | ToolBox.JavaToolArgs javacArgs = |
aoqi@0 | 166 | new ToolBox.JavaToolArgs() |
aoqi@0 | 167 | .setOptions("-cp", outDir, "-d", outDir, "-source", source) |
aoqi@0 | 168 | .setSources(LegacyInvocationSrc); |
aoqi@0 | 169 | ToolBox.javac(javacArgs); |
aoqi@0 | 170 | } |
aoqi@0 | 171 | |
aoqi@0 | 172 | void testSuperInvocations() throws Exception { |
aoqi@0 | 173 | //compile SubA, SubB |
aoqi@0 | 174 | ToolBox.JavaToolArgs javacArgs = |
aoqi@0 | 175 | new ToolBox.JavaToolArgs() |
aoqi@0 | 176 | .setOptions("-cp", outDir, "-d", outDir, "-source", source) |
aoqi@0 | 177 | .setSources(SubASrc, SubBSrc); |
aoqi@0 | 178 | ToolBox.javac(javacArgs); |
aoqi@0 | 179 | } |
aoqi@0 | 180 | } |