use of net.sourceforge.pmd.lang.java.ast.ASTType in project pmd by pmd.
the class UselessStringValueOfRule method visit.
@Override
public Object visit(ASTPrimaryPrefix node, Object data) {
if (node.jjtGetNumChildren() == 0 || !(node.jjtGetChild(0) instanceof ASTName)) {
return super.visit(node, data);
}
String image = ((ASTName) node.jjtGetChild(0)).getImage();
if ("String.valueOf".equals(image)) {
Node parent = node.jjtGetParent();
if (parent.jjtGetNumChildren() != 2) {
return super.visit(node, data);
}
// skip String.valueOf(anyarraytype[])
ASTArgumentList args = parent.getFirstDescendantOfType(ASTArgumentList.class);
if (args != null) {
ASTName arg = args.getFirstDescendantOfType(ASTName.class);
if (arg != null) {
NameDeclaration declaration = arg.getNameDeclaration();
if (declaration != null) {
ASTType argType = declaration.getNode().jjtGetParent().jjtGetParent().getFirstDescendantOfType(ASTType.class);
if (argType != null && argType.jjtGetChild(0) instanceof ASTReferenceType && ((ASTReferenceType) argType.jjtGetChild(0)).isArray()) {
return super.visit(node, data);
}
}
}
}
Node gp = parent.jjtGetParent();
if (parent instanceof ASTPrimaryExpression && gp instanceof ASTAdditiveExpression && "+".equals(gp.getImage())) {
boolean ok = false;
if (gp.jjtGetChild(0) == parent) {
ok = !isPrimitive(gp.jjtGetChild(1));
} else {
for (int i = 0; !ok && gp.jjtGetChild(i) != parent; i++) {
ok = !isPrimitive(gp.jjtGetChild(i));
}
}
if (ok) {
super.addViolation(data, node);
return data;
}
}
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTType 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.ASTType in project pmd by pmd.
the class ClassTypeResolver method getTypeDefinitionOfVariableFromScope.
/**
* Search for a field by it's image stating from a scope and taking into account if it's visible from the
* accessingClass Class. The method takes into account that Nested inherited fields shadow outer scope fields.
*
* @param scope The scope to start the search from.
* @param image The name of the field, local variable or method parameter.
* @param accessingClass The Class (which is defined in the current ACU) that is trying to access the field.
* @return Type def. of the field, or null if it could not be resolved.
*/
private JavaTypeDefinition getTypeDefinitionOfVariableFromScope(Scope scope, String image, Class<?> accessingClass) {
if (accessingClass == null) {
return null;
}
for (; /* empty */
scope != null; scope = scope.getParent()) {
// search each enclosing scope one by one
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : scope.getDeclarations(VariableNameDeclaration.class).entrySet()) {
if (entry.getKey().getImage().equals(image)) {
ASTType typeNode = entry.getKey().getDeclaratorId().getTypeNode();
if (typeNode == null) {
// TODO : Type is infered, ie, this is a lambda such as (var) -> var.equals(other)
return null;
}
if (typeNode.jjtGetChild(0) instanceof ASTReferenceType) {
return ((TypeNode) typeNode.jjtGetChild(0)).getTypeDefinition();
} else {
// primitive type
return JavaTypeDefinition.forClass(typeNode.getType());
}
}
}
// Nested class' inherited fields shadow enclosing variables
if (scope instanceof ClassScope) {
try {
// get the superclass type def. ot the Class the ClassScope belongs to
JavaTypeDefinition superClass = getSuperClassTypeDefinition(((ClassScope) scope).getClassDeclaration().getNode(), null);
// TODO: check if anonymous classes are class scope
// try searching this type def.
JavaTypeDefinition foundTypeDef = getFieldType(superClass, image, accessingClass);
if (foundTypeDef != null) {
// if null, then it's not an inherited field
return foundTypeDef;
}
} catch (ClassCastException ignored) {
// if there is an anonymous class, getClassDeclaration().getType() will throw
// TODO: maybe there is a better way to handle this, maybe this hides bugs
}
}
}
// will return null if not found
return searchImportedStaticFields(image);
}
use of net.sourceforge.pmd.lang.java.ast.ASTType in project pmd by pmd.
the class VariableNameDeclaration method isArray.
public boolean isArray() {
ASTVariableDeclaratorId astVariableDeclaratorId = (ASTVariableDeclaratorId) node;
ASTType typeNode = astVariableDeclaratorId.getTypeNode();
if (typeNode != null) {
return ((Dimensionable) typeNode.jjtGetParent()).isArray();
} else {
return false;
}
}
use of net.sourceforge.pmd.lang.java.ast.ASTType in project pmd by pmd.
the class CouplingBetweenObjectsRule method visit.
@Override
public Object visit(ASTResultType node, Object data) {
for (int x = 0; x < node.jjtGetNumChildren(); x++) {
Node tNode = node.jjtGetChild(x);
if (tNode instanceof ASTType) {
Node reftypeNode = tNode.jjtGetChild(0);
if (reftypeNode instanceof ASTReferenceType) {
Node classOrIntType = reftypeNode.jjtGetChild(0);
if (classOrIntType instanceof ASTClassOrInterfaceType) {
Node nameNode = classOrIntType;
this.checkVariableType(nameNode, nameNode.getImage());
}
}
}
}
return super.visit(node, data);
}
Aggregations