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.List;
7   import java.util.Map;
8   
9   import net.sourceforge.pmd.lang.ast.Node;
10  import net.sourceforge.pmd.lang.java.ast.ASTLambdaExpression;
11  import net.sourceforge.pmd.lang.java.ast.ASTName;
12  import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
13  import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
14  
15  /**
16   * A LocalScope can have variable declarations and class declarations within it.
17   */
18  public class LocalScope extends AbstractJavaScope {
19  
20      public Map<VariableNameDeclaration, List<NameOccurrence>> getVariableDeclarations() {
21          return getDeclarations(VariableNameDeclaration.class);
22      }
23  
24      public NameDeclaration addNameOccurrence(NameOccurrence occurrence) {
25          JavaNameOccurrence javaOccurrence = (JavaNameOccurrence)occurrence;
26          NameDeclaration decl = findVariableHere(javaOccurrence);
27          if (decl != null && !javaOccurrence.isThisOrSuper()) {
28              List<NameOccurrence> nameOccurrences = getVariableDeclarations().get(decl);
29              nameOccurrences.add(javaOccurrence);
30              Node n = javaOccurrence.getLocation();
31              if (n instanceof ASTName) {
32                  ((ASTName) n).setNameDeclaration(decl);
33              } // TODO what to do with PrimarySuffix case?
34          }
35          return decl;
36      }
37  
38      public void addDeclaration(NameDeclaration nameDecl) {
39          if (!(nameDecl instanceof VariableNameDeclaration || nameDecl instanceof ClassNameDeclaration)) {
40              throw new IllegalArgumentException("A LocalScope can contain only VariableNameDeclarations or ClassNameDeclarations. "
41                      + "Tried to add " + nameDecl.getClass() + "(" + nameDecl + ")");
42          }
43          if (nameDecl.getNode().jjtGetParent() instanceof ASTLambdaExpression
44              || nameDecl.getNode().jjtGetParent().jjtGetParent().jjtGetParent() instanceof ASTLambdaExpression) {
45              // don't add the variable declaration, but verify, that there is no other local variable declaration
46              // with the same name - this would be a compiler error
47              checkForDuplicatedNameDeclaration(nameDecl);
48          } else {
49              super.addDeclaration(nameDecl);
50          }
51      }
52  
53      public NameDeclaration findVariableHere(JavaNameOccurrence occurrence) {
54          if (occurrence.isThisOrSuper() || occurrence.isMethodOrConstructorInvocation()) {
55              return null;
56          }
57          ImageFinderFunction finder = new ImageFinderFunction(occurrence.getImage());
58          Applier.apply(finder, getVariableDeclarations().keySet().iterator());
59          return finder.getDecl();
60      }
61  
62      public String toString() {
63          return "LocalScope:" + glomNames(getVariableDeclarations().keySet());
64      }
65  }