Fixed several rules (annotations related exceptions for on jdk 1.5 and jdk 1.6 source code)

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4742 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Xavier Le Vourch
2006-10-25 19:40:45 +00:00
parent 70a6550716
commit 9bad2be979
8 changed files with 42 additions and 27 deletions

View File

@ -11,8 +11,12 @@ public class ASTConstructorDeclaration extends AccessNode {
super(p, id);
}
public ASTFormalParameters getParameters() {
return (ASTFormalParameters) (jjtGetChild(0) instanceof ASTFormalParameters?jjtGetChild(0):jjtGetChild(1));
}
public int getParameterCount() {
return ((ASTFormalParameters) jjtGetChild(0)).getParameterCount();
return getParameters().getParameterCount();
}

View File

@ -40,18 +40,25 @@ public class ASTFormalParameter extends AccessNode implements Dimensionable, Can
return checkType() + checkDecl();
}
private int checkType() {
if (jjtGetNumChildren() == 0 || !(jjtGetChild(0) instanceof ASTType)) {
return 0;
public ASTType getTypeNode() {
for (int i = 0; i < jjtGetNumChildren(); i++) {
if (jjtGetChild(i) instanceof ASTType) {
return (ASTType) jjtGetChild(i);
}
}
return ((ASTType) jjtGetChild(0)).getArrayDepth();
throw new IllegalStateException("ASTType not found");
}
private int checkType() {
return getTypeNode().getArrayDepth();
}
private ASTVariableDeclaratorId getDecl() {
return (ASTVariableDeclaratorId) jjtGetChild(jjtGetNumChildren()-1);
}
private int checkDecl() {
if (jjtGetNumChildren() < 2 || !(jjtGetChild(1) instanceof ASTVariableDeclarator)) {
return 0;
}
return ((ASTVariableDeclaratorId) (jjtGetChild(1).jjtGetChild(0))).getArrayDepth();
return getDecl().getArrayDepth();
}
public void dump(String prefix) {

View File

@ -38,24 +38,28 @@ public class ASTLocalVariableDeclaration extends AccessNode implements Dimension
}
public int getArrayDepth() {
if (!isArray()) {
return 0;
}
return checkType() + checkDecl();
}
private int checkType() {
if (jjtGetNumChildren() == 0 || !(jjtGetChild(0) instanceof ASTType)) {
return 0;
public ASTType getTypeNode() {
for (int i = 0; i < jjtGetNumChildren(); i++) {
if (jjtGetChild(i) instanceof ASTType) {
return (ASTType) jjtGetChild(i);
}
}
return ((ASTType) jjtGetChild(0)).getArrayDepth();
throw new IllegalStateException("ASTType not found");
}
private int checkType() {
return getTypeNode().getArrayDepth();
}
private ASTVariableDeclaratorId getDecl() {
return (ASTVariableDeclaratorId) jjtGetChild(jjtGetNumChildren()-1).jjtGetChild(0);
}
private int checkDecl() {
if (jjtGetNumChildren() < 2 || !(jjtGetChild(1) instanceof ASTVariableDeclarator)) {
return 0;
}
return ((ASTVariableDeclaratorId) (jjtGetChild(1).jjtGetChild(0))).getArrayDepth();
return getDecl().getArrayDepth();
}
public void dump(String prefix) {

View File

@ -65,7 +65,7 @@ public class ASTVariableDeclaratorId extends SimpleJavaNode {
public ASTType getTypeNode() {
if (jjtGetParent() instanceof ASTFormalParameter) {
return (ASTType) jjtGetParent().jjtGetChild(0);
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);

View File

@ -77,7 +77,7 @@ public class CloseResource extends AbstractRule {
// find all variable references to Connection objects
for (Iterator it = vars.iterator(); it.hasNext();) {
ASTLocalVariableDeclaration var = (ASTLocalVariableDeclaration) it.next();
ASTType type = (ASTType) var.jjtGetChild(0);
ASTType type = var.getTypeNode();
if (type.jjtGetChild(0) instanceof ASTReferenceType) {
ASTReferenceType ref = (ASTReferenceType) type.jjtGetChild(0);

View File

@ -58,7 +58,7 @@ public class UselessOperationOnImmutable extends AbstractRule {
* @return ASTVariableDeclaratorId
*/
private ASTVariableDeclaratorId getDeclaration(ASTLocalVariableDeclaration node) {
ASTType type = (ASTType) node.jjtGetChild(0);
ASTType type = node.getTypeNode();
if (targetClasses.contains(type.getTypeImage())) {
return (ASTVariableDeclaratorId) node.jjtGetChild(1).jjtGetChild(0);
}

View File

@ -37,7 +37,7 @@ public class ArrayIsStoredDirectly extends AbstractSunSecureRule {
}
public Object visit(ASTConstructorDeclaration node, Object data) {
ASTFormalParameter[] arrs = getArrays((ASTFormalParameters) node.jjtGetChild(0));
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);

View File

@ -31,7 +31,7 @@ public class MethodNameDeclaration extends AbstractNameDeclaration {
// no need to trim at the end
for (int i = 0; i < ((ASTMethodDeclarator) node).getParameterCount(); i++) {
ASTFormalParameter p = (ASTFormalParameter) params.jjtGetChild(i);
sb.append(((ASTType) p.getFirstChildOfType(ASTType.class)).getTypeImage());
sb.append(p.getTypeNode().getTypeImage());
sb.append(',');
}
if (sb.charAt(sb.length() - 1) == ',') {
@ -61,8 +61,8 @@ public class MethodNameDeclaration extends AbstractNameDeclaration {
ASTFormalParameter myParam = (ASTFormalParameter) myParams.jjtGetChild(i);
ASTFormalParameter otherParam = (ASTFormalParameter) otherParams.jjtGetChild(i);
SimpleNode myTypeNode = (SimpleNode) myParam.jjtGetChild(0).jjtGetChild(0);
SimpleNode otherTypeNode = (SimpleNode) otherParam.jjtGetChild(0).jjtGetChild(0);
SimpleNode myTypeNode = (SimpleNode) myParam.getTypeNode().jjtGetChild(0);
SimpleNode otherTypeNode = (SimpleNode) otherParam.getTypeNode().jjtGetChild(0);
// compare primitive vs reference type
if (myTypeNode.getClass() != otherTypeNode.getClass()) {