'val' in javac now errors out with an appropriate message on val x = { .. }, and arrays no longer cause "Symbol not found: Array" errors.

This commit is contained in:
Reinier Zwitserloot committed 2010-11-08 15:30:51 +01:00
1 parent 2f202525af
commit 09cebbdc59
2 files changed
+23

No files matched your search

@@ -12,6 +12,7 @@ import javax.tools.DiagnosticListener;
import com.sun.tools.javac.code.BoundKind;
import com.sun.tools.javac.code.Symbol.TypeSymbol;
import com.sun.tools.javac.code.Type.ArrayType;
import com.sun.tools.javac.code.Type.CapturedType;
import com.sun.tools.javac.code.Type.ClassType;
import com.sun.tools.javac.code.Type;
@@ -388,6 +389,22 @@ public class JavacResolution {
}
private static JCExpression typeToJCTree(Type type, TreeMaker maker, JavacAST ast, boolean allowCompound) throws TypeNotConvertibleException {
int dims = 0;
Type type0 = type;
while (type0 instanceof ArrayType) {
dims++;
type0 = ((ArrayType)type0).elemtype;
}
JCExpression result = typeToJCTree0(type0, maker, ast, allowCompound);
while (dims > 0) {
result = maker.TypeArray(result);
dims--;
}
return result;
}
private static JCExpression typeToJCTree0(Type type, TreeMaker maker, JavacAST ast, boolean allowCompound) throws TypeNotConvertibleException {
// NB: There's such a thing as maker.Type(type), but this doesn't work very well; it screws up anonymous classes, captures, and adds an extra prefix dot for some reason too.
// -- so we write our own take on that here.
@@ -33,6 +33,7 @@ import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCEnhancedForLoop;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.JCTree.JCNewArray;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
@ProviderFor(JavacASTVisitor.class)
@@ -57,6 +58,11 @@ public class HandleVal extends JavacASTAdapter {
return;
}
if (local.init instanceof JCNewArray && ((JCNewArray)local.init).elemtype == null) {
localNode.addError("'val' is not compatible with array initializer expressions. Use the full form (new int[] { ... } instead of just { ... })");
return;
}
local.mods.flags |= Flags.FINAL;
JCExpression oldVarType = local.vartype;
local.vartype = JavacResolution.createJavaLangObject(localNode.getTreeMaker(), localNode.getAst());