use of net.sourceforge.pmd.lang.java.ast.ASTFormalParameters 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.ASTFormalParameters in project pmd by pmd.
the class MethodNameDeclaration method getParameterDisplaySignature.
public String getParameterDisplaySignature() {
StringBuilder sb = new StringBuilder("(");
ASTFormalParameters params = (ASTFormalParameters) node.jjtGetChild(0);
// no need to trim at the end
for (int i = 0; i < ((ASTMethodDeclarator) node).getParameterCount(); i++) {
ASTFormalParameter p = (ASTFormalParameter) params.jjtGetChild(i);
sb.append(p.getTypeNode().getTypeImage());
if (p.isVarargs()) {
sb.append("...");
}
sb.append(',');
}
if (sb.charAt(sb.length() - 1) == ',') {
sb.deleteCharAt(sb.length() - 1);
}
sb.append(')');
return sb.toString();
}
use of net.sourceforge.pmd.lang.java.ast.ASTFormalParameters 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.ASTFormalParameters in project pmd by pmd.
the class ScopeAndDeclarationFinder method visit.
@Override
public Object visit(ASTConstructorDeclaration node, Object data) {
/*
* Local variables declared inside the constructor need to be in a
* different scope so special handling is needed
*/
createMethodScope(node);
Scope methodScope = node.getScope();
Node formalParameters = node.jjtGetChild(0);
int i = 1;
int n = node.jjtGetNumChildren();
if (!(formalParameters instanceof ASTFormalParameters)) {
visit((ASTTypeParameters) formalParameters, data);
formalParameters = node.jjtGetChild(1);
i++;
}
visit((ASTFormalParameters) formalParameters, data);
Scope localScope = null;
for (; i < n; i++) {
JavaNode b = (JavaNode) node.jjtGetChild(i);
if (b instanceof ASTBlockStatement) {
if (localScope == null) {
createLocalScope(node);
localScope = node.getScope();
}
b.setScope(localScope);
visit(b, data);
} else {
visit(b, data);
}
}
if (localScope != null) {
// pop the local scope
scopes.pop();
// reset the correct scope for the constructor
node.setScope(methodScope);
}
// pop the method scope
scopes.pop();
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTFormalParameters in project pmd by pmd.
the class ArrayIsStoredDirectlyRule method visit.
@Override
public Object visit(ASTMethodDeclaration node, Object data) {
final ASTFormalParameters params = node.getFirstDescendantOfType(ASTFormalParameters.class);
ASTFormalParameter[] arrs = getArrays(params);
checkAll(data, arrs, node.findDescendantsOfType(ASTBlockStatement.class));
return data;
}
Aggregations