# HG changeset patch # User mcimadamore # Date 1282215178 -3600 # Node ID 84c44db80d067cac427d8ccc3bca2b98e5d6d48c # Parent 7070b0722ebbb08bb5432e7b472a538ea67bae22 6885255: Improve usability of raw warnings Summary: raw warnings should be disabled in (i) instanceof expressions and (ii) when java.lang.Class is not parameterized Reviewed-by: jjg diff -r 7070b0722ebb -r 84c44db80d06 langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java --- a/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java Thu Aug 19 11:50:50 2010 +0100 +++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java Thu Aug 19 11:52:58 2010 +0100 @@ -2008,7 +2008,7 @@ public void visitTypeCast(JCTypeCast tree) { Type clazztype = attribType(tree.clazz, env); - chk.validate(tree.clazz, env); + chk.validate(tree.clazz, env, false); Type exprtype = attribExpr(tree.expr, env, Infer.anyPoly); Type owntype = chk.checkCastable(tree.expr.pos(), exprtype, clazztype); if (exprtype.constValue() != null) @@ -2021,7 +2021,7 @@ tree.expr.pos(), attribExpr(tree.expr, env)); Type clazztype = chk.checkReifiableReferenceType( tree.clazz.pos(), attribType(tree.clazz, env)); - chk.validate(tree.clazz, env); + chk.validate(tree.clazz, env, false); chk.checkCastable(tree.expr.pos(), exprtype, clazztype); result = check(tree, syms.booleanType, VAL, pkind, pt); } diff -r 7070b0722ebb -r 84c44db80d06 langtools/src/share/classes/com/sun/tools/javac/comp/Check.java --- a/langtools/src/share/classes/com/sun/tools/javac/comp/Check.java Thu Aug 19 11:50:50 2010 +0100 +++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Check.java Thu Aug 19 11:52:58 2010 +0100 @@ -906,33 +906,15 @@ * * and we can't make sure that the bound is already attributed because * of possible cycles. - */ - private Validator validator = new Validator(); - - /** Visitor method: Validate a type expression, if it is not null, catching + * + * Visitor method: Validate a type expression, if it is not null, catching * and reporting any completion failures. */ void validate(JCTree tree, Env env) { - try { - if (tree != null) { - validator.env = env; - tree.accept(validator); - checkRaw(tree, env); - } - } catch (CompletionFailure ex) { - completionError(tree.pos(), ex); - } + validate(tree, env, true); } - //where - void checkRaw(JCTree tree, Env env) { - if (lint.isEnabled(Lint.LintCategory.RAW) && - tree.type.tag == CLASS && - !TreeInfo.isDiamond(tree) && - !env.enclClass.name.isEmpty() && //anonymous or intersection - tree.type.isRaw()) { - log.warning(Lint.LintCategory.RAW, - tree.pos(), "raw.class.use", tree.type, tree.type.tsym.type); - } + void validate(JCTree tree, Env env, boolean checkRaw) { + new Validator(env).validateTree(tree, checkRaw, true); } /** Visitor method: Validate a list of type expressions. @@ -946,9 +928,16 @@ */ class Validator extends JCTree.Visitor { + boolean isOuter; + Env env; + + Validator(Env env) { + this.env = env; + } + @Override public void visitTypeArray(JCArrayTypeTree tree) { - validate(tree.elemtype, env); + tree.elemtype.accept(this); } @Override @@ -960,10 +949,14 @@ List forms = tree.type.tsym.type.getTypeArguments(); ListBuffer tvars_buf = new ListBuffer(); + boolean is_java_lang_Class = tree.type.tsym.flatName() == names.java_lang_Class; + // For matching pairs of actual argument types `a' and // formal type parameters with declared bound `b' ... while (args.nonEmpty() && forms.nonEmpty()) { - validate(args.head, env); + validateTree(args.head, + !(isOuter && is_java_lang_Class), + false); // exact type arguments needs to know their // bounds (for upper and lower bound @@ -1015,14 +1008,14 @@ @Override public void visitTypeParameter(JCTypeParameter tree) { - validate(tree.bounds, env); + validateTrees(tree.bounds, true, isOuter); checkClassBounds(tree.pos(), tree.type); } @Override public void visitWildcard(JCWildcard tree) { if (tree.inner != null) - validate(tree.inner, env); + validateTree(tree.inner, true, isOuter); } @Override @@ -1060,7 +1053,34 @@ public void visitTree(JCTree tree) { } - Env env; + public void validateTree(JCTree tree, boolean checkRaw, boolean isOuter) { + try { + if (tree != null) { + this.isOuter = isOuter; + tree.accept(this); + if (checkRaw) + checkRaw(tree, env); + } + } catch (CompletionFailure ex) { + completionError(tree.pos(), ex); + } + } + + public void validateTrees(List trees, boolean checkRaw, boolean isOuter) { + for (List l = trees; l.nonEmpty(); l = l.tail) + validateTree(l.head, checkRaw, isOuter); + } + + void checkRaw(JCTree tree, Env env) { + if (lint.isEnabled(Lint.LintCategory.RAW) && + tree.type.tag == CLASS && + !TreeInfo.isDiamond(tree) && + !env.enclClass.name.isEmpty() && //anonymous or intersection + tree.type.isRaw()) { + log.warning(Lint.LintCategory.RAW, + tree.pos(), "raw.class.use", tree.type, tree.type.tsym.type); + } + } } /* ************************************************************************* diff -r 7070b0722ebb -r 84c44db80d06 langtools/test/tools/javac/warnings/6747671/T6747671.java --- a/langtools/test/tools/javac/warnings/6747671/T6747671.java Thu Aug 19 11:50:50 2010 +0100 +++ b/langtools/test/tools/javac/warnings/6747671/T6747671.java Thu Aug 19 11:52:58 2010 +0100 @@ -27,8 +27,8 @@ A.Z> z3;//raw warning (2) void test(Object arg1, B arg2) {//raw warning - boolean b = arg1 instanceof A;//raw warning - Object a = (A)arg1;//raw warning + boolean b = arg1 instanceof A;//ok + Object a = (A)arg1;//ok A a2 = new A() {};//raw warning (2) a2.new Z() {};//raw warning } diff -r 7070b0722ebb -r 84c44db80d06 langtools/test/tools/javac/warnings/6747671/T6747671.out --- a/langtools/test/tools/javac/warnings/6747671/T6747671.out Thu Aug 19 11:50:50 2010 +0100 +++ b/langtools/test/tools/javac/warnings/6747671/T6747671.out Thu Aug 19 11:52:58 2010 +0100 @@ -4,9 +4,7 @@ T6747671.java:27:14: compiler.warn.raw.class.use: T6747671.B, T6747671.B T6747671.java:27:7: compiler.warn.raw.class.use: T6747671.B, T6747671.B T6747671.java:29:28: compiler.warn.raw.class.use: T6747671.B, T6747671.B -T6747671.java:30:37: compiler.warn.raw.class.use: T6747671.A, T6747671.A -T6747671.java:31:21: compiler.warn.raw.class.use: T6747671.A, T6747671.A T6747671.java:32:9: compiler.warn.raw.class.use: T6747671.A, T6747671.A T6747671.java:32:20: compiler.warn.raw.class.use: T6747671.A, T6747671.A T6747671.java:33:16: compiler.warn.raw.class.use: T6747671.A.Z, T6747671.A.Z -11 warnings +9 warnings diff -r 7070b0722ebb -r 84c44db80d06 langtools/test/tools/javac/warnings/6885255/T6885255.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/langtools/test/tools/javac/warnings/6885255/T6885255.java Thu Aug 19 11:52:58 2010 +0100 @@ -0,0 +1,31 @@ +/** + * @test /nodynamiccopyright/ + * @bug 6885255 + * @summary -Xlint:rawtypes + * @compile/ref=T6885255.out -XDrawDiagnostics -Xlint:rawtypes T6885255.java + */ + +class T6885255 { + + static class Test {} + + Class ct; //no warn - outer Class w/ raw param + Class> ctt; //warn - outer Class w/o raw param (2) + + Class> cct; //warn - outer Class w/o raw param + Class>> cctt; //warn - outer Class w/o raw param (2) + + Object o1 = (Test)null; //no warn - outer raw and cast + Object o2 = (Test)null; //warn - inner raw (2) + + Object o3 = (Class)null; //no warn - outer raw and cast + Object o4 = (Class)null; //no warn - outer Class w/ raw param + + Object o5 = (Class>)null; //warn - outer Class w/ non raw param (2) + Object o6 = (Class>>)null; //warn - outer Class w/ non raw param (2) + + Object o7 = (Test)null; //warn - inner raw (2) + Object o8 = (Test, Class>)null; //warn - inner Class (2) + + boolean b = null instanceof Test; //no warn - raw and instanceof +} diff -r 7070b0722ebb -r 84c44db80d06 langtools/test/tools/javac/warnings/6885255/T6885255.out --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/langtools/test/tools/javac/warnings/6885255/T6885255.out Thu Aug 19 11:52:58 2010 +0100 @@ -0,0 +1,16 @@ +T6885255.java:13:16: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test +T6885255.java:13:22: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test +T6885255.java:15:17: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test +T6885255.java:16:22: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test +T6885255.java:16:28: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test +T6885255.java:19:23: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test +T6885255.java:19:29: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test +T6885255.java:24:29: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test +T6885255.java:24:35: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test +T6885255.java:25:35: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test +T6885255.java:25:41: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test +T6885255.java:27:23: compiler.warn.raw.class.use: java.lang.Class, java.lang.Class +T6885255.java:27:30: compiler.warn.raw.class.use: java.lang.Class, java.lang.Class +T6885255.java:28:29: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test +T6885255.java:28:42: compiler.warn.raw.class.use: T6885255.Test, T6885255.Test +15 warnings