Search in sources :

Example 21 with ASTExpression

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

the class ClassTypeResolverTest method testUnaryNumericPromotion.

@Test
public void testUnaryNumericPromotion() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(Promotion.class);
    List<ASTExpression> expressions = convertList(acu.findChildNodesWithXPath("//Block[preceding-sibling::MethodDeclarator[@Image = " + "'unaryNumericPromotion']]//Expression[UnaryExpression]"), ASTExpression.class);
    int index = 0;
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Long.TYPE, expressions.get(index++).getType());
    assertEquals(Float.TYPE, expressions.get(index++).getType());
    assertEquals(Double.TYPE, expressions.get(index++).getType());
    // Make sure we got them all.
    assertEquals("All expressions not tested", index, expressions.size());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression) Test(org.junit.Test)

Example 22 with ASTExpression

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

the class ClassTypeResolverTest method testBinaryNumericOperators.

@Test
public void testBinaryNumericOperators() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(Operators.class);
    List<ASTExpression> expressions = convertList(acu.findChildNodesWithXPath("//Block[preceding-sibling::MethodDeclarator[@Image = 'binaryNumericOperators']]//Expression"), ASTExpression.class);
    int index = 0;
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    // Make sure we got them all.
    assertEquals("All expressions not tested", index, expressions.size());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression) Test(org.junit.Test)

Example 23 with ASTExpression

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

the class ClassTypeResolverTest method testBinaryNumericPromotion.

@Test
public void testBinaryNumericPromotion() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(Promotion.class);
    List<ASTExpression> expressions = convertList(acu.findChildNodesWithXPath("//Block[preceding-sibling::MethodDeclarator[@Image = " + "'binaryNumericPromotion']]//Expression[AdditiveExpression]"), ASTExpression.class);
    int index = 0;
    // LHS = byte
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Long.TYPE, expressions.get(index++).getType());
    assertEquals(Float.TYPE, expressions.get(index++).getType());
    assertEquals(Double.TYPE, expressions.get(index++).getType());
    // LHS = short
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Long.TYPE, expressions.get(index++).getType());
    assertEquals(Float.TYPE, expressions.get(index++).getType());
    assertEquals(Double.TYPE, expressions.get(index++).getType());
    // LHS = char
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Long.TYPE, expressions.get(index++).getType());
    assertEquals(Float.TYPE, expressions.get(index++).getType());
    assertEquals(Double.TYPE, expressions.get(index++).getType());
    // LHS = int
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Long.TYPE, expressions.get(index++).getType());
    assertEquals(Float.TYPE, expressions.get(index++).getType());
    assertEquals(Double.TYPE, expressions.get(index++).getType());
    // LHS = long
    assertEquals(Long.TYPE, expressions.get(index++).getType());
    assertEquals(Long.TYPE, expressions.get(index++).getType());
    assertEquals(Long.TYPE, expressions.get(index++).getType());
    assertEquals(Long.TYPE, expressions.get(index++).getType());
    assertEquals(Long.TYPE, expressions.get(index++).getType());
    assertEquals(Float.TYPE, expressions.get(index++).getType());
    assertEquals(Double.TYPE, expressions.get(index++).getType());
    // LHS = float
    assertEquals(Float.TYPE, expressions.get(index++).getType());
    assertEquals(Float.TYPE, expressions.get(index++).getType());
    assertEquals(Float.TYPE, expressions.get(index++).getType());
    assertEquals(Float.TYPE, expressions.get(index++).getType());
    assertEquals(Float.TYPE, expressions.get(index++).getType());
    assertEquals(Float.TYPE, expressions.get(index++).getType());
    assertEquals(Double.TYPE, expressions.get(index++).getType());
    // LHS = double
    assertEquals(Double.TYPE, expressions.get(index++).getType());
    assertEquals(Double.TYPE, expressions.get(index++).getType());
    assertEquals(Double.TYPE, expressions.get(index++).getType());
    assertEquals(Double.TYPE, expressions.get(index++).getType());
    assertEquals(Double.TYPE, expressions.get(index++).getType());
    assertEquals(Double.TYPE, expressions.get(index++).getType());
    assertEquals(Double.TYPE, expressions.get(index++).getType());
    // Make sure we got them all.
    assertEquals("All expressions not tested", index, expressions.size());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression) Test(org.junit.Test)

Example 24 with ASTExpression

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

the class ClassTypeResolverTest method testBinaryLogicalOperators.

@Test
public void testBinaryLogicalOperators() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(Operators.class);
    List<ASTExpression> expressions = convertList(acu.findChildNodesWithXPath("//Block[preceding-sibling::MethodDeclarator[@Image = 'binaryLogicalOperators']]//Expression"), ASTExpression.class);
    int index = 0;
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    // Make sure we got them all.
    assertEquals("All expressions not tested", index, expressions.size());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression) Test(org.junit.Test)

Example 25 with ASTExpression

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

the class StatementAndBraceFinderTest method testIfStmtHasCorrectTypes.

@Test
public void testIfStmtHasCorrectTypes() {
    ASTExpression exp = getOrderedNodes(ASTExpression.class, TEST3).get(0);
    DataFlowNode dfn = exp.getDataFlowNode().getFlow().get(2);
    assertTrue(dfn.isType(NodeType.IF_EXPR));
    assertTrue(dfn.isType(NodeType.IF_LAST_STATEMENT_WITHOUT_ELSE));
}
Also used : DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression) Test(org.junit.Test)

Aggregations

ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)26 Test (org.junit.Test)9 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)8 Constraint (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint)8 Node (net.sourceforge.pmd.lang.ast.Node)7 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)6 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)6 DataFlowNode (net.sourceforge.pmd.lang.dfa.DataFlowNode)3 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)3 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)3 ArrayList (java.util.ArrayList)2 ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)2 ASTForInit (net.sourceforge.pmd.lang.java.ast.ASTForInit)2 ASTForUpdate (net.sourceforge.pmd.lang.java.ast.ASTForUpdate)2 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)2 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)2 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)2 ResolutionFailedException (net.sourceforge.pmd.lang.java.typeresolution.typeinference.TypeInferenceResolver.ResolutionFailedException)2 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)2 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)2