ApexParser { public ApexNode<Compilation> parse(final Reader reader) { try { final String sourceCode = IOUtils.toString(reader); final Compilation astRoot = parseApex(sourceCode); final ApexTreeBuilder treeBuilder = new ApexTreeBuilder(sourceCode, parserOptions); suppressMap = treeBuilder.getSuppressMap(); if (astRoot == null) { throw new ParseException("Couldn't parse the source - there is not root node - Syntax Error??"); } return treeBuilder.build(astRoot); } catch (IOException | apex.jorje.services.exception.ParseException e) { throw new ParseException(e); } } ApexParser(ApexParserOptions parserOptions); Compilation parseApex(final String sourceCode); ApexNode<Compilation> parse(final Reader reader); Map<Integer, String> getSuppressMap();  }
@Test public void understandsSimpleFile() { String code = "@isTest\n public class SimpleClass {\n" + " @isTest\n public static void testAnything() {\n" + " \n" + " }\n" + "}"; ApexNode<Compilation> rootNode = parse(code); List<ASTMethod> methods = rootNode.findDescendantsOfType(ASTMethod.class); assertEquals(4, methods.size()); }
@Test public void verifyLineColumnNumbers() { ApexNode<Compilation> rootNode = parse(testCodeForLineNumbers); assertLineNumbersForTestCode(rootNode); }
@Test public void verifyLineColumnNumbersWithWindowsLineEndings() { String windowsLineEndings = testCodeForLineNumbers.replaceAll(" \n", "\r\n"); ApexNode<Compilation> rootNode = parse(windowsLineEndings); assertLineNumbersForTestCode(rootNode); }
@Test public void verifyEndLine() { String code = "public class SimpleClass {\n" + " public void method1() {\n" + " }\n" + " public void method2() {\n" + " }\n" + "}\n"; ApexNode<Compilation> rootNode = parse(code); Node method1 = rootNode.getChild(1); assertEquals("Wrong begin line", 2, method1.getBeginLine()); assertEquals("Wrong end line", 3, method1.getEndLine()); Node method2 = rootNode.getChild(2); assertEquals("Wrong begin line", 4, method2.getBeginLine()); assertEquals("Wrong end line", 5, method2.getEndLine()); }
@Test public void checkComments() { String code = "public class SimpleClass {\n" + " " + " public void method1() {\n" + " }\n" + " public void method2() {\n" + " }\n" + "}\n"; ApexNode<Compilation> root = parse(code); assertThat(root, instanceOf(ASTUserClass.class)); ApexNode<?> comment = root.getChild(0); assertThat(comment, instanceOf(ASTFormalComment.class)); assertPosition(comment, 1, 9, 1, 31); assertEquals("", ((ASTFormalComment) comment).getToken()); ApexNode<?> m1 = root.getChild(2); assertThat(m1, instanceOf(ASTMethod.class)); ApexNode<?> comment2 = m1.getChild(0); assertThat(comment2, instanceOf(ASTFormalComment.class)); assertEquals("", ((ASTFormalComment) comment2).getToken()); }
@Test public void parsesRealWorldClasses() throws Exception { File directory = new File("src/test/resources"); File[] fList = directory.listFiles(); for (File file : fList) { if (file.isFile() && file.getName().endsWith(".cls")) { String sourceCode = FileUtils.readFileToString(file, StandardCharsets.UTF_8); ApexNode<Compilation> rootNode = parse(sourceCode); Assert.assertNotNull(rootNode); } } }
@Test public void parseInheritedSharingClass() throws IOException { String source = IOUtils.toString(ApexParserTest.class.getResourceAsStream("InheritedSharing.cls"), StandardCharsets.UTF_8); ApexNode<Compilation> rootNode = parse(source); Assert.assertNotNull(rootNode); }
@Test public void stackOverflowDuringClassParsing() throws Exception { String source = IOUtils.toString(ApexParserTest.class.getResourceAsStream("StackOverflowClass.cls"), StandardCharsets.UTF_8); ApexNode<Compilation> rootNode = parse(source); Assert.assertNotNull(rootNode); int count = visitPosition(rootNode, 0); Assert.assertEquals(487, count); }