Skip to content

Commit

Permalink
[apex] Improve AST for try-catch-finally statements
Browse files Browse the repository at this point in the history
  • Loading branch information
adangel committed Jul 24, 2020
1 parent 6464b34 commit 8a6975f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ public String getVariableName() {
}
return null;
}

public ASTBlockStatement getBody() {
return (ASTBlockStatement) getChild(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package net.sourceforge.pmd.lang.apex.ast;

import java.util.List;

import net.sourceforge.pmd.annotation.InternalApi;

import apex.jorje.semantic.ast.statement.TryCatchFinallyBlockStatement;
Expand All @@ -20,4 +22,23 @@ public ASTTryCatchFinallyBlockStatement(TryCatchFinallyBlockStatement tryCatchFi
public Object jjtAccept(ApexParserVisitor visitor, Object data) {
return visitor.visit(this, data);
}

public ASTBlockStatement getTryBlock() {
return (ASTBlockStatement) getChild(0);
}

public List<ASTCatchBlockStatement> getCatchClauses() {
return findChildrenOfType(ASTCatchBlockStatement.class);
}

public ASTBlockStatement getFinallyBlock() {
ApexNode<?> lastChild = null;
if (getNumChildren() >= 2) {
lastChild = getChild(getNumChildren() - 1);
}
if (lastChild instanceof ASTBlockStatement) {
return (ASTBlockStatement) lastChild;
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/

package net.sourceforge.pmd.lang.apex.ast;

import org.junit.Assert;
import org.junit.Test;

import apex.jorje.semantic.ast.compilation.Compilation;

public class ASTTryCatchFinallyBlockStatementTest extends ApexParserTestBase {

@Test
public void testTryFinally() {
ApexNode<Compilation> node = parse("class Foo { void bar() { try { methodCall(); } finally { methodCall(); } } }");
ASTTryCatchFinallyBlockStatement statement = node.getFirstDescendantOfType(ASTTryCatchFinallyBlockStatement.class);
Assert.assertNotNull(statement.getTryBlock());
Assert.assertEquals(0, statement.getTryBlock().getIndexInParent());
Assert.assertNotNull(statement.getFinallyBlock());
Assert.assertEquals(1, statement.getFinallyBlock().getIndexInParent());
Assert.assertEquals(0, statement.getCatchClauses().size());
}

@Test
public void testTryCatch() {
ApexNode<Compilation> node = parse("class Foo { void bar() { try { methodCall(); } catch (Exception e) { methodCall(); } } }");
ASTTryCatchFinallyBlockStatement statement = node.getFirstDescendantOfType(ASTTryCatchFinallyBlockStatement.class);
Assert.assertNotNull(statement.getTryBlock());
Assert.assertEquals(0, statement.getTryBlock().getIndexInParent());
Assert.assertNull(statement.getFinallyBlock());
Assert.assertEquals(1, statement.getCatchClauses().size());
Assert.assertNotNull(statement.getCatchClauses().get(0).getBody());
Assert.assertEquals(1, statement.getCatchClauses().get(0).getIndexInParent());
}

@Test
public void testTryCatchFinally() {
ApexNode<Compilation> node = parse("class Foo { void bar() { try { methodCall(); } catch (Exception e) { methodCall(); } finally { } } }");
ASTTryCatchFinallyBlockStatement statement = node.getFirstDescendantOfType(ASTTryCatchFinallyBlockStatement.class);
Assert.assertNotNull(statement.getTryBlock());
Assert.assertEquals(0, statement.getTryBlock().getIndexInParent());
Assert.assertNotNull(statement.getFinallyBlock());
Assert.assertEquals(2, statement.getFinallyBlock().getIndexInParent());
Assert.assertEquals(1, statement.getCatchClauses().size());
Assert.assertNotNull(statement.getCatchClauses().get(0).getBody());
Assert.assertEquals(1, statement.getCatchClauses().get(0).getIndexInParent());
}
}

0 comments on commit 8a6975f

Please sign in to comment.