1/*
2 * Copyright (c) 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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package jdk.jshell;
27
28import com.sun.source.tree.ClassTree;
29import com.sun.source.tree.IdentifierTree;
30import com.sun.source.tree.ImportTree;
31import com.sun.source.tree.MemberSelectTree;
32import com.sun.source.tree.MethodTree;
33import com.sun.source.tree.PackageTree;
34import com.sun.source.tree.Tree;
35import com.sun.source.tree.VariableTree;
36import com.sun.source.util.TreeScanner;
37import java.util.Collection;
38import java.util.HashSet;
39import java.util.Set;
40import javax.lang.model.element.Name;
41
42/**
43 * Search a compiler API parse tree for dependencies.
44 */
45class TreeDependencyScanner extends TreeScanner<Void, Set<String>> {
46
47    private final Set<String> decl = new HashSet<>();
48    private final Set<String> body = new HashSet<>();
49
50    public void scan(Tree node) {
51        scan(node, decl);
52    }
53
54    public Collection<String> declareReferences() {
55        return decl;
56    }
57
58    public Collection<String> bodyReferences() {
59        return body;
60    }
61
62    private void add(Set<String> p, Name name) {
63        p.add(name.toString());
64    }
65
66    // -- Differentiate declaration references from body references ---
67
68    @Override
69    public Void visitClass(ClassTree node, Set<String> p) {
70        scan(node.getModifiers(), p);
71        scan(node.getTypeParameters(), p);
72        scan(node.getExtendsClause(), p);
73        scan(node.getImplementsClause(), p);
74        scan(node.getMembers(), body);
75        return null;
76    }
77
78    @Override
79    public Void visitMethod(MethodTree node, Set<String> p) {
80        scan(node.getModifiers(), p);
81        scan(node.getReturnType(), p);
82        scan(node.getTypeParameters(), p);
83        scan(node.getParameters(), p);
84        scan(node.getReceiverParameter(), p);
85        scan(node.getThrows(), p);
86        scan(node.getBody(), body);
87        scan(node.getDefaultValue(), body);
88        return null;
89    }
90
91    @Override
92    public Void visitVariable(VariableTree node, Set<String> p) {
93        scan(node.getModifiers(), p);
94        scan(node.getType(), p);
95        scan(node.getNameExpression(), p);
96        scan(node.getInitializer(), body);
97        return null;
98    }
99
100    // --- Ignore these ---
101
102    @Override
103    public Void visitPackage(PackageTree node, Set<String> p) {
104        return null;
105    }
106
107    @Override
108    public Void visitImport(ImportTree node, Set<String> p) {
109        return null;
110    }
111
112
113    // -- Actual Symbol names ---
114
115    @Override
116    public Void visitMemberSelect(MemberSelectTree node, Set<String> p) {
117        add(p, node.getIdentifier());
118        return super.visitMemberSelect(node, p);
119    }
120
121    @Override
122    public Void visitIdentifier(IdentifierTree node, Set<String> p) {
123        add(p, node.getName());
124        return super.visitIdentifier(node, p);
125    }
126}
127