View Javadoc

    1   /**
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package net.sourceforge.pmd.symboltable;
    5
    6   import java.util.List;
    7   import java.util.Map;
    8   import java.util.Set;
    9
    10  public abstract class AbstractScope implements Scope {
    11
    12      private Scope parent;
    13
    14      public Map<ClassNameDeclaration, List<NameOccurrence>> getClassDeclarations() {
    15          throw new RuntimeException("AbstractScope.getClassDeclarations() was invoked.  That shouldn't happen... bug.");
    16      }
    17
    18      public MethodScope getEnclosingMethodScope() {
    19          return parent.getEnclosingMethodScope();
    20      }
    21
    22      public ClassScope getEnclosingClassScope() {
    23          return parent.getEnclosingClassScope();
    24      }
    25
    26      public SourceFileScope getEnclosingSourceFileScope() {
    27          return parent.getEnclosingSourceFileScope();
    28      }
    29
    30      public void setParent(Scope parent) {
    31          this.parent = parent;
    32      }
    33
    34      public Scope getParent() {
    35          return parent;
    36      }
    37
    38      public void addDeclaration(MethodNameDeclaration methodDecl) {
    39          parent.addDeclaration(methodDecl);
    40      }
    41
    42      public void addDeclaration(ClassNameDeclaration classDecl) {
    43          parent.addDeclaration(classDecl);
    44      }
    45
    46      public boolean contains(NameOccurrence occurrence) {
    47          return findVariableHere(occurrence) != null;
    48      }
    49
    50      protected abstract NameDeclaration findVariableHere(NameOccurrence occurrence);
    51
    52      protected <T> String glomNames(Set<T> s) {
    53          StringBuffer result = new StringBuffer();
    54          for (T t: s) {
    55              result.append(t.toString());
    56              result.append(',');
    57          }
    58          return result.length() == 0 ? "" : result.toString().substring(0, result.length() - 1);
    59      }
    60
    61  }