List of usage examples for org.eclipse.jdt.core.dom ASTParser setStatementsRecovery
public void setStatementsRecovery(boolean enabled)
From source file:cideplus.ui.astview.ASTView.java
License:Open Source License
private CompilationUnit createAST(ITypeRoot input, int astLevel, int offset) throws JavaModelException, CoreException { long startTime; long endTime; CompilationUnit root;/*from ww w . j a va 2 s .c o m*/ if ((getCurrentInputKind() == ASTInputKindAction.USE_RECONCILE)) { final IProblemRequestor problemRequestor = new IProblemRequestor() { //strange: don't get bindings when supplying null as problemRequestor public void acceptProblem(IProblem problem) { /*not interested*/} public void beginReporting() { /*not interested*/} public void endReporting() { /*not interested*/} public boolean isActive() { return true; } }; WorkingCopyOwner workingCopyOwner = new WorkingCopyOwner() { @Override public IProblemRequestor getProblemRequestor(ICompilationUnit workingCopy) { return problemRequestor; } }; ICompilationUnit wc = input.getWorkingCopy(workingCopyOwner, null); try { int reconcileFlags = ICompilationUnit.FORCE_PROBLEM_DETECTION; if (fStatementsRecovery) reconcileFlags |= ICompilationUnit.ENABLE_STATEMENTS_RECOVERY; if (fBindingsRecovery) reconcileFlags |= ICompilationUnit.ENABLE_BINDINGS_RECOVERY; if (fIgnoreMethodBodies) reconcileFlags |= ICompilationUnit.IGNORE_METHOD_BODIES; startTime = System.currentTimeMillis(); root = wc.reconcile(getCurrentASTLevel(), reconcileFlags, null, null); endTime = System.currentTimeMillis(); } finally { wc.discardWorkingCopy(); } } else if (input instanceof ICompilationUnit && (getCurrentInputKind() == ASTInputKindAction.USE_CACHE)) { ICompilationUnit cu = (ICompilationUnit) input; startTime = System.currentTimeMillis(); root = SharedASTProvider.getAST(cu, SharedASTProvider.WAIT_NO, null); endTime = System.currentTimeMillis(); } else { ASTParser parser = ASTParser.newParser(astLevel); parser.setResolveBindings(fCreateBindings); if (input instanceof ICompilationUnit) { parser.setSource((ICompilationUnit) input); } else { parser.setSource((IClassFile) input); } parser.setStatementsRecovery(fStatementsRecovery); parser.setBindingsRecovery(fBindingsRecovery); parser.setIgnoreMethodBodies(fIgnoreMethodBodies); if (getCurrentInputKind() == ASTInputKindAction.USE_FOCAL) { parser.setFocalPosition(offset); } startTime = System.currentTimeMillis(); root = (CompilationUnit) parser.createAST(null); endTime = System.currentTimeMillis(); } if (root != null) { updateContentDescription(input, root, endTime - startTime); } return root; }
From source file:com.facebook.nuclide.debugger.EvaluationManager.java
License:Open Source License
private CompilationUnit compileSource(String source, String sourceFilePath, String unitName, StringBuilder errors) throws Exception { if (!unitName.endsWith(".java")) { // The AST compiler is surprisingly insistent about this. unitName += ".java"; }/*from w ww . ja va2s . c o m*/ final File sourceFile = new File(sourceFilePath); final String directoryPath = sourceFile.getAbsoluteFile().getParent(); Utils.logVerbose("compiling source for: " + directoryPath); final String[] sources = _contextManager.getSourceLocator().getSourceSearchPaths(); final String[] classpath = _contextManager.getSourceLocator().getBinaryJarPaths(); final ASTParser parser = ASTParser.newParser(AST.JLS8); final Map<String, String> options = JavaCore.getOptions(); JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options); parser.setCompilerOptions(options); parser.setUnitName(unitName); parser.setResolveBindings(true); parser.setStatementsRecovery(true); parser.setBindingsRecovery(true); parser.setEnvironment(classpath, sources, null, true); parser.setSource(source.toCharArray()); CompilationUnit unit = (CompilationUnit) parser.createAST(null); String errorMsg = checkUnitForProblems(unit); if (errorMsg != null) { errors.append(errorMsg); } return unit; }
From source file:com.flamefire.importsmalinames.utils.Util.java
License:Open Source License
public static CompilationUnit createCU(ICompilationUnit cu, boolean resolveBindings) { ASTParser parser = ASTParser.newParser(AST.JLS4); parser.setProject(cu.getJavaProject()); parser.setSource(cu);/*from w w w .j a v a2s .c om*/ parser.setResolveBindings(resolveBindings); parser.setStatementsRecovery(true); parser.setBindingsRecovery(true); CompilationUnit unit = (CompilationUnit) parser.createAST(null); return unit; }
From source file:com.halware.nakedide.eclipse.ext.annot.tracker.AstJob.java
License:Open Source License
@Override protected IStatus run(IProgressMonitor monitor) { getLOGGER().debug("run(): started"); try {//from w w w .ja va 2s . com if (!editorContent.isValid() || editorContent.isNull()) { return Status.OK_STATUS; } if (isCancelRequested()) { return Status.CANCEL_STATUS; } ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setResolveBindings(isResolveBindings()); IOpenable openable = editorContent.getOpenable(); if (openable instanceof ICompilationUnit) { parser.setSource((ICompilationUnit) openable); } else { parser.setSource((IClassFile) openable); } parser.setStatementsRecovery(isStatementsRecovery()); CompilationUnit parsedCompilationUnit = (CompilationUnit) parser.createAST(null); editorContent.setParsedCompilationUnit(parsedCompilationUnit); if (isCancelRequested()) { return Status.CANCEL_STATUS; } return Status.OK_STATUS; } finally { getLOGGER().debug("run(): completed"); } }
From source file:com.kodebeagle.javaparser.JavaASTParser.java
License:Apache License
/** * Return an ASTNode given the content// w w w . j av a2s. c o m * * @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); 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); } }
From source file:com.liferay.blade.eclipse.provider.CUCache.java
License:Open Source License
@SuppressWarnings("unchecked") private static CompilationUnit createCompilationUnit(String unitName, char[] javaSource) { ASTParser parser = ASTParser.newParser(AST.JLS8); Map<String, String> options = JavaCore.getOptions(); JavaCore.setComplianceOptions(JavaCore.VERSION_1_6, options); parser.setCompilerOptions(options);/*from w w w. j ava2 s . c o m*/ //setUnitName for resolve bindings parser.setUnitName(unitName); String[] sources = { "" }; String[] classpath = { "" }; //setEnvironment for resolve bindings even if the args is empty parser.setEnvironment(classpath, sources, new String[] { "UTF-8" }, true); parser.setResolveBindings(true); parser.setStatementsRecovery(true); parser.setBindingsRecovery(true); parser.setSource(javaSource); parser.setIgnoreMethodBodies(false); return (CompilationUnit) parser.createAST(null); }
From source file:com.liferay.blade.eclipse.provider.CUCacheJDT.java
License:Open Source License
@SuppressWarnings("unchecked") private CompilationUnit createCompilationUnit(String unitName, char[] javaSource) { ASTParser parser = ASTParser.newParser(AST.JLS8); Map<String, String> options = JavaCore.getOptions(); JavaCore.setComplianceOptions(JavaCore.VERSION_1_6, options); parser.setCompilerOptions(options);/*w ww .j a v a 2s . c o m*/ //setUnitName for resolve bindings parser.setUnitName(unitName); String[] sources = { "" }; String[] classpath = { "" }; //setEnvironment for resolve bindings even if the args is empty parser.setEnvironment(classpath, sources, new String[] { "UTF-8" }, true); parser.setResolveBindings(true); parser.setStatementsRecovery(true); parser.setBindingsRecovery(true); parser.setSource(javaSource); parser.setIgnoreMethodBodies(false); return (CompilationUnit) parser.createAST(null); }
From source file:com.liferay.ide.project.ui.migration.CUCache.java
License:Open Source License
@SuppressWarnings("unchecked") private static CompilationUnit createCompilationUnit(String unitName, char[] javaSource) { ASTParser parser = ASTParser.newParser(AST.JLS2); Map<String, String> options = JavaCore.getOptions(); JavaCore.setComplianceOptions(JavaCore.VERSION_1_6, options); parser.setCompilerOptions(options);//from w w w .ja v a 2 s .co m // setUnitName for resolve bindings parser.setUnitName(unitName); String[] sources = { "" }; String[] classpath = { "" }; // setEnvironment for resolve bindings even if the args is empty parser.setEnvironment(classpath, sources, new String[] { "UTF-8" }, true); parser.setResolveBindings(true); parser.setStatementsRecovery(true); parser.setBindingsRecovery(true); parser.setSource(javaSource); parser.setIgnoreMethodBodies(false); return (CompilationUnit) parser.createAST(null); }
From source file:com.motorolamobility.preflighting.samplechecker.findviewbyid.quickfix.FindViewByIdMarkerResolution.java
License:Apache License
public void run(IMarker marker) { IResource resource = marker.getResource(); final ICompilationUnit iCompilationUnit = JavaCore.createCompilationUnitFrom((IFile) resource); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setProject(JavaCore.create(resource.getProject())); parser.setResolveBindings(true);/*ww w . ja v a 2 s . c o m*/ parser.setSource(iCompilationUnit); parser.setStatementsRecovery(true); parser.setBindingsRecovery(true); final CompilationUnit compUnit = (CompilationUnit) parser.createAST(null); try { final int lineNumber = marker.getAttribute(IMarker.LINE_NUMBER, -1); MethodInvocation invokedMethod = null; //Look for the invokedMethod that shall be moved invokedMethod = getMethodInvocation(compUnit, lineNumber, invokedMethod); IEditorPart editor = openEditor(iCompilationUnit); ASTNode loopNode = getLoopNode(invokedMethod); //Retrieve block parent for the loop statement. Block targetBlock = (Block) loopNode.getParent(); List<Statement> statements = targetBlock.statements(); int i = getLoopStatementIndex(loopNode, statements); //Add the node before the loop. compUnit.recordModifications(); ASTNode invokedMethodStatement = getInvokedStatement(invokedMethod); final VariableDeclarationStatement varDeclarationStatement[] = new VariableDeclarationStatement[1]; //Verify if the invoke statement contains a variable attribution if (invokedMethodStatement instanceof ExpressionStatement) { ExpressionStatement expressionStatement = (ExpressionStatement) invokedMethodStatement; Expression expression = expressionStatement.getExpression(); if (expression instanceof Assignment) { Expression leftHandSide = ((Assignment) expression).getLeftHandSide(); if (leftHandSide instanceof SimpleName) //Search for the variable declaration { SimpleName simpleName = (SimpleName) leftHandSide; final String varName = simpleName.getIdentifier(); loopNode.accept(new ASTVisitor() { @Override public boolean visit(VariableDeclarationStatement node) //Visit all variable declarations inside the loop looking for the variable which receives the findViewById result { List<VariableDeclarationFragment> fragments = node.fragments(); for (VariableDeclarationFragment fragment : fragments) { if (fragment.getName().getIdentifier().equals(varName)) { varDeclarationStatement[0] = node; break; } } return super.visit(node); } }); } } } //Variable is declared inside the loop, now let's move the variable declaration if needed if (varDeclarationStatement[0] != null) { ASTNode varDeclarationSubTree = ASTNode.copySubtree(targetBlock.getAST(), varDeclarationStatement[0]); statements.add(i, (Statement) varDeclarationSubTree.getRoot()); //Delete the node inside loop. varDeclarationStatement[0].delete(); i++; } ASTNode copySubtree = ASTNode.copySubtree(targetBlock.getAST(), invokedMethodStatement); statements.add(i, (Statement) copySubtree.getRoot()); //Delete the node inside loop. invokedMethodStatement.delete(); // apply changes to file final Map<?, ?> mapOptions = JavaCore.create(resource.getProject()).getOptions(true); final IDocument document = ((AbstractTextEditor) editor).getDocumentProvider() .getDocument(editor.getEditorInput()); PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() { public void run() { try { TextEdit edit = compUnit.rewrite(document, mapOptions); iCompilationUnit.applyTextEdit(edit, new NullProgressMonitor()); } catch (JavaModelException e) { EclipseUtils.showErrorDialog(MessagesNLS.FindViewByIdMarkerResolution_Error_Msg_Title, MessagesNLS.FindViewByIdMarkerResolution_Error_Aplying_Changes + e.getMessage()); } } }); marker.delete(); } catch (Exception e) { EclipseUtils.showErrorDialog(MessagesNLS.FindViewByIdMarkerResolution_Error_Msg_Title, MessagesNLS.FindViewByIdMarkerResolution_Error_Could_Not_Fix_Code); } }
From source file:de.ovgu.cide.language.jdt.JDTParserWrapper.java
License:Open Source License
public static CompilationUnit parseCompilationUnit(ICompilationUnit compUnit) throws ParseException { ASTParser parser = ASTParser.newParser(AST.JLS3);// TODO: find parser.setResolveBindings(true);/*w w w .ja v a2s . c o m*/ parser.setSource(compUnit); parser.setStatementsRecovery(false); CompilationUnit root = (CompilationUnit) parser.createAST(null); return root; }