1 package net.sourceforge.pmd.symboltable; 2 3 import net.sourceforge.pmd.util.Applier; 4 5 import java.util.ArrayList; 6 import java.util.List; 7 import java.util.HashMap; 8 import java.util.Map; 9 10 public class SourceFileScope extends AbstractScope { 11 12 protected Map<ClassNameDeclaration, List<NameOccurrence>> classNames = new HashMap<ClassNameDeclaration, List<NameOccurrence>>(); 13 private String packageImage; 14 15 public SourceFileScope() { 16 this(""); 17 } 18 19 public SourceFileScope(String image) { 20 this.packageImage = image; 21 } 22 23 public ClassScope getEnclosingClassScope() { 24 throw new RuntimeException("getEnclosingClassScope() called on SourceFileScope"); 25 } 26 27 public MethodScope getEnclosingMethodScope() { 28 throw new RuntimeException("getEnclosingMethodScope() called on SourceFileScope"); 29 } 30 31 public String getPackageName() { 32 return packageImage; 33 } 34 35 public SourceFileScope getEnclosingSourceFileScope() { 36 return this; 37 } 38 39 public void addDeclaration(ClassNameDeclaration classDecl) { 40 classNames.put(classDecl, new ArrayList<NameOccurrence>()); 41 } 42 43 public void addDeclaration(MethodNameDeclaration decl) { 44 throw new RuntimeException("SourceFileScope.addDeclaration(MethodNameDeclaration decl) called"); 45 } 46 47 public void addDeclaration(VariableNameDeclaration decl) { 48 throw new RuntimeException("SourceFileScope.addDeclaration(VariableNameDeclaration decl) called"); 49 } 50 51 public Map<ClassNameDeclaration, List<NameOccurrence>> getClassDeclarations() { 52 return classNames; 53 } 54 55 public Map<VariableNameDeclaration, List<NameOccurrence>> getVariableDeclarations() { 56 throw new RuntimeException("PackageScope.getVariableDeclarations() called"); 57 } 58 59 public NameDeclaration addVariableNameOccurrence(NameOccurrence occ) { 60 return null; 61 } 62 63 public String toString() { 64 return "SourceFileScope: " + glomNames(classNames.keySet()); 65 } 66 67 protected NameDeclaration findVariableHere(NameOccurrence occ) { 68 ImageFinderFunction finder = new ImageFinderFunction(occ.getImage()); 69 Applier.apply(finder, classNames.keySet().iterator()); 70 return finder.getDecl(); 71 } 72 73 }