src/share/classes/com/sun/tools/javac/comp/Check.java

changeset 1237
568e70bbd9aa
parent 1226
97bec6ab1227
child 1238
e28a06a3c5d9
     1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Check.java	Wed Mar 14 13:53:41 2012 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Check.java	Mon Mar 26 15:27:51 2012 +0100
     1.3 @@ -516,6 +516,35 @@
     1.4                               found, req);
     1.5          }
     1.6      }
     1.7 +
     1.8 +    /** Check for redundant casts (i.e. where source type is a subtype of target type)
     1.9 +     * The problem should only be reported for non-292 cast
    1.10 +     */
    1.11 +    public void checkRedundantCast(Env<AttrContext> env, JCTypeCast tree) {
    1.12 +        if (!tree.type.isErroneous() &&
    1.13 +            (env.info.lint == null || env.info.lint.isEnabled(Lint.LintCategory.CAST))
    1.14 +            && types.isSameType(tree.expr.type, tree.clazz.type)
    1.15 +            && !is292targetTypeCast(tree)) {
    1.16 +            log.warning(Lint.LintCategory.CAST,
    1.17 +                    tree.pos(), "redundant.cast", tree.expr.type);
    1.18 +        }
    1.19 +    }
    1.20 +    //where
    1.21 +            private boolean is292targetTypeCast(JCTypeCast tree) {
    1.22 +                boolean is292targetTypeCast = false;
    1.23 +                JCExpression expr = TreeInfo.skipParens(tree.expr);
    1.24 +                if (expr.hasTag(APPLY)) {
    1.25 +                    JCMethodInvocation apply = (JCMethodInvocation)expr;
    1.26 +                    Symbol sym = TreeInfo.symbol(apply.meth);
    1.27 +                    is292targetTypeCast = sym != null &&
    1.28 +                        sym.kind == MTH &&
    1.29 +                        (sym.flags() & POLYMORPHIC_SIGNATURE) != 0;
    1.30 +                }
    1.31 +                return is292targetTypeCast;
    1.32 +            }
    1.33 +
    1.34 +
    1.35 +
    1.36  //where
    1.37          /** Is type a type variable, or a (possibly multi-dimensional) array of
    1.38           *  type variables?

mercurial