View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.symboltable;
5   
6   import java.util.ArrayList;
7   import java.util.LinkedHashMap;
8   import java.util.List;
9   import java.util.Map;
10  
11  /**
12   * Base class for any {@link Scope}.
13   * Provides useful default implementations.
14   */
15  public abstract class AbstractScope implements Scope {
16  
17      private Scope parent;
18      private Map<NameDeclaration, List<NameOccurrence>> nameDeclarations = new LinkedHashMap<NameDeclaration, List<NameOccurrence>>();
19  
20      @Override
21      public Scope getParent() {
22          return parent;
23      }
24  
25      @Override
26      public void setParent(Scope parent) {
27          this.parent = parent;
28      }
29  
30      @Override
31      public Map<NameDeclaration, List<NameOccurrence>> getDeclarations() {
32          return nameDeclarations;
33      }
34  
35      @Override
36      public <T extends NameDeclaration> Map<T, List<NameOccurrence>> getDeclarations(Class<T> clazz) {
37          Map<T, List<NameOccurrence>> result = new LinkedHashMap<T, List<NameOccurrence>>();
38          for (Map.Entry<NameDeclaration, List<NameOccurrence>> e : nameDeclarations.entrySet()) {
39              if (clazz.isAssignableFrom(e.getKey().getClass())) {
40                  @SuppressWarnings("unchecked") // it's assignable from, so should be ok
41                  T cast = (T)e.getKey();
42                  result.put(cast, e.getValue());
43              }
44          }
45          return result;
46      }
47  
48      @Override
49      public boolean contains(NameOccurrence occ) {
50          for (NameDeclaration d : nameDeclarations.keySet()) {
51              if (d.getImage().equals(occ.getImage())) {
52                  return true;
53              }
54          }
55          return false;
56      }
57  
58      @Override
59      public void addDeclaration(NameDeclaration declaration) {
60          nameDeclarations.put(declaration, new ArrayList<NameOccurrence>());
61      }
62  
63      @Override
64      public <T extends Scope> T getEnclosingScope(Class<T> clazz) {
65          T result = null;
66          Scope current = this;
67          while (result == null && current != null) {
68              if (clazz.isAssignableFrom(current.getClass())) {
69                  @SuppressWarnings("unchecked")
70                  T cast = (T)current;
71                  result = cast;
72              }
73              current = current.getParent();
74          }
75          return result;
76      }
77  
78      @Override
79      public NameDeclaration addNameOccurrence(NameOccurrence occurrence) {
80          NameDeclaration result = null;
81          for (Map.Entry<NameDeclaration, List<NameOccurrence>> e : nameDeclarations.entrySet()) {
82              if (e.getKey().getImage().equals(occurrence.getImage())) {
83                  result = e.getKey();
84                  e.getValue().add(occurrence);
85              }
86          }
87          return result;
88      }
89  }