Fri, 18 Feb 2011 12:28:49 +0000
7020043: Project Coin: diamond allowed on non-generic type
Summary: Diamond oerator should be disallowed on non-generic class types (i.e. String)
Reviewed-by: jjg
1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java Wed Feb 16 10:27:00 2011 -0800 1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java Fri Feb 18 12:28:49 2011 +0000 1.3 @@ -1584,6 +1584,11 @@ 1.4 if (!TreeInfo.isDiamond(tree)) { 1.5 clazztype = chk.checkClassType( 1.6 tree.clazz.pos(), clazztype, true); 1.7 + } else if (!clazztype.isErroneous() && 1.8 + !clazztype.tsym.type.isParameterized()) { 1.9 + log.error(tree.clazz.pos(), 1.10 + "cant.apply.diamond.1", 1.11 + clazztype, diags.fragment("diamond.non.generic", clazztype)); 1.12 } 1.13 chk.validate(clazz, localEnv); 1.14 if (tree.encl != null) { 1.15 @@ -1609,7 +1614,7 @@ 1.16 List<Type> argtypes = attribArgs(tree.args, localEnv); 1.17 List<Type> typeargtypes = attribTypes(tree.typeargs, localEnv); 1.18 1.19 - if (TreeInfo.isDiamond(tree)) { 1.20 + if (TreeInfo.isDiamond(tree) && clazztype.tsym.type.isParameterized()) { 1.21 clazztype = attribDiamond(localEnv, tree, clazztype, mapping, argtypes, typeargtypes); 1.22 clazz.type = clazztype; 1.23 } else if (allowDiamondFinder &&
2.1 --- a/src/share/classes/com/sun/tools/javac/resources/compiler.properties Wed Feb 16 10:27:00 2011 -0800 2.2 +++ b/src/share/classes/com/sun/tools/javac/resources/compiler.properties Fri Feb 18 12:28:49 2011 +0000 2.3 @@ -1584,6 +1584,10 @@ 2.4 compiler.misc.diamond=\ 2.5 {0}<> 2.6 2.7 +# 0: type 2.8 +compiler.misc.diamond.non.generic=\ 2.9 + cannot use ''<>'' with non-generic class {0} 2.10 + 2.11 # 0: list of type, 1: message segment 2.12 compiler.misc.diamond.invalid.arg=\ 2.13 type argument {0} inferred for {1} is not allowed in this context
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/generics/diamond/neg/Neg12.java Fri Feb 18 12:28:49 2011 +0000 3.3 @@ -0,0 +1,15 @@ 3.4 +/* 3.5 + * @test /nodynamiccopyright/ 3.6 + * @bug 7020043 3.7 + * 3.8 + * @summary Project Coin: diamond allowed on non-generic type 3.9 + * @author Rémi Forax 3.10 + * @compile/fail/ref=Neg12.out Neg12.java -XDrawDiagnostics 3.11 + * 3.12 + */ 3.13 + 3.14 +class DiamondRaw { 3.15 + public static void main(String[] args) { 3.16 + String s = new String<>("foo"); 3.17 + } 3.18 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/generics/diamond/neg/Neg12.out Fri Feb 18 12:28:49 2011 +0000 4.3 @@ -0,0 +1,2 @@ 4.4 +Neg12.java:13:27: compiler.err.cant.apply.diamond.1: java.lang.String, (compiler.misc.diamond.non.generic: java.lang.String) 4.5 +1 error