Checking in some Java 5 changes

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@5018 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Xavier Le Vourch
2007-01-31 01:37:56 +00:00
parent 02a809e807
commit 653f75f89f
16 changed files with 64 additions and 83 deletions

View File

@ -16,22 +16,22 @@ public class NameOccurrencesTest extends STBBaseTst {
parseCode(TEST1);
List nodes = acu.findChildrenOfType(ASTPrimaryExpression.class);
NameFinder occs = new NameFinder((ASTPrimaryExpression) nodes.get(0));
assertEquals("super", ((NameOccurrence) occs.getNames().get(0)).getImage());
assertEquals("super", occs.getNames().get(0).getImage());
}
public void testThis() {
parseCode(TEST2);
List nodes = acu.findChildrenOfType(ASTPrimaryExpression.class);
NameFinder occs = new NameFinder((ASTPrimaryExpression) nodes.get(0));
assertEquals("this", ((NameOccurrence) occs.getNames().get(0)).getImage());
assertEquals("x", ((NameOccurrence) occs.getNames().get(1)).getImage());
assertEquals("this", occs.getNames().get(0).getImage());
assertEquals("x", occs.getNames().get(1).getImage());
}
public void testNameLinkage() {
parseCode(TEST2);
List nodes = acu.findChildrenOfType(ASTPrimaryExpression.class);
NameFinder occs = new NameFinder((ASTPrimaryExpression) nodes.get(0));
NameOccurrence thisNameOccurrence = (NameOccurrence) occs.getNames().get(0);
NameOccurrence thisNameOccurrence = occs.getNames().get(0);
assertEquals(thisNameOccurrence.getNameForWhichThisIsAQualifier(), occs.getNames().get(1));
}
@ -39,30 +39,30 @@ public class NameOccurrencesTest extends STBBaseTst {
parseCode(TEST3);
List nodes = acu.findChildrenOfType(ASTPrimaryExpression.class);
NameFinder occs = new NameFinder((ASTPrimaryExpression) nodes.get(0));
assertEquals("x", ((NameOccurrence) occs.getNames().get(0)).getImage());
assertFalse(((NameOccurrence) occs.getNames().get(0)).isThisOrSuper());
assertFalse(((NameOccurrence) occs.getNames().get(0)).isMethodOrConstructorInvocation());
assertTrue(((NameOccurrence) occs.getNames().get(0)).isOnLeftHandSide());
assertEquals("x", occs.getNames().get(0).getImage());
assertFalse(occs.getNames().get(0).isThisOrSuper());
assertFalse(occs.getNames().get(0).isMethodOrConstructorInvocation());
assertTrue(occs.getNames().get(0).isOnLeftHandSide());
}
public void testQualifiedOccurrence() {
parseCode(TEST4);
List nodes = acu.findChildrenOfType(ASTPrimaryExpression.class);
NameFinder occs = new NameFinder((ASTPrimaryExpression) nodes.get(0));
assertEquals("b", ((NameOccurrence) occs.getNames().get(0)).getImage());
assertEquals("x", ((NameOccurrence) occs.getNames().get(1)).getImage());
assertEquals("b", occs.getNames().get(0).getImage());
assertEquals("x", occs.getNames().get(1).getImage());
}
public void testIsSelfAssignment(){
parseCode(TEST5);
List nodes = acu.findChildrenOfType(ASTPrimaryExpression.class);
NameFinder occs = new NameFinder((ASTPrimaryExpression) nodes.get(2));
assertTrue(((NameOccurrence) occs.getNames().get(0)).isSelfAssignment());
assertTrue(occs.getNames().get(0).isSelfAssignment());
parseCode(TEST6);
nodes = acu.findChildrenOfType(ASTPrimaryExpression.class);
occs = new NameFinder((ASTPrimaryExpression) nodes.get(2));
assertTrue(((NameOccurrence) occs.getNames().get(0)).isSelfAssignment());
assertTrue(occs.getNames().get(0).isSelfAssignment());
}
public static final String TEST1 =

View File

@ -13,7 +13,6 @@ import net.sourceforge.pmd.ast.SimpleNode;
import net.sourceforge.pmd.symboltable.NameOccurrence;
import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@ -24,26 +23,24 @@ import java.util.Map;
public class AssignmentToNonFinalStatic extends AbstractRule {
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
Map vars = node.getScope().getVariableDeclarations();
for (Iterator i = vars.entrySet().iterator(); i.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
VariableNameDeclaration decl = (VariableNameDeclaration) entry.getKey();
Map<VariableNameDeclaration, List<NameOccurrence>> vars = node.getScope().getVariableDeclarations();
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry: vars.entrySet()) {
VariableNameDeclaration decl = entry.getKey();
if (!decl.getAccessNodeParent().isStatic() || decl.getAccessNodeParent().isFinal()) {
continue;
}
if (initializedInConstructor((List) entry.getValue())) {
if (initializedInConstructor(entry.getValue())) {
addViolation(data, decl.getNode(), decl.getImage());
}
}
return super.visit(node, data);
}
private boolean initializedInConstructor(List usages) {
private boolean initializedInConstructor(List<NameOccurrence> usages) {
boolean initInConstructor = false;
for (Iterator j = usages.iterator(); j.hasNext();) {
NameOccurrence occ = (NameOccurrence) j.next();
for (NameOccurrence occ: usages) {
if (occ.isOnLeftHandSide()) { // specifically omitting prefix and postfix operators as there are legitimate usages of these with static fields, e.g. typesafe enum pattern.
SimpleNode node = occ.getLocation();
SimpleNode constructor = node.getFirstParentOfType(ASTConstructorDeclaration.class);

View File

@ -11,10 +11,10 @@ import net.sourceforge.pmd.ast.ASTPrimaryExpression;
import net.sourceforge.pmd.ast.ASTPrimaryPrefix;
import net.sourceforge.pmd.ast.ASTThrowStatement;
import net.sourceforge.pmd.ast.SimpleNode;
import net.sourceforge.pmd.symboltable.NameOccurrence;
import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@ -47,9 +47,8 @@ public class PreserveStackTrace extends AbstractRule {
}
if (child != null){
if( child.getClass().equals(ASTName.class) && (!target.equals(child.getImage()) && !child.hasImageEqualTo(target + ".fillInStackTrace"))) {
Map vars = ((ASTName) child).getScope().getVariableDeclarations();
for (Iterator i = vars.keySet().iterator(); i.hasNext();) {
VariableNameDeclaration decl = (VariableNameDeclaration) i.next();
Map<VariableNameDeclaration, List<NameOccurrence>> vars = ((ASTName) child).getScope().getVariableDeclarations();
for (VariableNameDeclaration decl: vars.keySet()) {
args = ((SimpleNode) decl.getNode().jjtGetParent())
.getFirstChildOfType(ASTArgumentList.class);
if (args != null) {

View File

@ -1,6 +1,5 @@
package net.sourceforge.pmd.rules.design;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@ -36,13 +35,11 @@ public class UnnecessaryLocalBeforeReturn extends AbstractRule {
return data;
}
Map vars = name.getScope().getVariableDeclarations();
for (Iterator i = vars.entrySet().iterator(); i.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
VariableNameDeclaration key = (VariableNameDeclaration) entry.getKey();
List usages = (List) entry.getValue();
for (Iterator j = usages.iterator(); j.hasNext();) {
NameOccurrence occ = (NameOccurrence) j.next();
Map<VariableNameDeclaration, List<NameOccurrence>> vars = name.getScope().getVariableDeclarations();
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry: vars.entrySet()) {
VariableNameDeclaration key = entry.getKey();
List<NameOccurrence> usages = entry.getValue();
for (NameOccurrence occ: usages) {
if (occ.getLocation().equals(name)) {
// only check declarations that occur one line earlier
if (key.getNode().getBeginLine() == name.getBeginLine() - 1) {
@ -66,9 +63,8 @@ public class UnnecessaryLocalBeforeReturn extends AbstractRule {
* @return true if any method calls are made within the given return
*/
private boolean isMethodCall(ASTReturnStatement rtn) {
List suffix = rtn.findChildrenOfType( ASTPrimarySuffix.class );
for ( Iterator iter = suffix.iterator(); iter.hasNext(); ) {
ASTPrimarySuffix element = (ASTPrimarySuffix) iter.next();
List<ASTPrimarySuffix> suffix = rtn.findChildrenOfType( ASTPrimarySuffix.class );
for ( ASTPrimarySuffix element: suffix ) {
if ( element.isArguments() ) {
return true;
}

View File

@ -3,11 +3,11 @@
*/
package net.sourceforge.pmd.rules.optimization;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sourceforge.pmd.ast.ASTLocalVariableDeclaration;
import net.sourceforge.pmd.symboltable.NameOccurrence;
import net.sourceforge.pmd.symboltable.Scope;
import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
@ -18,14 +18,13 @@ public class LocalVariableCouldBeFinal extends AbstractOptimizationRule {
return data;
}
Scope s = node.getScope();
Map decls = s.getVariableDeclarations();
for (Iterator i = decls.entrySet().iterator(); i.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
VariableNameDeclaration var = (VariableNameDeclaration) entry.getKey();
Map<VariableNameDeclaration, List<NameOccurrence>> decls = s.getVariableDeclarations();
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry: decls.entrySet()) {
VariableNameDeclaration var = entry.getKey();
if (var.getAccessNodeParent() != node) {
continue;
}
if (!assigned((List) entry.getValue())) {
if (!assigned(entry.getValue())) {
addViolation(data, var.getAccessNodeParent(), var.getImage());
}
}

View File

@ -3,13 +3,13 @@
*/
package net.sourceforge.pmd.rules.optimization;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sourceforge.pmd.ast.ASTFormalParameter;
import net.sourceforge.pmd.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.ast.AccessNode;
import net.sourceforge.pmd.symboltable.NameOccurrence;
import net.sourceforge.pmd.symboltable.Scope;
import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
@ -20,12 +20,11 @@ public class MethodArgumentCouldBeFinal extends AbstractOptimizationRule {
return data;
}
Scope s = meth.getScope();
Map decls = s.getVariableDeclarations();
for (Iterator i = decls.entrySet().iterator(); i.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
VariableNameDeclaration var = (VariableNameDeclaration) entry.getKey();
Map<VariableNameDeclaration, List<NameOccurrence>> decls = s.getVariableDeclarations();
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry: decls.entrySet()) {
VariableNameDeclaration var = entry.getKey();
AccessNode node = var.getAccessNodeParent();
if (!node.isFinal() && (node instanceof ASTFormalParameter) && !assigned((List) entry.getValue())) {
if (!node.isFinal() && (node instanceof ASTFormalParameter) && !assigned(entry.getValue())) {
addViolation(data, node, var.getImage());
}
}

View File

@ -23,9 +23,9 @@ import net.sourceforge.pmd.ast.Node;
import net.sourceforge.pmd.ast.SimpleNode;
import net.sourceforge.pmd.properties.IntegerProperty;
import net.sourceforge.pmd.symboltable.NameOccurrence;
import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -89,17 +89,15 @@ public class ConsecutiveLiteralAppends extends AbstractRule {
int concurrentCount = checkConstructor(node, data);
Node lastBlock = getFirstParentBlock(node);
Node currentBlock = lastBlock;
Map decls = node.getScope().getVariableDeclarations();
Map<VariableNameDeclaration, List<NameOccurrence>> decls = node.getScope().getVariableDeclarations();
SimpleNode rootNode = null;
// only want the constructor flagged if it's really containing strings
if (concurrentCount == 1) {
rootNode = node;
}
for (Iterator iter = decls.entrySet().iterator(); iter.hasNext();) {
Map.Entry entry = (Map.Entry) iter.next();
List decl = (List) entry.getValue();
for (int ix = 0; ix < decl.size(); ix++) {
NameOccurrence no = (NameOccurrence) decl.get(ix);
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry: decls.entrySet()) {
List<NameOccurrence> decl = entry.getValue();
for (NameOccurrence no: decl) {
SimpleNode n = no.getLocation();
currentBlock = getFirstParentBlock(n);

View File

@ -15,7 +15,6 @@ import net.sourceforge.pmd.ast.ASTTypeDeclaration;
import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.ast.SimpleNode;
import java.util.Iterator;
import java.util.List;
/**
@ -33,10 +32,9 @@ public abstract class AbstractSunSecureRule extends AbstractRule {
* @return <code>true</code> if there is a field in the type declaration named varName, <code>false</code> in other case
*/
protected final boolean isField(String varName, ASTTypeDeclaration typeDeclaration) {
final List fds = typeDeclaration.findChildrenOfType(ASTFieldDeclaration.class);
final List<ASTFieldDeclaration> fds = typeDeclaration.findChildrenOfType(ASTFieldDeclaration.class);
if (fds != null) {
for (Iterator it = fds.iterator(); it.hasNext();) {
final ASTFieldDeclaration fd = (ASTFieldDeclaration) it.next();
for (ASTFieldDeclaration fd: fds) {
final ASTVariableDeclaratorId vid = fd.getFirstChildOfType(ASTVariableDeclaratorId.class);
if (vid != null && vid.hasImageEqualTo(varName)) {
return true;
@ -76,10 +74,9 @@ public abstract class AbstractSunSecureRule extends AbstractRule {
* @return <code>true</code> if the method declaration contains any local variable named vn and <code>false</code> in other case
*/
protected boolean isLocalVariable(String vn, SimpleNode node) {
final List lvars = node.findChildrenOfType(ASTLocalVariableDeclaration.class);
final List<ASTLocalVariableDeclaration> lvars = node.findChildrenOfType(ASTLocalVariableDeclaration.class);
if (lvars != null) {
for (Iterator it = lvars.iterator(); it.hasNext();) {
final ASTLocalVariableDeclaration lvd = (ASTLocalVariableDeclaration) it.next();
for (ASTLocalVariableDeclaration lvd: lvars) {
final ASTVariableDeclaratorId vid = lvd.getFirstChildOfType(ASTVariableDeclaratorId.class);
if (vid != null && vid.hasImageEqualTo(vn)) {
return true;

View File

@ -20,7 +20,6 @@ import net.sourceforge.pmd.ast.ASTStatementExpression;
import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.ast.SimpleNode;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
@ -40,7 +39,7 @@ public class ArrayIsStoredDirectly extends AbstractSunSecureRule {
ASTFormalParameter[] arrs = getArrays(node.getParameters());
if (arrs != null) {
//TODO check if one of these arrays is stored in a non local variable
List bs = node.findChildrenOfType(ASTBlockStatement.class);
List<ASTBlockStatement> bs = node.findChildrenOfType(ASTBlockStatement.class);
checkAll(data, arrs, bs);
}
return data;
@ -55,7 +54,7 @@ public class ArrayIsStoredDirectly extends AbstractSunSecureRule {
return data;
}
private void checkAll(Object context, ASTFormalParameter[] arrs, List bs) {
private void checkAll(Object context, ASTFormalParameter[] arrs, List<ASTBlockStatement> bs) {
for (int i = 0; i < arrs.length; i++) {
checkForDirectAssignment(context, arrs[i], bs);
}
@ -64,11 +63,10 @@ public class ArrayIsStoredDirectly extends AbstractSunSecureRule {
/**
* Checks if the variable designed in parameter is written to a field (not local variable) in the statements.
*/
private boolean checkForDirectAssignment(Object ctx, final ASTFormalParameter parameter, final List bs) {
private boolean checkForDirectAssignment(Object ctx, final ASTFormalParameter parameter, final List<ASTBlockStatement> bs) {
final ASTVariableDeclaratorId vid = parameter.getFirstChildOfType(ASTVariableDeclaratorId.class);
final String varName = vid.getImage();
for (Iterator it = bs.iterator(); it.hasNext();) {
final ASTBlockStatement b = (ASTBlockStatement) it.next();
for (ASTBlockStatement b: bs) {
if (b.containsChildOfType(ASTAssignmentOperator.class)) {
final ASTStatementExpression se = b.getFirstChildOfType(ASTStatementExpression.class);
if (se == null || !(se.jjtGetChild(0) instanceof ASTPrimaryExpression)) {

View File

@ -3,9 +3,9 @@
*/
package net.sourceforge.pmd.symboltable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public abstract class AbstractScope implements Scope {
@ -49,10 +49,10 @@ public abstract class AbstractScope implements Scope {
protected abstract NameDeclaration findVariableHere(NameOccurrence occurrence);
protected String glomNames(Iterator i) {
protected <T> String glomNames(Set<T> s) {
StringBuffer result = new StringBuffer();
while (i.hasNext()) {
result.append(i.next().toString());
for (T t: s) {
result.append(t.toString());
result.append(',');
}
return result.length() == 0 ? "" : result.toString().substring(0, result.length() - 1);

View File

@ -157,16 +157,16 @@ public class ClassScope extends AbstractScope {
public String toString() {
String res = "ClassScope (" + className + "): ";
if (!classNames.isEmpty()) res += "(" + glomNames(classNames.keySet().iterator()) + ")";
if (!classNames.isEmpty()) res += "(" + glomNames(classNames.keySet()) + ")";
if (!methodNames.isEmpty()) {
for (MethodNameDeclaration mnd: methodNames.keySet()) {
res += mnd.toString();
int usages = ((List) methodNames.get(mnd)).size();
int usages = methodNames.get(mnd).size();
res += "(begins at line " + mnd.getNode().getBeginLine() + ", " + usages + " usages)";
res += ",";
}
}
if (!variableNames.isEmpty()) res += "(" + glomNames(variableNames.keySet().iterator()) + ")";
if (!variableNames.isEmpty()) res += "(" + glomNames(variableNames.keySet()) + ")";
return res;
}

View File

@ -52,6 +52,6 @@ public class LocalScope extends AbstractScope {
}
public String toString() {
return "LocalScope:" + glomNames(variableNames.keySet().iterator());
return "LocalScope:" + glomNames(variableNames.keySet());
}
}

View File

@ -68,6 +68,6 @@ public class MethodScope extends AbstractScope {
}
public String toString() {
return "MethodScope:" + glomNames(variableNames.keySet().iterator());
return "MethodScope:" + glomNames(variableNames.keySet());
}
}

View File

@ -30,7 +30,7 @@ public class NameFinder {
}
}
public List getNames() {
public List<NameOccurrence> getNames() {
return names;
}

View File

@ -3,7 +3,6 @@ package net.sourceforge.pmd.symboltable;
import net.sourceforge.pmd.ast.ASTPrimaryExpression;
import net.sourceforge.pmd.ast.JavaParserVisitorAdapter;
import java.util.Iterator;
import java.util.List;
public class OccurrenceFinder extends JavaParserVisitorAdapter {
@ -15,9 +14,8 @@ public class OccurrenceFinder extends JavaParserVisitorAdapter {
// is null/not null?
NameDeclaration decl = null;
List names = nameFinder.getNames();
for (Iterator i = names.iterator(); i.hasNext();) {
NameOccurrence occ = (NameOccurrence) i.next();
List<NameOccurrence> names = nameFinder.getNames();
for (NameOccurrence occ: names) {
Search search = new Search(occ);
if (decl == null) {
// doing the first name lookup

View File

@ -61,7 +61,7 @@ public class SourceFileScope extends AbstractScope {
}
public String toString() {
return "SourceFileScope: " + glomNames(classNames.keySet().iterator());
return "SourceFileScope: " + glomNames(classNames.keySet());
}
protected NameDeclaration findVariableHere(NameOccurrence occ) {