Refine search
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); }
public static CompilationUnit parse(char[] src) { ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setCompilerOptions(compilerOptions); parser.setSource(src); return (CompilationUnit) 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 }
private void extractJavaDoc(Class<?> clazz) { ASTParser parser = ASTParser.newParser(AST.JLS4); 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;
/** * @param newCuSource the source * @param originalCu the compilation unit to get the name and project from * @param resolveBindings whether bindings are to be resolved * @param recovery whether statements and binding recovery should be enabled * @param pm an {@link IProgressMonitor}, or <code>null</code> * @return the parsed CompilationUnit */ public CompilationUnit parse(String newCuSource, ICompilationUnit originalCu, boolean resolveBindings, boolean recovery, IProgressMonitor pm) { fParser.setResolveBindings(resolveBindings); fParser.setStatementsRecovery(recovery); fParser.setBindingsRecovery(recovery); fParser.setSource(newCuSource.toCharArray()); fParser.setUnitName(originalCu.getElementName()); fParser.setProject(originalCu.getJavaProject()); fParser.setCompilerOptions(getCompilerOptions(originalCu)); CompilationUnit newCUNode= (CompilationUnit) fParser.createAST(pm); return newCUNode; }
public parseJavaFile(String pathOfJavaFile, String[] sources, String[] classpath, String nameOfJavaFile) throws Exception { ASTParser parser = ASTParser.newParser(AST.JLS8);//choose source code analyzing strategy parser.setResolveBindings(true);// turn on binding strategy parser.setKind(ASTParser.K_COMPILATION_UNIT);// the source code is a file .java parser.setBindingsRecovery(true); parser.setCompilerOptions(JavaCore.getOptions()); parser.setUnitName(nameOfJavaFile); parser.setEnvironment(classpath, sources, new String[] { "UTF-8" }, true); parser.setSource(Utils.getFile(pathOfJavaFile).toCharArray()); CompilationUnit cu = (CompilationUnit) parser.createAST(null); if (cu.getAST().hasBindingsRecovery()) { // Now binding is activated. Do something else } }
private CompilationUnit getRecoveredAST(IDocument document, int offset, Document recoveredDocument) { CompilationUnit ast= SharedASTProviderCore.getAST(fCompilationUnit, SharedASTProviderCore.WAIT_ACTIVE_ONLY, null); if (ast != null) { recoveredDocument.set(document.get()); return ast; } char[] content= document.get().toCharArray(); // clear prefix to avoid compile errors int index= offset - 1; while (index >= 0 && Character.isJavaIdentifierPart(content[index])) { content[index]= ' '; index--; } recoveredDocument.set(new String(content)); final ASTParser parser= ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL); parser.setResolveBindings(true); parser.setStatementsRecovery(true); parser.setSource(content); parser.setUnitName(fCompilationUnit.getElementName()); parser.setProject(fCompilationUnit.getJavaProject()); return (CompilationUnit) parser.createAST(new NullProgressMonitor()); }
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); }
public CompilationUnit parse(ITypeRoot typeRoot, WorkingCopyOwner owner, boolean resolveBindings, boolean statementsRecovery, boolean bindingsRecovery, IProgressMonitor pm) { fParser.setResolveBindings(resolveBindings); fParser.setStatementsRecovery(statementsRecovery); fParser.setBindingsRecovery(bindingsRecovery); fParser.setSource(typeRoot); if (owner != null) fParser.setWorkingCopyOwner(owner); fParser.setCompilerOptions(getCompilerOptions(typeRoot)); CompilationUnit result= (CompilationUnit) fParser.createAST(pm); return result; }
public static ASTParser createCleanUpASTParser() { ASTParser result= ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL); result.setResolveBindings(true); result.setStatementsRecovery(IASTSharedValues.SHARED_AST_STATEMENT_RECOVERY); result.setBindingsRecovery(IASTSharedValues.SHARED_BINDING_RECOVERY); return result; }
private CompilationUnit createAST(final String content, String unitName) { Document doc = new Document(content); final ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setResolveBindings(true); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setBindingsRecovery(true); parser.setStatementsRecovery(true); parser.setUnitName(unitName); Map<String, String> options = JavaCore.getOptions(); JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options); parser.setCompilerOptions(options); String[] sources = { inputArgs.getSourceFolder() }; parser.setEnvironment(null, sources, null, true); parser.setSource(doc.get().toCharArray()); CompilationUnit cu = (CompilationUnit) parser.createAST(null); if (!cu.getAST().hasBindingsRecovery()) { System.out.println("Binding not activated."); } return cu; }
public static TypeParameter newTypeParameter(AST ast, String content) { StringBuffer buffer= new StringBuffer(TYPEPARAM_HEADER); buffer.append(content); buffer.append(TYPEPARAM_FOOTER); ASTParser p= ASTParser.newParser(ast.apiLevel()); p.setSource(buffer.toString().toCharArray()); CompilationUnit root= (CompilationUnit) p.createAST(null); List list= root.types(); TypeDeclaration typeDecl= (TypeDeclaration) list.get(0); MethodDeclaration methodDecl= typeDecl.getMethods()[0]; TypeParameter tp= (TypeParameter) methodDecl.typeParameters().get(0); ASTNode result= ASTNode.copySubtree(ast, tp); result.accept(new PositionClearer()); return (TypeParameter) result; }
ASTParser parser = ASTParser.newParser(AST.JLS4); parser.setKind(ASTParser.K_COMPILATION_UNIT); // you must set compilation unit name, so I just name it as a path to source file parser.setUnitName(inputFilePath.toString()); //params classpathEntries, sourcepathEntries, encodings, IncludeRunningVMBootclasspath parser.setEnvironment(null, null, null, true); //fileContent is a char array of some source code parser.setSource(fileContent); parser.setResolveBindings(true); CompilationUnit unit = (CompilationUnit) parser.createAST(null);
ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setSource(compilationUnit); parser.setSourceRange(method.getSourceRange().getOffset(), method.getSourceRange().getLength()); parser.setResolveBindings(true); CompilationUnit cu = (CompilationUnit)parser.createAST(null); cu.accept(new ASTMethodVisitor());
public static ITestContent parseType(IType type) { BaseVisitor result = new AnnotationVisitor(); ICompilationUnit compilationUnit = type.getCompilationUnit(); if (compilationUnit == null) { return result; } ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setResolveBindings(true); parser.setSource(compilationUnit); parser.setProject(type.getJavaProject()); parser.setUnitName(compilationUnit.getPath().toString()); CompilationUnit cu = (CompilationUnit) parser.createAST(null); cu.accept(result); return result; }
private void deleteElement(IJavaElement elementToRemove, ICompilationUnit cu) throws JavaModelException { // ensure cu is consistent (noop if already consistent) cu.makeConsistent(this.progressMonitor); this.parser.setSource(cu); CompilationUnit astCU = (CompilationUnit) this.parser.createAST(this.progressMonitor); ASTNode node = ((JavaElement) elementToRemove).findNode(astCU); if (node == null) Assert.isTrue(false, "Failed to locate " + elementToRemove.getElementName() + " in " + cu.getElementName()); //$NON-NLS-1$//$NON-NLS-2$ AST ast = astCU.getAST(); ASTRewrite rewriter = ASTRewrite.create(ast); rewriter.remove(node, null); TextEdit edits = rewriter.rewriteAST(); applyTextEdit(cu, edits); }
private static ASTParser buildAstParser(String[] sourceFolders) { ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setKind(ASTParser.K_COMPILATION_UNIT); Map options = JavaCore.getOptions(); JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options); parser.setCompilerOptions(options); parser.setResolveBindings(true); parser.setBindingsRecovery(true); parser.setEnvironment(new String[0], sourceFolders, null, true); //parser.setEnvironment(new String[0], new String[]{"tmp\\refactoring-toy-example\\src"}, null, false); return parser; }
public static CompilationUnit parse(final ITypeRoot unit, final IProgressMonitor mon) { ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource(unit); parser.setProject(unit.getJavaProject()); parser.setResolveBindings(true); parser.setStatementsRecovery(true); return (CompilationUnit) parser.createAST(mon); // parse }
ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setResolveBindings(true); parser.setBindingsRecovery(true); Hashtable<String, String> options = JavaCore.getDefaultOptions(); options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_6); parser.setCompilerOptions(options); parser.setEnvironment(classpath, sources, new String[] { "UTF-8", "UTF-8" }, true); parser.setSource(fileContents.toCharArray()); CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null); IProblem[] problems = compilationUnit.getProblems(); if (problems != null && problems.length > 0) { logger.warn("Got {} problems compiling the source file: ", problems.length); for (IProblem problem : problems) { logger.warn("{}", problem); } } return compilationUnit;