Fixed symbol table bug

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@2040 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2003-06-26 21:11:54 +00:00
parent 1960b2b346
commit c63615a765
10 changed files with 81 additions and 12 deletions

View File

@ -1,8 +1,10 @@
???? - 1.11:
Added new rules: VariableNamingConventionsRule, MethodNamingConventionsRule, ClassNamingConventionsRule
Fixed bug 583047 - columns were wrong on ASTName nodes
Fixed bug 583047 - ASTName column numbers are now correct
Fixed bug 761048 - Symbol table now creates a scope level for anonymous inner classes
ASTViewer now shows node images and modifiers
ASTViewer now saves last edited text to ~/.pmd_astviewer
Moved the PMD Swing UI into a separate module - pmd-swingui.
TODO - fix it so tests and rules don't duplicate the xpath expressions
June 19, 2003 - 1.1:

View File

@ -18,7 +18,7 @@ public class StringToStringRuleTest extends SimpleAggregatorTst {
}
private static final String TEST1 =
"public class StringToString1 {" + PMD.EOL +
"public class Foo {" + PMD.EOL +
" private String baz() {" + PMD.EOL +
" String bar = \"howdy\";" + PMD.EOL +
" return bar.toString();" + PMD.EOL +
@ -26,14 +26,14 @@ public class StringToStringRuleTest extends SimpleAggregatorTst {
"}";
private static final String TEST2 =
"public class StringToString2 {" + PMD.EOL +
"public class Foo {" + PMD.EOL +
" private String baz(String bar) {" + PMD.EOL +
" return bar.toString();" + PMD.EOL +
" }" + PMD.EOL +
"}";
private static final String TEST3 =
"public class StringToString3 {" + PMD.EOL +
"public class Foo {" + PMD.EOL +
" private String baz;" + PMD.EOL +
" private int getBaz() {" + PMD.EOL +
" return baz.toString();" + PMD.EOL +
@ -41,7 +41,7 @@ public class StringToStringRuleTest extends SimpleAggregatorTst {
"}";
private static final String TEST4 =
"public class StringToString4 {" + PMD.EOL +
"public class Foo {" + PMD.EOL +
" private int baz;" + PMD.EOL +
" private int getBaz() {" + PMD.EOL +
" return baz;" + PMD.EOL +
@ -49,14 +49,14 @@ public class StringToStringRuleTest extends SimpleAggregatorTst {
"}";
private static final String TEST5 =
"public class StringToString5 {" + PMD.EOL +
"public class Foo {" + PMD.EOL +
" private String getBaz(String foo, StringBuffer buffer) {" + PMD.EOL +
" return buffer.toString();" + PMD.EOL +
" }" + PMD.EOL +
"}";
private static final String TEST6 =
"public class StringToString6 {" + PMD.EOL +
"public class Foo {" + PMD.EOL +
" private String getBaz() {" + PMD.EOL +
" String[] foo = {\"hi\"};" + PMD.EOL +
" return foo[0].toString();" + PMD.EOL +

View File

@ -0,0 +1,29 @@
package test.net.sourceforge.pmd.symboltable;
import junit.framework.TestCase;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.symboltable.SymbolFacade;
import net.sourceforge.pmd.ast.JavaParser;
import net.sourceforge.pmd.ast.ASTCompilationUnit;
import java.io.StringReader;
public class AcceptanceTest extends TestCase {
private static final String TEST1 =
"import java.io.*;" + PMD.EOL +
"public class Foo {" + PMD.EOL +
" void buz( ) {" + PMD.EOL +
" Object o = new Serializable() { int x; };" + PMD.EOL +
" Object o1 = new Serializable() { int x; };" + PMD.EOL +
" }" + PMD.EOL +
"}" + PMD.EOL;
public void testClashingSymbols() {
JavaParser parser = new JavaParser(new StringReader(TEST1));
ASTCompilationUnit c = parser.CompilationUnit();
SymbolFacade stb = new SymbolFacade();
stb.initializeWith(c);
}
}

View File

@ -4,9 +4,13 @@ import junit.framework.TestCase;
import net.sourceforge.pmd.ast.ASTCompilationUnit;
import net.sourceforge.pmd.ast.ASTIfStatement;
import net.sourceforge.pmd.ast.ASTTryStatement;
import net.sourceforge.pmd.ast.ASTClassBodyDeclaration;
import net.sourceforge.pmd.ast.ASTClassBody;
import net.sourceforge.pmd.ast.ASTAllocationExpression;
import net.sourceforge.pmd.symboltable.GlobalScope;
import net.sourceforge.pmd.symboltable.LocalScope;
import net.sourceforge.pmd.symboltable.ScopeCreator;
import net.sourceforge.pmd.symboltable.ClassScope;
public class ScopeCreatorTest extends TestCase {
public void testScopesAreCreated() {
@ -27,6 +31,7 @@ public class ScopeCreatorTest extends TestCase {
assertTrue(ifNode.getScope() instanceof LocalScope);
}
/*
public void testPush() {
SymbolTable s = new SymbolTable();

View File

@ -11,6 +11,8 @@ import net.sourceforge.pmd.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.ast.ASTTryStatement;
import net.sourceforge.pmd.ast.ASTUnmodifiedClassDeclaration;
import net.sourceforge.pmd.ast.ASTUnmodifiedInterfaceDeclaration;
import net.sourceforge.pmd.ast.ASTClassBodyDeclaration;
import net.sourceforge.pmd.ast.ASTAllocationExpression;
import net.sourceforge.pmd.symboltable.ClassScope;
import net.sourceforge.pmd.symboltable.GlobalScope;
import net.sourceforge.pmd.symboltable.LocalScope;

View File

@ -7,10 +7,18 @@ import java.util.List;
public class ClassScope extends AbstractScope {
private static int anonymousCounter = 1;
private String className;
public ClassScope(String className) {
this.className = className;
anonymousCounter = 0;
}
public ClassScope() {
this.className = "Anonymous$" + String.valueOf(anonymousCounter);
anonymousCounter++;
}
public Scope getEnclosingClassScope() {
@ -41,7 +49,6 @@ public class ClassScope extends AbstractScope {
return (NameDeclaration) variableNames.keySet().iterator().next();
}
List images = new ArrayList();
images.add(occurrence.getImage());
if (occurrence.getImage().startsWith(className)) {
@ -53,12 +60,11 @@ public class ClassScope extends AbstractScope {
}
public String toString() {
return "ClassScope:" + super.glomNames();
return "ClassScope:" + className + ":" + super.glomNames();
}
private String clipClassName(String in) {
int firstDot = in.indexOf('.');
return in.substring(firstDot + 1);
}
}

View File

@ -12,6 +12,9 @@ import net.sourceforge.pmd.ast.ASTUnmodifiedClassDeclaration;
import net.sourceforge.pmd.ast.ASTUnmodifiedInterfaceDeclaration;
import net.sourceforge.pmd.ast.JavaParserVisitorAdapter;
import net.sourceforge.pmd.ast.SimpleNode;
import net.sourceforge.pmd.ast.ASTClassBodyDeclaration;
import net.sourceforge.pmd.ast.ASTClassBody;
import net.sourceforge.pmd.ast.ASTAllocationExpression;
import java.util.Stack;
@ -30,6 +33,11 @@ public class ScopeCreator extends JavaParserVisitorAdapter {
return data;
}
public Object visit(ASTClassBodyDeclaration node, Object data) {
openScope(node);
return data;
}
public Object visit(ASTUnmodifiedInterfaceDeclaration node, Object data) {
openScope(node);
return data;
@ -82,7 +90,21 @@ public class ScopeCreator extends JavaParserVisitorAdapter {
}
private void openScope(SimpleNode node) {
Scope scope = sf.createScope(node);
// special case - anonymous inner class - blahh
if (node instanceof ASTClassBodyDeclaration) {
if (node.jjtGetParent() instanceof ASTClassBody && node.jjtGetParent().jjtGetParent() instanceof ASTAllocationExpression) {
Scope scope = new ClassScope();
processScope(scope, node);
} else {
super.visit(node, null);
}
} else {
Scope scope = sf.createScope(node);
processScope(scope, node);
}
}
private void processScope(Scope scope, SimpleNode node) {
push(scope);
node.setScope((Scope) scopes.peek());
super.visit(node, null);

View File

@ -12,6 +12,8 @@ import net.sourceforge.pmd.ast.ASTUnmodifiedClassDeclaration;
import net.sourceforge.pmd.ast.ASTUnmodifiedInterfaceDeclaration;
import net.sourceforge.pmd.ast.Node;
import net.sourceforge.pmd.ast.SimpleNode;
import net.sourceforge.pmd.ast.ASTClassBodyDeclaration;
import net.sourceforge.pmd.ast.ASTClassBody;
import java.util.HashSet;
import java.util.Set;

View File

@ -31,7 +31,7 @@ public class Search {
private NameDeclaration searchUpward(NameOccurrence nameOccurrence, Scope scope) {
if (!scope.contains(nameOccurrence) && scope.getParent() != null) {
if (TRACE)
System.out.println("moving up fm " + getClsName(scope.getClass()) + " to " + getClsName(scope.getParent().getClass()));
System.out.println("moving up fm " + scope + " to " + scope.getParent());
return searchUpward(nameOccurrence, scope.getParent());
}
if (scope.contains(nameOccurrence)) {

View File

@ -10,6 +10,7 @@
<section name="Credits">
<subsection name="Individuals">
<ul>
<li>Jarle Naess - bug report</li>
<li>Jeff Anderson - VariableNamingConventionsRule, MethodNamingConventionsRule, ClassNamingConventionsRule</li>
<li>Frank van Puffelen - documentation suggestions</li>
<li>Conrad Roche - bug report</li>