Search in sources :

Example 1 with ASTName

use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.

the class LocalScopeTest method testNameWithThisOrSuperIsNotFlaggedAsUnused.

@Test
public void testNameWithThisOrSuperIsNotFlaggedAsUnused() {
    LocalScope scope = new LocalScope();
    ASTName name = new ASTName(1);
    name.setImage("foo");
    ASTPrimaryPrefix prefix = new ASTPrimaryPrefix(2);
    prefix.setUsesThisModifier();
    name.jjtAddChild(prefix, 1);
    JavaNameOccurrence occ = new JavaNameOccurrence(name, "foo");
    scope.addNameOccurrence(occ);
    assertFalse(scope.getDeclarations().keySet().iterator().hasNext());
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Test(org.junit.Test)

Example 2 with ASTName

use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.

the class InefficientStringBufferingRule method isInStringBufferOperation.

protected static boolean isInStringBufferOperation(Node node, int length, String methodName) {
    if (!(node.getNthParent(length) instanceof ASTStatementExpression)) {
        return false;
    }
    ASTStatementExpression s = node.getFirstParentOfType(ASTStatementExpression.class);
    if (s == null) {
        return false;
    }
    ASTName n = s.getFirstDescendantOfType(ASTName.class);
    if (n == null || n.getImage().indexOf(methodName) == -1 || !(n.getNameDeclaration() instanceof TypedNameDeclaration)) {
        return false;
    }
    // TODO having to hand-code this kind of dredging around is ridiculous
    // we need something to support this in the framework
    // but, "for now" (tm):
    // if more than one arg to append(), skip it
    ASTArgumentList argList = s.getFirstDescendantOfType(ASTArgumentList.class);
    if (argList == null || argList.jjtGetNumChildren() > 1) {
        return false;
    }
    return TypeHelper.isEither((TypedNameDeclaration) n.getNameDeclaration(), StringBuffer.class, StringBuilder.class);
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) TypedNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.TypedNameDeclaration) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList)

Example 3 with ASTName

use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.

the class StringInstantiationRule method visit.

@Override
public Object visit(ASTAllocationExpression node, Object data) {
    if (!(node.jjtGetChild(0) instanceof ASTClassOrInterfaceType)) {
        return data;
    }
    if (!TypeHelper.isA((ASTClassOrInterfaceType) node.jjtGetChild(0), String.class)) {
        return data;
    }
    List<ASTExpression> exp = node.findDescendantsOfType(ASTExpression.class);
    if (exp.size() >= 2) {
        return data;
    }
    if (node.hasDescendantOfAnyType(ASTArrayDimsAndInits.class, ASTAdditiveExpression.class)) {
        return data;
    }
    ASTName name = node.getFirstDescendantOfType(ASTName.class);
    // Literal, i.e., new String("foo")
    if (name == null) {
        addViolation(data, node);
        return data;
    }
    NameDeclaration nd = name.getNameDeclaration();
    if (nd == null) {
        return data;
    }
    if (nd instanceof TypedNameDeclaration && TypeHelper.isA((TypedNameDeclaration) nd, String.class)) {
        addViolation(data, node);
    }
    return data;
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) TypedNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.TypedNameDeclaration) TypedNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.TypedNameDeclaration) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 4 with ASTName

use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.

the class UnnecessaryWrapperObjectCreationRule method visit.

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 (image.startsWith("java.lang.")) {
        image = image.substring(10);
    }
    boolean checkBoolean = ((RuleContext) data).getLanguageVersion().compareTo(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.5")) >= 0;
    if (PREFIX_SET.contains(image) || checkBoolean && "Boolean.valueOf".equals(image)) {
        ASTPrimaryExpression parent = (ASTPrimaryExpression) node.jjtGetParent();
        if (parent.jjtGetNumChildren() >= 3) {
            Node n = parent.jjtGetChild(2);
            if (n instanceof ASTPrimarySuffix) {
                ASTPrimarySuffix suffix = (ASTPrimarySuffix) n;
                image = suffix.getImage();
                if (SUFFIX_SET.contains(image) || checkBoolean && "booleanValue".equals(image)) {
                    super.addViolation(data, node);
                    return data;
                }
            }
        }
    }
    return super.visit(node, data);
}
Also used : RuleContext(net.sourceforge.pmd.RuleContext) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)

Example 5 with ASTName

use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.

the class CompareObjectsWithEqualsRule method visit.

public Object visit(ASTEqualityExpression node, Object data) {
    Node c0 = node.jjtGetChild(0).jjtGetChild(0);
    Node c1 = node.jjtGetChild(1).jjtGetChild(0);
    // equals expression is correct
    if (isAllocation(c0) || isAllocation(c1)) {
        addViolation(data, node);
        return data;
    }
    // skip if either child is not a simple name
    if (!hasName(c0) || !hasName(c1)) {
        return data;
    }
    // skip if either is a qualified name
    if (isQualifiedName(c0.jjtGetChild(0)) || isQualifiedName(c1.jjtGetChild(0))) {
        return data;
    }
    // skip if either is part of a qualified name
    if (isPartOfQualifiedName(node.jjtGetChild(0)) || isPartOfQualifiedName(node.jjtGetChild(1))) {
        return data;
    }
    // skip static initializers... missing some cases here
    if (!node.getParentsOfType(ASTInitializer.class).isEmpty()) {
        return data;
    }
    ASTName n0 = (ASTName) c0.jjtGetChild(0);
    ASTName n1 = (ASTName) c1.jjtGetChild(0);
    if (n0.getNameDeclaration() instanceof VariableNameDeclaration && n1.getNameDeclaration() instanceof VariableNameDeclaration) {
        VariableNameDeclaration nd0 = (VariableNameDeclaration) n0.getNameDeclaration();
        VariableNameDeclaration nd1 = (VariableNameDeclaration) n1.getNameDeclaration();
        // FIXME catch comparisons btwn array elements of reference types
        if (nd0.isArray() || nd1.isArray()) {
            return data;
        }
        if (nd0.isReferenceType() && nd1.isReferenceType()) {
            ASTReferenceType type0 = ((Node) nd0.getAccessNodeParent()).getFirstDescendantOfType(ASTReferenceType.class);
            ASTReferenceType type1 = ((Node) nd1.getAccessNodeParent()).getFirstDescendantOfType(ASTReferenceType.class);
            // skip, if it is an enum
            if (type0.getType() != null && type0.getType().equals(type1.getType()) && // It may be a custom enum class or an explicit Enum class usage
            (type0.getType().isEnum() || type0.getType() == java.lang.Enum.class)) {
                return data;
            }
            addViolation(data, node);
        }
    }
    return data;
}
Also used : VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTReferenceType(net.sourceforge.pmd.lang.java.ast.ASTReferenceType)

Aggregations

ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)53 Node (net.sourceforge.pmd.lang.ast.Node)25 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)17 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)14 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)11 ArrayList (java.util.ArrayList)10 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)10 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)8 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)8 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)7 List (java.util.List)6 ASTAnnotation (net.sourceforge.pmd.lang.java.ast.ASTAnnotation)6 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)6 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)6 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)5 Map (java.util.Map)4 ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)4 ASTClassOrInterfaceBodyDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration)4 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)4 ASTAdditiveExpression (net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression)3