1
2
3
4 package net.sourceforge.pmd.lang.java.symboltable;
5
6
7 import java.util.List;
8 import java.util.Map;
9
10 import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
11 import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
12 import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
13
14
15
16
17
18 public class SourceFileScope extends AbstractJavaScope {
19
20 private String packageImage;
21 private TypeSet types;
22
23 public SourceFileScope() {
24 this("");
25 }
26
27 public SourceFileScope(String packageImage) {
28 this.packageImage = packageImage;
29 }
30
31
32
33
34
35
36 public void configureImports(ClassLoader classLoader, List<ASTImportDeclaration> imports) {
37 this.types = new TypeSet(classLoader);
38 types.setASTCompilationUnitPackage(packageImage);
39 for (ASTImportDeclaration i : imports) {
40 if (i.isImportOnDemand()) {
41 types.addImport(i.getImportedName() + ".*");
42 } else {
43 types.addImport(i.getImportedName());
44 }
45 }
46 }
47
48
49
50
51
52
53 public Class<?> resolveType(String name) {
54 try {
55 return types.findClass(name);
56 } catch (ClassNotFoundException e) {
57 return null;
58 }
59 }
60
61 public String getPackageName() {
62 return packageImage;
63 }
64
65
66
67
68
69 @Override
70 public void addDeclaration(NameDeclaration declaration) {
71 if (!(declaration instanceof ClassNameDeclaration)) {
72 throw new IllegalArgumentException("A SourceFileScope can only contain classes.");
73 }
74 super.addDeclaration(declaration);
75 }
76
77
78
79
80
81
82 public Map<ClassNameDeclaration, List<NameOccurrence>> getClassDeclarations() {
83 return getDeclarations(ClassNameDeclaration.class);
84 }
85
86 public String toString() {
87 return "SourceFileScope: " + glomNames(getClassDeclarations().keySet());
88 }
89
90 protected NameDeclaration findVariableHere(JavaNameOccurrence occ) {
91 ImageFinderFunction finder = new ImageFinderFunction(occ.getImage());
92 Applier.apply(finder, getDeclarations().keySet().iterator());
93 return finder.getDecl();
94 }
95 }