Fri, 20 Sep 2013 16:33:35 +0200
8023835: TreeMaker.QualIdent() too leafy
Reviewed-by: jjg
1.1 --- a/src/share/classes/com/sun/tools/javac/tree/TreeMaker.java Thu Sep 19 19:18:37 2013 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/tree/TreeMaker.java Fri Sep 20 16:33:35 2013 +0200 1.3 @@ -946,6 +946,7 @@ 1.4 boolean isUnqualifiable(Symbol sym) { 1.5 if (sym.name == names.empty || 1.6 sym.owner == null || 1.7 + sym.owner == syms.rootPackage || 1.8 sym.owner.kind == MTH || sym.owner.kind == VAR) { 1.9 return true; 1.10 } else if (sym.kind == TYP && toplevel != null) {
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/tree/MakeQualIdent.java Fri Sep 20 16:33:35 2013 +0200 2.3 @@ -0,0 +1,77 @@ 2.4 +/* 2.5 + * Copyright (c) 2013, 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 8023835 2.30 + * @summary Verify that TreeMaker.QualIdent(Symbol) field access cascade ends with 2.31 + * the top-level package (when no toplevel is set in TreeMaker) 2.32 + * @run main MakeQualIdent 2.33 + */ 2.34 + 2.35 +import com.sun.source.tree.IdentifierTree; 2.36 +import com.sun.source.tree.MemberSelectTree; 2.37 +import com.sun.source.tree.Tree; 2.38 +import com.sun.source.util.JavacTask; 2.39 +import com.sun.source.util.TreeScanner; 2.40 +import com.sun.tools.javac.api.JavacTaskImpl; 2.41 +import com.sun.tools.javac.api.JavacTool; 2.42 +import com.sun.tools.javac.code.Symtab; 2.43 +import com.sun.tools.javac.tree.TreeMaker; 2.44 +import com.sun.tools.javac.util.Context; 2.45 +import java.util.ArrayList; 2.46 + 2.47 +public class MakeQualIdent { 2.48 + public static void main(String... args) throws Exception { 2.49 + JavacTool tool = JavacTool.create(); 2.50 + JavacTask task = tool.getTask(null, null, null, new ArrayList<String>(), null, null); 2.51 + Context ctx = ((JavacTaskImpl)task).getContext(); 2.52 + TreeMaker treeMaker = TreeMaker.instance(ctx); 2.53 + Symtab syms = Symtab.instance(ctx); 2.54 + 2.55 + String stringTree = printTree(treeMaker.QualIdent(syms.stringType.tsym)); 2.56 + 2.57 + if (!"java.lang.String".equals(stringTree)) { 2.58 + throw new IllegalStateException(stringTree); 2.59 + } 2.60 + } 2.61 + 2.62 + private static String printTree(Tree tree) { 2.63 + final StringBuilder result = new StringBuilder(); 2.64 + 2.65 + new TreeScanner<Void, Void>() { 2.66 + @Override public Void visitIdentifier(IdentifierTree node, Void p) { 2.67 + result.append(node.getName()); 2.68 + return super.visitIdentifier(node, p); 2.69 + } 2.70 + @Override public Void visitMemberSelect(MemberSelectTree node, Void p) { 2.71 + scan(node.getExpression(), null); 2.72 + result.append("."); 2.73 + result.append(node.getIdentifier()); 2.74 + return null; 2.75 + } 2.76 + }.scan(tree, null); 2.77 + 2.78 + return result.toString(); 2.79 + } 2.80 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/tree/ScopeTest.java Fri Sep 20 16:33:35 2013 +0200 3.3 @@ -0,0 +1,114 @@ 3.4 +/* 3.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 + * 3.8 + * This code is free software; you can redistribute it and/or modify it 3.9 + * under the terms of the GNU General Public License version 2 only, as 3.10 + * published by the Free Software Foundation. 3.11 + * 3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.15 + * version 2 for more details (a copy is included in the LICENSE file that 3.16 + * accompanied this code). 3.17 + * 3.18 + * You should have received a copy of the GNU General Public License version 3.19 + * 2 along with this work; if not, write to the Free Software Foundation, 3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.21 + * 3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 3.23 + * or visit www.oracle.com if you need additional information or have any 3.24 + * questions. 3.25 + */ 3.26 + 3.27 +/* 3.28 + * @test 3.29 + * @bug 8023835 3.30 + * @summary Verify that implicit type of lambda parameter is correctly attributed 3.31 + * in Scope 3.32 + * @run main ScopeTest 3.33 + */ 3.34 + 3.35 +import com.sun.source.tree.CompilationUnitTree; 3.36 +import com.sun.source.tree.MemberSelectTree; 3.37 +import com.sun.source.tree.Scope; 3.38 +import com.sun.source.util.JavacTask; 3.39 +import com.sun.source.util.TreePath; 3.40 +import com.sun.source.util.TreePathScanner; 3.41 +import com.sun.source.util.Trees; 3.42 +import com.sun.tools.javac.api.JavacTaskImpl; 3.43 +import com.sun.tools.javac.api.JavacTool; 3.44 +import com.sun.tools.javac.model.JavacTypes; 3.45 +import java.io.IOException; 3.46 +import java.net.URI; 3.47 +import java.util.ArrayList; 3.48 +import java.util.Collections; 3.49 +import javax.lang.model.element.Element; 3.50 +import javax.lang.model.type.TypeMirror; 3.51 +import javax.lang.model.util.Types; 3.52 +import javax.tools.JavaFileObject; 3.53 +import javax.tools.JavaFileObject.Kind; 3.54 +import javax.tools.SimpleJavaFileObject; 3.55 + 3.56 +public class ScopeTest { 3.57 + 3.58 + private static final String SOURCE_CODE = 3.59 + "public class Test {\n" + 3.60 + " private static void test() {\n" + 3.61 + " InvokeOn f = null;\n" + 3.62 + " f.run(x -> { x.correct(); });\n" + 3.63 + " }\n" + 3.64 + " public static final class FooBar {\n" + 3.65 + " public void dontRun() { }\n" + 3.66 + " }\n" + 3.67 + "}\n" + 3.68 + "class InvokeOn {\n" + 3.69 + " public void run(I i) { }\n" + 3.70 + "}\n" + 3.71 + "class FooBar {\n" + 3.72 + " public void correct() { }\n" + 3.73 + "}\n" + 3.74 + "interface I {\n" + 3.75 + " public void run(FooBar f);\n" + 3.76 + "}"; 3.77 + 3.78 + public static void main(String... args) throws Exception { 3.79 + verifyLambdaScopeCorrect(""); 3.80 + verifyLambdaScopeCorrect("package test;"); 3.81 + } 3.82 + 3.83 + private static void verifyLambdaScopeCorrect(final String packageClause) throws Exception { 3.84 + JavacTool tool = JavacTool.create(); 3.85 + JavaFileObject source = new SimpleJavaFileObject(URI.create("mem://Test.java"), Kind.SOURCE) { 3.86 + @Override public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { 3.87 + return packageClause + SOURCE_CODE; 3.88 + } 3.89 + @Override public boolean isNameCompatible(String simpleName, Kind kind) { 3.90 + return true; 3.91 + } 3.92 + }; 3.93 + Iterable<? extends JavaFileObject> fos = Collections.singletonList(source); 3.94 + JavacTask task = tool.getTask(null, null, null, new ArrayList<String>(), null, fos); 3.95 + final Types types = JavacTypes.instance(((JavacTaskImpl) task).getContext()); 3.96 + final Trees trees = Trees.instance(task); 3.97 + CompilationUnitTree cu = task.parse().iterator().next(); 3.98 + 3.99 + task.analyze(); 3.100 + 3.101 + new TreePathScanner<Void, Void>() { 3.102 + @Override public Void visitMemberSelect(MemberSelectTree node, Void p) { 3.103 + if (node.getIdentifier().contentEquals("correct")) { 3.104 + TypeMirror xType = trees.getTypeMirror(new TreePath(getCurrentPath(), node.getExpression())); 3.105 + Scope scope = trees.getScope(getCurrentPath()); 3.106 + for (Element l : scope.getLocalElements()) { 3.107 + if (!l.getSimpleName().contentEquals("x")) continue; 3.108 + if (!types.isSameType(xType, l.asType())) { 3.109 + throw new IllegalStateException("Incorrect variable type in scope: " + l.asType() + "; should be: " + xType); 3.110 + } 3.111 + } 3.112 + } 3.113 + return super.visitMemberSelect(node, p); 3.114 + } 3.115 + }.scan(cu, null); 3.116 + } 3.117 +}