Search in sources :

Example 1 with ASTArguments

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);
}
Also used : RuleContext(net.sourceforge.pmd.RuleContext) Node(net.sourceforge.pmd.lang.ast.Node) BigInteger(java.math.BigInteger) ASTArguments(net.sourceforge.pmd.lang.java.ast.ASTArguments) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)

Example 2 with ASTArguments

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;
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) QualifiableNode(net.sourceforge.pmd.lang.ast.QualifiableNode) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) Node(net.sourceforge.pmd.lang.ast.Node) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) ASTAllocationExpression(net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression) ASTArguments(net.sourceforge.pmd.lang.java.ast.ASTArguments)

Example 3 with ASTArguments

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()));
            }
        }
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) ASTMethodReference(net.sourceforge.pmd.lang.java.ast.ASTMethodReference) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTArguments(net.sourceforge.pmd.lang.java.ast.ASTArguments) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix) ASTMemberSelector(net.sourceforge.pmd.lang.java.ast.ASTMemberSelector)

Example 4 with ASTArguments

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;
}
Also used : ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) ASTArguments(net.sourceforge.pmd.lang.java.ast.ASTArguments) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) ClassScope(net.sourceforge.pmd.lang.java.symboltable.ClassScope)

Example 5 with ASTArguments

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);
}
Also used : JavaTypeDefinition(net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) ASTArguments(net.sourceforge.pmd.lang.java.ast.ASTArguments) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList)

Aggregations

ASTArguments (net.sourceforge.pmd.lang.java.ast.ASTArguments)7 Node (net.sourceforge.pmd.lang.ast.Node)3 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)3 AbstractJavaTypeNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)3 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)2 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)2 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)2 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)2 JavaNode (net.sourceforge.pmd.lang.java.ast.JavaNode)2 TypeNode (net.sourceforge.pmd.lang.java.ast.TypeNode)2 JavaTypeDefinition (net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition)2 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 StringTokenizer (java.util.StringTokenizer)1 RuleContext (net.sourceforge.pmd.RuleContext)1 QualifiableNode (net.sourceforge.pmd.lang.ast.QualifiableNode)1 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)1 ASTAnnotation (net.sourceforge.pmd.lang.java.ast.ASTAnnotation)1 ASTBlock (net.sourceforge.pmd.lang.java.ast.ASTBlock)1