use of net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType in project pmd by pmd.
the class UnsynchronizedStaticDateFormatterRule method visit.
@Override
public Object visit(ASTFieldDeclaration node, Object data) {
if (!node.isStatic()) {
return data;
}
ASTClassOrInterfaceType cit = node.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
if (cit == null || !targets.contains(cit.getImage())) {
return data;
}
ASTVariableDeclaratorId var = node.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
for (NameOccurrence occ : var.getUsages()) {
Node n = occ.getLocation();
if (n.getFirstParentOfType(ASTSynchronizedStatement.class) != null) {
continue;
}
// ignore usages, that don't call a method.
if (!n.getImage().contains(".")) {
continue;
}
ASTMethodDeclaration method = n.getFirstParentOfType(ASTMethodDeclaration.class);
if (method != null && !method.isSynchronized()) {
addViolation(data, n);
}
}
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType in project pmd by pmd.
the class OverrideBothEqualsAndHashcodeRule method visit.
@Override
public Object visit(ASTImplementsList node, Object data) {
for (int ix = 0; ix < node.jjtGetNumChildren(); ix++) {
if (node.jjtGetChild(ix) instanceof ASTClassOrInterfaceType) {
ASTClassOrInterfaceType cit = (ASTClassOrInterfaceType) node.jjtGetChild(ix);
Class<?> clazz = cit.getType();
if (clazz != null && node.jjtGetChild(ix).hasImageEqualTo("Comparable")) {
implementsComparable = true;
return data;
}
}
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType in project pmd by pmd.
the class OverrideBothEqualsAndHashcodeRule method visit.
@Override
public Object visit(ASTMethodDeclarator node, Object data) {
if (implementsComparable) {
return data;
}
int iFormalParams = 0;
String paramName = null;
for (int ix = 0; ix < node.jjtGetNumChildren(); ix++) {
Node sn = node.jjtGetChild(ix);
if (sn instanceof ASTFormalParameters) {
List<ASTFormalParameter> allParams = ((ASTFormalParameters) sn).findChildrenOfType(ASTFormalParameter.class);
for (ASTFormalParameter formalParam : allParams) {
iFormalParams++;
ASTClassOrInterfaceType param = formalParam.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
if (param != null) {
paramName = param.getImage();
}
}
}
}
if (iFormalParams == 0 && node.hasImageEqualTo("hashCode")) {
containsHashCode = true;
nodeFound = node;
} else if (iFormalParams == 1 && node.hasImageEqualTo("equals") && ("Object".equals(paramName) || "java.lang.Object".equals(paramName))) {
containsEquals = true;
nodeFound = node;
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType in project pmd by pmd.
the class UnnecessaryCastRule method process.
private Object process(Node node, Object data) {
ASTClassOrInterfaceType cit = node.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
if (cit == null || !implClassNames.contains(cit.getImage())) {
return data;
}
cit = cit.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
if (cit == null) {
return data;
}
ASTVariableDeclaratorId decl = node.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
List<NameOccurrence> usages = decl.getUsages();
for (NameOccurrence no : usages) {
ASTName name = (ASTName) no.getLocation();
Node n = name.jjtGetParent().jjtGetParent().jjtGetParent();
if (n instanceof ASTCastExpression) {
addViolation(data, n);
}
}
return null;
}
use of net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType in project pmd by pmd.
the class SignatureDeclareThrowsExceptionRule method visit.
@Override
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
if (junitImported) {
return super.visit(node, data);
}
ASTImplementsList impl = node.getFirstChildOfType(ASTImplementsList.class);
if (impl != null && impl.jjtGetParent().equals(node)) {
for (int ix = 0; ix < impl.jjtGetNumChildren(); ix++) {
Node child = impl.jjtGetChild(ix);
if (child.getClass() != ASTClassOrInterfaceType.class) {
continue;
}
ASTClassOrInterfaceType type = (ASTClassOrInterfaceType) child;
if (isJUnitTest(type)) {
junitImported = true;
return super.visit(node, data);
}
}
}
if (node.jjtGetNumChildren() != 0 && node.jjtGetChild(0) instanceof ASTExtendsList) {
ASTClassOrInterfaceType type = (ASTClassOrInterfaceType) node.jjtGetChild(0).jjtGetChild(0);
if (isJUnitTest(type)) {
junitImported = true;
return super.visit(node, data);
}
}
return super.visit(node, data);
}
Aggregations