View Javadoc
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 8 public class ClassScope extends AbstractScope { 9 10 // FIXME - this breaks give sufficiently nested code 11 private static int anonymousInnerClassCounter = 1; 12 private String className; 13 14 public ClassScope(String className) { 15 this.className = className; 16 anonymousInnerClassCounter = 1; 17 } 18 19 /*** 20 * This is only for anonymous inner classes 21 * 22 * FIXME - should have name like Foo$1, not Anonymous$1 23 * to get this working right, the parent scope needs 24 * to be passed in when instantiating a ClassScope 25 */ 26 public ClassScope() { 27 //this.className = getParent().getEnclosingClassScope().getClassName() + "$" + String.valueOf(anonymousInnerClassCounter); 28 this.className = "Anonymous$" + String.valueOf(anonymousInnerClassCounter); 29 anonymousInnerClassCounter++; 30 } 31 32 public ClassScope getEnclosingClassScope() { 33 return this; 34 } 35 36 public String getClassName() { 37 return this.className; 38 } 39 40 public void addDeclaration(MethodNameDeclaration decl) { 41 methodNames.put(decl, new ArrayList()); 42 } 43 44 protected NameDeclaration findVariableHere(NameOccurrence occurrence) { 45 if (occurrence.isThisOrSuper() || occurrence.getImage().equals(className)) { 46 if (variableNames.isEmpty()) { 47 // this could happen if you do this: 48 // public class Foo { 49 // private String x = super.toString(); 50 // } 51 return null; 52 } 53 // return any name declaration, since all we really want is to get the scope 54 // for example, if there's a 55 // public class Foo { 56 // private static final int X = 2; 57 // private int y = Foo.X; 58 // } 59 // we'll look up Foo just to get a handle to the class scope 60 // and then we'll look up X. 61 return (NameDeclaration) variableNames.keySet().iterator().next(); 62 } 63 64 List images = new ArrayList(); 65 images.add(occurrence.getImage()); 66 if (occurrence.getImage().startsWith(className)) { 67 images.add(clipClassName(occurrence.getImage())); 68 } 69 ImageFinderFunction finder = new ImageFinderFunction(images); 70 Applier.apply(finder, variableNames.keySet().iterator()); 71 return finder.getDecl(); 72 } 73 74 public String toString() { 75 return "ClassScope:" + className + ":" + super.glomNames(); 76 } 77 78 private String clipClassName(String in) { 79 int firstDot = in.indexOf('.'); 80 return in.substring(firstDot + 1); 81 } 82 }

This page was automatically generated by Maven