author | hseigel |
Wed, 01 Mar 2017 08:00:02 -0500 | |
changeset 46194 | 5596e6f63072 |
parent 40606 | eb2c81860c86 |
permissions | -rw-r--r-- |
33362 | 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 |
||
26 |
package jdk.jshell; |
|
27 |
||
28 |
import com.sun.source.tree.ClassTree; |
|
29 |
import com.sun.source.tree.IdentifierTree; |
|
30 |
import com.sun.source.tree.ImportTree; |
|
31 |
import com.sun.source.tree.MemberSelectTree; |
|
32 |
import com.sun.source.tree.MethodTree; |
|
33 |
import com.sun.source.tree.PackageTree; |
|
34 |
import com.sun.source.tree.Tree; |
|
35 |
import com.sun.source.tree.VariableTree; |
|
36 |
import com.sun.source.util.TreeScanner; |
|
37 |
import java.util.Collection; |
|
38 |
import java.util.HashSet; |
|
39 |
import java.util.Set; |
|
40 |
import javax.lang.model.element.Name; |
|
41 |
||
42 |
/** |
|
43 |
* Search a compiler API parse tree for dependencies. |
|
44 |
*/ |
|
45 |
class 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 |
||
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
33362
diff
changeset
|
68 |
@Override |
33362 | 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 |
||
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
33362
diff
changeset
|
78 |
@Override |
33362 | 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 |
||
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
33362
diff
changeset
|
91 |
@Override |
33362 | 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 |
||
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
33362
diff
changeset
|
102 |
@Override |
33362 | 103 |
public Void visitPackage(PackageTree node, Set<String> p) { |
104 |
return null; |
|
105 |
} |
|
106 |
||
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
33362
diff
changeset
|
107 |
@Override |
33362 | 108 |
public Void visitImport(ImportTree node, Set<String> p) { |
109 |
return null; |
|
110 |
} |
|
111 |
||
112 |
||
113 |
// -- Actual Symbol names --- |
|
114 |
||
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
33362
diff
changeset
|
115 |
@Override |
33362 | 116 |
public Void visitMemberSelect(MemberSelectTree node, Set<String> p) { |
117 |
add(p, node.getIdentifier()); |
|
118 |
return super.visitMemberSelect(node, p); |
|
119 |
} |
|
120 |
||
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
33362
diff
changeset
|
121 |
@Override |
33362 | 122 |
public Void visitIdentifier(IdentifierTree node, Set<String> p) { |
123 |
add(p, node.getName()); |
|
124 |
return super.visitIdentifier(node, p); |
|
125 |
} |
|
126 |
} |