@Test public void testGetASTString() { final JavaASTExtractor ex = new JavaASTExtractor(false); assertTrue(classContent.length() > 0); final ASTNode classCU = ex.getASTNode(classContent, ParseType.COMPILATION_UNIT); assertTrue(snippetMatchesAstTokens(classContent, classCU)); assertTrue(methodContent.length() > 0); final ASTNode methodCU = ex.getASTNode(methodContent, ParseType.METHOD); assertTrue(snippetMatchesAstTokens(methodContent, methodCU)); }
public final ASTNode getASTNode(final char[] content, final ParseType parseType) { final ASTParser parser = ASTParser.newParser(AST.JLS8); final int astKind; switch (parseType) { case CLASS_BODY: case METHOD: astKind = ASTParser.K_CLASS_BODY_DECLARATIONS; break; case COMPILATION_UNIT: astKind = ASTParser.K_COMPILATION_UNIT; break; case EXPRESSION: astKind = ASTParser.K_EXPRESSION; break; case STATEMENTS: astKind = ASTParser.K_STATEMENTS; break; default: astKind = ASTParser.K_COMPILATION_UNIT; } parser.setKind(astKind); final Map<String, String> options = new Hashtable<String, String>(); options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8); options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8); if (useJavadocs) { options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED); } parser.setCompilerOptions(options); parser.setSource(content); parser.setResolveBindings(useBindings); parser.setBindingsRecovery(useBindings); parser.setStatementsRecovery(true); if (parseType != ParseType.METHOD) { return parser.createAST(null); } else { final ASTNode cu = parser.createAST(null); return getFirstMethodDeclaration(cu); } }
JavaASTExtractor { public final ASTNode getASTNode(final char[] content, final ParseType parseType) { final ASTParser parser = ASTParser.newParser(AST.JLS8); final int astKind; switch (parseType) { case CLASS_BODY: case METHOD: astKind = ASTParser.K_CLASS_BODY_DECLARATIONS; break; case COMPILATION_UNIT: astKind = ASTParser.K_COMPILATION_UNIT; break; case EXPRESSION: astKind = ASTParser.K_EXPRESSION; break; case STATEMENTS: astKind = ASTParser.K_STATEMENTS; break; default: astKind = ASTParser.K_COMPILATION_UNIT; } parser.setKind(astKind); final Map<String, String> options = new Hashtable<String, String>(); options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8); options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8); if (useJavadocs) { options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED); } parser.setCompilerOptions(options); parser.setSource(content); parser.setResolveBindings(useBindings); parser.setBindingsRecovery(useBindings); parser.setStatementsRecovery(true); if (parseType != ParseType.METHOD) { return parser.createAST(null); } else { final ASTNode cu = parser.createAST(null); return getFirstMethodDeclaration(cu); } } }
JavaASTExtractor { public final ASTNode getASTNode(final char[] content, final ParseType parseType) { final ASTParser parser = ASTParser.newParser(AST.JLS8); final int astKind; switch (parseType) { case CLASS_BODY: case METHOD: astKind = ASTParser.K_CLASS_BODY_DECLARATIONS; break; case COMPILATION_UNIT: astKind = ASTParser.K_COMPILATION_UNIT; break; case EXPRESSION: astKind = ASTParser.K_EXPRESSION; break; case STATEMENTS: astKind = ASTParser.K_STATEMENTS; break; default: astKind = ASTParser.K_COMPILATION_UNIT; } parser.setKind(astKind); final Map<String, String> options = new Hashtable<String, String>(); options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8); options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8); if (useJavadocs) { options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED); } parser.setCompilerOptions(options); parser.setSource(content); parser.setResolveBindings(useBindings); parser.setBindingsRecovery(useBindings); parser.setStatementsRecovery(true); if (parseType != ParseType.METHOD) { return parser.createAST(null); } else { final ASTNode cu = parser.createAST(null); return getFirstMethodDeclaration(cu); } } JavaASTExtractor(final boolean useBindings);  JavaASTExtractor(final boolean useBindings, final boolean useJavadocs); }
JavaASTExtractor { public final ASTNode getASTNode(final char[] content, final ParseType parseType) { final ASTParser parser = ASTParser.newParser(AST.JLS8); final int astKind; switch (parseType) { case CLASS_BODY: case METHOD: astKind = ASTParser.K_CLASS_BODY_DECLARATIONS; break; case COMPILATION_UNIT: astKind = ASTParser.K_COMPILATION_UNIT; break; case EXPRESSION: astKind = ASTParser.K_EXPRESSION; break; case STATEMENTS: astKind = ASTParser.K_STATEMENTS; break; default: astKind = ASTParser.K_COMPILATION_UNIT; } parser.setKind(astKind); final Map<String, String> options = new Hashtable<String, String>(); options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8); options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8); if (useJavadocs) { options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED); } parser.setCompilerOptions(options); parser.setSource(content); parser.setResolveBindings(useBindings); parser.setBindingsRecovery(useBindings); parser.setStatementsRecovery(true); if (parseType != ParseType.METHOD) { return parser.createAST(null); } else { final ASTNode cu = parser.createAST(null); return getFirstMethodDeclaration(cu); } } JavaASTExtractor(final boolean useBindings);  JavaASTExtractor(final boolean useBindings, final boolean useJavadocs); final CompilationUnit getAST(final File file); final CompilationUnit getAST(final File file,
			final Set<String> srcPaths); final ASTNode getAST(final String fileContent,
			final ParseType parseType); final ASTNode getASTNode(final char[] content,
			final ParseType parseType); final ASTNode getASTNode(final String fileContent,
			final ParseType parseType); final ASTNode getBestEffortAstNode(final char[] content); final ASTNode getBestEffortAstNode(final String fileContent); }
JavaASTExtractor { public final ASTNode getASTNode(final char[] content, final ParseType parseType) { final ASTParser parser = ASTParser.newParser(AST.JLS8); final int astKind; switch (parseType) { case CLASS_BODY: case METHOD: astKind = ASTParser.K_CLASS_BODY_DECLARATIONS; break; case COMPILATION_UNIT: astKind = ASTParser.K_COMPILATION_UNIT; break; case EXPRESSION: astKind = ASTParser.K_EXPRESSION; break; case STATEMENTS: astKind = ASTParser.K_STATEMENTS; break; default: astKind = ASTParser.K_COMPILATION_UNIT; } parser.setKind(astKind); final Map<String, String> options = new Hashtable<String, String>(); options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8); options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8); if (useJavadocs) { options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED); } parser.setCompilerOptions(options); parser.setSource(content); parser.setResolveBindings(useBindings); parser.setBindingsRecovery(useBindings); parser.setStatementsRecovery(true); if (parseType != ParseType.METHOD) { return parser.createAST(null); } else { final ASTNode cu = parser.createAST(null); return getFirstMethodDeclaration(cu); } } JavaASTExtractor(final boolean useBindings);  JavaASTExtractor(final boolean useBindings, final boolean useJavadocs); final CompilationUnit getAST(final File file); final CompilationUnit getAST(final File file,
			final Set<String> srcPaths); final ASTNode getAST(final String fileContent,
			final ParseType parseType); final ASTNode getASTNode(final char[] content,
			final ParseType parseType); final ASTNode getASTNode(final String fileContent,
			final ParseType parseType); final ASTNode getBestEffortAstNode(final char[] content); final ASTNode getBestEffortAstNode(final String fileContent);  }