TreeDependencyScanner.java revision 3062:15bdc18525ff
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 com.sun.tools.javac.util.DefinedBy; 38import com.sun.tools.javac.util.DefinedBy.Api; 39import java.util.Collection; 40import java.util.HashSet; 41import java.util.Set; 42import javax.lang.model.element.Name; 43 44/** 45 * Search a compiler API parse tree for dependencies. 46 */ 47class TreeDependencyScanner extends TreeScanner<Void, Set<String>> { 48 49 private final Set<String> decl = new HashSet<>(); 50 private final Set<String> body = new HashSet<>(); 51 52 public void scan(Tree node) { 53 scan(node, decl); 54 } 55 56 public Collection<String> declareReferences() { 57 return decl; 58 } 59 60 public Collection<String> bodyReferences() { 61 return body; 62 } 63 64 private void add(Set<String> p, Name name) { 65 p.add(name.toString()); 66 } 67 68 // -- Differentiate declaration references from body references --- 69 70 @Override @DefinedBy(Api.COMPILER_TREE) 71 public Void visitClass(ClassTree node, Set<String> p) { 72 scan(node.getModifiers(), p); 73 scan(node.getTypeParameters(), p); 74 scan(node.getExtendsClause(), p); 75 scan(node.getImplementsClause(), p); 76 scan(node.getMembers(), body); 77 return null; 78 } 79 80 @Override @DefinedBy(Api.COMPILER_TREE) 81 public Void visitMethod(MethodTree node, Set<String> p) { 82 scan(node.getModifiers(), p); 83 scan(node.getReturnType(), p); 84 scan(node.getTypeParameters(), p); 85 scan(node.getParameters(), p); 86 scan(node.getReceiverParameter(), p); 87 scan(node.getThrows(), p); 88 scan(node.getBody(), body); 89 scan(node.getDefaultValue(), body); 90 return null; 91 } 92 93 @Override @DefinedBy(Api.COMPILER_TREE) 94 public Void visitVariable(VariableTree node, Set<String> p) { 95 scan(node.getModifiers(), p); 96 scan(node.getType(), p); 97 scan(node.getNameExpression(), p); 98 scan(node.getInitializer(), body); 99 return null; 100 } 101 102 // --- Ignore these --- 103 104 @Override @DefinedBy(Api.COMPILER_TREE) 105 public Void visitPackage(PackageTree node, Set<String> p) { 106 return null; 107 } 108 109 @Override @DefinedBy(Api.COMPILER_TREE) 110 public Void visitImport(ImportTree node, Set<String> p) { 111 return null; 112 } 113 114 115 // -- Actual Symbol names --- 116 117 @Override @DefinedBy(Api.COMPILER_TREE) 118 public Void visitMemberSelect(MemberSelectTree node, Set<String> p) { 119 add(p, node.getIdentifier()); 120 return super.visitMemberSelect(node, p); 121 } 122 123 @Override @DefinedBy(Api.COMPILER_TREE) 124 public Void visitIdentifier(IdentifierTree node, Set<String> p) { 125 add(p, node.getName()); 126 return super.visitIdentifier(node, p); 127 } 128} 129