Search in sources :

Example 16 with ASTLiteral

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

the class BooleanInstantiationRule method visit.

@Override
public Object visit(ASTPrimaryPrefix node, Object data) {
    if (!customBoolean) {
        if (node.jjtGetNumChildren() == 0 || !(node.jjtGetChild(0) instanceof ASTName)) {
            return super.visit(node, data);
        }
        if ("Boolean.valueOf".equals(((ASTName) node.jjtGetChild(0)).getImage()) || "java.lang.Boolean.valueOf".equals(((ASTName) node.jjtGetChild(0)).getImage())) {
            ASTPrimaryExpression parent = (ASTPrimaryExpression) node.jjtGetParent();
            ASTPrimarySuffix suffix = parent.getFirstDescendantOfType(ASTPrimarySuffix.class);
            if (suffix == null) {
                return super.visit(node, data);
            }
            ASTPrimaryPrefix prefix = suffix.getFirstDescendantOfType(ASTPrimaryPrefix.class);
            if (prefix == null) {
                return super.visit(node, data);
            }
            if (prefix.hasDescendantOfType(ASTBooleanLiteral.class)) {
                super.addViolation(data, node);
                return data;
            }
            ASTLiteral literal = prefix.getFirstDescendantOfType(ASTLiteral.class);
            if (literal != null && ("\"true\"".equals(literal.getImage()) || "\"false\"".equals(literal.getImage()))) {
                super.addViolation(data, node);
                return data;
            }
        }
    }
    return super.visit(node, data);
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)

Example 17 with ASTLiteral

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

the class ConsecutiveLiteralAppendsRule method checkInitializerExpressions.

/**
 * Determine if during the variable initializer calls to ".append" are done.
 *
 * @param node
 * @return
 */
private int checkInitializerExpressions(ASTVariableDeclaratorId node) {
    ASTVariableInitializer initializer = node.jjtGetParent().getFirstChildOfType(ASTVariableInitializer.class);
    ASTPrimaryExpression primary = initializer.getFirstDescendantOfType(ASTPrimaryExpression.class);
    int result = 0;
    boolean previousWasAppend = false;
    for (int i = 0; i < primary.jjtGetNumChildren(); i++) {
        Node child = primary.jjtGetChild(i);
        if (child.jjtGetNumChildren() > 0 && child.jjtGetChild(0) instanceof ASTAllocationExpression) {
            // skip the constructor call, that has already been checked
            continue;
        }
        if (child instanceof ASTPrimarySuffix) {
            ASTPrimarySuffix suffix = (ASTPrimarySuffix) child;
            if (suffix.jjtGetNumChildren() == 0 && suffix.hasImageEqualTo("append")) {
                previousWasAppend = true;
            } else if (suffix.jjtGetNumChildren() > 0 && previousWasAppend) {
                previousWasAppend = false;
                ASTLiteral literal = suffix.getFirstDescendantOfType(ASTLiteral.class);
                if (literal != null && literal.isStringLiteral()) {
                    result++;
                } else {
                    // checking the remainder of the initializer
                    break;
                }
            }
        }
    }
    return result;
}
Also used : ASTVariableInitializer(net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTAllocationExpression(net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)

Example 18 with ASTLiteral

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

the class ClassTypeResolverTest method testLiterals.

@Test
public void testLiterals() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(Literals.class);
    List<ASTLiteral> literals = convertList(acu.findChildNodesWithXPath("//Literal"), ASTLiteral.class);
    int index = 0;
    // String s = "s";
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(String.class, literals.get(index++).getType());
    // boolean boolean1 = false;
    assertEquals(Boolean.TYPE, literals.get(index).getFirstDescendantOfType(ASTBooleanLiteral.class).getType());
    assertEquals(Boolean.TYPE, literals.get(index++).getType());
    // boolean boolean2 = true;
    assertEquals(Boolean.TYPE, literals.get(index).getFirstDescendantOfType(ASTBooleanLiteral.class).getType());
    assertEquals(Boolean.TYPE, literals.get(index++).getType());
    // Object obj = null;
    assertNull(literals.get(index).getFirstDescendantOfType(ASTNullLiteral.class).getType());
    assertNull(literals.get(index++).getType());
    // byte byte1 = 0;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // byte byte2 = 0x0F;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // byte byte3 = -007;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // short short1 = 0;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // short short2 = 0x0F;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // short short3 = -007;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // char char1 = 0;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // char char2 = 0x0F;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // char char3 = 007;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // char char4 = 'a';
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Character.TYPE, literals.get(index++).getType());
    // int int1 = 0;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // int int2 = 0x0F;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // int int3 = -007;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // int int4 = 'a';
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Character.TYPE, literals.get(index++).getType());
    // long long1 = 0;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // long long2 = 0x0F;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // long long3 = -007;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // long long4 = 0L;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Long.TYPE, literals.get(index++).getType());
    // long long5 = 0x0Fl;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Long.TYPE, literals.get(index++).getType());
    // long long6 = -007L;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Long.TYPE, literals.get(index++).getType());
    // long long7 = 'a';
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Character.TYPE, literals.get(index++).getType());
    // float float1 = 0.0f;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Float.TYPE, literals.get(index++).getType());
    // float float2 = -10e+01f;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Float.TYPE, literals.get(index++).getType());
    // float float3 = 0x08.08p3f;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Float.TYPE, literals.get(index++).getType());
    // float float4 = 0xFF;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // float float5 = 'a';
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Character.TYPE, literals.get(index++).getType());
    // double double1 = 0.0;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Double.TYPE, literals.get(index++).getType());
    // double double2 = -10e+01;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Double.TYPE, literals.get(index++).getType());
    // double double3 = 0x08.08p3;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Double.TYPE, literals.get(index++).getType());
    // double double4 = 0xFF;
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Integer.TYPE, literals.get(index++).getType());
    // double double5 = 'a';
    assertEquals(0, literals.get(index).jjtGetNumChildren());
    assertEquals(Character.TYPE, literals.get(index++).getType());
    // Make sure we got them all.
    assertEquals("All literals not tested", index, literals.size());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) Test(org.junit.Test)

Aggregations

ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)18 Node (net.sourceforge.pmd.lang.ast.Node)13 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)7 ASTAdditiveExpression (net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression)4 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)4 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)4 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)4 ArrayList (java.util.ArrayList)3 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)3 ASTBooleanLiteral (net.sourceforge.pmd.lang.java.ast.ASTBooleanLiteral)3 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)3 TypeNode (net.sourceforge.pmd.lang.java.ast.TypeNode)3 List (java.util.List)2 ASTCastExpression (net.sourceforge.pmd.lang.java.ast.ASTCastExpression)2 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)2 ASTFieldDeclaration (net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration)2 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)2 AccessNode (net.sourceforge.pmd.lang.java.ast.AccessNode)2 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)2 BigInteger (java.math.BigInteger)1