use of net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType 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.ASTPrimitiveType 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.ASTPrimitiveType in project pmd by pmd.
the class ClassScope method createBuiltInMethodDeclaration.
/**
* Creates a fake method name declaration for built-in methods from Java
* like the Enum Method "valueOf".
*
* @param methodName
* the method name
* @param parameterTypes
* the reference types of each parameter of the method
* @return a method name declaration
*/
private MethodNameDeclaration createBuiltInMethodDeclaration(final String methodName, final String... parameterTypes) {
ASTMethodDeclaration methodDeclaration = new ASTMethodDeclaration(JavaParserTreeConstants.JJTMETHODDECLARATION);
methodDeclaration.setPublic(true);
methodDeclaration.setScope(this);
ASTMethodDeclarator methodDeclarator = new ASTMethodDeclarator(JavaParserTreeConstants.JJTMETHODDECLARATOR);
methodDeclarator.setImage(methodName);
methodDeclarator.setScope(this);
ASTFormalParameters formalParameters = new ASTFormalParameters(JavaParserTreeConstants.JJTFORMALPARAMETERS);
formalParameters.setScope(this);
methodDeclaration.jjtAddChild(methodDeclarator, 0);
methodDeclarator.jjtSetParent(methodDeclaration);
methodDeclarator.jjtAddChild(formalParameters, 0);
formalParameters.jjtSetParent(methodDeclarator);
/*
* jjtAddChild resizes it's child node list according to known indexes.
* Going backwards makes sure the first time it gets the right size avoiding copies.
*/
for (int i = parameterTypes.length - 1; i >= 0; i--) {
ASTFormalParameter formalParameter = new ASTFormalParameter(JavaParserTreeConstants.JJTFORMALPARAMETER);
formalParameters.jjtAddChild(formalParameter, i);
formalParameter.jjtSetParent(formalParameters);
ASTVariableDeclaratorId variableDeclaratorId = new ASTVariableDeclaratorId(JavaParserTreeConstants.JJTVARIABLEDECLARATORID);
variableDeclaratorId.setImage("arg" + i);
formalParameter.jjtAddChild(variableDeclaratorId, 1);
variableDeclaratorId.jjtSetParent(formalParameter);
ASTType type = new ASTType(JavaParserTreeConstants.JJTTYPE);
formalParameter.jjtAddChild(type, 0);
type.jjtSetParent(formalParameter);
if (PRIMITIVE_TYPES.contains(parameterTypes[i])) {
ASTPrimitiveType primitiveType = new ASTPrimitiveType(JavaParserTreeConstants.JJTPRIMITIVETYPE);
primitiveType.setImage(parameterTypes[i]);
type.jjtAddChild(primitiveType, 0);
primitiveType.jjtSetParent(type);
} else {
ASTReferenceType referenceType = new ASTReferenceType(JavaParserTreeConstants.JJTREFERENCETYPE);
type.jjtAddChild(referenceType, 0);
referenceType.jjtSetParent(type);
// TODO : this could actually be a primitive array...
ASTClassOrInterfaceType classOrInterfaceType = new ASTClassOrInterfaceType(JavaParserTreeConstants.JJTCLASSORINTERFACETYPE);
classOrInterfaceType.setImage(parameterTypes[i]);
referenceType.jjtAddChild(classOrInterfaceType, 0);
classOrInterfaceType.jjtSetParent(referenceType);
}
}
return new MethodNameDeclaration(methodDeclarator);
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType in project pmd by pmd.
the class MethodNameDeclaration method equals.
@Override
public boolean equals(Object o) {
if (!(o instanceof MethodNameDeclaration)) {
return false;
}
MethodNameDeclaration other = (MethodNameDeclaration) o;
// compare name
if (!other.node.getImage().equals(node.getImage())) {
return false;
}
// params, too
if (((ASTMethodDeclarator) other.node).getParameterCount() != ((ASTMethodDeclarator) node).getParameterCount()) {
return false;
}
// compare parameter types
ASTFormalParameters myParams = (ASTFormalParameters) node.jjtGetChild(0);
ASTFormalParameters otherParams = (ASTFormalParameters) other.node.jjtGetChild(0);
for (int i = 0; i < ((ASTMethodDeclarator) node).getParameterCount(); i++) {
ASTFormalParameter myParam = (ASTFormalParameter) myParams.jjtGetChild(i);
ASTFormalParameter otherParam = (ASTFormalParameter) otherParams.jjtGetChild(i);
// Compare vararg
if (myParam.isVarargs() != otherParam.isVarargs()) {
return false;
}
Node myTypeNode = myParam.getTypeNode().jjtGetChild(0);
Node otherTypeNode = otherParam.getTypeNode().jjtGetChild(0);
// compare primitive vs reference type
if (myTypeNode.getClass() != otherTypeNode.getClass()) {
return false;
}
// simple comparison of type images
// this can be fooled by one method using "String"
// and the other method using "java.lang.String"
// once we get real types in here that should get fixed
String myTypeImg;
String otherTypeImg;
if (myTypeNode instanceof ASTPrimitiveType) {
myTypeImg = myTypeNode.getImage();
otherTypeImg = otherTypeNode.getImage();
} else {
myTypeImg = myTypeNode.jjtGetChild(0).getImage();
otherTypeImg = otherTypeNode.jjtGetChild(0).getImage();
}
if (!myTypeImg.equals(otherTypeImg)) {
return false;
}
// if type is ASTPrimitiveType and is an array, make sure the other
// one is also
}
return true;
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType in project pmd by pmd.
the class AbstractInefficientZeroCheck method visit.
@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
Node nameNode = node.getTypeNameNode();
if (nameNode == null || nameNode instanceof ASTPrimitiveType || !appliesToClassName(node.getNameDeclaration().getTypeImage())) {
return data;
}
List<NameOccurrence> declars = node.getUsages();
for (NameOccurrence occ : declars) {
JavaNameOccurrence jocc = (JavaNameOccurrence) occ;
if (!isTargetMethod(jocc)) {
continue;
}
Node expr = jocc.getLocation().jjtGetParent().jjtGetParent().jjtGetParent();
checkNodeAndReport(data, jocc.getLocation(), expr);
}
return data;
}
Aggregations