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   import java.util.HashSet;
7   import java.util.List;
8   import java.util.Map;
9   import java.util.Set;
10  
11  import net.sourceforge.pmd.lang.ast.Node;
12  import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration;
13  import net.sourceforge.pmd.lang.java.ast.ASTName;
14  import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
15  import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
16  
17  /**
18   * A Method Scope can have variable declarations and class declarations within it.
19   */
20  public class MethodScope extends AbstractJavaScope {
21  
22      private Node node;
23  
24      public MethodScope(Node node) {
25          this.node = node;
26      }
27  
28      public Map<VariableNameDeclaration, List<NameOccurrence>> getVariableDeclarations() {
29          return getDeclarations(VariableNameDeclaration.class);
30      }
31  
32      public Set<NameDeclaration> addNameOccurrence(NameOccurrence occurrence) {
33          JavaNameOccurrence javaOccurrence = (JavaNameOccurrence)occurrence;
34          Set<NameDeclaration> declarations = findVariableHere(javaOccurrence);
35          if (!declarations.isEmpty() && !javaOccurrence.isThisOrSuper()) {
36              for (NameDeclaration decl : declarations) {
37                  getVariableDeclarations().get(decl).add(javaOccurrence);
38                  Node n = javaOccurrence.getLocation();
39                  if (n instanceof ASTName) {
40                      ((ASTName) n).setNameDeclaration(decl);
41                  } // TODO what to do with PrimarySuffix case?
42              }
43          }
44          return declarations;
45      }
46  
47      public void addDeclaration(NameDeclaration variableDecl) {
48          if (!(variableDecl instanceof VariableNameDeclaration || variableDecl instanceof ClassNameDeclaration)) {
49              throw new IllegalArgumentException("A MethodScope can contain only VariableNameDeclarations or ClassNameDeclarations");
50          }
51          super.addDeclaration(variableDecl);
52      }
53  
54      public Set<NameDeclaration> findVariableHere(JavaNameOccurrence occurrence) {
55          Set<NameDeclaration> result = new HashSet<>();
56          if (occurrence.isThisOrSuper() || occurrence.isMethodOrConstructorInvocation()) {
57              return result;
58          }
59          ImageFinderFunction finder = new ImageFinderFunction(occurrence.getImage());
60          Applier.apply(finder, getVariableDeclarations().keySet().iterator());
61          if (finder.getDecl() != null) {
62              result.add(finder.getDecl());
63          }
64          return result;
65      }
66  
67      public String getName() {
68          if (node instanceof ASTConstructorDeclaration) {
69              return this.getEnclosingScope(ClassScope.class).getClassName();
70          }
71          return node.jjtGetChild(1).getImage();
72      }
73  
74      public String toString() {
75          return "MethodScope:" + glomNames(getVariableDeclarations().keySet());
76      }
77  }