ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(document.get().toCharArray()); CompilationUnit unit = (CompilationUnit)parser.createAST(null); unit.recordModifications();
parser.setSource(sourceCodeFetcher.get(clazz).toCharArray()); parser.setKind(ASTParser.K_COMPILATION_UNIT); final CompilationUnit cu = (CompilationUnit) parser.createAST(null); cu.accept(new ASTVisitor() { MethodDeclaration currentMethod = null;
public static ASTNode newStatement(AST ast, String content) { StringBuffer buffer= new StringBuffer(STATEMENT_HEADER); buffer.append(content); buffer.append(STATEMENT_FOOTER); ASTParser p= ASTParser.newParser(ast.apiLevel()); p.setSource(buffer.toString().toCharArray()); CompilationUnit root= (CompilationUnit) p.createAST(null); ASTNode result= ASTNode.copySubtree(ast, NodeFinder.perform(root, STATEMENT_HEADER.length(), content.length())); result.accept(new PositionClearer()); return result; }
ASTParser parser = ASTParser.newParser(AST.JLS2); parser.setSource("".toCharArray()); CompilationUnit unit = (CompilationUnit) parser.createAST(null); unit.recordModifications(); AST ast = unit.getAST();
private static CompilationUnit createAST(IJavaElement element, String cuSource) { Assert.isNotNull(element); ASTParser parser = ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL); IJavaProject javaProject = element.getJavaProject(); parser.setProject(javaProject); Map<String, String> options = javaProject.getOptions(true); options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED); // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=212207 parser.setCompilerOptions(options); parser.setSource(cuSource.toCharArray()); return (CompilationUnit) parser.createAST(null); }
ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource(unit); parser.setResolveBindings(true); CompilationUnit cUnit = parser.createAST(null);
private static CompilationUnit createCompilationUnit(String sourceFile, IJavaProject javaProject) { String source = readWithStringBuilder(sourceFile); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource(source.toCharArray()); // set source parser.setProject(javaProject); parser.setResolveBindings(true); // we need bindings later on return (CompilationUnit) parser.createAST(null /* IProgressMonitor */); // parse }
protected CompilationUnit parse(ICompilationUnit cu) throws JavaModelException { // ensure cu is consistent (noop if already consistent) cu.makeConsistent(this.progressMonitor); // create an AST for the compilation unit ASTParser parser = ASTParser.newParser(AST.JLS11); parser.setSource(cu); return (CompilationUnit) parser.createAST(this.progressMonitor); } /**
protected CompilationUnit getCompilationUnit(String unitName, char[] contents, boolean resolveBindings, boolean errorRecovery, IString javaVersion, String[] sourcePath, String[] classPath) throws IOException { ASTParser parser = constructASTParser(resolveBindings, errorRecovery, javaVersion, sourcePath, classPath); parser.setUnitName(unitName); parser.setSource(contents); return (CompilationUnit) parser.createAST(null); }
private static CompilationUnit buildASTRoot(IClassFile classFile, boolean resolveBindings) { ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(classFile); parser.setResolveBindings(resolveBindings); return (CompilationUnit) parser.createAST(null); }
ASTParser parser = ASTParser.newParser(AST.JLS3); // or JLS_4 for java 7 and above parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource(source); // give your java source here as char array parser.setResolveBindings(true); CompilationUnit compilationUnit = parser.createAST(null);
private static CompilationUnit createAST(IJavaElement element, String cuSource) { ASTParser parser = ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL); IJavaProject javaProject= element.getJavaProject(); parser.setProject(javaProject); Map<String, String> options= javaProject.getOptions(true); options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED); // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=212207 parser.setCompilerOptions(options); parser.setSource(cuSource.toCharArray()); return (CompilationUnit) parser.createAST(null); }
Document document = new Document("import java.util.List;\n\nclass X\n{\n\n\tpublic void deleteme()\n\t{\n\t}\n\n}\n"); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(document.get().toCharArray()); CompilationUnit cu = (CompilationUnit)parser.createAST(null); cu.recordModifications();
private CompilationUnit createASTForImports(ICompilationUnit cu) { ASTParser parser= ASTParser.newParser(AST.JLS3); parser.setSource(cu); parser.setResolveBindings(false); parser.setFocalPosition(0); return (CompilationUnit) parser.createAST(null); }
private CompilationUnit createASTForImports(ICompilationUnit cu) { ASTParser parser= ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL); parser.setSource(cu); parser.setResolveBindings(true); parser.setFocalPosition(0); return (CompilationUnit) parser.createAST(null); }
public static CompilationUnit createQuickFixAST(ICompilationUnit compilationUnit, IProgressMonitor monitor) { ASTParser astParser= ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL); astParser.setSource(compilationUnit); astParser.setResolveBindings(true); astParser.setStatementsRecovery(IASTSharedValues.SHARED_AST_STATEMENT_RECOVERY); astParser.setBindingsRecovery(IASTSharedValues.SHARED_BINDING_RECOVERY); return (CompilationUnit) astParser.createAST(monitor); }
@Override public void process(JCas aJCas) throws AnalysisEngineProcessException { // Create the Java parser and parse the source code into an abstract syntax tree ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(aJCas.getDocumentText().toCharArray()); CompilationUnit result = (CompilationUnit) parser.createAST(null); // Generate JavaDoc related annotations result.accept(new JavaDocVisitor(aJCas)); // Generate Java code related annotations result.accept(new CodeVisitor(aJCas)); }
private static CompilationUnit getCuNode(WorkingCopyOwner workingCopyOwner, ICompilationUnit cu) { ASTParser p = ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL); p.setSource(cu); p.setResolveBindings(true); p.setWorkingCopyOwner(workingCopyOwner); p.setCompilerOptions(RefactoringASTParser.getCompilerOptions(cu)); return (CompilationUnit) p.createAST(null); }
private static CompilationUnit getCuNode(WorkingCopyOwner workingCopyOwner, ICompilationUnit cu) { ASTParser p = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL); p.setSource(cu); p.setResolveBindings(true); p.setWorkingCopyOwner(workingCopyOwner); p.setCompilerOptions(RefactoringASTParser.getCompilerOptions(cu)); return (CompilationUnit) p.createAST(null); }
private CompilationUnit createSourceCuNode() { Assert.isTrue(getSourceCu() != null || getSourceClassFile() != null); Assert.isTrue(getSourceCu() == null || getSourceClassFile() == null); ASTParser parser= ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL); parser.setBindingsRecovery(true); parser.setResolveBindings(true); if (getSourceCu() != null) parser.setSource(getSourceCu()); else parser.setSource(getSourceClassFile()); return (CompilationUnit) parser.createAST(null); }