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.Set;
7   
8   import net.sourceforge.pmd.lang.symboltable.AbstractScope;
9   import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
10  import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
11  
12  /**
13   * Provides the basic java scope implementation
14   * <p/>
15   * @see <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.3">JLS 6.3</a>
16   */
17  public abstract class AbstractJavaScope extends AbstractScope {
18  
19      @Override
20      public void addDeclaration(NameDeclaration declaration) {
21          checkForDuplicatedNameDeclaration(declaration);
22          super.addDeclaration(declaration);
23      }
24  
25      protected void checkForDuplicatedNameDeclaration(NameDeclaration declaration) {
26          if (declaration instanceof VariableNameDeclaration && getDeclarations().keySet().contains(declaration)) {
27              throw new RuntimeException(declaration + " is already in the symbol table");
28          }
29      }
30  
31      @Override
32      public boolean contains(NameOccurrence occurrence) {
33          return findVariableHere((JavaNameOccurrence)occurrence) != null;
34      }
35  
36      protected abstract NameDeclaration findVariableHere(JavaNameOccurrence occurrence);
37  
38      protected <T> String glomNames(Set<T> s) {
39      	StringBuilder result = new StringBuilder();
40          for (T t: s) {
41              result.append(t.toString());
42              result.append(',');
43          }
44          return result.length() == 0 ? "" : result.toString().substring(0, result.length() - 1);
45      }
46  }