Java Code Examples for org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser#parseWithASTProvider()
The following examples show how to use
org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser#parseWithASTProvider() .
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: ExtractTempRefactoring.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
@Override public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException { try { pm.beginTask("", 6); //$NON-NLS-1$ RefactoringStatus result = Checks.validateModifiesFiles(ResourceUtil.getFiles(new ICompilationUnit[] { fCu }), getValidationContext(), pm); if (result.hasFatalError()) { return result; } if (fCompilationUnitNode == null) { fCompilationUnitNode = RefactoringASTParser.parseWithASTProvider(fCu, true, new SubProgressMonitor(pm, 3)); } else { pm.worked(3); } result.merge(checkSelection(new SubProgressMonitor(pm, 3))); if (!result.hasFatalError() && isLiteralNodeSelected()) { fReplaceAllOccurrences = false; } return result; } finally { pm.done(); } }
Example 2
Source File: ChangeUtil.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
private static void convertMoveCompilationUnitChange(WorkspaceEdit edit, MoveCompilationUnitChange change) throws JavaModelException { IPackageFragment newPackage = change.getDestinationPackage(); ICompilationUnit unit = change.getCu(); CompilationUnit astCU = RefactoringASTParser.parseWithASTProvider(unit, true, new NullProgressMonitor()); ASTRewrite rewrite = ASTRewrite.create(astCU.getAST()); IPackageDeclaration[] packDecls = unit.getPackageDeclarations(); String oldPackageName = packDecls.length > 0 ? packDecls[0].getElementName() : ""; if (!Objects.equals(oldPackageName, newPackage.getElementName())) { // update the package declaration if (updatePackageStatement(astCU, newPackage.getElementName(), rewrite, unit)) { convertTextEdit(edit, unit, rewrite.rewriteAST()); } } RenameFile cuResourceChange = new RenameFile(); cuResourceChange.setOldUri(JDTUtils.toURI(unit)); IPath newCUPath = newPackage.getResource().getLocation().append(unit.getPath().lastSegment()); String newUri = ResourceUtils.fixURI(newCUPath.toFile().toURI()); cuResourceChange.setNewUri(newUri); edit.getDocumentChanges().add(Either.forRight(cuResourceChange)); }
Example 3
Source File: ExtractTempRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
@Override public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException { try { pm.beginTask("", 6); //$NON-NLS-1$ RefactoringStatus result= Checks.validateModifiesFiles(ResourceUtil.getFiles(new ICompilationUnit[] { fCu}), getValidationContext()); if (result.hasFatalError()) return result; if (fCompilationUnitNode == null) { fCompilationUnitNode= RefactoringASTParser.parseWithASTProvider(fCu, true, new SubProgressMonitor(pm, 3)); } else { pm.worked(3); } result.merge(checkSelection(new SubProgressMonitor(pm, 3))); if (!result.hasFatalError() && isLiteralNodeSelected()) fReplaceAllOccurrences= false; return result; } finally { pm.done(); } }
Example 4
Source File: InlineAction.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
@Override public void run(ITextSelection selection) { if (!ActionUtil.isEditable(fEditor)) return; ITypeRoot typeRoot= SelectionConverter.getInput(fEditor); if (typeRoot == null) return; CompilationUnit node= RefactoringASTParser.parseWithASTProvider(typeRoot, true, null); if (typeRoot instanceof ICompilationUnit) { ICompilationUnit cu= (ICompilationUnit) typeRoot; if (fInlineTemp.isEnabled() && fInlineTemp.tryInlineTemp(cu, node, selection, getShell())) return; if (fInlineConstant.isEnabled() && fInlineConstant.tryInlineConstant(cu, node, selection, getShell())) return; } //InlineMethod is last (also tries enclosing element): if (fInlineMethod.isEnabled() && fInlineMethod.tryInlineMethod(typeRoot, node, selection, getShell())) return; MessageDialog.openInformation(getShell(), RefactoringMessages.InlineAction_dialog_title, RefactoringMessages.InlineAction_select); }
Example 5
Source File: ConvertAnonymousToNestedRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private void initAST(IProgressMonitor pm) { if (fCompilationUnitNode == null) { fCompilationUnitNode= RefactoringASTParser.parseWithASTProvider(fCu, true, pm); } if (fAnonymousInnerClassNode == null) { fAnonymousInnerClassNode= getAnonymousInnerClass(NodeFinder.perform(fCompilationUnitNode, fSelectionStart, fSelectionLength)); } if (fAnonymousInnerClassNode != null) { final AbstractTypeDeclaration declaration= (AbstractTypeDeclaration) ASTNodes.getParent(fAnonymousInnerClassNode, AbstractTypeDeclaration.class); if (declaration instanceof TypeDeclaration) { final AbstractTypeDeclaration[] nested= ((TypeDeclaration) declaration).getTypes(); fClassNamesUsed= new HashSet<String>(nested.length); for (int index= 0; index < nested.length; index++) fClassNamesUsed.add(nested[index].getName().getIdentifier()); } else fClassNamesUsed= Collections.emptySet(); } }
Example 6
Source File: RenameTypeParameterProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Creates the necessary changes for the renaming of the type parameter. * * @param monitor * the progress monitor to display progress * @return the status of the operation * @throws CoreException * if the change could not be generated */ private RefactoringStatus createRenameChanges(IProgressMonitor monitor) throws CoreException { Assert.isNotNull(monitor); RefactoringStatus status= new RefactoringStatus(); try { monitor.beginTask(RefactoringCoreMessages.RenameTypeParameterRefactoring_searching, 2); ICompilationUnit cu= fTypeParameter.getDeclaringMember().getCompilationUnit(); CompilationUnit root= RefactoringASTParser.parseWithASTProvider(cu, true, null); CompilationUnitRewrite rewrite= new CompilationUnitRewrite(cu, root); IMember member= fTypeParameter.getDeclaringMember(); ASTNode declaration= null; if (member instanceof IMethod) { declaration= ASTNodeSearchUtil.getMethodDeclarationNode((IMethod) member, root); } else if (member instanceof IType) { declaration= ASTNodeSearchUtil.getAbstractTypeDeclarationNode((IType) member, root); } else { JavaPlugin.logErrorMessage("Unexpected sub-type of IMember: " + member.getClass().getName()); //$NON-NLS-1$ Assert.isTrue(false); } monitor.worked(1); RenameTypeParameterVisitor visitor= new RenameTypeParameterVisitor(rewrite, fTypeParameter.getNameRange(), status); if (declaration != null) declaration.accept(visitor); fChange= visitor.getResult(); } finally { monitor.done(); } return status; }
Example 7
Source File: InlineMethodAction.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private void run(int offset, int length, ITypeRoot typeRoot) { if (!ActionUtil.isEditable(fEditor, getShell(), typeRoot)) return; CompilationUnit compilationUnit= RefactoringASTParser.parseWithASTProvider(typeRoot, true, null); if (!RefactoringExecutionStarter.startInlineMethodRefactoring(typeRoot, compilationUnit, offset, length, getShell())) { MessageDialog.openInformation(getShell(), RefactoringMessages.InlineMethodAction_dialog_title, RefactoringMessages.InlineMethodAction_no_method_invocation_or_declaration_selected); } }
Example 8
Source File: InlineConstantAction.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private void run(int offset, int length, ICompilationUnit cu) { Assert.isNotNull(cu); Assert.isTrue(offset >= 0); Assert.isTrue(length >= 0); if (!ActionUtil.isEditable(fEditor, getShell(), cu)) return; CompilationUnit node= RefactoringASTParser.parseWithASTProvider(cu, true, null); if (!RefactoringExecutionStarter.startInlineConstantRefactoring(cu, node, offset, length, getShell())) { MessageDialog.openInformation(getShell(), RefactoringMessages.InlineConstantAction_dialog_title, RefactoringMessages.InlineConstantAction_no_constant_reference_or_declaration); } }
Example 9
Source File: DeleteChangeCreator.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private static Change createDeleteChange(ICompilationUnit cu, List<IJavaElement> javaElements, TextChangeManager manager) throws CoreException { CompilationUnit cuNode= RefactoringASTParser.parseWithASTProvider(cu, false, null); CompilationUnitRewrite rewriter= new CompilationUnitRewrite(cu, cuNode); IJavaElement[] elements= javaElements.toArray(new IJavaElement[javaElements.size()]); ASTNodeDeleteUtil.markAsDeleted(elements, rewriter, null); return addTextEditFromRewrite(manager, cu, rewriter.getASTRewrite()); }
Example 10
Source File: ExtractConstantRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
@Override public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException { try { pm.beginTask("", 7); //$NON-NLS-1$ RefactoringStatus result= Checks.validateEdit(fCu, getValidationContext()); if (result.hasFatalError()) return result; pm.worked(1); if (fCuRewrite == null) { CompilationUnit cuNode= RefactoringASTParser.parseWithASTProvider(fCu, true, new SubProgressMonitor(pm, 3)); fCuRewrite= new CompilationUnitRewrite(fCu, cuNode); } else { pm.worked(3); } result.merge(checkSelection(new SubProgressMonitor(pm, 3))); if (result.hasFatalError()) return result; if (isLiteralNodeSelected()) fReplaceAllOccurrences= false; if (isInTypeDeclarationAnnotation(getSelectedExpression().getAssociatedNode())) { fVisibility= JdtFlags.VISIBILITY_STRING_PACKAGE; } ITypeBinding targetType= getContainingTypeBinding(); if (targetType.isInterface()) { fTargetIsInterface= true; fVisibility= JdtFlags.VISIBILITY_STRING_PUBLIC; } return result; } finally { pm.done(); } }
Example 11
Source File: ExtractMethodRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Checks if the refactoring can be activated. Activation typically means, if a * corresponding menu entry can be added to the UI. * * @param pm a progress monitor to report progress during activation checking. * @return the refactoring status describing the result of the activation check. * @throws CoreException if checking fails */ @Override public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException { RefactoringStatus result= new RefactoringStatus(); pm.beginTask("", 100); //$NON-NLS-1$ if (fSelectionStart < 0 || fSelectionLength == 0) return mergeTextSelectionStatus(result); IFile[] changedFiles= ResourceUtil.getFiles(new ICompilationUnit[]{fCUnit}); result.merge(Checks.validateModifiesFiles(changedFiles, getValidationContext())); if (result.hasFatalError()) return result; result.merge(ResourceChangeChecker.checkFilesToBeChanged(changedFiles, new SubProgressMonitor(pm, 1))); if (fRoot == null) { fRoot= RefactoringASTParser.parseWithASTProvider(fCUnit, true, new SubProgressMonitor(pm, 99)); } fImportRewriter= StubUtility.createImportRewrite(fRoot, true); fAST= fRoot.getAST(); fRoot.accept(createVisitor()); fSelectionStart= fAnalyzer.getSelection().getOffset(); fSelectionLength= fAnalyzer.getSelection().getLength(); result.merge(fAnalyzer.checkInitialConditions(fImportRewriter)); if (result.hasFatalError()) return result; if (fVisibility == -1) { setVisibility(Modifier.PRIVATE); } initializeParameterInfos(); initializeUsedNames(); initializeDuplicates(); initializeDestinations(); return result; }
Example 12
Source File: RenameLocalVariableProcessor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
private void initAST() { if (!fIsComposite) { fCompilationUnitNode= RefactoringASTParser.parseWithASTProvider(fCu, true, null); } ISourceRange sourceRange= fLocalVariable.getNameRange(); ASTNode name= NodeFinder.perform(fCompilationUnitNode, sourceRange); if (name == null) { return; } if (name.getParent() instanceof VariableDeclaration) { fTempDeclarationNode= (VariableDeclaration) name.getParent(); } }
Example 13
Source File: RenameLocalVariableProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private void initAST() { if (!fIsComposite) fCompilationUnitNode= RefactoringASTParser.parseWithASTProvider(fCu, true, null); ISourceRange sourceRange= fLocalVariable.getNameRange(); ASTNode name= NodeFinder.perform(fCompilationUnitNode, sourceRange); if (name == null) return; if (name.getParent() instanceof VariableDeclaration) fTempDeclarationNode= (VariableDeclaration) name.getParent(); }
Example 14
Source File: HierarchyProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
protected RefactoringStatus checkConstructorCalls(final IType type, final IProgressMonitor monitor) throws JavaModelException { try { monitor.beginTask(RefactoringCoreMessages.PullUpRefactoring_checking, 2); final RefactoringStatus result= new RefactoringStatus(); final SearchResultGroup[] groups= ConstructorReferenceFinder.getConstructorReferences(type, fOwner, new SubProgressMonitor(monitor, 1), result); final String message= Messages.format(RefactoringCoreMessages.HierarchyRefactoring_gets_instantiated, new Object[] { JavaElementLabels.getTextLabel(type, JavaElementLabels.ALL_FULLY_QUALIFIED)}); ICompilationUnit unit= null; for (int index= 0; index < groups.length; index++) { unit= groups[index].getCompilationUnit(); if (unit != null) { final CompilationUnit cuNode= RefactoringASTParser.parseWithASTProvider(unit, false, new SubProgressMonitor(monitor, 1)); final ASTNode[] references= ASTNodeSearchUtil.getAstNodes(groups[index].getSearchResults(), cuNode); ASTNode node= null; for (int offset= 0; offset < references.length; offset++) { node= references[offset]; if ((node instanceof ClassInstanceCreation) || ConstructorReferenceFinder.isImplicitConstructorReferenceNodeInClassCreations(node)) { final RefactoringStatusContext context= JavaStatusContext.create(unit, node); result.addError(message, context); } } } } return result; } finally { monitor.done(); } }
Example 15
Source File: DeleteChangeCreator.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
private static Change createDeleteChange(ICompilationUnit cu, List<IJavaElement> javaElements, TextChangeManager manager) throws CoreException { CompilationUnit cuNode= RefactoringASTParser.parseWithASTProvider(cu, false, null); CompilationUnitRewrite rewriter= new CompilationUnitRewrite(cu, cuNode); IJavaElement[] elements= javaElements.toArray(new IJavaElement[javaElements.size()]); ASTNodeDeleteUtil.markAsDeleted(elements, rewriter, null); return addTextEditFromRewrite(manager, cu, rewriter.getASTRewrite()); }
Example 16
Source File: RenameTypeParameterProcessor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
/** * Creates the necessary changes for the renaming of the type parameter. * * @param monitor * the progress monitor to display progress * @return the status of the operation * @throws CoreException * if the change could not be generated */ private RefactoringStatus createRenameChanges(IProgressMonitor monitor) throws CoreException { Assert.isNotNull(monitor); RefactoringStatus status= new RefactoringStatus(); try { monitor.beginTask(RefactoringCoreMessages.RenameTypeParameterRefactoring_searching, 2); ICompilationUnit cu= fTypeParameter.getDeclaringMember().getCompilationUnit(); CompilationUnit root= RefactoringASTParser.parseWithASTProvider(cu, true, null); CompilationUnitRewrite rewrite= new CompilationUnitRewrite(cu, root); IMember member= fTypeParameter.getDeclaringMember(); ASTNode declaration= null; if (member instanceof IMethod) { declaration= ASTNodeSearchUtil.getMethodDeclarationNode((IMethod) member, root); } else if (member instanceof IType) { declaration= ASTNodeSearchUtil.getAbstractTypeDeclarationNode((IType) member, root); } else { JavaLanguageServerPlugin.logError("Unexpected sub-type of IMember: " + member.getClass().getName()); //$NON-NLS-1$ Assert.isTrue(false); } monitor.worked(1); RenameTypeParameterVisitor visitor= new RenameTypeParameterVisitor(rewrite, fTypeParameter.getNameRange(), status); if (declaration != null) { declaration.accept(visitor); } fChange= visitor.getResult(); } finally { monitor.done(); } return status; }
Example 17
Source File: InlineTempRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private CompilationUnit getASTRoot() { if (fASTRoot == null) { fASTRoot= RefactoringASTParser.parseWithASTProvider(fCu, true, null); } return fASTRoot; }
Example 18
Source File: ExtractConstantRefactoring.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
@Override public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException { try { pm.beginTask("", 7); //$NON-NLS-1$ RefactoringStatus result = Checks.validateEdit(fCu, getValidationContext(), pm); if (result.hasFatalError()) { return result; } pm.worked(1); if (fCuRewrite == null) { CompilationUnit cuNode = RefactoringASTParser.parseWithASTProvider(fCu, true, new SubProgressMonitor(pm, 3)); fCuRewrite = new CompilationUnitRewrite(null, fCu, cuNode, fFormatterOptions); } else { pm.worked(3); } result.merge(checkSelection(new SubProgressMonitor(pm, 3))); if (result.hasFatalError()) { return result; } if (isLiteralNodeSelected()) { fReplaceAllOccurrences = false; } if (isInTypeDeclarationAnnotation(getSelectedExpression().getAssociatedNode())) { fVisibility = JdtFlags.VISIBILITY_STRING_PACKAGE; } ITypeBinding targetType = getContainingTypeBinding(); if (targetType.isInterface()) { fTargetIsInterface = true; fVisibility = JdtFlags.VISIBILITY_STRING_PUBLIC; } return result; } finally { pm.done(); } }
Example 19
Source File: ExtractMethodRefactoring.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
/** * Checks if the refactoring can be activated. Activation typically means, * if a corresponding menu entry can be added to the UI. * * @param pm * a progress monitor to report progress during activation * checking. * @return the refactoring status describing the result of the activation * check. * @throws CoreException * if checking fails */ @Override public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException { RefactoringStatus result = new RefactoringStatus(); pm.beginTask("", 100); //$NON-NLS-1$ if (fSelectionStart < 0 || fSelectionLength == 0) { return mergeTextSelectionStatus(result); } IFile[] changedFiles = ResourceUtil.getFiles(new ICompilationUnit[] { fCUnit }); result.merge(Checks.validateModifiesFiles(changedFiles, getValidationContext(), pm)); if (result.hasFatalError()) { return result; } result.merge(ResourceChangeChecker.checkFilesToBeChanged(changedFiles, new SubProgressMonitor(pm, 1))); if (fRoot == null) { fRoot = RefactoringASTParser.parseWithASTProvider(fCUnit, true, new SubProgressMonitor(pm, 99)); } fImportRewriter = CodeStyleConfiguration.createImportRewrite(fRoot, true); fAST = fRoot.getAST(); fRoot.accept(createVisitor()); fSelectionStart = fAnalyzer.getSelection().getOffset(); fSelectionLength = fAnalyzer.getSelection().getLength(); result.merge(fAnalyzer.checkInitialConditions(fImportRewriter)); if (result.hasFatalError()) { return result; } if (fVisibility == -1) { setVisibility(Modifier.PRIVATE); } initializeParameterInfos(); initializeUsedNames(); initializeDuplicates(); initializeDestinations(); return result; }
Example 20
Source File: ExtractFieldRefactoring.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
@Override public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException { try { pm.beginTask("", 16); //$NON-NLS-1$ RefactoringStatus result = Checks.validateModifiesFiles(ResourceUtil.getFiles(new ICompilationUnit[] { fCu }), getValidationContext(), pm); if (result.hasFatalError()) { return result; } if (fCompilationUnitNode == null) { fCompilationUnitNode = RefactoringASTParser.parseWithASTProvider(fCu, true, new SubProgressMonitor(pm, 3)); } pm.worked(1); if (fCURewrite == null) { fCURewrite = new CompilationUnitRewrite(fCu, fCompilationUnitNode); fCURewrite.setFormattingOptions(fFormatterOptions); fCURewrite.getASTRewrite().setTargetSourceRangeComputer(new NoCommentSourceRangeComputer()); } pm.worked(1); // Check the conditions for extracting an expression to a variable. IExpressionFragment selectedExpression = getSelectedExpression(); if (selectedExpression == null) { String message = RefactoringCoreMessages.ExtractTempRefactoring_select_expression; return CodeRefactoringUtil.checkMethodSyntaxErrors(fSelectionStart, fSelectionLength, fCompilationUnitNode, message); } pm.worked(1); if (isUsedInExplicitConstructorCall()) { return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_explicit_constructor); } pm.worked(1); ASTNode associatedNode = selectedExpression.getAssociatedNode(); if (getEnclosingBodyNode() == null || ASTNodes.getParent(associatedNode, Annotation.class) != null) { return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_expr_in_method_or_initializer); } pm.worked(1); if (associatedNode instanceof Name && associatedNode.getParent() instanceof ClassInstanceCreation && associatedNode.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) { return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_name_in_new); } pm.worked(1); result.merge(checkExpression()); if (result.hasFatalError()) { return result; } pm.worked(1); result.merge(checkExpressionFragmentIsRValue()); if (result.hasFatalError()) { return result; } pm.worked(1); Expression associatedExpression = selectedExpression.getAssociatedExpression(); if (isUsedInForInitializerOrUpdater(associatedExpression)) { return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_for_initializer_updater); } pm.worked(1); if (isReferringToLocalVariableFromFor(associatedExpression)) { return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_refers_to_for_variable); } pm.worked(1); // Check the conditions for extracting an expression to field. ASTNode declaringType = getEnclosingTypeDeclaration(); if (declaringType instanceof TypeDeclaration && ((TypeDeclaration) declaringType).isInterface()) { return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractFieldRefactoring_interface_methods); } pm.worked(1); result.merge(checkTempTypeForLocalTypeUsage()); if (result.hasFatalError()) { return result; } pm.worked(1); checkTempInitializerForLocalTypeUsage(); initializeDefaults(); pm.worked(1); return result; } finally { pm.done(); } }