8029240: Default methods not always visible under -source 7

Thu, 06 Feb 2014 21:03:03 +0000

author
vromero
date
Thu, 06 Feb 2014 21:03:03 +0000
changeset 2260
fb870c70e774
parent 2259
fcebccb14d4b
child 2261
79dc4b992c0a

8029240: Default methods not always visible under -source 7
Reviewed-by: jjg

src/share/classes/com/sun/tools/javac/code/Source.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/code/Types.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/comp/Resolve.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/jvm/ClassReader.java file | annotate | diff | comparison | revisions
test/tools/javac/T8029240/DefaultMethodsNotVisibileForSource7Test.java file | annotate | diff | comparison | revisions
test/tools/javac/scope/7046348/EagerInterfaceCompletionTest.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/code/Source.java	Tue Feb 04 18:49:58 2014 -0800
     1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Source.java	Thu Feb 06 21:03:03 2014 +0000
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.
     1.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8   *
     1.9   * This code is free software; you can redistribute it and/or modify it
    1.10 @@ -203,6 +203,9 @@
    1.11      public boolean allowDefaultMethods() {
    1.12          return compareTo(JDK1_8) >= 0;
    1.13      }
    1.14 +    public boolean allowDefaultMethodsResolution() {
    1.15 +        return compareTo(JDK1_7) >= 0;
    1.16 +    }
    1.17      public boolean allowStaticInterfaceMethods() {
    1.18          return compareTo(JDK1_8) >= 0;
    1.19      }
     2.1 --- a/src/share/classes/com/sun/tools/javac/code/Types.java	Tue Feb 04 18:49:58 2014 -0800
     2.2 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java	Thu Feb 06 21:03:03 2014 +0000
     2.3 @@ -1,5 +1,5 @@
     2.4  /*
     2.5 - * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
     2.6 + * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
     2.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.8   *
     2.9   * This code is free software; you can redistribute it and/or modify it
    2.10 @@ -84,7 +84,6 @@
    2.11      final boolean allowBoxing;
    2.12      final boolean allowCovariantReturns;
    2.13      final boolean allowObjectToPrimitiveCast;
    2.14 -    final boolean allowDefaultMethods;
    2.15      final ClassReader reader;
    2.16      final Check chk;
    2.17      final Enter enter;
    2.18 @@ -111,7 +110,6 @@
    2.19          allowBoxing = source.allowBoxing();
    2.20          allowCovariantReturns = source.allowCovariantReturns();
    2.21          allowObjectToPrimitiveCast = source.allowObjectToPrimitiveCast();
    2.22 -        allowDefaultMethods = source.allowDefaultMethods();
    2.23          reader = ClassReader.instance(context);
    2.24          chk = Check.instance(context);
    2.25          enter = Enter.instance(context);
     3.1 --- a/src/share/classes/com/sun/tools/javac/comp/Resolve.java	Tue Feb 04 18:49:58 2014 -0800
     3.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Resolve.java	Thu Feb 06 21:03:03 2014 +0000
     3.3 @@ -1,5 +1,5 @@
     3.4  /*
     3.5 - * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
     3.6 + * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
     3.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.8   *
     3.9   * This code is free software; you can redistribute it and/or modify it
    3.10 @@ -95,7 +95,7 @@
    3.11      public final boolean boxingEnabled; // = source.allowBoxing();
    3.12      public final boolean varargsEnabled; // = source.allowVarargs();
    3.13      public final boolean allowMethodHandles;
    3.14 -    public final boolean allowDefaultMethods;
    3.15 +    public final boolean allowDefaultMethodsResolution;
    3.16      public final boolean allowStructuralMostSpecific;
    3.17      private final boolean debugResolve;
    3.18      private final boolean compactMethodDiags;
    3.19 @@ -137,7 +137,7 @@
    3.20          verboseResolutionMode = VerboseResolutionMode.getVerboseResolutionMode(options);
    3.21          Target target = Target.instance(context);
    3.22          allowMethodHandles = target.hasMethodHandles();
    3.23 -        allowDefaultMethods = source.allowDefaultMethods();
    3.24 +        allowDefaultMethodsResolution = source.allowDefaultMethodsResolution();
    3.25          allowStructuralMostSpecific = source.allowStructuralMostSpecific();
    3.26          polymorphicSignatureScope = new Scope(syms.noSymbol);
    3.27  
    3.28 @@ -1681,7 +1681,7 @@
    3.29                  bestSoFar : methodNotFound;
    3.30  
    3.31          for (InterfaceLookupPhase iphase2 : InterfaceLookupPhase.values()) {
    3.32 -            if (iphase2 == InterfaceLookupPhase.DEFAULT_OK && !allowDefaultMethods) break;
    3.33 +            if (iphase2 == InterfaceLookupPhase.DEFAULT_OK && !allowDefaultMethodsResolution) break;
    3.34              //keep searching for abstract methods
    3.35              for (Type itype : itypes[iphase2.ordinal()]) {
    3.36                  if (!itype.isInterface()) continue; //skip j.l.Object (included by Types.closure())
    3.37 @@ -1714,7 +1714,7 @@
    3.38                  //from superinterfaces)
    3.39                  if ((s.flags() & (ABSTRACT | INTERFACE | ENUM)) != 0) {
    3.40                      return this;
    3.41 -                } else if (rs.allowDefaultMethods) {
    3.42 +                } else if (rs.allowDefaultMethodsResolution) {
    3.43                      return DEFAULT_OK;
    3.44                  } else {
    3.45                      return null;
    3.46 @@ -3341,7 +3341,7 @@
    3.47              if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
    3.48              env1 = env1.outer;
    3.49          }
    3.50 -        if (allowDefaultMethods && c.isInterface() &&
    3.51 +        if (allowDefaultMethodsResolution && c.isInterface() &&
    3.52                  name == names._super && !isStatic(env) &&
    3.53                  types.isDirectSuperInterface(c, env.enclClass.sym)) {
    3.54              //this might be a default super call if one of the superinterfaces is 'c'
     4.1 --- a/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Tue Feb 04 18:49:58 2014 -0800
     4.2 +++ b/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Thu Feb 06 21:03:03 2014 +0000
     4.3 @@ -1,5 +1,5 @@
     4.4  /*
     4.5 - * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
     4.6 + * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
     4.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4.8   *
     4.9   * This code is free software; you can redistribute it and/or modify it
    4.10 @@ -116,10 +116,6 @@
    4.11       */
    4.12      boolean lintClassfile;
    4.13  
    4.14 -    /** Switch: allow default methods
    4.15 -     */
    4.16 -    boolean allowDefaultMethods;
    4.17 -
    4.18      /** Switch: preserve parameter names from the variable table.
    4.19       */
    4.20      public boolean saveParameterNames;
    4.21 @@ -307,7 +303,6 @@
    4.22          allowVarargs     = source.allowVarargs();
    4.23          allowAnnotations = source.allowAnnotations();
    4.24          allowSimplifiedVarargs = source.allowSimplifiedVarargs();
    4.25 -        allowDefaultMethods = source.allowDefaultMethods();
    4.26  
    4.27          saveParameterNames = options.isSet("save-parameter-names");
    4.28          cacheCompletionFailure = options.isUnset("dev");
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/test/tools/javac/T8029240/DefaultMethodsNotVisibileForSource7Test.java	Thu Feb 06 21:03:03 2014 +0000
     5.3 @@ -0,0 +1,164 @@
     5.4 +/*
     5.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
     5.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     5.7 + *
     5.8 + * This code is free software; you can redistribute it and/or modify it
     5.9 + * under the terms of the GNU General Public License version 2 only, as
    5.10 + * published by the Free Software Foundation.
    5.11 + *
    5.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    5.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    5.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    5.15 + * version 2 for more details (a copy is included in the LICENSE file that
    5.16 + * accompanied this code).
    5.17 + *
    5.18 + * You should have received a copy of the GNU General Public License version
    5.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    5.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    5.21 + *
    5.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    5.23 + * or visit www.oracle.com if you need additional information or have any
    5.24 + * questions.
    5.25 + */
    5.26 +
    5.27 +/*
    5.28 + * @test
    5.29 + * @bug 8029240
    5.30 + * @summary Default methods not always visible under -source 7
    5.31 + * @library /tools/javac/lib
    5.32 + * @build ToolBox
    5.33 + * @run main DefaultMethodsNotVisibileForSource7Test
    5.34 + */
    5.35 +
    5.36 +import java.nio.file.Files;
    5.37 +import java.nio.file.Paths;
    5.38 +
    5.39 +public class DefaultMethodsNotVisibileForSource7Test {
    5.40 +    // common definitions
    5.41 +
    5.42 +    // this one should be compiled with source 8, the rest with source 7
    5.43 +    static final String ISrc =
    5.44 +        "interface I {\n" +
    5.45 +        "    default void m() {}\n" +
    5.46 +        "}";
    5.47 +
    5.48 +    static final String JSrc =
    5.49 +        "interface J extends I {}";
    5.50 +
    5.51 +    static final String ASrc =
    5.52 +        "abstract class A implements I {}";
    5.53 +
    5.54 +    static final String BSrc =
    5.55 +        "class B implements I {}";
    5.56 +
    5.57 +    // test legacy implementations
    5.58 +    static final String C1Src =
    5.59 +        "class C1 implements I {\n" +
    5.60 +        "    @Override public void m() {}\n" +
    5.61 +        "}";
    5.62 +
    5.63 +    static final String C2Src =
    5.64 +        "class C2 implements J {\n" +
    5.65 +        "    @Override public void m() {}\n" +
    5.66 +        "}";
    5.67 +
    5.68 +    static final String C3Src =
    5.69 +        "class C3 extends A {\n" +
    5.70 +        "    @Override public void m() {}\n" +
    5.71 +        "}";
    5.72 +
    5.73 +    static final String C4Src =
    5.74 +        "class C4 extends B {\n" +
    5.75 +        "    @Override public void m() {}\n" +
    5.76 +        "}";
    5.77 +
    5.78 +    //test legacy invocations
    5.79 +    static final String LegacyInvocationSrc =
    5.80 +        "class LegacyInvocation {\n" +
    5.81 +        "    public static void test(I i, J j, A a, B b) {\n" +
    5.82 +        "        i.m();\n" +
    5.83 +        "        j.m();\n" +
    5.84 +        "        a.m();\n" +
    5.85 +        "        b.m();\n" +
    5.86 +        "    }\n" +
    5.87 +        "}";
    5.88 +
    5.89 +    //test case super invocations
    5.90 +    static final String SubASrc =
    5.91 +        "class SubA extends A {\n" +
    5.92 +        "    public void test() {\n" +
    5.93 +        "        super.m();\n" +
    5.94 +        "    }\n" +
    5.95 +        "}";
    5.96 +
    5.97 +    static final String SubBSrc =
    5.98 +        "class SubB extends B {\n" +
    5.99 +        "    public void test() {\n" +
   5.100 +        "        super.m();\n" +
   5.101 +        "    }\n" +
   5.102 +        "}";
   5.103 +
   5.104 +    public static void main(String[] args) throws Exception {
   5.105 +        new DefaultMethodsNotVisibileForSource7Test().run();
   5.106 +    }
   5.107 +
   5.108 +    void run() throws Exception {
   5.109 +        testsPreparation();
   5.110 +        testLegacyImplementations();
   5.111 +        testLegacyInvocations();
   5.112 +        testSuperInvocations();
   5.113 +    }
   5.114 +
   5.115 +    void testsPreparation() throws Exception {
   5.116 +        Files.createDirectory(Paths.get("out"));
   5.117 +
   5.118 +        /* as an extra check let's make sure that interface 'I' can't be compiled
   5.119 +         * with source 7
   5.120 +         */
   5.121 +        ToolBox.JavaToolArgs javacArgs =
   5.122 +                new ToolBox.JavaToolArgs(ToolBox.Expect.FAIL)
   5.123 +                .setOptions("-d", "out", "-source", "7")
   5.124 +                .setSources(ISrc);
   5.125 +        ToolBox.javac(javacArgs);
   5.126 +
   5.127 +        //but it should compile with source >= 8
   5.128 +        javacArgs =
   5.129 +                new ToolBox.JavaToolArgs()
   5.130 +                .setOptions("-d", "out")
   5.131 +                .setSources(ISrc);
   5.132 +        ToolBox.javac(javacArgs);
   5.133 +
   5.134 +        javacArgs =
   5.135 +                new ToolBox.JavaToolArgs()
   5.136 +                .setOptions("-cp", "out", "-d", "out", "-source", "7")
   5.137 +                .setSources(JSrc, ASrc, BSrc);
   5.138 +        ToolBox.javac(javacArgs);
   5.139 +    }
   5.140 +
   5.141 +    void testLegacyImplementations() throws Exception {
   5.142 +        //compile C1-4
   5.143 +        ToolBox.JavaToolArgs javacArgs =
   5.144 +                new ToolBox.JavaToolArgs()
   5.145 +                .setOptions("-cp", "out", "-d", "out", "-source", "7")
   5.146 +                .setSources(C1Src, C2Src, C3Src, C4Src);
   5.147 +        ToolBox.javac(javacArgs);
   5.148 +    }
   5.149 +
   5.150 +    void testLegacyInvocations() throws Exception {
   5.151 +        //compile LegacyInvocation
   5.152 +        ToolBox.JavaToolArgs javacArgs =
   5.153 +                new ToolBox.JavaToolArgs()
   5.154 +                .setOptions("-cp", "out", "-d", "out", "-source", "7")
   5.155 +                .setSources(LegacyInvocationSrc);
   5.156 +        ToolBox.javac(javacArgs);
   5.157 +    }
   5.158 +
   5.159 +    void testSuperInvocations() throws Exception {
   5.160 +        //compile SubA, SubB
   5.161 +        ToolBox.JavaToolArgs javacArgs =
   5.162 +                new ToolBox.JavaToolArgs()
   5.163 +                .setOptions("-cp", "out", "-d", "out", "-source", "7")
   5.164 +                .setSources(SubASrc, SubBSrc);
   5.165 +        ToolBox.javac(javacArgs);
   5.166 +    }
   5.167 +}
     6.1 --- a/test/tools/javac/scope/7046348/EagerInterfaceCompletionTest.java	Tue Feb 04 18:49:58 2014 -0800
     6.2 +++ b/test/tools/javac/scope/7046348/EagerInterfaceCompletionTest.java	Thu Feb 06 21:03:03 2014 +0000
     6.3 @@ -1,5 +1,5 @@
     6.4  /*
     6.5 - * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     6.6 + * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
     6.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     6.8   *
     6.9   * This code is free software; you can redistribute it and/or modify it
    6.10 @@ -181,7 +181,7 @@
    6.11                  case FIELD:
    6.12                  case SUPER: return true;
    6.13                  case METHOD: return hk != HierarchyKind.INTERFACE || ak == ActionKind.REMOVE_B ||
    6.14 -                        (hk == HierarchyKind.INTERFACE && ak == ActionKind.REMOVE_A && vk == VersionKind.LAMBDA);
    6.15 +                        (hk == HierarchyKind.INTERFACE && ak == ActionKind.REMOVE_A);
    6.16                  default: throw new AssertionError("Unexpected test kind " + this);
    6.17              }
    6.18          }

mercurial