use of net.sourceforge.pmd.lang.java.ast.ASTResultType in project pmd by pmd.
the class BeanMembersShouldSerializeRule method isBeanAccessor.
private boolean isBeanAccessor(ASTMethodDeclarator meth) {
String methodName = meth.getImage();
if (methodName.startsWith("get") || methodName.startsWith("set")) {
return true;
}
if (methodName.startsWith("is")) {
ASTResultType ret = ((ASTMethodDeclaration) meth.jjtGetParent()).getResultType();
List<ASTPrimitiveType> primitives = ret.findDescendantsOfType(ASTPrimitiveType.class);
if (!primitives.isEmpty() && primitives.get(0).isBoolean()) {
return true;
}
}
return false;
}
use of net.sourceforge.pmd.lang.java.ast.ASTResultType in project pmd by pmd.
the class UselessOverridingMethodRule method isMethodType.
// TODO: this method should be externalize into an utility class, shouldn't it ?
private boolean isMethodType(ASTMethodDeclaration node, String methodType) {
boolean result = false;
ASTResultType type = node.getResultType();
if (type != null) {
result = type.hasDescendantMatchingXPath("./Type/ReferenceType/ClassOrInterfaceType[@Image = '" + methodType + "']");
}
return result;
}
use of net.sourceforge.pmd.lang.java.ast.ASTResultType in project pmd by pmd.
the class SuspiciousHashcodeMethodNameRule method visit.
public Object visit(ASTMethodDeclaration node, Object data) {
/*
* original XPath rule was //MethodDeclaration [ResultType
* //PrimitiveType [@Image='int'] [//MethodDeclarator [@Image='hashcode'
* or @Image='HashCode' or @Image='Hashcode']
* [not(FormalParameters/*)]]]
*/
ASTResultType type = node.getResultType();
ASTMethodDeclarator decl = node.getFirstChildOfType(ASTMethodDeclarator.class);
String name = decl.getImage();
if ("hashcode".equalsIgnoreCase(name) && !"hashCode".equals(name) && decl.jjtGetChild(0).jjtGetNumChildren() == 0 && type.jjtGetNumChildren() != 0) {
Node t = type.jjtGetChild(0).jjtGetChild(0);
if (t instanceof ASTPrimitiveType && "int".equals(t.getImage())) {
addViolation(data, node);
return data;
}
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTResultType in project pmd by pmd.
the class LooseCouplingRule method visit.
// TODO - these should be brought in via external properties
// private static final Set implClassNames = CollectionUtil.asSet( new
// Object[] {
// "ArrayList", "HashSet", "HashMap", "LinkedHashMap", "LinkedHashSet",
// "TreeSet", "TreeMap", "Vector",
// "java.util.ArrayList", "java.util.HashSet", "java.util.HashMap",
// "java.util.LinkedHashMap", "java.util.LinkedHashSet",
// "java.util.TreeSet",
// "java.util.TreeMap", "java.util.Vector"
// });
@Override
public Object visit(ASTClassOrInterfaceType node, Object data) {
if (methodHasOverride(node)) {
return data;
}
Node parent = node.getNthParent(3);
Class<?> clazzType = node.getType();
boolean isType = CollectionUtil.isCollectionType(clazzType, false);
if (isType && (parent instanceof ASTFieldDeclaration || parent instanceof ASTFormalParameter || parent instanceof ASTResultType)) {
addViolation(data, node, node.getImage());
}
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTResultType in project pmd by pmd.
the class UseUtilityClassRule method visit.
@Override
public Object visit(ASTClassOrInterfaceBody decl, Object data) {
if (decl.jjtGetParent() instanceof ASTClassOrInterfaceDeclaration) {
ASTClassOrInterfaceDeclaration parent = (ASTClassOrInterfaceDeclaration) decl.jjtGetParent();
if (parent.isAbstract() || parent.isInterface() || isExceptionType(parent)) {
return super.visit(decl, data);
}
if (isOkUsingLombok(parent)) {
return super.visit(decl, data);
}
int i = decl.jjtGetNumChildren();
int methodCount = 0;
boolean isOK = false;
while (i > 0) {
Node p = decl.jjtGetChild(--i);
if (p.jjtGetNumChildren() == 0) {
continue;
}
Node n = skipAnnotations(p);
if (n instanceof ASTFieldDeclaration) {
if (!((ASTFieldDeclaration) n).isStatic()) {
isOK = true;
break;
}
} else if (n instanceof ASTConstructorDeclaration) {
if (((ASTConstructorDeclaration) n).isPrivate()) {
isOK = true;
break;
}
} else if (n instanceof ASTMethodDeclaration) {
ASTMethodDeclaration m = (ASTMethodDeclaration) n;
if (!m.isPrivate()) {
methodCount++;
}
if (!m.isStatic()) {
isOK = true;
break;
}
// TODO use symbol table
if (m.getMethodName().equals("suite")) {
ASTResultType res = m.getResultType();
ASTClassOrInterfaceType c = res.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
if (c != null && c.hasImageEqualTo("Test")) {
isOK = true;
break;
}
}
}
}
if (!isOK && methodCount > 0) {
addViolation(data, decl);
}
}
return super.visit(decl, data);
}
Aggregations