forked from phoedos/pmd
use of entrySet to iterate over Maps.
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4714 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -25,6 +25,7 @@ CloseResource rule now checks code without java.sql import.
|
||||
ArrayIsStoredDirectly rule now checks Constructors
|
||||
undo/redo added to text areas in Designer.
|
||||
Better 'create rule XML' panel in Designer.
|
||||
use of entrySet to iterate over Maps.
|
||||
|
||||
October 4, 2006 - 3.8:
|
||||
New rules:
|
||||
|
@ -134,12 +134,14 @@ public class ClassScopeTest extends STBBaseTst {
|
||||
parseCode(METHOD_USAGE_SEEN);
|
||||
ASTClassOrInterfaceDeclaration n = (ASTClassOrInterfaceDeclaration) acu.findChildrenOfType(ASTClassOrInterfaceDeclaration.class).get(0);
|
||||
Map m = ((ClassScope) n.getScope()).getMethodDeclarations();
|
||||
Iterator i = m.keySet().iterator();
|
||||
MethodNameDeclaration mnd = (MethodNameDeclaration) i.next();
|
||||
Iterator i = m.entrySet().iterator();
|
||||
Map.Entry entry = (Map.Entry) i.next();
|
||||
|
||||
MethodNameDeclaration mnd = (MethodNameDeclaration) entry.getKey();
|
||||
if (!mnd.getImage().equals("bar")) {
|
||||
mnd = (MethodNameDeclaration) i.next();
|
||||
}
|
||||
List usages = (List) m.get(mnd);
|
||||
List usages = (List) entry.getValue();
|
||||
assertEquals(1, usages.size());
|
||||
assertEquals("bar", ((NameOccurrence) usages.get(0)).getImage());
|
||||
}
|
||||
@ -148,12 +150,13 @@ public class ClassScopeTest extends STBBaseTst {
|
||||
parseCode(METHOD_USAGE_SEEN_WITH_THIS);
|
||||
ASTClassOrInterfaceDeclaration n = (ASTClassOrInterfaceDeclaration) acu.findChildrenOfType(ASTClassOrInterfaceDeclaration.class).get(0);
|
||||
Map m = ((ClassScope) n.getScope()).getMethodDeclarations();
|
||||
Iterator i = m.keySet().iterator();
|
||||
MethodNameDeclaration mnd = (MethodNameDeclaration) i.next();
|
||||
Iterator i = m.entrySet().iterator();
|
||||
Map.Entry entry = (Map.Entry) i.next();
|
||||
MethodNameDeclaration mnd = (MethodNameDeclaration) entry.getKey();
|
||||
if (!mnd.getImage().equals("bar")) {
|
||||
mnd = (MethodNameDeclaration) i.next();
|
||||
}
|
||||
List usages = (List) m.get(mnd);
|
||||
List usages = (List) entry.getValue();
|
||||
assertEquals(1, usages.size());
|
||||
assertEquals("bar", ((NameOccurrence) usages.get(0)).getImage());
|
||||
}
|
||||
@ -162,10 +165,11 @@ public class ClassScopeTest extends STBBaseTst {
|
||||
parseCode(METHOD_USAGE_SEEN2);
|
||||
ASTClassOrInterfaceDeclaration n = (ASTClassOrInterfaceDeclaration) acu.findChildrenOfType(ASTClassOrInterfaceDeclaration.class).get(0);
|
||||
Map m = ((ClassScope) n.getScope()).getMethodDeclarations();
|
||||
Iterator i = m.keySet().iterator();
|
||||
MethodNameDeclaration mnd = (MethodNameDeclaration) i.next();
|
||||
Iterator i = m.entrySet().iterator();
|
||||
Map.Entry entry = (Map.Entry) i.next();
|
||||
MethodNameDeclaration mnd = (MethodNameDeclaration) entry.getKey();
|
||||
if (mnd.getNode().getBeginLine() == 2) {
|
||||
List usages = (List) m.get(mnd);
|
||||
List usages = (List) entry.getValue();
|
||||
System.out.println(usages.size());
|
||||
System.out.println(mnd);
|
||||
mnd = (MethodNameDeclaration) i.next();
|
||||
|
@ -57,11 +57,12 @@ public class VariableAccessVisitor extends JavaParserVisitorAdapter {
|
||||
Set variableDeclarations = collectDeclarations(inode);
|
||||
for (Iterator i = variableDeclarations.iterator(); i.hasNext();) {
|
||||
Map declarations = (Map) i.next();
|
||||
for (Iterator j = declarations.keySet().iterator(); j.hasNext();) {
|
||||
VariableNameDeclaration vnd = (VariableNameDeclaration) j.next();
|
||||
for (Iterator j = declarations.entrySet().iterator(); j.hasNext();) {
|
||||
Map.Entry entry = (Map.Entry) j.next();
|
||||
VariableNameDeclaration vnd = (VariableNameDeclaration) entry.getKey();
|
||||
addVariableAccess(vnd.getNode().getBeginLine(), new VariableAccess(VariableAccess.DEFINITION, vnd.getImage()), inode.getFlow());
|
||||
undefinitions.add(new VariableAccess(VariableAccess.UNDEFINITION, vnd.getImage()));
|
||||
for (Iterator k = ((List) declarations.get(vnd)).iterator(); k.hasNext();) {
|
||||
for (Iterator k = ((List) entry.getValue()).iterator(); k.hasNext();) {
|
||||
addAccess(k, inode);
|
||||
}
|
||||
}
|
||||
|
@ -100,9 +100,10 @@ public class PapariTextRenderer extends AbstractRenderer implements Renderer {
|
||||
buf.append(PMD.EOL + PMD.EOL);
|
||||
buf.append("Summary:" + PMD.EOL + PMD.EOL);
|
||||
Map summary = report.getCountSummary();
|
||||
for (Iterator i = summary.keySet().iterator(); i.hasNext();) {
|
||||
String key = (String) i.next();
|
||||
buf.append(key + " : " + ((Integer) summary.get(key)).intValue() + PMD.EOL);
|
||||
for (Iterator i = summary.entrySet().iterator(); i.hasNext();) {
|
||||
Map.Entry entry = (Map.Entry) i.next();
|
||||
String key = (String) entry.getKey();
|
||||
buf.append(key + " : " + ((Integer) entry.getValue()).intValue() + PMD.EOL);
|
||||
}
|
||||
|
||||
for (Iterator i = report.errors(); i.hasNext();) {
|
||||
|
@ -34,11 +34,12 @@ public class SummaryHTMLRenderer extends AbstractRenderer implements Renderer {
|
||||
buf.append("<th>Rule name</th>");
|
||||
buf.append("<th>Number of violations</th>");
|
||||
Map summary = report.getSummary();
|
||||
for (Iterator i = summary.keySet().iterator(); i.hasNext();) {
|
||||
String ruleName = (String) i.next();
|
||||
for (Iterator i = summary.entrySet().iterator(); i.hasNext();) {
|
||||
Map.Entry entry = (Map.Entry) i.next();
|
||||
String ruleName = (String) entry.getKey();
|
||||
buf.append("<tr>");
|
||||
buf.append("<td>" + ruleName + "</td>");
|
||||
buf.append("<td align=center>" + ((Integer) summary.get(ruleName)).intValue() + "</td>");
|
||||
buf.append("<td align=center>" + ((Integer) entry.getValue()).intValue() + "</td>");
|
||||
buf.append("</tr>");
|
||||
}
|
||||
buf.append("</table>");
|
||||
|
@ -16,9 +16,11 @@ public class AvoidReassigningParameters extends AbstractRule {
|
||||
|
||||
public Object visit(ASTMethodDeclarator node, Object data) {
|
||||
Map params = node.getScope().getVariableDeclarations();
|
||||
for (Iterator i = params.keySet().iterator(); i.hasNext();) {
|
||||
VariableNameDeclaration decl = (VariableNameDeclaration) i.next();
|
||||
List usages = (List) params.get(decl);
|
||||
for (Iterator i = params.entrySet().iterator(); i.hasNext();) {
|
||||
Map.Entry entry = (Map.Entry) i.next();
|
||||
|
||||
VariableNameDeclaration decl = (VariableNameDeclaration) entry.getKey();
|
||||
List usages = (List) entry.getValue();
|
||||
for (Iterator j = usages.iterator(); j.hasNext();) {
|
||||
NameOccurrence occ = (NameOccurrence) j.next();
|
||||
if ((occ.isOnLeftHandSide() || occ.isSelfAssignment()) && occ.getNameForWhichThisIsAQualifier() == null && !decl.isArray()) {
|
||||
|
@ -617,9 +617,11 @@ public final class ConstructorCallsOverridableMethod extends AbstractRule {
|
||||
private boolean evaluateDangerOfMethods(Map classMethodMap) {
|
||||
//check each method if it calls overridable method
|
||||
boolean found = false;
|
||||
for (Iterator methodsIter = classMethodMap.keySet().iterator(); methodsIter.hasNext();) {
|
||||
MethodHolder h = (MethodHolder) methodsIter.next();
|
||||
List calledMeths = (List) classMethodMap.get(h);
|
||||
for (Iterator methodsIter = classMethodMap.entrySet().iterator(); methodsIter.hasNext();) {
|
||||
Map.Entry entry = (Map.Entry) methodsIter.next();
|
||||
|
||||
MethodHolder h = (MethodHolder) entry.getKey();
|
||||
List calledMeths = (List) entry.getValue();
|
||||
for (Iterator calledMethsIter = calledMeths.iterator(); calledMethsIter.hasNext() && !h.isDangerous();) {
|
||||
//if this method matches one of our dangerous methods, mark it dangerous
|
||||
MethodInvocation meth = (MethodInvocation) calledMethsIter.next();
|
||||
@ -651,11 +653,12 @@ public final class ConstructorCallsOverridableMethod extends AbstractRule {
|
||||
*/
|
||||
private void evaluateDangerOfConstructors1(Map classConstructorMap, Set evaluatedMethods) {
|
||||
//check each constructor in the class
|
||||
for (Iterator constIter = classConstructorMap.keySet().iterator(); constIter.hasNext();) {
|
||||
ConstructorHolder ch = (ConstructorHolder) constIter.next();
|
||||
for (Iterator constIter = classConstructorMap.entrySet().iterator(); constIter.hasNext();) {
|
||||
Map.Entry entry = (Map.Entry) constIter.next();
|
||||
ConstructorHolder ch = (ConstructorHolder) entry.getKey();
|
||||
if (!ch.isDangerous()) {//if its not dangerous then evaluate if it should be
|
||||
//if it calls dangerous method mark it as dangerous
|
||||
List calledMeths = (List) classConstructorMap.get(ch);
|
||||
List calledMeths = (List) entry.getValue();
|
||||
//check each method it calls
|
||||
for (Iterator calledMethsIter = calledMeths.iterator(); calledMethsIter.hasNext() && !ch.isDangerous();) {//but thee are diff objects which represent same thing but were never evaluated, they need reevaluation
|
||||
MethodInvocation meth = (MethodInvocation) calledMethsIter.next();//CCE
|
||||
|
@ -37,9 +37,10 @@ public class UnusedFormalParameterRule extends AbstractRule {
|
||||
Node parent = node.jjtGetParent().jjtGetParent().jjtGetParent();
|
||||
if (parent instanceof ASTClassOrInterfaceDeclaration && !((ASTClassOrInterfaceDeclaration) parent).isInterface()) {
|
||||
Map vars = node.getScope().getVariableDeclarations();
|
||||
for (Iterator i = vars.keySet().iterator(); i.hasNext();) {
|
||||
VariableNameDeclaration nameDecl = (VariableNameDeclaration) i.next();
|
||||
if (nameDecl.isArray() || actuallyUsed((List)vars.get(nameDecl))) {
|
||||
for (Iterator i = vars.entrySet().iterator(); i.hasNext();) {
|
||||
Map.Entry entry = (Map.Entry) i.next();
|
||||
VariableNameDeclaration nameDecl = (VariableNameDeclaration) entry.getKey();
|
||||
if (nameDecl.isArray() || actuallyUsed((List) entry.getValue())) {
|
||||
continue;
|
||||
}
|
||||
addViolation(data, node, new Object[]{node instanceof ASTMethodDeclaration ? "method" : "constructor", nameDecl.getImage()});
|
||||
|
@ -16,12 +16,13 @@ public class UnusedPrivateFieldRule extends AbstractRule {
|
||||
|
||||
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
|
||||
Map vars = node.getScope().getVariableDeclarations();
|
||||
for (Iterator i = vars.keySet().iterator(); i.hasNext();) {
|
||||
VariableNameDeclaration decl = (VariableNameDeclaration) i.next();
|
||||
for (Iterator i = vars.entrySet().iterator(); i.hasNext();) {
|
||||
Map.Entry entry = (Map.Entry) i.next();
|
||||
VariableNameDeclaration decl = (VariableNameDeclaration) entry.getKey();
|
||||
if (!decl.getAccessNodeParent().isPrivate() || isOK(decl.getImage())) {
|
||||
continue;
|
||||
}
|
||||
if (!actuallyUsed((List) vars.get(decl))) {
|
||||
if (!actuallyUsed((List) entry.getValue())) {
|
||||
addViolation(data, decl.getNode(), decl.getImage());
|
||||
}
|
||||
}
|
||||
|
@ -25,13 +25,14 @@ public class AssignmentToNonFinalStatic extends AbstractRule {
|
||||
|
||||
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
|
||||
Map vars = node.getScope().getVariableDeclarations();
|
||||
for (Iterator i = vars.keySet().iterator(); i.hasNext();) {
|
||||
VariableNameDeclaration decl = (VariableNameDeclaration) i.next();
|
||||
for (Iterator i = vars.entrySet().iterator(); i.hasNext();) {
|
||||
Map.Entry entry = (Map.Entry) i.next();
|
||||
VariableNameDeclaration decl = (VariableNameDeclaration) entry.getKey();
|
||||
if (!decl.getAccessNodeParent().isStatic() || decl.getAccessNodeParent().isFinal()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (initializedInConstructor((List) vars.get(decl))) {
|
||||
if (initializedInConstructor((List) entry.getValue())) {
|
||||
addViolation(data, decl.getNode(), decl.getImage());
|
||||
}
|
||||
}
|
||||
|
@ -36,13 +36,14 @@ public class ImmutableField extends AbstractRule {
|
||||
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
|
||||
Map vars = node.getScope().getVariableDeclarations();
|
||||
List constructors = findAllConstructors(node);
|
||||
for (Iterator i = vars.keySet().iterator(); i.hasNext();) {
|
||||
VariableNameDeclaration field = (VariableNameDeclaration) i.next();
|
||||
for (Iterator i = vars.entrySet().iterator(); i.hasNext();) {
|
||||
Map.Entry entry = (Map.Entry) i.next();
|
||||
VariableNameDeclaration field = (VariableNameDeclaration) entry.getKey();
|
||||
if (field.getAccessNodeParent().isStatic() || !field.getAccessNodeParent().isPrivate() || field.getAccessNodeParent().isFinal()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int result = initializedInConstructor((List) vars.get(field), new HashSet(constructors));
|
||||
int result = initializedInConstructor((List) entry.getValue(), new HashSet(constructors));
|
||||
if (result == MUTABLE) {
|
||||
continue;
|
||||
}
|
||||
|
@ -37,9 +37,10 @@ public class UnnecessaryLocalBeforeReturn extends AbstractRule {
|
||||
}
|
||||
|
||||
Map vars = name.getScope().getVariableDeclarations();
|
||||
for (Iterator i = vars.keySet().iterator(); i.hasNext();) {
|
||||
VariableNameDeclaration key = (VariableNameDeclaration) i.next();
|
||||
List usages = (List) vars.get(key);
|
||||
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();
|
||||
if (occ.getLocation().equals(name)) {
|
||||
|
@ -29,9 +29,10 @@ public class MethodArgumentCouldBeFinal extends AbstractOptimizationRule {
|
||||
}
|
||||
Scope s = meth.getScope();
|
||||
Map decls = s.getVariableDeclarations();
|
||||
for (Iterator i = decls.keySet().iterator(); i.hasNext();) {
|
||||
VariableNameDeclaration var = (VariableNameDeclaration) i.next();
|
||||
if (!var.getAccessNodeParent().isFinal() && (var.getAccessNodeParent() instanceof ASTFormalParameter) && !assigned((List) decls.get(var))) {
|
||||
for (Iterator i = decls.entrySet().iterator(); i.hasNext();) {
|
||||
Map.Entry entry = (Map.Entry) i.next();
|
||||
VariableNameDeclaration var = (VariableNameDeclaration) entry.getKey();
|
||||
if (!var.getAccessNodeParent().isFinal() && (var.getAccessNodeParent() instanceof ASTFormalParameter) && !assigned((List) entry.getValue())) {
|
||||
addViolation(data, var.getAccessNodeParent(), var.getImage());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user