8006749: compiler does not allow Object protected methods to be used in lambda

Fri, 15 Feb 2013 16:28:07 +0000

author
mcimadamore
date
Fri, 15 Feb 2013 16:28:07 +0000
changeset 1579
0baaae675b19
parent 1577
88286a36bb34
child 1580
f6e667f52af4

8006749: compiler does not allow Object protected methods to be used in lambda
Summary: Check.checkFunctionalInterface should take into account 'fake' override
Reviewed-by: jjg

src/share/classes/com/sun/tools/javac/code/Types.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/comp/Check.java file | annotate | diff | comparison | revisions
test/tools/javac/lambda/LambdaConv26.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/code/Types.java	Thu Feb 14 09:43:00 2013 -0800
     1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java	Fri Feb 15 16:28:07 2013 +0000
     1.3 @@ -356,26 +356,11 @@
     1.4              }
     1.5  
     1.6              public Type getType(Type site) {
     1.7 -                if (capture(site) != site) {
     1.8 -                    Type formalInterface = site.tsym.type;
     1.9 -                    ListBuffer<Type> typeargs = ListBuffer.lb();
    1.10 -                    List<Type> actualTypeargs = site.getTypeArguments();
    1.11 -                    //simply replace the wildcards with its bound
    1.12 -                    for (Type t : formalInterface.getTypeArguments()) {
    1.13 -                        if (actualTypeargs.head.hasTag(WILDCARD)) {
    1.14 -                            WildcardType wt = (WildcardType)actualTypeargs.head;
    1.15 -                            typeargs.append(wt.type);
    1.16 -                        } else {
    1.17 -                            typeargs.append(actualTypeargs.head);
    1.18 -                        }
    1.19 -                        actualTypeargs = actualTypeargs.tail;
    1.20 -                    }
    1.21 -                    site = subst(formalInterface, formalInterface.getTypeArguments(), typeargs.toList());
    1.22 -                    if (!chk.checkValidGenericType(site)) {
    1.23 -                        //if the inferred functional interface type is not well-formed,
    1.24 -                        //or if it's not a subtype of the original target, issue an error
    1.25 -                        throw failure(diags.fragment("no.suitable.functional.intf.inst", site));
    1.26 -                    }
    1.27 +                site = removeWildcards(site);
    1.28 +                if (!chk.checkValidGenericType(site)) {
    1.29 +                    //if the inferred functional interface type is not well-formed,
    1.30 +                    //or if it's not a subtype of the original target, issue an error
    1.31 +                    throw failure(diags.fragment("no.suitable.functional.intf.inst", site));
    1.32                  }
    1.33                  return memberType(site, descSym);
    1.34              }
    1.35 @@ -584,6 +569,27 @@
    1.36              return false;
    1.37          }
    1.38      }
    1.39 +
    1.40 +    public Type removeWildcards(Type site) {
    1.41 +        if (capture(site) != site) {
    1.42 +            Type formalInterface = site.tsym.type;
    1.43 +            ListBuffer<Type> typeargs = ListBuffer.lb();
    1.44 +            List<Type> actualTypeargs = site.getTypeArguments();
    1.45 +            //simply replace the wildcards with its bound
    1.46 +            for (Type t : formalInterface.getTypeArguments()) {
    1.47 +                if (actualTypeargs.head.hasTag(WILDCARD)) {
    1.48 +                    WildcardType wt = (WildcardType)actualTypeargs.head;
    1.49 +                    typeargs.append(wt.type);
    1.50 +                } else {
    1.51 +                    typeargs.append(actualTypeargs.head);
    1.52 +                }
    1.53 +                actualTypeargs = actualTypeargs.tail;
    1.54 +            }
    1.55 +            return subst(formalInterface, formalInterface.getTypeArguments(), typeargs.toList());
    1.56 +        } else {
    1.57 +            return site;
    1.58 +        }
    1.59 +    }
    1.60      // </editor-fold>
    1.61  
    1.62     /**
     2.1 --- a/src/share/classes/com/sun/tools/javac/comp/Check.java	Thu Feb 14 09:43:00 2013 -0800
     2.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Check.java	Fri Feb 15 16:28:07 2013 +0000
     2.3 @@ -2232,10 +2232,13 @@
     2.4      void checkFunctionalInterface(JCTree tree, Type funcInterface) {
     2.5          ClassType c = new ClassType(Type.noType, List.<Type>nil(), null);
     2.6          ClassSymbol csym = new ClassSymbol(0, names.empty, c, syms.noSymbol);
     2.7 -        c.interfaces_field = List.of(funcInterface);
     2.8 +        c.interfaces_field = List.of(types.removeWildcards(funcInterface));
     2.9          c.supertype_field = syms.objectType;
    2.10          c.tsym = csym;
    2.11          csym.members_field = new Scope(csym);
    2.12 +        Symbol descSym = types.findDescriptorSymbol(funcInterface.tsym);
    2.13 +        Type descType = types.findDescriptorType(funcInterface);
    2.14 +        csym.members_field.enter(new MethodSymbol(PUBLIC, descSym.name, descType, csym));
    2.15          csym.completer = null;
    2.16          checkImplementations(tree, csym, csym);
    2.17      }
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/tools/javac/lambda/LambdaConv26.java	Fri Feb 15 16:28:07 2013 +0000
     3.3 @@ -0,0 +1,41 @@
     3.4 +/*
     3.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.7 + *
     3.8 + * This code is free software; you can redistribute it and/or modify it
     3.9 + * under the terms of the GNU General Public License version 2 only, as
    3.10 + * published by the Free Software Foundation.
    3.11 + *
    3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    3.15 + * version 2 for more details (a copy is included in the LICENSE file that
    3.16 + * accompanied this code).
    3.17 + *
    3.18 + * You should have received a copy of the GNU General Public License version
    3.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    3.21 + *
    3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    3.23 + * or visit www.oracle.com if you need additional information or have any
    3.24 + * questions.
    3.25 + */
    3.26 +
    3.27 +/*
    3.28 + * @test
    3.29 + * @bug 8006749
    3.30 + * @summary compiler does not allow Object protected methods to be used in lambda
    3.31 + * @compile LambdaConv26.java
    3.32 + */
    3.33 +public class LambdaConv26 {
    3.34 +    interface I {
    3.35 +        Object clone();
    3.36 +    }
    3.37 +
    3.38 +    Object m() { return null; }
    3.39 +
    3.40 +    void test() {
    3.41 +        I i1 = ()->null;
    3.42 +        I i2 = this::m;
    3.43 +    }
    3.44 +}

mercurial