1 /* |
|
2 * Copyright (c) 2014, 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 package com.sun.tools.sjavac.comp.dependencies; |
|
26 |
|
27 import java.util.HashSet; |
|
28 import java.util.Set; |
|
29 |
|
30 import com.sun.tools.javac.code.Symbol; |
|
31 import com.sun.tools.javac.code.Symbol.ClassSymbol; |
|
32 import com.sun.tools.javac.code.Symbol.PackageSymbol; |
|
33 import com.sun.tools.javac.code.Type; |
|
34 import com.sun.tools.javac.code.TypeTag; |
|
35 import com.sun.tools.javac.tree.JCTree.JCFieldAccess; |
|
36 import com.sun.tools.javac.tree.JCTree.JCIdent; |
|
37 import com.sun.tools.javac.tree.TreeScanner; |
|
38 |
|
39 class DependencyScanner extends TreeScanner { |
|
40 |
|
41 public final Set<Dependency> dependencies = new HashSet<>(); |
|
42 |
|
43 private boolean isValidDependency(Type t) { |
|
44 if (t == null || t.isPrimitiveOrVoid() || t.isErroneous()) |
|
45 return false; |
|
46 TypeTag tag = t.getTag(); |
|
47 return tag != TypeTag.PACKAGE |
|
48 && tag != TypeTag.METHOD |
|
49 && tag != TypeTag.ARRAY |
|
50 && tag != TypeTag.TYPEVAR; |
|
51 } |
|
52 |
|
53 @Override |
|
54 public void visitIdent(JCIdent tree) { |
|
55 if (isValidDependency(tree.type)) |
|
56 dependencies.add(new TypeAndSupertypesDependency(tree.type.tsym)); |
|
57 super.visitIdent(tree); |
|
58 } |
|
59 |
|
60 @Override |
|
61 public void visitSelect(JCFieldAccess tree) { |
|
62 if (tree.getIdentifier().contentEquals("*")) { |
|
63 Symbol sym = tree.selected instanceof JCIdent ? ((JCIdent) tree.selected).sym |
|
64 : ((JCFieldAccess) tree.selected).sym; |
|
65 if (sym instanceof ClassSymbol) { |
|
66 ClassSymbol clsSym = (ClassSymbol) sym; |
|
67 dependencies.add(new TypeAndSupertypesDependency(clsSym.type.tsym)); |
|
68 } else { |
|
69 dependencies.add(new PackageDependency((PackageSymbol) sym)); |
|
70 } |
|
71 } else if (tree.type != null && tree.type.hasTag(TypeTag.METHOD)) { // Method call? Depend on the result (even though we never access it elsewhere) |
|
72 Type retType = tree.type.getReturnType(); |
|
73 if (isValidDependency(retType)) |
|
74 dependencies.add(new TypeAndSupertypesDependency(retType.tsym)); |
|
75 } else if (isValidDependency(tree.type)) { |
|
76 dependencies.add(new TypeAndSupertypesDependency(tree.type.tsym)); |
|
77 } |
|
78 super.visitSelect(tree); |
|
79 } |
|
80 |
|
81 public Set<Dependency> getResult() { |
|
82 return dependencies; |
|
83 } |
|
84 } |
|