View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
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   * This scope is the outer most scope of a Java file.
16   * A Source File can contain one ore more classes.
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       * Configures the type resolution for the symbol table.
33       * @param classLoader the class loader to use to find additional classes
34       * @param imports the import declarations
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       * Tries to resolve a class by name.
50       * @param name the name of the class
51       * @return the class or <code>null</code> if no class could be found
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       * {@inheritDoc}
67       * @throws IllegalArgumentException if declaration is not a {@link ClassNameDeclaration}
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       * Convenience method that casts the declarations to {@link ClassNameDeclaration}s.
79       * @see #getDeclarations()
80       * @return all class name declarations
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  }