use of org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager in project che by eclipse.
the class CreateCopyOfCompilationUnitChange method getCopiedFileSource.
private static String getCopiedFileSource(IProgressMonitor monitor, ICompilationUnit unit, String newTypeName) throws CoreException {
ICompilationUnit copy = unit.getPrimary().getWorkingCopy(null);
try {
TextChangeManager manager = createChangeManager(monitor, copy, newTypeName);
String result = manager.get(copy).getPreviewContent(new NullProgressMonitor());
return result;
} finally {
copy.discardWorkingCopy();
}
}
use of org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager in project che by eclipse.
the class CreateCopyOfCompilationUnitChange method createChangeManager.
private static TextChangeManager createChangeManager(IProgressMonitor monitor, ICompilationUnit copy, String newName) throws CoreException {
TextChangeManager manager = new TextChangeManager();
SearchResultGroup refs = getReferences(copy, monitor);
if (refs == null)
return manager;
if (refs.getCompilationUnit() == null)
return manager;
String name = RefactoringCoreMessages.CopyRefactoring_update_ref;
SearchMatch[] results = refs.getSearchResults();
for (int j = 0; j < results.length; j++) {
SearchMatch searchResult = results[j];
if (searchResult.getAccuracy() == SearchMatch.A_INACCURATE)
continue;
int offset = searchResult.getOffset();
int length = searchResult.getLength();
TextChangeCompatibility.addTextEdit(manager.get(copy), name, new ReplaceEdit(offset, length, newName));
}
return manager;
}
use of org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager in project che by eclipse.
the class ChangeSignatureProcessor method createChangeManager.
private TextChangeManager createChangeManager(IProgressMonitor pm, RefactoringStatus result) throws CoreException {
pm.beginTask(RefactoringCoreMessages.ChangeSignatureRefactoring_preview, 2);
fChangeManager = new TextChangeManager();
boolean isNoArgConstructor = isNoArgConstructor();
Map<ICompilationUnit, Set<IType>> namedSubclassMapping = null;
if (isNoArgConstructor) {
//create only when needed;
namedSubclassMapping = createNamedSubclassMapping(new SubProgressMonitor(pm, 1));
} else {
pm.worked(1);
}
for (int i = 0; i < fOccurrences.length; i++) {
if (pm.isCanceled())
throw new OperationCanceledException();
SearchResultGroup group = fOccurrences[i];
ICompilationUnit cu = group.getCompilationUnit();
if (cu == null)
continue;
CompilationUnitRewrite cuRewrite;
if (cu.equals(getCu())) {
cuRewrite = fBaseCuRewrite;
} else {
cuRewrite = new CompilationUnitRewrite(cu);
cuRewrite.getASTRewrite().setTargetSourceRangeComputer(new TightSourceRangeComputer());
}
ASTNode[] nodes = ASTNodeSearchUtil.findNodes(group.getSearchResults(), cuRewrite.getRoot());
//IntroduceParameterObjectRefactoring needs to update declarations first:
List<OccurrenceUpdate<? extends ASTNode>> deferredUpdates = new ArrayList<OccurrenceUpdate<? extends ASTNode>>();
for (int j = 0; j < nodes.length; j++) {
OccurrenceUpdate<? extends ASTNode> update = createOccurrenceUpdate(nodes[j], cuRewrite, result);
if (update instanceof DeclarationUpdate) {
update.updateNode();
} else {
deferredUpdates.add(update);
}
}
for (Iterator<OccurrenceUpdate<? extends ASTNode>> iter = deferredUpdates.iterator(); iter.hasNext(); ) {
iter.next().updateNode();
}
if (isNoArgConstructor && namedSubclassMapping.containsKey(cu)) {
//only non-anonymous subclasses may have noArgConstructors to modify - see bug 43444
Set<IType> subtypes = namedSubclassMapping.get(cu);
for (Iterator<IType> iter = subtypes.iterator(); iter.hasNext(); ) {
IType subtype = iter.next();
AbstractTypeDeclaration subtypeNode = ASTNodeSearchUtil.getAbstractTypeDeclarationNode(subtype, cuRewrite.getRoot());
if (subtypeNode != null)
modifyImplicitCallsToNoArgConstructor(subtypeNode, cuRewrite);
}
}
TextChange change = cuRewrite.createChange(true);
if (change != null)
fChangeManager.manage(cu, change);
}
pm.done();
return fChangeManager;
}
use of org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager 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;
}
use of org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager in project che by eclipse.
the class RenamePackageProcessor method doCheckFinalConditions.
@Override
protected RefactoringStatus doCheckFinalConditions(IProgressMonitor pm, CheckConditionsContext context) throws CoreException {
try {
//$NON-NLS-1$
pm.beginTask("", 23 + (fUpdateQualifiedNames ? 10 : 0) + (fUpdateTextualMatches ? 10 : 0));
pm.setTaskName(RefactoringCoreMessages.RenamePackageRefactoring_checking);
RefactoringStatus result = new RefactoringStatus();
result.merge(checkNewElementName(getNewElementName()));
pm.worked(1);
result.merge(checkForMainAndNativeMethods());
pm.worked(2);
if (fPackage.isReadOnly()) {
String message = Messages.format(RefactoringCoreMessages.RenamePackageRefactoring_Packagered_only, getElementLabel(fPackage));
result.addFatalError(message);
} else if (Resources.isReadOnly(fPackage.getResource())) {
String message = Messages.format(RefactoringCoreMessages.RenamePackageRefactoring_resource_read_only, getElementLabel(fPackage));
result.addError(message);
}
result.merge(checkPackageName(getNewElementName()));
if (result.hasFatalError())
return result;
fChangeManager = new TextChangeManager();
fImportsManager = new ImportsManager();
SubProgressMonitor subPm = new SubProgressMonitor(pm, 16);
if (fRenameSubpackages) {
IPackageFragment[] allSubpackages = JavaElementUtil.getPackageAndSubpackages(fPackage);
//$NON-NLS-1$
subPm.beginTask("", allSubpackages.length);
for (int i = 0; i < allSubpackages.length; i++) {
new PackageRenamer(allSubpackages[i], this, fChangeManager, fImportsManager).doRename(new SubProgressMonitor(subPm, 1), result);
}
subPm.done();
} else {
new PackageRenamer(fPackage, this, fChangeManager, fImportsManager).doRename(subPm, result);
}
fImportsManager.rewriteImports(fChangeManager, new SubProgressMonitor(pm, 3));
if (fUpdateTextualMatches) {
pm.subTask(RefactoringCoreMessages.RenamePackageRefactoring_searching_text);
TextMatchUpdater.perform(new SubProgressMonitor(pm, 10), RefactoringScopeFactory.create(fPackage), this, fChangeManager, new SearchResultGroup[0]);
}
if (fUpdateQualifiedNames)
computeQualifiedNameMatches(new SubProgressMonitor(pm, 10));
return result;
} finally {
pm.done();
}
}
Aggregations