Checking in some Java 5 changes

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@5017 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Xavier Le Vourch
2007-01-31 00:53:53 +00:00
parent 017b1aef93
commit 02a809e807
50 changed files with 144 additions and 186 deletions

View File

@ -18,7 +18,7 @@ public class ASTFormalParameterTest extends ParserTst {
Set ops = getNodes(new TargetJDK1_5(), ASTFormalParameter.class, TEST1);
for (Iterator iter = ops.iterator(); iter.hasNext();) {
ASTFormalParameter b = (ASTFormalParameter) iter.next();
ASTVariableDeclaratorId variableDeclId = (ASTVariableDeclaratorId)b.getFirstChildOfType(ASTVariableDeclaratorId.class);
ASTVariableDeclaratorId variableDeclId = b.getFirstChildOfType(ASTVariableDeclaratorId.class);
if (!"x".equals(variableDeclId.getImage())) {
assertTrue(b.isVarargs());
nrOfVarArgs++;

View File

@ -86,19 +86,19 @@ public class JspDocStyleTest extends AbstractJspNodesTst {
assertEquals("Correct attribute name expected!",
"foo", attr.getName());
assertEquals("Correct attribute value expected!",
"CREATE", ((ASTAttributeValue) attr.getFirstChildOfType(ASTAttributeValue.class)).getImage());
"CREATE", attr.getFirstChildOfType(ASTAttributeValue.class).getImage());
attr = attrsList.get(1);
assertEquals("Correct attribute name expected!",
"href", attr.getName());
assertEquals("Correct attribute value expected!",
"#", ((ASTAttributeValue) attr.getFirstChildOfType(ASTAttributeValue.class)).getImage());
"#", attr.getFirstChildOfType(ASTAttributeValue.class).getImage());
attr = attrsList.get(2);
assertEquals("Correct attribute name expected!",
"something", attr.getName());
assertEquals("Correct attribute value expected!",
"#yes#", ((ASTAttributeValue) attr.getFirstChildOfType(ASTAttributeValue.class)).getImage());
"#yes#", attr.getFirstChildOfType(ASTAttributeValue.class).getImage());
}
/**

View File

@ -126,7 +126,7 @@ public class ASTFieldDeclaration extends AccessNode implements Dimensionable {
* @return a String representing the name of the variable
*/
public String getVariableName() {
ASTVariableDeclaratorId decl = (ASTVariableDeclaratorId) getFirstChildOfType(ASTVariableDeclaratorId.class);
ASTVariableDeclaratorId decl = getFirstChildOfType(ASTVariableDeclaratorId.class);
if (decl != null) {
return decl.getImage();
}

View File

@ -29,7 +29,7 @@ public class ASTMethodDeclaration extends AccessNode {
* @return a String representing the name of the method
*/
public String getMethodName() {
ASTMethodDeclarator md = (ASTMethodDeclarator) getFirstChildOfType(ASTMethodDeclarator.class);
ASTMethodDeclarator md = getFirstChildOfType(ASTMethodDeclarator.class);
if (md != null)
return md.getImage();
return null;
@ -63,11 +63,11 @@ public class ASTMethodDeclaration extends AccessNode {
}
public boolean isVoid() {
return ((ASTResultType) getFirstChildOfType(ASTResultType.class)).isVoid();
return getFirstChildOfType(ASTResultType.class).isVoid();
}
public ASTResultType getResultType() {
return (ASTResultType) getFirstChildOfType(ASTResultType.class);
return getFirstChildOfType(ASTResultType.class);
}
public ASTBlock getBlock() {

View File

@ -34,7 +34,7 @@ public class ASTPrimarySuffix extends SimpleJavaNode {
if (!this.isArguments()) {
throw new RuntimeException("ASTPrimarySuffix.getArgumentCount called, but this is not a method call");
}
return ((ASTArguments)this.getFirstChildOfType(ASTArguments.class)).getArgumentCount();
return this.getFirstChildOfType(ASTArguments.class).getArgumentCount();
}
public void dump(String prefix) {

View File

@ -31,7 +31,7 @@ public class ASTThrowStatement extends SimpleJavaNode {
* @return the image of the first ASTClassOrInterfaceType node found or <code>null</code>
*/
public final String getFirstClassOrInterfaceTypeImage() {
final ASTClassOrInterfaceType t = (ASTClassOrInterfaceType) getFirstChildOfType(ASTClassOrInterfaceType.class);
final ASTClassOrInterfaceType t = getFirstChildOfType(ASTClassOrInterfaceType.class);
return t == null ? null : t.getImage();
}
}

View File

@ -19,11 +19,11 @@ public class ASTType extends SimpleJavaNode {
}
public String getTypeImage() {
ASTPrimitiveType prim = (ASTPrimitiveType) getFirstChildOfType(ASTPrimitiveType.class);
ASTPrimitiveType prim = getFirstChildOfType(ASTPrimitiveType.class);
if (prim != null) {
return prim.getImage();
}
return ((ASTClassOrInterfaceType) getFirstChildOfType(ASTClassOrInterfaceType.class)).getImage();
return getFirstChildOfType(ASTClassOrInterfaceType.class).getImage();
}
public int getArrayDepth() {

View File

@ -69,7 +69,7 @@ public class ASTVariableDeclaratorId extends SimpleJavaNode {
return ((ASTFormalParameter) jjtGetParent()).getTypeNode();
} else if (jjtGetParent().jjtGetParent() instanceof ASTLocalVariableDeclaration || jjtGetParent().jjtGetParent() instanceof ASTFieldDeclaration) {
SimpleNode n = (SimpleNode) jjtGetParent().jjtGetParent();
return (ASTType) n.getFirstChildOfType(ASTType.class);
return n.getFirstChildOfType(ASTType.class);
}
throw new RuntimeException("Don't know how to get the type for anything other than ASTLocalVariableDeclaration/ASTFormalParameter/ASTFieldDeclaration");
}

View File

@ -289,17 +289,17 @@ public abstract class SimpleNode implements Node {
* @param childType class which you want to find.
* @return Node of type childType. Returns <code>null</code> if none found.
*/
public <T> Node getFirstChildOfType(Class<T> childType) {
public <T> T getFirstChildOfType(Class<T> childType) {
return getFirstChildOfType(childType, this);
}
private <T> Node getFirstChildOfType(Class<T> childType, Node node) {
private <T> T getFirstChildOfType(Class<T> childType, Node node) {
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
Node n = node.jjtGetChild(i);
if (n != null) {
if (n.getClass().equals(childType))
return n;
Node n2 = getFirstChildOfType(childType, n);
return (T) n;
T n2 = getFirstChildOfType(childType, n);
if (n2 != null)
return n2;
}

View File

@ -18,7 +18,7 @@ public class CurrentPath {
return list.size();
}
public Iterator iterator() {
public Iterator<IDataFlowNode> iterator() {
return list.iterator();
}

View File

@ -9,7 +9,6 @@ import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.ast.SimpleNode;
import net.sourceforge.pmd.symboltable.NameOccurrence;
import java.util.Iterator;
import java.util.List;
/**
@ -34,9 +33,8 @@ public abstract class AbstractInefficientZeroCheck extends AbstractRule {
return data;
}
List declars = node.getUsages();
for (Iterator i = declars.iterator(); i.hasNext();) {
NameOccurrence occ = (NameOccurrence) i.next();
List<NameOccurrence> declars = node.getUsages();
for (NameOccurrence occ: declars) {
if (!isTargetMethod(occ)) {
continue;
}

View File

@ -8,7 +8,6 @@ import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.ast.SimpleNode;
import net.sourceforge.pmd.symboltable.NameOccurrence;
import java.util.Iterator;
import java.util.List;
/**
@ -94,8 +93,7 @@ public abstract class AbstractPoorMethodCall extends AbstractRule {
return data;
}
for (Iterator i = node.getUsages().iterator(); i.hasNext();) {
NameOccurrence occ = (NameOccurrence) i.next();
for (NameOccurrence occ: node.getUsages()) {
if (isNotedMethod(occ.getNameForWhichThisIsAQualifier())) {
SimpleNode parent = (SimpleNode)occ.getLocation().jjtGetParent().jjtGetParent();
if (parent instanceof ASTPrimaryExpression) {

View File

@ -68,7 +68,7 @@ public class AccessorClassGeneration extends AbstractRule {
m_Instantiations.add(ad);
}
public Iterator getInstantiationIterator() {
public Iterator<AllocData> getInstantiationIterator() {
return m_Instantiations.iterator();
}
@ -76,7 +76,7 @@ public class AccessorClassGeneration extends AbstractRule {
m_PrivateConstructors.add(cd);
}
public Iterator getPrivateConstructorIterator() {
public Iterator<ASTConstructorDeclaration> getPrivateConstructorIterator() {
return m_PrivateConstructors.iterator();
}
@ -88,7 +88,7 @@ public class AccessorClassGeneration extends AbstractRule {
m_ClassQualifyingNames.add(name);
}
public List getClassQualifyingNamesList() {
public List<String> getClassQualifyingNamesList() {
return m_ClassQualifyingNames;
}
}
@ -99,7 +99,7 @@ public class AccessorClassGeneration extends AbstractRule {
private ASTAllocationExpression m_ASTAllocationExpression;
private boolean isArray;
public AllocData(ASTAllocationExpression node, String aPackageName, List classQualifyingNames) {
public AllocData(ASTAllocationExpression node, String aPackageName, List<String> classQualifyingNames) {
if (node.jjtGetChild(1) instanceof ASTArguments) {
ASTArguments aa = (ASTArguments) node.jjtGetChild(1);
m_ArgumentCount = aa.getArgumentCount();
@ -114,8 +114,8 @@ public class AccessorClassGeneration extends AbstractRule {
//strip off outer class names
//try OuterClass, then try OuterClass.InnerClass, then try OuterClass.InnerClass.InnerClass2, etc...
String findName = "";
for (ListIterator li = classQualifyingNames.listIterator(classQualifyingNames.size()); li.hasPrevious();) {
String aName = (String) li.previous();
for (ListIterator<String> li = classQualifyingNames.listIterator(classQualifyingNames.size()); li.hasPrevious();) {
String aName = li.previous();
findName = aName + "." + findName;
if (m_Name.startsWith(findName)) {
//strip off name and exit
@ -244,15 +244,15 @@ public class AccessorClassGeneration extends AbstractRule {
private void processRule(Object ctx) {
//check constructors of outerIterator against allocations of innerIterator
for (ClassData outerDataSet : classDataList) {
for (Iterator constructors = outerDataSet.getPrivateConstructorIterator(); constructors.hasNext();) {
ASTConstructorDeclaration cd = (ASTConstructorDeclaration) constructors.next();
for (Iterator<ASTConstructorDeclaration> constructors = outerDataSet.getPrivateConstructorIterator(); constructors.hasNext();) {
ASTConstructorDeclaration cd = constructors.next();
for (ClassData innerDataSet : classDataList) {
if (outerDataSet == innerDataSet) {
continue;
}
for (Iterator allocations = innerDataSet.getInstantiationIterator(); allocations.hasNext();) {
AllocData ad = (AllocData) allocations.next();
for (Iterator<AllocData> allocations = innerDataSet.getInstantiationIterator(); allocations.hasNext();) {
AllocData ad = allocations.next();
//if the constructor matches the instantiation
//flag the instantiation as a generator of an extra class
if (outerDataSet.getClassName().equals(ad.getName()) && (cd.getParameterCount() == ad.getArgumentCount())) {

View File

@ -8,7 +8,6 @@ import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
import net.sourceforge.pmd.ast.ASTFieldDeclaration;
import net.sourceforge.pmd.ast.ASTMethodDeclaration;
import java.util.Iterator;
import java.util.List;
public class AvoidFieldNameMatchingMethodName extends AbstractRule {
@ -27,9 +26,8 @@ public class AvoidFieldNameMatchingMethodName extends AbstractRule {
varName = varName.toLowerCase();
ASTClassOrInterfaceDeclaration cl = node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
if (cl != null) {
List methods = cl.findChildrenOfType(ASTMethodDeclaration.class);
for (Iterator it = methods.iterator(); it.hasNext();) {
ASTMethodDeclaration m = (ASTMethodDeclaration) it.next();
List<ASTMethodDeclaration> methods = cl.findChildrenOfType(ASTMethodDeclaration.class);
for (ASTMethodDeclaration m: methods) {
//Make sure we are comparing fields and methods inside same type
if (fieldDeclaringType.equals(getDeclaringType(m))) {
String n = m.getMethodName();

View File

@ -3,7 +3,6 @@
*/
package net.sourceforge.pmd.rules;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@ -15,14 +14,11 @@ import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
public class AvoidReassigningParameters extends AbstractRule {
public Object visit(ASTMethodDeclarator node, Object data) {
Map params = node.getScope().getVariableDeclarations();
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();
Map<VariableNameDeclaration, List<NameOccurrence>> params = node.getScope().getVariableDeclarations();
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry: params.entrySet()) {
VariableNameDeclaration decl = entry.getKey();
List<NameOccurrence> usages = entry.getValue();
for (NameOccurrence occ: usages) {
if ((occ.isOnLeftHandSide() || occ.isSelfAssignment()) &&
occ.getNameForWhichThisIsAQualifier() == null &&
(!decl.isArray() || occ.getLocation().jjtGetParent().jjtGetParent().jjtGetNumChildren() == 1))

View File

@ -5,7 +5,6 @@ package net.sourceforge.pmd.rules;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@ -67,10 +66,9 @@ public class BeanMembersShouldSerializeRule extends AbstractRule {
Arrays.sort(methNameArray);
Map vars = node.getScope().getVariableDeclarations();
for (Iterator i = vars.keySet().iterator(); i.hasNext();) {
VariableNameDeclaration decl = (VariableNameDeclaration) i.next();
if (((List) vars.get(decl)).isEmpty() || decl.getAccessNodeParent().isTransient() || decl.getAccessNodeParent().isStatic()) {
Map<VariableNameDeclaration, List<NameOccurrence>> vars = node.getScope().getVariableDeclarations();
for (VariableNameDeclaration decl: vars.keySet()) {
if (vars.get(decl).isEmpty() || decl.getAccessNodeParent().isTransient() || decl.getAccessNodeParent().isStatic()) {
continue;
}
String varName = trimIfPrefix(decl.getImage());

View File

@ -20,7 +20,6 @@ import net.sourceforge.pmd.properties.StringProperty;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -70,12 +69,11 @@ public class CloseResource extends AbstractRule {
}
public Object visit(ASTMethodDeclaration node, Object data) {
List vars = node.findChildrenOfType(ASTLocalVariableDeclaration.class);
List<ASTLocalVariableDeclaration> vars = node.findChildrenOfType(ASTLocalVariableDeclaration.class);
List<ASTVariableDeclaratorId> ids = new ArrayList<ASTVariableDeclaratorId>();
// find all variable references to Connection objects
for (Iterator it = vars.iterator(); it.hasNext();) {
ASTLocalVariableDeclaration var = (ASTLocalVariableDeclaration) it.next();
for (ASTLocalVariableDeclaration var: vars) {
ASTType type = var.getTypeNode();
if (type.jjtGetChild(0) instanceof ASTReferenceType) {

View File

@ -68,7 +68,7 @@ public class CyclomaticComplexity extends AbstractRule {
}
public Object visit(ASTIfStatement node, Object data) {
int boolCompIf = NpathComplexity.sumExpressionComplexity( (ASTExpression) node.getFirstChildOfType( ASTExpression.class ) );
int boolCompIf = NpathComplexity.sumExpressionComplexity( node.getFirstChildOfType( ASTExpression.class ) );
// If statement always has a complexity of at least 1
boolCompIf++;
@ -84,7 +84,7 @@ public class CyclomaticComplexity extends AbstractRule {
}
public Object visit(ASTForStatement node, Object data) {
int boolCompFor = NpathComplexity.sumExpressionComplexity( (ASTExpression) node.getFirstChildOfType( ASTExpression.class ) );
int boolCompFor = NpathComplexity.sumExpressionComplexity( node.getFirstChildOfType( ASTExpression.class ) );
// For statement always has a complexity of at least 1
boolCompFor++;
@ -94,7 +94,7 @@ public class CyclomaticComplexity extends AbstractRule {
}
public Object visit(ASTDoStatement node, Object data) {
int boolCompDo = NpathComplexity.sumExpressionComplexity( (ASTExpression) node.getFirstChildOfType( ASTExpression.class ) );
int boolCompDo = NpathComplexity.sumExpressionComplexity( node.getFirstChildOfType( ASTExpression.class ) );
// Do statement always has a complexity of at least 1
boolCompDo++;
@ -106,7 +106,7 @@ public class CyclomaticComplexity extends AbstractRule {
public Object visit(ASTSwitchStatement node, Object data) {
Entry entry = entryStack.peek();
int boolCompSwitch = NpathComplexity.sumExpressionComplexity( (ASTExpression) node.getFirstChildOfType( ASTExpression.class ) );
int boolCompSwitch = NpathComplexity.sumExpressionComplexity( node.getFirstChildOfType( ASTExpression.class ) );
entry.bumpDecisionPoints( boolCompSwitch );
int childCount = node.jjtGetNumChildren();
@ -129,7 +129,7 @@ public class CyclomaticComplexity extends AbstractRule {
}
public Object visit(ASTWhileStatement node, Object data) {
int boolCompWhile = NpathComplexity.sumExpressionComplexity( (ASTExpression) node.getFirstChildOfType( ASTExpression.class ) );
int boolCompWhile = NpathComplexity.sumExpressionComplexity( node.getFirstChildOfType( ASTExpression.class ) );
// While statement always has a complexity of at least 1
boolCompWhile++;
@ -140,7 +140,7 @@ public class CyclomaticComplexity extends AbstractRule {
public Object visit(ASTConditionalExpression node, Object data) {
if ( node.isTernary() ) {
int boolCompTern = NpathComplexity.sumExpressionComplexity( (ASTExpression) node.getFirstChildOfType( ASTExpression.class ) );
int boolCompTern = NpathComplexity.sumExpressionComplexity( node.getFirstChildOfType( ASTExpression.class ) );
// Ternary statement always has a complexity of at least 1
boolCompTern++;

View File

@ -4,15 +4,13 @@ import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
import net.sourceforge.pmd.ast.ASTMethodDeclarator;
import java.util.Iterator;
import java.util.List;
public class MethodWithSameNameAsEnclosingClass extends AbstractRule {
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
List methods = node.findChildrenOfType(ASTMethodDeclarator.class);
for (Iterator i = methods.iterator(); i.hasNext();) {
ASTMethodDeclarator m = (ASTMethodDeclarator) i.next();
List<ASTMethodDeclarator> methods = node.findChildrenOfType(ASTMethodDeclarator.class);
for (ASTMethodDeclarator m: methods) {
if (m.hasImageEqualTo(node.getImage())) {
addViolation(data, m);
}

View File

@ -45,7 +45,7 @@ public class MoreThanOneLogger extends AbstractRule {
if (count > 1) {
return super.visit(node, data);
}
SimpleNode type = (SimpleNode) ((SimpleNode) node.jjtGetParent()).getFirstChildOfType(ASTType.class);
SimpleNode type = ((SimpleNode) node.jjtGetParent()).getFirstChildOfType(ASTType.class);
if (type != null) {
SimpleNode reftypeNode = (SimpleNode) type.jjtGetChild(0);
if (reftypeNode instanceof ASTReferenceType) {

View File

@ -58,11 +58,10 @@ public class OverrideBothEqualsAndHashcode extends AbstractRule {
for (int ix = 0; ix < node.jjtGetNumChildren(); ix++) {
SimpleNode sn = (SimpleNode) node.jjtGetChild(ix);
if (sn.getClass().equals(ASTFormalParameters.class)) {
List allParams = ((ASTFormalParameters) sn).findChildrenOfType(ASTFormalParameter.class);
for (int i = 0; i < allParams.size(); i++) {
List<ASTFormalParameter> allParams = ((ASTFormalParameters) sn).findChildrenOfType(ASTFormalParameter.class);
for (ASTFormalParameter formalParam: allParams) {
iFormalParams++;
ASTFormalParameter formalParam = (ASTFormalParameter) allParams.get(i);
ASTClassOrInterfaceType param = (ASTClassOrInterfaceType) formalParam.getFirstChildOfType(ASTClassOrInterfaceType.class);
ASTClassOrInterfaceType param = formalParam.getFirstChildOfType(ASTClassOrInterfaceType.class);
if (param != null) {
paramName = param.getImage();
}

View File

@ -9,14 +9,11 @@ import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.ast.SimpleNode;
import net.sourceforge.pmd.symboltable.NameOccurrence;
import java.util.Iterator;
public class SymbolTableTestRule extends AbstractRule {
public Object visit(ASTFieldDeclaration node,Object data) {
ASTVariableDeclaratorId declaration = node.findChildrenOfType(ASTVariableDeclaratorId.class).get(0);
for (Iterator iter = declaration.getUsages().iterator();iter.hasNext();) {
NameOccurrence no = (NameOccurrence)iter.next();
for (NameOccurrence no: declaration.getUsages()) {
SimpleNode location = no.getLocation();
System.out.println(declaration.getImage() + " is used here: " + location.getImage());
}

View File

@ -12,7 +12,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;
@ -36,11 +35,10 @@ public class UnusedFormalParameterRule extends AbstractRule {
private void check(SimpleNode node, Object data) {
Node parent = node.jjtGetParent().jjtGetParent().jjtGetParent();
if (parent instanceof ASTClassOrInterfaceDeclaration && !((ASTClassOrInterfaceDeclaration) parent).isInterface()) {
Map vars = node.getScope().getVariableDeclarations();
for (Iterator i = vars.entrySet().iterator(); i.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
VariableNameDeclaration nameDecl = (VariableNameDeclaration) entry.getKey();
if (actuallyUsed(nameDecl, (List) entry.getValue())) {
Map<VariableNameDeclaration, List<NameOccurrence>> vars = node.getScope().getVariableDeclarations();
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry: vars.entrySet()) {
VariableNameDeclaration nameDecl = entry.getKey();
if (actuallyUsed(nameDecl, entry.getValue())) {
continue;
}
addViolation(data, node, new Object[]{node instanceof ASTMethodDeclaration ? "method" : "constructor", nameDecl.getImage()});
@ -48,9 +46,8 @@ public class UnusedFormalParameterRule extends AbstractRule {
}
}
private boolean actuallyUsed(VariableNameDeclaration nameDecl, List usages) {
for (Iterator j = usages.iterator(); j.hasNext();) {
NameOccurrence occ = (NameOccurrence) j.next();
private boolean actuallyUsed(VariableNameDeclaration nameDecl, List<NameOccurrence> usages) {
for (NameOccurrence occ: usages) {
if (occ.isOnLeftHandSide()) {
if (nameDecl.isArray() && occ.getLocation().jjtGetParent().jjtGetParent().jjtGetNumChildren() > 1) {
// array element access

View File

@ -9,7 +9,6 @@ import net.sourceforge.pmd.ast.ASTVariableDeclarator;
import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.symboltable.NameOccurrence;
import java.util.Iterator;
import java.util.List;
public class UnusedLocalVariableRule extends AbstractRule {
@ -30,9 +29,8 @@ public class UnusedLocalVariableRule extends AbstractRule {
return data;
}
private boolean actuallyUsed(List usages) {
for (Iterator j = usages.iterator(); j.hasNext();) {
NameOccurrence occ = (NameOccurrence) j.next();
private boolean actuallyUsed(List<NameOccurrence> usages) {
for (NameOccurrence occ: usages) {
if (occ.isOnLeftHandSide()) {
continue;
} else {

View File

@ -8,30 +8,27 @@ import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
import net.sourceforge.pmd.symboltable.NameOccurrence;
import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class UnusedPrivateFieldRule 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().isPrivate() || isOK(decl.getImage())) {
continue;
}
if (!actuallyUsed((List) entry.getValue())) {
if (!actuallyUsed(entry.getValue())) {
addViolation(data, decl.getNode(), decl.getImage());
}
}
return super.visit(node, data);
}
private boolean actuallyUsed(List usages) {
for (Iterator j = usages.iterator(); j.hasNext();) {
NameOccurrence nameOccurrence = (NameOccurrence) j.next();
private boolean actuallyUsed(List<NameOccurrence> usages) {
for (NameOccurrence nameOccurrence: usages) {
if (!nameOccurrence.isOnLeftHandSide()) {
return true;
}

View File

@ -57,9 +57,9 @@ public class UselessAssignment extends AbstractRule implements Executable {
public void execute(CurrentPath path) {
Map<String, Usage> hash = new HashMap<String, Usage>();
//System.out.println("path size is " + path.size());
for (Iterator i = path.iterator(); i.hasNext();) {
for (Iterator<IDataFlowNode> i = path.iterator(); i.hasNext();) {
//System.out.println("i = " + i);
IDataFlowNode inode = (IDataFlowNode) i.next();
IDataFlowNode inode = i.next();
if (inode.getVariableAccess() == null) {
continue;
}

View File

@ -1,6 +1,5 @@
package net.sourceforge.pmd.rules;
import java.util.Iterator;
import java.util.Set;
import net.sourceforge.pmd.AbstractRule;
@ -36,8 +35,7 @@ public class UselessOperationOnImmutable extends AbstractRule {
return super.visit(node, data);
}
String variableName = var.getImage();
for (Iterator it = var.getUsages().iterator(); it.hasNext();) {
NameOccurrence no = (NameOccurrence) it.next();
for (NameOccurrence no: var.getUsages()) {
// FIXME - getUsages will return everything with the same name as the variable,
// see JUnit test, case 6. Changing to SimpleNode below, revisit when getUsages is fixed
SimpleNode sn = no.getLocation();

View File

@ -29,9 +29,9 @@ public class BigIntegerInstantiation extends AbstractRule {
if (("BigInteger".equals(img) || (jdk15 && "BigDecimal".equals(img))) &&
(node.getFirstChildOfType(ASTArrayDimsAndInits.class) == null)
) {
ASTArguments args = (ASTArguments) node.getFirstChildOfType(ASTArguments.class);
ASTArguments args = node.getFirstChildOfType(ASTArguments.class);
if (args.getArgumentCount() == 1) {
ASTLiteral literal = (ASTLiteral) node.getFirstChildOfType(ASTLiteral.class);
ASTLiteral literal = node.getFirstChildOfType(ASTLiteral.class);
if (literal == null || literal.jjtGetParent().jjtGetParent().jjtGetParent().jjtGetParent().jjtGetParent() != args) {
return super.visit(node, data);
}

View File

@ -47,11 +47,11 @@ public class BooleanInstantiation extends AbstractRule {
if ("Boolean.valueOf".equals(((ASTName) node.jjtGetChild(0)).getImage())
|| "java.lang.Boolean.valueOf".equals(((ASTName) node.jjtGetChild(0)).getImage())) {
ASTPrimaryExpression parent = (ASTPrimaryExpression) node.jjtGetParent();
ASTPrimarySuffix suffix = (ASTPrimarySuffix) parent.getFirstChildOfType(ASTPrimarySuffix.class);
ASTPrimarySuffix suffix = parent.getFirstChildOfType(ASTPrimarySuffix.class);
if (suffix == null) {
return super.visit(node, data);
}
ASTPrimaryPrefix prefix = (ASTPrimaryPrefix) suffix.getFirstChildOfType(ASTPrimaryPrefix.class);
ASTPrimaryPrefix prefix = suffix.getFirstChildOfType(ASTPrimaryPrefix.class);
if (prefix == null) {
return super.visit(node, data);
}
@ -60,7 +60,7 @@ public class BooleanInstantiation extends AbstractRule {
super.addViolation(data, node);
return data;
}
ASTLiteral literal = (ASTLiteral) prefix.getFirstChildOfType(ASTLiteral.class);
ASTLiteral literal = prefix.getFirstChildOfType(ASTLiteral.class);
if (literal != null && ("\"true\"".equals(literal.getImage()) || "\"false\"".equals(literal.getImage()))) {
super.addViolation(data, node);
return data;

View File

@ -1,7 +1,6 @@
package net.sourceforge.pmd.rules.basic;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import net.sourceforge.pmd.AbstractRule;
@ -26,12 +25,12 @@ public class BrokenNullCheck extends AbstractRule {
public Object visit(ASTIfStatement node, Object data) {
ASTExpression expression = (ASTExpression)node.jjtGetChild(0);
ASTConditionalAndExpression conditionalAndExpression = (ASTConditionalAndExpression)expression.getFirstChildOfType(ASTConditionalAndExpression.class);
ASTConditionalAndExpression conditionalAndExpression = expression.getFirstChildOfType(ASTConditionalAndExpression.class);
if (conditionalAndExpression != null) {
checkForViolations(node, data, conditionalAndExpression);
}
ASTConditionalOrExpression conditionalOrExpression = (ASTConditionalOrExpression)expression.getFirstChildOfType(ASTConditionalOrExpression.class);
ASTConditionalOrExpression conditionalOrExpression = expression.getFirstChildOfType(ASTConditionalOrExpression.class);
if (conditionalOrExpression != null) {
checkForViolations(node, data, conditionalOrExpression);
}
@ -53,7 +52,7 @@ public class BrokenNullCheck extends AbstractRule {
!"!=".equals(equalityExpression.getImage())) {
return;
}
ASTNullLiteral nullLiteral = (ASTNullLiteral)equalityExpression.getFirstChildOfType(ASTNullLiteral.class);
ASTNullLiteral nullLiteral = equalityExpression.getFirstChildOfType(ASTNullLiteral.class);
if (nullLiteral == null) {
return; //No null check
}
@ -82,7 +81,7 @@ public class BrokenNullCheck extends AbstractRule {
conditionalPrimaryExpression = (ASTPrimaryExpression)conditionalSubnode;
} else {
//The ASTPrimaryExpression is hidden (in a negation, braces or EqualityExpression)
conditionalPrimaryExpression = (ASTPrimaryExpression) conditionalSubnode
conditionalPrimaryExpression = conditionalSubnode
.getFirstChildOfType(ASTPrimaryExpression.class);
}
@ -151,13 +150,11 @@ public class BrokenNullCheck extends AbstractRule {
}
private ASTPrimaryExpression findNullCompareExpression(ASTEqualityExpression equalityExpression) {
List primaryExpressions = equalityExpression.findChildrenOfType(ASTPrimaryExpression.class);
for (Iterator iter = primaryExpressions.iterator(); iter.hasNext();) {
ASTPrimaryExpression primaryExpression = (ASTPrimaryExpression) iter.next();
List primaryPrefixes = primaryExpression.findChildrenOfType(ASTPrimaryPrefix.class);
for (Iterator iterator = primaryPrefixes.iterator(); iterator.hasNext();) {
ASTPrimaryPrefix primaryPrefix = (ASTPrimaryPrefix) iterator.next();
ASTName name = (ASTName)primaryPrefix.getFirstChildOfType(ASTName.class);
List<ASTPrimaryExpression> primaryExpressions = equalityExpression.findChildrenOfType(ASTPrimaryExpression.class);
for (ASTPrimaryExpression primaryExpression: primaryExpressions) {
List<ASTPrimaryPrefix> primaryPrefixes = primaryExpression.findChildrenOfType(ASTPrimaryPrefix.class);
for (ASTPrimaryPrefix primaryPrefix: primaryPrefixes) {
ASTName name = primaryPrefix.getFirstChildOfType(ASTName.class);
if (name != null) {
//We found the variable that is compared to null
return primaryExpression;

Some files were not shown because too many files have changed in this diff Show More