use of org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager in project che by eclipse.
the class RenameTypeProcessor method doCheckFinalConditions.
@Override
protected RefactoringStatus doCheckFinalConditions(IProgressMonitor pm, CheckConditionsContext context) throws CoreException {
//$NON-NLS-1$
Assert.isNotNull(fType, "type");
//$NON-NLS-1$
Assert.isNotNull(getNewElementName(), "newName");
RefactoringStatus result = new RefactoringStatus();
int referenceSearchTicks = fUpdateReferences || fUpdateSimilarElements ? 15 : 0;
int affectedCusTicks = fUpdateReferences || fUpdateSimilarElements ? 10 : 1;
int similarElementTicks = fUpdateSimilarElements ? 85 : 0;
int createChangeTicks = 5;
int qualifiedNamesTicks = fUpdateQualifiedNames ? 50 : 0;
try {
//$NON-NLS-1$
pm.beginTask("", 12 + referenceSearchTicks + affectedCusTicks + similarElementTicks + createChangeTicks + qualifiedNamesTicks);
pm.setTaskName(RefactoringCoreMessages.RenameTypeRefactoring_checking);
fChangeManager = new TextChangeManager(true);
result.merge(checkNewElementName(getNewElementName()));
if (result.hasFatalError())
return result;
result.merge(Checks.checkIfCuBroken(fType));
if (result.hasFatalError())
return result;
pm.worked(1);
result.merge(checkTypesInCompilationUnit());
pm.worked(1);
result.merge(checkForMethodsWithConstructorNames());
pm.worked(1);
result.merge(checkImportedTypes());
pm.worked(1);
if (Checks.isTopLevel(fType)) {
ICompilationUnit cu = fType.getCompilationUnit();
String newCUName = JavaModelUtil.getRenamedCUName(cu, getNewElementName());
if (!newCUName.equals(cu.getElementName()))
result.merge(Checks.checkCompilationUnitNewName(cu, getNewElementName()));
}
pm.worked(1);
if (isPrimaryType())
result.merge(checkNewPathValidity());
pm.worked(1);
result.merge(checkEnclosingTypes());
pm.worked(1);
result.merge(checkEnclosedTypes());
pm.worked(1);
result.merge(checkTypesInPackage());
pm.worked(1);
result.merge(checkTypesImportedInCu());
pm.worked(1);
result.merge(Checks.checkForMainAndNativeMethods(fType));
pm.worked(1);
// before doing any expensive analysis
if (result.hasFatalError())
return result;
result.merge(analyseEnclosedTypes());
pm.worked(1);
// before doing _the really_ expensive analysis
if (result.hasFatalError())
return result;
// Load references, including similarly named elements
if (fUpdateReferences || fUpdateSimilarElements) {
pm.setTaskName(RefactoringCoreMessages.RenameTypeRefactoring_searching);
result.merge(initializeReferences(new SubProgressMonitor(pm, referenceSearchTicks)));
} else {
fReferences = new SearchResultGroup[0];
}
pm.setTaskName(RefactoringCoreMessages.RenameTypeRefactoring_checking);
if (pm.isCanceled())
throw new OperationCanceledException();
if (fUpdateReferences || fUpdateSimilarElements) {
result.merge(analyzeAffectedCompilationUnits(new SubProgressMonitor(pm, affectedCusTicks)));
} else {
Checks.checkCompileErrorsInAffectedFile(result, fType.getResource());
pm.worked(affectedCusTicks);
}
if (result.hasFatalError())
return result;
if (fUpdateSimilarElements) {
result.merge(initializeSimilarElementsRenameProcessors(new SubProgressMonitor(pm, similarElementTicks), context));
if (result.hasFatalError())
return result;
}
createChanges(new SubProgressMonitor(pm, createChangeTicks));
if (fUpdateQualifiedNames)
computeQualifiedNameMatches(new SubProgressMonitor(pm, qualifiedNamesTicks));
return result;
} finally {
pm.done();
}
}
use of org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager in project che by eclipse.
the class RenameTypeProcessor method checkCUCompleteConditions.
private void checkCUCompleteConditions(final RefactoringStatus status, CompilationUnit currentResolvedCU, ICompilationUnit currentCU, List<RefactoringProcessor> processors) throws CoreException {
// check local variable conditions
List<RefactoringProcessor> locals = getProcessorsOfType(processors, RenameLocalVariableProcessor.class);
if (!locals.isEmpty()) {
RenameAnalyzeUtil.LocalAnalyzePackage[] analyzePackages = new RenameAnalyzeUtil.LocalAnalyzePackage[locals.size()];
TextChangeManager manager = new TextChangeManager();
int current = 0;
TextChange textChange = manager.get(currentCU);
textChange.setKeepPreviewEdits(true);
for (Iterator<RefactoringProcessor> iterator = locals.iterator(); iterator.hasNext(); ) {
RenameLocalVariableProcessor localProcessor = (RenameLocalVariableProcessor) iterator.next();
RenameAnalyzeUtil.LocalAnalyzePackage analyzePackage = localProcessor.getLocalAnalyzePackage();
analyzePackages[current] = analyzePackage;
for (int i = 0; i < analyzePackage.fOccurenceEdits.length; i++) {
//$NON-NLS-1$
TextChangeCompatibility.addTextEdit(textChange, "", analyzePackage.fOccurenceEdits[i], GroupCategorySet.NONE);
}
current++;
}
status.merge(RenameAnalyzeUtil.analyzeLocalRenames(analyzePackages, textChange, currentResolvedCU, false));
}
/*
* There is room for performance improvement here: One could move
* shadowing analyzes out of the field and method processors and perform
* it here, thus saving on working copy creation. Drawback is increased
* heap consumption.
*/
}
use of org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager in project che by eclipse.
the class InlineMethodRefactoring method checkFinalConditions.
@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException {
//$NON-NLS-1$
pm.beginTask("", 20);
fChangeManager = new TextChangeManager();
RefactoringStatus result = new RefactoringStatus();
fSourceProvider.initialize();
fTargetProvider.initialize();
pm.setTaskName(RefactoringCoreMessages.InlineMethodRefactoring_searching);
RefactoringStatus searchStatus = new RefactoringStatus();
String binaryRefsDescription = Messages.format(RefactoringCoreMessages.ReferencesInBinaryContext_ref_in_binaries_description, BasicElementLabels.getJavaElementName(fSourceProvider.getMethodName()));
ReferencesInBinaryContext binaryRefs = new ReferencesInBinaryContext(binaryRefsDescription);
ICompilationUnit[] units = fTargetProvider.getAffectedCompilationUnits(searchStatus, binaryRefs, new SubProgressMonitor(pm, 1));
binaryRefs.addErrorIfNecessary(searchStatus);
if (searchStatus.hasFatalError()) {
result.merge(searchStatus);
return result;
}
IFile[] filesToBeModified = getFilesToBeModified(units);
result.merge(Checks.validateModifiesFiles(filesToBeModified, getValidationContext()));
if (result.hasFatalError())
return result;
result.merge(ResourceChangeChecker.checkFilesToBeChanged(filesToBeModified, new SubProgressMonitor(pm, 1)));
checkOverridden(result, new SubProgressMonitor(pm, 4));
IProgressMonitor sub = new SubProgressMonitor(pm, 15);
//$NON-NLS-1$
sub.beginTask("", units.length * 3);
for (int c = 0; c < units.length; c++) {
ICompilationUnit unit = units[c];
sub.subTask(Messages.format(RefactoringCoreMessages.InlineMethodRefactoring_processing, BasicElementLabels.getFileName(unit)));
CallInliner inliner = null;
try {
boolean added = false;
MultiTextEdit root = new MultiTextEdit();
CompilationUnitChange change = (CompilationUnitChange) fChangeManager.get(unit);
change.setEdit(root);
BodyDeclaration[] bodies = fTargetProvider.getAffectedBodyDeclarations(unit, new SubProgressMonitor(pm, 1));
if (bodies.length == 0)
continue;
inliner = new CallInliner(unit, (CompilationUnit) bodies[0].getRoot(), fSourceProvider);
for (int b = 0; b < bodies.length; b++) {
BodyDeclaration body = bodies[b];
inliner.initialize(body);
RefactoringStatus nestedInvocations = new RefactoringStatus();
ASTNode[] invocations = removeNestedCalls(nestedInvocations, unit, fTargetProvider.getInvocations(body, new SubProgressMonitor(sub, 2)));
for (int i = 0; i < invocations.length; i++) {
ASTNode invocation = invocations[i];
result.merge(inliner.initialize(invocation, fTargetProvider.getStatusSeverity()));
if (result.hasFatalError())
break;
if (result.getSeverity() < fTargetProvider.getStatusSeverity()) {
added = true;
TextEditGroup group = new TextEditGroup(RefactoringCoreMessages.InlineMethodRefactoring_edit_inline);
change.addTextEditGroup(group);
result.merge(inliner.perform(group));
} else {
fDeleteSource = false;
}
}
// to generate the modifications.
if (!nestedInvocations.isOK()) {
result.merge(nestedInvocations);
fDeleteSource = false;
}
}
if (!added) {
fChangeManager.remove(unit);
} else {
root.addChild(inliner.getModifications());
ImportRewrite rewrite = inliner.getImportEdit();
if (rewrite.hasRecordedChanges()) {
TextEdit edit = rewrite.rewriteImports(null);
if (edit instanceof MultiTextEdit ? ((MultiTextEdit) edit).getChildrenSize() > 0 : true) {
root.addChild(edit);
change.addTextEditGroup(new TextEditGroup(RefactoringCoreMessages.InlineMethodRefactoring_edit_import, new TextEdit[] { edit }));
}
}
}
} finally {
if (inliner != null)
inliner.dispose();
}
sub.worked(1);
if (sub.isCanceled())
throw new OperationCanceledException();
}
result.merge(searchStatus);
sub.done();
pm.done();
return result;
}
use of org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager in project che by eclipse.
the class InferTypeArgumentsRefactoring method checkFinalConditions.
/*
* @see org.eclipse.ltk.core.refactoring.Refactoring#checkFinalConditions(org.eclipse.core.runtime.IProgressMonitor)
*/
@Override
public RefactoringStatus checkFinalConditions(final IProgressMonitor pm) throws CoreException, OperationCanceledException {
HashMap<IJavaProject, ArrayList<IJavaElement>> projectsToElements = getJavaElementsPerProject(fElements);
//$NON-NLS-1$
pm.beginTask("", projectsToElements.size() + 2);
final RefactoringStatus result = new RefactoringStatus();
try {
fTCModel = new InferTypeArgumentsTCModel();
final InferTypeArgumentsConstraintCreator unitCollector = new InferTypeArgumentsConstraintCreator(fTCModel, fAssumeCloneReturnsSameType);
for (Iterator<Entry<IJavaProject, ArrayList<IJavaElement>>> iter = projectsToElements.entrySet().iterator(); iter.hasNext(); ) {
Entry<IJavaProject, ArrayList<IJavaElement>> entry = iter.next();
IJavaProject project = entry.getKey();
ArrayList<IJavaElement> javaElementsList = entry.getValue();
IJavaElement[] javaElements = javaElementsList.toArray(new IJavaElement[javaElementsList.size()]);
List<ICompilationUnit> cus = Arrays.asList(JavaModelUtil.getAllCompilationUnits(javaElements));
int batchSize = 150;
int batches = ((cus.size() - 1) / batchSize) + 1;
SubProgressMonitor projectMonitor = new SubProgressMonitor(pm, 1);
//$NON-NLS-1$
projectMonitor.beginTask("", batches);
projectMonitor.setTaskName(RefactoringCoreMessages.InferTypeArgumentsRefactoring_building);
for (int i = 0; i < batches; i++) {
List<ICompilationUnit> batch = cus.subList(i * batchSize, Math.min(cus.size(), (i + 1) * batchSize));
ICompilationUnit[] batchCus = batch.toArray(new ICompilationUnit[batch.size()]);
final SubProgressMonitor batchMonitor = new SubProgressMonitor(projectMonitor, 1);
batchMonitor.subTask(RefactoringCoreMessages.InferTypeArgumentsRefactoring_calculating_dependencies);
ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
parser.setProject(project);
parser.setCompilerOptions(RefactoringASTParser.getCompilerOptions(project));
parser.setResolveBindings(true);
parser.createASTs(batchCus, new String[0], new ASTRequestor() {
@Override
public void acceptAST(final ICompilationUnit source, final CompilationUnit ast) {
batchMonitor.subTask(BasicElementLabels.getFileName(source));
SafeRunner.run(new ISafeRunnable() {
public void run() throws Exception {
IProblem[] problems = ast.getProblems();
for (int p = 0; p < problems.length; p++) {
if (problems[p].isError()) {
String cuName = JavaElementLabels.getElementLabel(source, JavaElementLabels.CU_QUALIFIED);
String msg = Messages.format(RefactoringCoreMessages.InferTypeArgumentsRefactoring_error_in_cu_skipped, new Object[] { cuName });
result.addError(msg, JavaStatusContext.create(source, SourceRangeFactory.create(problems[p])));
return;
}
}
ast.accept(unitCollector);
}
public void handleException(Throwable exception) {
String cuName = JavaElementLabels.getElementLabel(source, JavaElementLabels.CU_QUALIFIED);
String msg = Messages.format(RefactoringCoreMessages.InferTypeArgumentsRefactoring_internal_error, new Object[] { cuName });
JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, msg, null));
String msg2 = Messages.format(RefactoringCoreMessages.InferTypeArgumentsRefactoring_error_skipped, new Object[] { cuName });
result.addError(msg2, JavaStatusContext.create(source));
}
});
fTCModel.newCu();
}
@Override
public void acceptBinding(String bindingKey, IBinding binding) {
//do nothing
}
}, batchMonitor);
}
projectMonitor.done();
fTCModel.newCu();
}
// Display.getDefault().syncExec(new Runnable() {
// public void run() {
// MessageDialog.openInformation(Display.getCurrent().getActiveShell(), "Debugging...", "after constraint gen");
// }
// });
pm.setTaskName(RefactoringCoreMessages.InferTypeArgumentsRefactoring_solving);
InferTypeArgumentsConstraintsSolver solver = new InferTypeArgumentsConstraintsSolver(fTCModel);
InferTypeArgumentsUpdate updates = solver.solveConstraints(new SubProgressMonitor(pm, 1));
//free caches
solver = null;
fChangeManager = new TextChangeManager();
rewriteDeclarations(updates, new SubProgressMonitor(pm, 1));
IFile[] filesToModify = ResourceUtil.getFiles(fChangeManager.getAllCompilationUnits());
result.merge(Checks.validateModifiesFiles(filesToModify, getValidationContext()));
return result;
} finally {
pm.done();
clearGlobalState();
}
}
use of org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager in project che by eclipse.
the class ReplaceInvocationsRefactoring method checkFinalConditions.
@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException {
//$NON-NLS-1$
pm.beginTask("", 20);
fChangeManager = new TextChangeManager();
RefactoringStatus result = new RefactoringStatus();
fSourceProvider = resolveSourceProvider(fMethodBinding, result);
if (result.hasFatalError())
return result;
result.merge(fSourceProvider.checkActivation());
if (result.hasFatalError())
return result;
fSourceProvider.initialize();
fTargetProvider.initialize();
pm.setTaskName(RefactoringCoreMessages.InlineMethodRefactoring_searching);
RefactoringStatus searchStatus = new RefactoringStatus();
String binaryRefsDescription = Messages.format(RefactoringCoreMessages.ReferencesInBinaryContext_ref_in_binaries_description, BasicElementLabels.getJavaElementName(fSourceProvider.getMethodName()));
ReferencesInBinaryContext binaryRefs = new ReferencesInBinaryContext(binaryRefsDescription);
ICompilationUnit[] units = fTargetProvider.getAffectedCompilationUnits(searchStatus, binaryRefs, new SubProgressMonitor(pm, 1));
binaryRefs.addErrorIfNecessary(searchStatus);
if (searchStatus.hasFatalError()) {
result.merge(searchStatus);
return result;
}
IFile[] filesToBeModified = getFilesToBeModified(units);
result.merge(Checks.validateModifiesFiles(filesToBeModified, getValidationContext()));
if (result.hasFatalError())
return result;
result.merge(ResourceChangeChecker.checkFilesToBeChanged(filesToBeModified, new SubProgressMonitor(pm, 1)));
checkOverridden(result, new SubProgressMonitor(pm, 4));
IProgressMonitor sub = new SubProgressMonitor(pm, 15);
//$NON-NLS-1$
sub.beginTask("", units.length * 3);
for (int c = 0; c < units.length; c++) {
ICompilationUnit unit = units[c];
sub.subTask(Messages.format(RefactoringCoreMessages.InlineMethodRefactoring_processing, BasicElementLabels.getFileName(unit)));
CallInliner inliner = null;
try {
boolean added = false;
MultiTextEdit root = new MultiTextEdit();
CompilationUnitChange change = (CompilationUnitChange) fChangeManager.get(unit);
change.setEdit(root);
BodyDeclaration[] bodies = fTargetProvider.getAffectedBodyDeclarations(unit, new SubProgressMonitor(pm, 1));
if (bodies.length == 0)
continue;
inliner = new CallInliner(unit, (CompilationUnit) bodies[0].getRoot(), fSourceProvider);
for (int b = 0; b < bodies.length; b++) {
BodyDeclaration body = bodies[b];
inliner.initialize(body);
RefactoringStatus nestedInvocations = new RefactoringStatus();
ASTNode[] invocations = removeNestedCalls(nestedInvocations, unit, fTargetProvider.getInvocations(body, new SubProgressMonitor(sub, 2)));
for (int i = 0; i < invocations.length; i++) {
ASTNode invocation = invocations[i];
result.merge(inliner.initialize(invocation, fTargetProvider.getStatusSeverity()));
if (result.hasFatalError())
break;
if (result.getSeverity() < fTargetProvider.getStatusSeverity()) {
added = true;
TextEditGroup group = new TextEditGroup(RefactoringCoreMessages.InlineMethodRefactoring_edit_inline);
change.addTextEditGroup(group);
result.merge(inliner.perform(group));
}
}
// do this after we have inlined the method calls. We still want
// to generate the modifications.
result.merge(nestedInvocations);
}
if (!added) {
fChangeManager.remove(unit);
} else {
root.addChild(inliner.getModifications());
ImportRewrite rewrite = inliner.getImportEdit();
if (rewrite.hasRecordedChanges()) {
TextEdit edit = rewrite.rewriteImports(null);
if (edit instanceof MultiTextEdit ? ((MultiTextEdit) edit).getChildrenSize() > 0 : true) {
root.addChild(edit);
change.addTextEditGroup(new TextEditGroup(RefactoringCoreMessages.InlineMethodRefactoring_edit_import, new TextEdit[] { edit }));
}
}
}
} finally {
if (inliner != null)
inliner.dispose();
}
sub.worked(1);
if (sub.isCanceled())
throw new OperationCanceledException();
}
result.merge(searchStatus);
sub.done();
pm.done();
return result;
}
Aggregations