Java Code Examples for org.eclipse.jdt.core.dom.ASTParser#setResolveBindings()
The following examples show how to use
org.eclipse.jdt.core.dom.ASTParser#setResolveBindings() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OverrideCompletionProposal.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
private CompilationUnit getRecoveredAST(IDocument document, int offset, Document recoveredDocument) { CompilationUnit ast = CoreASTProvider.getInstance().getAST(fCompilationUnit, CoreASTProvider.WAIT_YES, 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()); }
Example 2
Source File: SharedUtils.java From txtUML with Eclipse Public License 1.0 | 6 votes |
/** * Parses the specified Java source file located in the given Java project. * * @param sourceFile * The specified Java source file. * @param project * The given Java project. * @return The parsed compilation unit. * @throws IOException * Thrown when I/O error occurs during reading the file. * @throws JavaModelException */ public static CompilationUnit parseJavaSource(File sourceFile, IJavaProject project) throws IOException, JavaModelException { ASTParser parser = ASTParser.newParser(AST.JLS8); char[] content = SharedUtils.getFileContents(sourceFile); parser.setSource(content); parser.setProject(project); parser.setResolveBindings(true); parser.setBindingsRecovery(true); parser.setUnitName(sourceFile.getName()); parser.setKind(ASTParser.K_COMPILATION_UNIT); CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null); return compilationUnit; }
Example 3
Source File: JavaFile.java From SimFix with GNU General Public License v2.0 | 5 votes |
/** * generate {@code CompilationUnit} from source code based on the specific * type (e.g., {@code ASTParser.K_COMPILATION_UNIT}) * * @param icu * @param type * @return */ public static ASTNode genASTFromSource(String icu, int type) { ASTParser astParser = ASTParser.newParser(AST.JLS8); Map<?, ?> options = JavaCore.getOptions(); JavaCore.setComplianceOptions(JavaCore.VERSION_1_7, options); astParser.setCompilerOptions(options); astParser.setSource(icu.toCharArray()); astParser.setKind(type); astParser.setResolveBindings(true); astParser.setBindingsRecovery(true); return astParser.createAST(null); }
Example 4
Source File: ReorgPolicyFactory.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
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); }
Example 5
Source File: JavaASTUtils.java From gwt-eclipse-plugin with Eclipse Public License 1.0 | 5 votes |
public static CompilationUnit parseCompilationUnit( ICompilationUnit compilationUnit) { ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setResolveBindings(true); parser.setSource(compilationUnit); return (CompilationUnit) parser.createAST(null); }
Example 6
Source File: CompilationUnitParser.java From SparkBuilderGenerator with MIT License | 5 votes |
public CompilationUnit parse(ICompilationUnit unit) { ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource(unit); parser.setResolveBindings(true); parser.setProject(unit.getJavaProject()); parser.setUnitName(unit.getPath().toString()); return (CompilationUnit) parser.createAST(null); }
Example 7
Source File: CleanUpRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
public static ASTParser createCleanUpASTParser() { ASTParser result= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL); result.setResolveBindings(true); result.setStatementsRecovery(ASTProvider.SHARED_AST_STATEMENT_RECOVERY); result.setBindingsRecovery(ASTProvider.SHARED_BINDING_RECOVERY); return result; }
Example 8
Source File: Utils.java From SimFix with GNU General Public License v2.0 | 5 votes |
public static ASTNode genASTFromSource(String icu, int type) { ASTParser astParser = ASTParser.newParser(AST.JLS8); Map<?, ?> options = JavaCore.getOptions(); JavaCore.setComplianceOptions(JavaCore.VERSION_1_7, options); astParser.setCompilerOptions(options); astParser.setSource(icu.toCharArray()); astParser.setKind(type); astParser.setResolveBindings(true); astParser.setBindingsRecovery(true); return astParser.createAST(null); }
Example 9
Source File: SortElementsOperation.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Method processElement. * @param unit * @param source */ private String processElement(ICompilationUnit unit, char[] source) { Document document = new Document(new String(source)); CompilerOptions options = new CompilerOptions(unit.getJavaProject().getOptions(true)); ASTParser parser = ASTParser.newParser(this.apiLevel); parser.setCompilerOptions(options.getMap()); parser.setSource(source); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setResolveBindings(false); org.eclipse.jdt.core.dom.CompilationUnit ast = (org.eclipse.jdt.core.dom.CompilationUnit) parser.createAST(null); ASTRewrite rewriter= sortCompilationUnit(ast, null); if (rewriter == null) return document.get(); TextEdit edits = rewriter.rewriteAST(document, unit.getJavaProject().getOptions(true)); RangeMarker[] markers = null; if (this.positions != null) { markers = new RangeMarker[this.positions.length]; for (int i = 0, max = this.positions.length; i < max; i++) { markers[i]= new RangeMarker(this.positions[i], 0); insert(edits, markers[i]); } } try { edits.apply(document, TextEdit.UPDATE_REGIONS); if (this.positions != null) { for (int i= 0, max = markers.length; i < max; i++) { this.positions[i]= markers[i].getOffset(); } } } catch (BadLocationException e) { // ignore } return document.get(); }
Example 10
Source File: Utils.java From txtUML with Eclipse Public License 1.0 | 5 votes |
private static CompilationUnit getCompilationUnit(IBinding binding) { try { ICompilationUnit unit = (ICompilationUnit) binding.getJavaElement() .getAncestor(IJavaElement.COMPILATION_UNIT); ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource(unit); parser.setResolveBindings(true); CompilationUnit cu = (CompilationUnit) parser.createAST(null); return cu; } catch (Exception ex) { return null; } }
Example 11
Source File: JavaCompilationParticipantTest.java From gwt-eclipse-plugin with Eclipse Public License 1.0 | 5 votes |
private ASTNode parseTestClass() { // Have JDT parse the compilation unit ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setProject(getTestProject()); parser.setResolveBindings(false); parser.setSource(testClass.getCompilationUnit()); return parser.createAST(null); }
Example 12
Source File: NewTypeWizardPage.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private CompilationUnit createASTForImports(ICompilationUnit cu) { ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL); parser.setSource(cu); parser.setResolveBindings(true); parser.setFocalPosition(0); return (CompilationUnit) parser.createAST(null); }
Example 13
Source File: JdtParser.java From j2cl with Apache License 2.0 | 5 votes |
private ASTParser newASTParser(boolean resolveBinding) { ASTParser parser = ASTParser.newParser(AST_JLS_VERSION); parser.setCompilerOptions(compilerOptions); parser.setResolveBindings(resolveBinding); parser.setEnvironment( Iterables.toArray(classpathEntries, String.class), new String[0], new String[0], false); return parser; }
Example 14
Source File: Java7BaseTest.java From compiler with Apache License 2.0 | 5 votes |
protected static String parseJava(final String content) { final StringBuilder sb = new StringBuilder(); final FileASTRequestor r = new FileASTRequestor() { @Override public void acceptAST(String sourceFilePath, CompilationUnit cu) { final ASTRoot.Builder ast = ASTRoot.newBuilder(); try { ast.addNamespaces(visitor.getNamespaces(cu)); } catch (final Exception e) { System.err.println(e); e.printStackTrace(); } sb.append(JsonFormat.printToString(ast.build())); } }; Map<String, String> fileContents = new HashMap<String, String>(); fileContents.put("", content); @SuppressWarnings("rawtypes") Map options = JavaCore.getOptions(); options.put(JavaCore.COMPILER_COMPLIANCE, javaVersion); options.put(JavaCore.COMPILER_SOURCE, javaVersion); ASTParser parser = ASTParser.newParser(astLevel); parser.setCompilerOptions(options); parser.setEnvironment(new String[0], new String[]{}, new String[]{}, true); parser.setResolveBindings(true); parser.createASTs(fileContents, new String[]{""}, null, new String[0], r, null); return FileIO.normalizeEOL(sb.toString()); }
Example 15
Source File: ReconcileContext.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * Returns a resolved AST with {@link AST#JLS3 JLS3} level. * It is created from the current state of the working copy. * Creates one if none exists yet. * Returns <code>null</code> if the current state of the working copy * doesn't allow the AST to be created (e.g. if the working copy's content * cannot be parsed). * <p> * If the AST level requested during reconciling is not {@link AST#JLS3} * or if binding resolutions was not requested, then a different AST is created. * Note that this AST does not become the current AST and it is only valid for * the requestor. * </p> * * @return the AST created from the current state of the working copy, * or <code>null</code> if none could be created * @exception JavaModelException if the contents of the working copy * cannot be accessed. Reasons include: * <ul> * <li> The working copy does not exist (ELEMENT_DOES_NOT_EXIST)</li> * </ul> * @deprecated JLS3 has been deprecated. This method has been replaced by {@link #getAST4()} which returns an AST * with JLS4 level. */ public org.eclipse.jdt.core.dom.CompilationUnit getAST3() throws JavaModelException { if (this.operation.astLevel != AST.JLS3 || !this.operation.resolveBindings) { // create AST (optionally resolving bindings) ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setCompilerOptions(this.workingCopy.getJavaProject().getOptions(true)); if (JavaProject.hasJavaNature(this.workingCopy.getJavaProject().getProject())) parser.setResolveBindings(true); parser.setStatementsRecovery((this.operation.reconcileFlags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0); parser.setBindingsRecovery((this.operation.reconcileFlags & ICompilationUnit.ENABLE_BINDINGS_RECOVERY) != 0); parser.setSource(this.workingCopy); parser.setIgnoreMethodBodies((this.operation.reconcileFlags & ICompilationUnit.IGNORE_METHOD_BODIES) != 0); return (org.eclipse.jdt.core.dom.CompilationUnit) parser.createAST(this.operation.progressMonitor); } return this.operation.makeConsistent(this.workingCopy); }
Example 16
Source File: JavaASTExtractor.java From codemining-core with BSD 3-Clause "New" or "Revised" License | 4 votes |
/** * Return an ASTNode given the content * * @param content * @return */ 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); // set source 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); } }
Example 17
Source File: JavaASTExtractor.java From codemining-core with BSD 3-Clause "New" or "Revised" License | 4 votes |
/** * Get the AST of a file, including additional source paths to resolve * cross-file bindings. It is assumed that a CompilationUnit will be * returned. A heuristic is used to set the file's path variable. * <p> * Note: this may only yield a big improvement if the above heuristic fails * and srcPaths contains the correct source path. * * @param file * @param srcPaths * for binding resolution * @return the compilation unit of the file * @throws IOException */ public final CompilationUnit getAST(final File file, final Set<String> srcPaths) throws IOException { final String sourceFile = FileUtils.readFileToString(file); final ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setKind(ASTParser.K_COMPILATION_UNIT); 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(sourceFile.toCharArray()); // set source parser.setResolveBindings(useBindings); parser.setBindingsRecovery(useBindings); parser.setStatementsRecovery(true); parser.setUnitName(file.getAbsolutePath()); // Heuristic to retrieve source file path final String srcFilePath; if (file.getAbsolutePath().contains("/src")) { srcFilePath = file.getAbsolutePath().substring(0, file.getAbsolutePath().indexOf("src", 0) + 3); } else { srcFilePath = ""; } // Add file to source paths if not already present srcPaths.add(srcFilePath); final String[] sourcePathEntries = srcPaths.toArray(new String[srcPaths .size()]); final String[] classPathEntries = new String[0]; parser.setEnvironment(classPathEntries, sourcePathEntries, null, true); final CompilationUnit compilationUnit = (CompilationUnit) parser .createAST(null); return compilationUnit; }
Example 18
Source File: CompilationUnitCache.java From JDeodorant with MIT License | 4 votes |
public CompilationUnit getCompilationUnit(ITypeRoot iTypeRoot) { if(iTypeRoot instanceof IClassFile) { IClassFile classFile = (IClassFile)iTypeRoot; return LibraryClassStorage.getInstance().getCompilationUnit(classFile); } else { if(iTypeRootList.contains(iTypeRoot)) { int position = iTypeRootList.indexOf(iTypeRoot); return compilationUnitList.get(position); } else { ASTParser parser = ASTParser.newParser(ASTReader.JLS); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource(iTypeRoot); parser.setResolveBindings(true); CompilationUnit compilationUnit = (CompilationUnit)parser.createAST(null); IPreferenceStore store = Activator.getDefault().getPreferenceStore(); int maximumCacheSize = store.getInt(PreferenceConstants.P_PROJECT_COMPILATION_UNIT_CACHE_SIZE); if(iTypeRootList.size() < maximumCacheSize) { iTypeRootList.add(iTypeRoot); compilationUnitList.add(compilationUnit); } else { if(!lockedTypeRoots.isEmpty()) { int indexToBeRemoved = 0; int counter = 0; for(ITypeRoot lockedTypeRoot : lockedTypeRoots) { if(iTypeRootList.get(counter).equals(lockedTypeRoot)) { indexToBeRemoved++; } counter++; } iTypeRootList.remove(indexToBeRemoved); compilationUnitList.remove(indexToBeRemoved); } else { iTypeRootList.removeFirst(); compilationUnitList.removeFirst(); } iTypeRootList.add(iTypeRoot); compilationUnitList.add(compilationUnit); } return compilationUnit; } } }
Example 19
Source File: ReconcileContext.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * Returns a resolved AST with {@link AST#JLS8 JLS8} level. * It is created from the current state of the working copy. * Creates one if none exists yet. * Returns <code>null</code> if the current state of the working copy * doesn't allow the AST to be created (e.g. if the working copy's content * cannot be parsed). * <p> * If the AST level requested during reconciling is not {@link AST#JLS8} * or if binding resolutions was not requested, then a different AST is created. * Note that this AST does not become the current AST and it is only valid for * the requestor. * </p> * * @return the AST created from the current state of the working copy, * or <code>null</code> if none could be created * @exception JavaModelException if the contents of the working copy * cannot be accessed. Reasons include: * <ul> * <li> The working copy does not exist (ELEMENT_DOES_NOT_EXIST)</li> * </ul> * @since 3.10 */ public org.eclipse.jdt.core.dom.CompilationUnit getAST8() throws JavaModelException { if (this.operation.astLevel != AST.JLS8 || !this.operation.resolveBindings) { // create AST (optionally resolving bindings) ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setCompilerOptions(this.workingCopy.getJavaProject().getOptions(true)); if (JavaProject.hasJavaNature(this.workingCopy.getJavaProject().getProject())) parser.setResolveBindings(true); parser.setStatementsRecovery((this.operation.reconcileFlags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0); parser.setBindingsRecovery((this.operation.reconcileFlags & ICompilationUnit.ENABLE_BINDINGS_RECOVERY) != 0); parser.setSource(this.workingCopy); parser.setIgnoreMethodBodies((this.operation.reconcileFlags & ICompilationUnit.IGNORE_METHOD_BODIES) != 0); return (org.eclipse.jdt.core.dom.CompilationUnit) parser.createAST(this.operation.progressMonitor); } return this.operation.makeConsistent(this.workingCopy); }
Example 20
Source File: ASTBatchParser.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 3 votes |
/** * Creates a new parser which can be used to create ASTs * for compilation units in <code>project</code> * <p> * Subclasses may override * </p> * * @param project the project for which ASTs are been generated * @return an AST parser capable of creating ASTs of compilation units in project */ protected ASTParser createParser(IJavaProject project) { ASTParser result= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL); result.setResolveBindings(true); result.setProject(project); return result; }