use of net.sourceforge.pmd.lang.java.ast.ASTArguments in project pmd by pmd.
the class BigIntegerInstantiationRule method visit.
@Override
public Object visit(ASTAllocationExpression node, Object data) {
Node type = node.jjtGetChild(0);
if (!(type instanceof ASTClassOrInterfaceType)) {
return super.visit(node, data);
}
boolean jdk15 = ((RuleContext) data).getLanguageVersion().compareTo(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.5")) >= 0;
if ((TypeHelper.isA((ASTClassOrInterfaceType) type, BigInteger.class) || jdk15 && TypeHelper.isA((ASTClassOrInterfaceType) type, BigDecimal.class)) && !node.hasDescendantOfType(ASTArrayDimsAndInits.class)) {
ASTArguments args = node.getFirstChildOfType(ASTArguments.class);
if (args.getArgumentCount() == 1) {
ASTLiteral literal = node.getFirstDescendantOfType(ASTLiteral.class);
if (literal == null || literal.jjtGetParent().jjtGetParent().jjtGetParent().jjtGetParent().jjtGetParent() != args) {
return super.visit(node, data);
}
String img = literal.getImage();
if (literal.isStringLiteral()) {
img = img.substring(1, img.length() - 1);
}
if ("0".equals(img) || "1".equals(img) || jdk15 && "10".equals(img)) {
addViolation(data, node);
return data;
}
}
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTArguments in project pmd by pmd.
the class ClassTypeResolver method getEnclosingTypeDeclaration.
/**
* Returns the the first Class declaration around the node.
*
* @param node The node with the enclosing Class declaration.
* @return The JavaTypeDefinition of the enclosing Class declaration.
*/
private TypeNode getEnclosingTypeDeclaration(Node node) {
Node previousNode = null;
while (node != null) {
if (node instanceof ASTClassOrInterfaceDeclaration) {
return (TypeNode) node;
// anonymous class declaration
} else if (// is anonymous class declaration
node instanceof ASTAllocationExpression && // array cant be anonymous
node.getFirstChildOfType(ASTArrayDimsAndInits.class) == null && !(previousNode instanceof ASTArguments)) {
// we might come out of the constructor
return (TypeNode) node;
}
previousNode = node;
node = node.jjtGetParent();
}
return null;
}
use of net.sourceforge.pmd.lang.java.ast.ASTArguments in project pmd by pmd.
the class NameFinder method checkForNameChild.
private void checkForNameChild(JavaNode node) {
if (node.getImage() != null) {
add(new JavaNameOccurrence(node, node.getImage()));
}
if (node.jjtGetNumChildren() > 0 && node.jjtGetChild(0) instanceof ASTName) {
ASTName grandchild = (ASTName) node.jjtGetChild(0);
for (StringTokenizer st = new StringTokenizer(grandchild.getImage(), "."); st.hasMoreTokens(); ) {
add(new JavaNameOccurrence(grandchild, st.nextToken()));
}
}
if (node.jjtGetNumChildren() > 1 && node.jjtGetChild(1) instanceof ASTMethodReference) {
ASTMethodReference methodRef = (ASTMethodReference) node.jjtGetChild(1);
add(new JavaNameOccurrence(methodRef, methodRef.getImage()));
}
if (node instanceof ASTPrimarySuffix) {
ASTPrimarySuffix suffix = (ASTPrimarySuffix) node;
if (suffix.isArguments()) {
JavaNameOccurrence occurrence = names.get(names.size() - 1);
occurrence.setIsMethodOrConstructorInvocation();
ASTArguments args = (ASTArguments) ((ASTPrimarySuffix) node).jjtGetChild(0);
occurrence.setArgumentCount(args.getArgumentCount());
} else if (suffix.jjtGetNumChildren() == 1 && suffix.jjtGetChild(0) instanceof ASTMemberSelector) {
ASTMemberSelector member = (ASTMemberSelector) suffix.jjtGetChild(0);
if (member.jjtGetNumChildren() == 1 && member.jjtGetChild(0) instanceof ASTMethodReference) {
ASTMethodReference methodRef = (ASTMethodReference) member.jjtGetChild(0);
add(new JavaNameOccurrence(methodRef, methodRef.getImage()));
} else {
add(new JavaNameOccurrence(member, member.getImage()));
}
}
}
}
use of net.sourceforge.pmd.lang.java.ast.ASTArguments in project pmd by pmd.
the class AccessorClassGenerationRule method visit.
@Override
public Object visit(final ASTAllocationExpression node, final Object data) {
if (node.jjtGetChild(0) instanceof ASTClassOrInterfaceType) {
// Ignore primitives
final ASTClassOrInterfaceType type = (ASTClassOrInterfaceType) node.jjtGetChild(0);
final List<ASTConstructorDeclaration> constructors = privateConstructors.get(type.getImage());
if (constructors != null) {
final ASTArguments callArguments = node.getFirstChildOfType(ASTArguments.class);
// Is this really a constructor call and not an array?
if (callArguments != null) {
final ClassScope enclosingScope = node.getScope().getEnclosingScope(ClassScope.class);
for (final ASTConstructorDeclaration cd : constructors) {
// Are we within the same class scope?
if (cd.getScope().getEnclosingScope(ClassScope.class) == enclosingScope) {
break;
}
if (cd.getParameterCount() == callArguments.getArgumentCount()) {
// TODO : Check types
addViolation(data, node);
break;
}
}
}
}
}
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTArguments in project pmd by pmd.
the class ClassTypeResolver method visit.
@Override
public Object visit(ASTName node, Object data) {
Class<?> accessingClass = getEnclosingTypeDeclarationClass(node);
String[] dotSplitImage = node.getImage().split("\\.");
int startIndex = searchNodeNameForClass(node);
ASTArguments astArguments = getSuffixMethodArgs(node);
ASTArgumentList astArgumentList = getArgumentList(astArguments);
int methodArgsArity = getArgumentListArity(astArgumentList);
JavaTypeDefinition previousType;
if (node.getType() != null) {
// static field or method
// node.getType() has been set by the call to searchNodeNameForClass above
// node.getType() will have the value equal to the Class found by that method
previousType = node.getTypeDefinition();
} else {
// non-static field or method
if (dotSplitImage.length == 1 && astArguments != null) {
// method
List<MethodType> methods = getLocalApplicableMethods(node, dotSplitImage[0], Collections.<JavaTypeDefinition>emptyList(), methodArgsArity, accessingClass);
TypeNode enclosingType = getEnclosingTypeDeclaration(node);
if (enclosingType == null) {
// we can't proceed, probably uncompiled sources
return data;
}
previousType = getBestMethodReturnType(enclosingType.getTypeDefinition(), methods, astArgumentList);
} else {
// field
previousType = getTypeDefinitionOfVariableFromScope(node.getScope(), dotSplitImage[0], accessingClass);
}
// first element's type in dotSplitImage has already been resolved
startIndex = 1;
}
// as the code is not compiled there and symbol table works on uncompiled code
if (node.getNameDeclaration() != null && // if it's not null, then let other code handle things
previousType == null && node.getNameDeclaration().getNode() instanceof TypeNode) {
// Carry over the type from the declaration
Class<?> nodeType = ((TypeNode) node.getNameDeclaration().getNode()).getType();
// FIXME : generic classes and class with generic super types could have the wrong type assigned here
if (nodeType != null) {
node.setType(nodeType);
return super.visit(node, data);
}
}
for (int i = startIndex; i < dotSplitImage.length; ++i) {
if (previousType == null) {
break;
}
if (i == dotSplitImage.length - 1 && astArguments != null) {
// method
List<MethodType> methods = getApplicableMethods(previousType, dotSplitImage[i], Collections.<JavaTypeDefinition>emptyList(), methodArgsArity, accessingClass);
previousType = getBestMethodReturnType(previousType, methods, astArgumentList);
} else {
// field
previousType = getFieldType(previousType, dotSplitImage[i], accessingClass);
}
}
if (previousType != null) {
node.setTypeDefinition(previousType);
}
return super.visit(node, data);
}
Aggregations