1 /*
   2  * Copyright (c) 2014, 2016, 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 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/lib
  30  * @modules jdk.compiler/com.sun.tools.javac.api
  31  *          jdk.compiler/com.sun.tools.javac.main
  32  * @build toolbox.ToolBox toolbox.JavacTask
  33  * @run main DefaultMethodsNotVisibleForSourceLessThan8Test
  34  */
  35 
  36 import java.nio.file.Files;
  37 import java.nio.file.Paths;
  38 
  39 import toolbox.JavacTask;
  40 import toolbox.Task;
  41 import toolbox.ToolBox;
  42 
  43 public class DefaultMethodsNotVisibleForSourceLessThan8Test {
  44     // common definitions
  45 
  46     // this one should be compiled with source 8, the rest with source < 8
  47     static final String ISrc =
  48         "interface I {\n" +
  49         "    default void m() {}\n" +
  50         "}";
  51 
  52     static final String JSrc =
  53         "interface J extends I {}";
  54 
  55     static final String ASrc =
  56         "abstract class A implements I {}";
  57 
  58     static final String BSrc =
  59         "class B implements I {}";
  60 
  61     // test legacy implementations
  62     static final String C1Src =
  63         "class C1 implements I {\n" +
  64         "    public void m() {}\n" +
  65         "}";
  66 
  67     static final String C2Src =
  68         "class C2 implements J {\n" +
  69         "    public void m() {}\n" +
  70         "}";
  71 
  72     static final String C3Src =
  73         "class C3 extends A {\n" +
  74         "    public void m() {}\n" +
  75         "}";
  76 
  77     static final String C4Src =
  78         "class C4 extends B {\n" +
  79         "    public void m() {}\n" +
  80         "}";
  81 
  82     //test legacy invocations
  83     static final String LegacyInvocationSrc =
  84         "class LegacyInvocation {\n" +
  85         "    public static void test(I i, J j, A a, B b) {\n" +
  86         "        i.m();\n" +
  87         "        j.m();\n" +
  88         "        a.m();\n" +
  89         "        b.m();\n" +
  90         "    }\n" +
  91         "}";
  92 
  93     //test case super invocations
  94     static final String SubASrc =
  95         "class SubA extends A {\n" +
  96         "    public void test() {\n" +
  97         "        super.m();\n" +
  98         "    }\n" +
  99         "}";
 100 
 101     static final String SubBSrc =
 102         "class SubB extends B {\n" +
 103         "    public void test() {\n" +
 104         "        super.m();\n" +
 105         "    }\n" +
 106         "}";
 107 
 108     public static void main(String[] args) throws Exception {
 109         String[] sources = new String[] {
 110             "1.6",
 111             "1.7",
 112         };
 113         for (String source : sources) {
 114             new DefaultMethodsNotVisibleForSourceLessThan8Test().run(source);
 115         }
 116     }
 117 
 118     String outDir;
 119     String source;
 120     ToolBox tb = new ToolBox();
 121 
 122     void run(String source) throws Exception {
 123         this.source = source;
 124         outDir = "out" + source.replace('.', '_');
 125         testsPreparation();
 126         testLegacyImplementations();
 127         testLegacyInvocations();
 128         testSuperInvocations();
 129     }
 130 
 131     void testsPreparation() throws Exception {
 132         Files.createDirectory(Paths.get(outDir));
 133 
 134         /* as an extra check let's make sure that interface 'I' can't be compiled
 135          * with source < 8
 136          */
 137         new JavacTask(tb)
 138                 .outdir(outDir)
 139                 .options("-source", source)
 140                 .sources(ISrc)
 141                 .run(Task.Expect.FAIL);
 142 
 143         //but it should compile with source >= 8
 144         new JavacTask(tb)
 145                 .outdir(outDir)
 146                 .sources(ISrc)
 147                 .run();
 148 
 149         new JavacTask(tb)
 150                 .outdir(outDir)
 151                 .classpath(outDir)
 152                 .options("-source", source)
 153                 .sources(JSrc, ASrc, BSrc)
 154                 .run();
 155     }
 156 
 157     void testLegacyImplementations() throws Exception {
 158         //compile C1-4
 159         new JavacTask(tb)
 160                 .outdir(outDir)
 161                 .classpath(outDir)
 162                 .options("-source", source)
 163                 .sources(C1Src, C2Src, C3Src, C4Src)
 164                 .run();
 165     }
 166 
 167     void testLegacyInvocations() throws Exception {
 168         //compile LegacyInvocation
 169         new JavacTask(tb)
 170                 .outdir(outDir)
 171                 .classpath(outDir)
 172                 .options("-source", source)
 173                 .sources(LegacyInvocationSrc)
 174                 .run();
 175     }
 176 
 177     void testSuperInvocations() throws Exception {
 178         //compile SubA, SubB
 179         new JavacTask(tb)
 180                 .outdir(outDir)
 181                 .classpath(outDir)
 182                 .options("-source", source)
 183                 .sources(SubASrc, SubBSrc)
 184                 .run();
 185     }
 186 }