Java Code Examples for org.eclipse.jdt.core.dom.rewrite.ImportRewrite#hasRecordedChanges()

The following examples show how to use org.eclipse.jdt.core.dom.rewrite.ImportRewrite#hasRecordedChanges() . 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: RenamePackageProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public void rewriteImports(TextChangeManager changeManager, IProgressMonitor pm) throws CoreException {
	for (Iterator<Entry<ICompilationUnit, ImportChange>> iter= fImportChanges.entrySet().iterator(); iter.hasNext();) {
		Entry<ICompilationUnit, ImportChange> entry= iter.next();
		ICompilationUnit cu= entry.getKey();
		ImportChange importChange= entry.getValue();

		ImportRewrite importRewrite= StubUtility.createImportRewrite(cu, true);
		importRewrite.setFilterImplicitImports(false);
		for (Iterator<String> iterator= importChange.fStaticToRemove.iterator(); iterator.hasNext();) {
			importRewrite.removeStaticImport(iterator.next());
		}
		for (Iterator<String> iterator= importChange.fToRemove.iterator(); iterator.hasNext();) {
			importRewrite.removeImport(iterator.next());
		}
		for (Iterator<String[]> iterator= importChange.fStaticToAdd.iterator(); iterator.hasNext();) {
			String[] toAdd= iterator.next();
			importRewrite.addStaticImport(toAdd[0], toAdd[1], true);
		}
		for (Iterator<String> iterator= importChange.fToAdd.iterator(); iterator.hasNext();) {
			importRewrite.addImport(iterator.next());
		}

		if (importRewrite.hasRecordedChanges()) {
			TextEdit importEdit= importRewrite.rewriteImports(pm);
			String name= RefactoringCoreMessages.RenamePackageRefactoring_update_imports;
			try {
				TextChangeCompatibility.addTextEdit(changeManager.get(cu), name, importEdit);
			} catch (MalformedTreeException e) {
				JavaLanguageServerPlugin.logError("MalformedTreeException while processing cu " + cu); //$NON-NLS-1$
				throw e;
			}
		}
	}
}
 
Example 2
Source File: MoveCuUpdateCreator.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void addImportRewriteUpdates(TextChangeManager changeManager) throws CoreException {
	for (Iterator<ICompilationUnit> iter = fImportRewrites.keySet().iterator(); iter.hasNext();) {
		ICompilationUnit cu = iter.next();
		ImportRewrite importRewrite = fImportRewrites.get(cu);
		if (importRewrite != null && importRewrite.hasRecordedChanges()) {
			TextChangeCompatibility.addTextEdit(changeManager.get(cu), RefactoringCoreMessages.MoveCuUpdateCreator_update_imports, importRewrite.rewriteImports(null));
		}
	}
}
 
Example 3
Source File: RenamePackageProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public void rewriteImports(TextChangeManager changeManager, IProgressMonitor pm) throws CoreException {
	for (Iterator<Entry<ICompilationUnit, ImportChange>> iter= fImportChanges.entrySet().iterator(); iter.hasNext();) {
		Entry<ICompilationUnit, ImportChange> entry= iter.next();
		ICompilationUnit cu= entry.getKey();
		ImportChange importChange= entry.getValue();

		ImportRewrite importRewrite= StubUtility.createImportRewrite(cu, true);
		importRewrite.setFilterImplicitImports(false);
		for (Iterator<String> iterator= importChange.fStaticToRemove.iterator(); iterator.hasNext();) {
			importRewrite.removeStaticImport(iterator.next());
		}
		for (Iterator<String> iterator= importChange.fToRemove.iterator(); iterator.hasNext();) {
			importRewrite.removeImport(iterator.next());
		}
		for (Iterator<String[]> iterator= importChange.fStaticToAdd.iterator(); iterator.hasNext();) {
			String[] toAdd= iterator.next();
			importRewrite.addStaticImport(toAdd[0], toAdd[1], true);
		}
		for (Iterator<String> iterator= importChange.fToAdd.iterator(); iterator.hasNext();) {
			importRewrite.addImport(iterator.next());
		}

		if (importRewrite.hasRecordedChanges()) {
			TextEdit importEdit= importRewrite.rewriteImports(pm);
			String name= RefactoringCoreMessages.RenamePackageRefactoring_update_imports;
			try {
				TextChangeCompatibility.addTextEdit(changeManager.get(cu), name, importEdit);
			} catch (MalformedTreeException e) {
				JavaPlugin.logErrorMessage("MalformedTreeException while processing cu " + cu); //$NON-NLS-1$
				throw e;
			}
		}
	}
}
 
Example 4
Source File: MoveCuUpdateCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void addImportRewriteUpdates(TextChangeManager changeManager) throws CoreException {
	for (Iterator<ICompilationUnit> iter= fImportRewrites.keySet().iterator(); iter.hasNext();) {
		ICompilationUnit cu= iter.next();
		ImportRewrite importRewrite= fImportRewrites.get(cu);
		if (importRewrite != null && importRewrite.hasRecordedChanges()) {
			TextChangeCompatibility.addTextEdit(changeManager.get(cu), RefactoringCoreMessages.MoveCuUpdateCreator_update_imports, importRewrite.rewriteImports(null));
		}
	}
}
 
Example 5
Source File: InlineMethodRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException {
	pm.beginTask("", 20); //$NON-NLS-1$
	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);
	sub.beginTask("", units.length * 3); //$NON-NLS-1$
	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;
					}
				}
				// do this after we have inlined the method calls. We still want
				// 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;
}
 
Example 6
Source File: ReplaceInvocationsRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException {
	pm.beginTask("", 20); //$NON-NLS-1$
	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);
	sub.beginTask("", units.length * 3); //$NON-NLS-1$
	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;
}