|
1 /* |
|
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 8023835 |
|
27 * @summary Verify that TreeMaker.QualIdent(Symbol) field access cascade ends with |
|
28 * the top-level package (when no toplevel is set in TreeMaker) |
|
29 * @run main MakeQualIdent |
|
30 */ |
|
31 |
|
32 import com.sun.source.tree.IdentifierTree; |
|
33 import com.sun.source.tree.MemberSelectTree; |
|
34 import com.sun.source.tree.Tree; |
|
35 import com.sun.source.util.JavacTask; |
|
36 import com.sun.source.util.TreeScanner; |
|
37 import com.sun.tools.javac.api.JavacTaskImpl; |
|
38 import com.sun.tools.javac.api.JavacTool; |
|
39 import com.sun.tools.javac.code.Symtab; |
|
40 import com.sun.tools.javac.tree.TreeMaker; |
|
41 import com.sun.tools.javac.util.Context; |
|
42 import java.util.ArrayList; |
|
43 |
|
44 public class MakeQualIdent { |
|
45 public static void main(String... args) throws Exception { |
|
46 JavacTool tool = JavacTool.create(); |
|
47 JavacTask task = tool.getTask(null, null, null, new ArrayList<String>(), null, null); |
|
48 Context ctx = ((JavacTaskImpl)task).getContext(); |
|
49 TreeMaker treeMaker = TreeMaker.instance(ctx); |
|
50 Symtab syms = Symtab.instance(ctx); |
|
51 |
|
52 String stringTree = printTree(treeMaker.QualIdent(syms.stringType.tsym)); |
|
53 |
|
54 if (!"java.lang.String".equals(stringTree)) { |
|
55 throw new IllegalStateException(stringTree); |
|
56 } |
|
57 } |
|
58 |
|
59 private static String printTree(Tree tree) { |
|
60 final StringBuilder result = new StringBuilder(); |
|
61 |
|
62 new TreeScanner<Void, Void>() { |
|
63 @Override public Void visitIdentifier(IdentifierTree node, Void p) { |
|
64 result.append(node.getName()); |
|
65 return super.visitIdentifier(node, p); |
|
66 } |
|
67 @Override public Void visitMemberSelect(MemberSelectTree node, Void p) { |
|
68 scan(node.getExpression(), null); |
|
69 result.append("."); |
|
70 result.append(node.getIdentifier()); |
|
71 return null; |
|
72 } |
|
73 }.scan(tree, null); |
|
74 |
|
75 return result.toString(); |
|
76 } |
|
77 } |