6999067: cast for invokeExact call gets redundant cast to <type> warnings

Fri, 12 Nov 2010 12:33:52 +0000

author
mcimadamore
date
Fri, 12 Nov 2010 12:33:52 +0000
changeset 742
fdc67f5170e9
parent 741
58ceeff50af8
child 743
6a99b741a1b0

6999067: cast for invokeExact call gets redundant cast to <type> warnings
Summary: Xlint:cast should not report cast used in order to specify target type in polymorphic signature calls
Reviewed-by: jjg

src/share/classes/com/sun/tools/javac/comp/Flow.java file | annotate | diff | comparison | revisions
test/tools/javac/meth/XlintWarn.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Flow.java	Fri Nov 12 12:32:43 2010 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Flow.java	Fri Nov 12 12:33:52 2010 +0000
     1.3 @@ -1371,11 +1371,24 @@
     1.4          if (!tree.type.isErroneous()
     1.5              && lint.isEnabled(Lint.LintCategory.CAST)
     1.6              && types.isSameType(tree.expr.type, tree.clazz.type)
     1.7 -            && !(ignoreAnnotatedCasts && containsTypeAnnotation(tree.clazz))) {
     1.8 +            && !(ignoreAnnotatedCasts && containsTypeAnnotation(tree.clazz))
     1.9 +            && !is292targetTypeCast(tree)) {
    1.10              log.warning(Lint.LintCategory.CAST,
    1.11                      tree.pos(), "redundant.cast", tree.expr.type);
    1.12          }
    1.13      }
    1.14 +    //where
    1.15 +        private boolean is292targetTypeCast(JCTypeCast tree) {
    1.16 +            boolean is292targetTypeCast = false;
    1.17 +            if (tree.expr.getTag() == JCTree.APPLY) {
    1.18 +                JCMethodInvocation apply = (JCMethodInvocation)tree.expr;
    1.19 +                Symbol sym = TreeInfo.symbol(apply.meth);
    1.20 +                is292targetTypeCast = sym != null &&
    1.21 +                    sym.kind == MTH &&
    1.22 +                    (sym.flags() & POLYMORPHIC_SIGNATURE) != 0;
    1.23 +            }
    1.24 +            return is292targetTypeCast;
    1.25 +        }
    1.26  
    1.27      public void visitTopLevel(JCCompilationUnit tree) {
    1.28          // Do nothing for TopLevel since each class is visited individually
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/tools/javac/meth/XlintWarn.java	Fri Nov 12 12:33:52 2010 +0000
     2.3 @@ -0,0 +1,42 @@
     2.4 +/*
     2.5 + * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + *
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + *
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + *
    2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.23 + * or visit www.oracle.com if you need additional information or have any
    2.24 + * questions.
    2.25 + */
    2.26 +
    2.27 +/*
    2.28 + * @test
    2.29 + * @bug 6999067
    2.30 + * @summary cast for invokeExact call gets redundant cast to <type> warnings
    2.31 + * @author mcimadamore
    2.32 + *
    2.33 + * @compile -Werror -Xlint:cast XlintWarn.java
    2.34 + */
    2.35 +
    2.36 +import java.dyn.*;
    2.37 +
    2.38 +class XlintWarn {
    2.39 +    void test(MethodHandle mh) throws Throwable {
    2.40 +        int i1 = (int)mh.invoke();
    2.41 +        int i2 = (int)mh.invokeExact();
    2.42 +        int i3 = (int)mh.invokeVarargs();
    2.43 +        int i4 = (int)InvokeDynamic.test();
    2.44 +    }
    2.45 +}

mercurial