1/*
2 * Copyright (c) 2013, 2015, 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 * @modules jdk.compiler/com.sun.tools.javac.api
30 *          jdk.compiler/com.sun.tools.javac.code
31 *          jdk.compiler/com.sun.tools.javac.tree
32 *          jdk.compiler/com.sun.tools.javac.util
33 * @run main MakeQualIdent
34 */
35
36import com.sun.source.tree.IdentifierTree;
37import com.sun.source.tree.MemberSelectTree;
38import com.sun.source.tree.Tree;
39import com.sun.source.util.JavacTask;
40import com.sun.source.util.TreeScanner;
41import com.sun.tools.javac.api.JavacTaskImpl;
42import com.sun.tools.javac.api.JavacTool;
43import com.sun.tools.javac.code.Symtab;
44import com.sun.tools.javac.tree.TreeMaker;
45import com.sun.tools.javac.util.Context;
46import java.util.ArrayList;
47
48public class MakeQualIdent {
49    public static void main(String... args) throws Exception {
50        JavacTool tool = JavacTool.create();
51        JavacTask task = tool.getTask(null, null, null, new ArrayList<String>(), null, null);
52        Context ctx = ((JavacTaskImpl)task).getContext();
53        TreeMaker treeMaker = TreeMaker.instance(ctx);
54        Symtab syms = Symtab.instance(ctx);
55
56        String stringTree = printTree(treeMaker.QualIdent(syms.stringType.tsym));
57
58        if (!"java.lang.String".equals(stringTree)) {
59            throw new IllegalStateException(stringTree);
60        }
61    }
62
63    private static String printTree(Tree tree) {
64        final StringBuilder result = new StringBuilder();
65
66        new TreeScanner<Void, Void>() {
67            @Override public Void visitIdentifier(IdentifierTree node, Void p) {
68                result.append(node.getName());
69                return super.visitIdentifier(node, p);
70            }
71            @Override public Void visitMemberSelect(MemberSelectTree node, Void p) {
72                scan(node.getExpression(), null);
73                result.append(".");
74                result.append(node.getIdentifier());
75                return null;
76            }
77        }.scan(tree, null);
78
79        return result.toString();
80    }
81}
82