use of org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite in project che by eclipse.
the class ExtractConstantRefactoring method checkInitialConditions.
@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
try {
//$NON-NLS-1$
pm.beginTask("", 7);
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();
}
}
use of org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite in project che by eclipse.
the class DeleteChangeCreator method createDeleteChange.
/*
* List<IJavaElement> javaElements
*/
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());
}
use of org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite in project che by eclipse.
the class RenameTypeParameterProcessor method createRenameChanges.
/**
* 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 {
//$NON-NLS-1$
JavaPlugin.logErrorMessage("Unexpected sub-type of IMember: " + member.getClass().getName());
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;
}
use of org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite in project che by eclipse.
the class IntroduceIndirectionRefactoring method createChangeAndDiscardRewrite.
private void createChangeAndDiscardRewrite(ICompilationUnit compilationUnit) throws CoreException {
CompilationUnitRewrite rewrite = fRewrites.get(compilationUnit);
if (rewrite != null) {
fTextChangeManager.manage(compilationUnit, rewrite.createChange(true));
fRewrites.remove(compilationUnit);
}
}
use of org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite in project che by eclipse.
the class IntroduceIndirectionRefactoring method checkFinalConditions.
@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException {
RefactoringStatus result = new RefactoringStatus();
fTextChangeManager = new TextChangeManager();
fIntermediaryFirstParameterType = null;
fIntermediaryTypeBinding = null;
for (Iterator<CompilationUnitRewrite> iter = fRewrites.values().iterator(); iter.hasNext(); ) iter.next().clearASTAndImportRewrites();
int startupTicks = 5;
int hierarchyTicks = 5;
int visibilityTicks = 5;
int referenceTicks = fUpdateReferences ? 30 : 5;
int creationTicks = 5;
//$NON-NLS-1$
pm.beginTask("", startupTicks + hierarchyTicks + visibilityTicks + referenceTicks + creationTicks);
pm.setTaskName(RefactoringCoreMessages.IntroduceIndirectionRefactoring_checking_conditions);
result.merge(Checks.checkMethodName(fIntermediaryMethodName, fIntermediaryType));
if (result.hasFatalError())
return result;
if (fIntermediaryType == null)
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_cannot_run_without_intermediary_type);
// intermediary type is already non binary/non-enum
CompilationUnitRewrite imRewrite = getCachedCURewrite(fIntermediaryType.getCompilationUnit());
fIntermediaryTypeBinding = typeToBinding(fIntermediaryType, imRewrite.getRoot());
fAdjustor = new MemberVisibilityAdjustor(fIntermediaryType, fIntermediaryType);
fIntermediaryAdjustments = new HashMap<IMember, IncomingMemberVisibilityAdjustment>();
// check static method in non-static nested type
if (fIntermediaryTypeBinding.isNested() && !Modifier.isStatic(fIntermediaryTypeBinding.getModifiers()))
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_cannot_create_in_nested_nonstatic, JavaStatusContext.create(fIntermediaryType));
pm.worked(startupTicks);
if (pm.isCanceled())
throw new OperationCanceledException();
if (fUpdateReferences) {
//$NON-NLS-1$
pm.setTaskName(RefactoringCoreMessages.IntroduceIndirectionRefactoring_checking_conditions + " " + RefactoringCoreMessages.IntroduceIndirectionRefactoring_looking_for_references);
result.merge(updateReferences(new NoOverrideProgressMonitor(pm, referenceTicks)));
pm.setTaskName(RefactoringCoreMessages.IntroduceIndirectionRefactoring_checking_conditions);
} else {
// only update the declaration and/or a selected method invocation
if (fSelectionMethodInvocation != null) {
fIntermediaryFirstParameterType = getExpressionType(fSelectionMethodInvocation);
final IMember enclosing = getEnclosingInitialSelectionMember();
// create an edit for this particular call
result.merge(updateMethodInvocation(fSelectionMethodInvocation, enclosing, getCachedCURewrite(fSelectionCompilationUnit)));
if (!isRewriteKept(fSelectionCompilationUnit))
createChangeAndDiscardRewrite(fSelectionCompilationUnit);
// does call see the intermediary method?
// => increase visibility of the type of the intermediary method.
result.merge(adjustVisibility(fIntermediaryType, enclosing.getDeclaringType(), new NoOverrideProgressMonitor(pm, 0)));
}
pm.worked(referenceTicks);
}
if (pm.isCanceled())
throw new OperationCanceledException();
if (fIntermediaryFirstParameterType == null)
fIntermediaryFirstParameterType = fTargetMethodBinding.getDeclaringClass();
// The target type and method may have changed - update them
IType actualTargetType = (IType) fIntermediaryFirstParameterType.getJavaElement();
if (!fTargetMethod.getDeclaringType().equals(actualTargetType)) {
IMethod actualTargetMethod = new MethodOverrideTester(actualTargetType, actualTargetType.newSupertypeHierarchy(null)).findOverriddenMethodInHierarchy(actualTargetType, fTargetMethod);
fTargetMethod = actualTargetMethod;
fTargetMethodBinding = findMethodBindingInHierarchy(fIntermediaryFirstParameterType, actualTargetMethod);
Assert.isNotNull(fTargetMethodBinding);
}
result.merge(checkCanCreateIntermediaryMethod());
createIntermediaryMethod();
pm.worked(creationTicks);
//$NON-NLS-1$
pm.setTaskName(RefactoringCoreMessages.IntroduceIndirectionRefactoring_checking_conditions + " " + RefactoringCoreMessages.IntroduceIndirectionRefactoring_adjusting_visibility);
result.merge(updateTargetVisibility(new NoOverrideProgressMonitor(pm, 0)));
result.merge(updateIntermediaryVisibility(new NoOverrideProgressMonitor(pm, 0)));
pm.worked(visibilityTicks);
pm.setTaskName(RefactoringCoreMessages.IntroduceIndirectionRefactoring_checking_conditions);
createChangeAndDiscardRewrite(fIntermediaryType.getCompilationUnit());
result.merge(Checks.validateModifiesFiles(getAllFilesToModify(), getValidationContext()));
pm.done();
return result;
}
Aggregations