use of org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup 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.SearchResultGroup in project che by eclipse.
the class MoveCuUpdateCreator method addReferenceUpdates.
private void addReferenceUpdates(TextChangeManager changeManager, ICompilationUnit movedUnit, IProgressMonitor pm, RefactoringStatus status) throws JavaModelException, CoreException {
List<ICompilationUnit> cuList = Arrays.asList(fCus);
SearchResultGroup[] references = getReferences(movedUnit, pm, status);
for (int i = 0; i < references.length; i++) {
SearchResultGroup searchResultGroup = references[i];
ICompilationUnit referencingCu = searchResultGroup.getCompilationUnit();
if (referencingCu == null)
continue;
boolean simpleReferencesNeedNewImport = simpleReferencesNeedNewImport(movedUnit, referencingCu, cuList);
SearchMatch[] results = searchResultGroup.getSearchResults();
for (int j = 0; j < results.length; j++) {
// TODO: should update type references with results from addImport
TypeReference reference = (TypeReference) results[j];
if (reference.isImportDeclaration()) {
ImportRewrite rewrite = getImportRewrite(referencingCu);
IImportDeclaration importDecl = (IImportDeclaration) SearchUtils.getEnclosingJavaElement(results[j]);
if (Flags.isStatic(importDecl.getFlags())) {
rewrite.removeStaticImport(importDecl.getElementName());
addStaticImport(movedUnit, importDecl, rewrite);
} else {
rewrite.removeImport(importDecl.getElementName());
rewrite.addImport(createStringForNewImport(movedUnit, importDecl));
}
} else if (reference.isQualified()) {
TextChange textChange = changeManager.get(referencingCu);
String changeName = RefactoringCoreMessages.MoveCuUpdateCreator_update_references;
TextEdit replaceEdit = new ReplaceEdit(reference.getOffset(), reference.getSimpleNameStart() - reference.getOffset(), fNewPackage);
TextChangeCompatibility.addTextEdit(textChange, changeName, replaceEdit);
} else if (simpleReferencesNeedNewImport) {
ImportRewrite importEdit = getImportRewrite(referencingCu);
String typeName = reference.getSimpleName();
importEdit.addImport(getQualifiedType(fDestination.getElementName(), typeName));
}
}
}
}
use of org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup in project che by eclipse.
the class MemberVisibilityAdjustor method adjustOutgoingVisibility.
/**
* Adjusts the visibilities of the outgoing references from the member represented by the specified search result groups.
*
* @param groups the search result groups representing the references
* @param monitor the progress monitor to us
* @throws JavaModelException if the visibility could not be determined
*/
private void adjustOutgoingVisibility(final SearchResultGroup[] groups, final IProgressMonitor monitor) throws JavaModelException {
try {
//$NON-NLS-1$
monitor.beginTask("", groups.length);
monitor.setTaskName(RefactoringCoreMessages.MemberVisibilityAdjustor_checking);
IJavaElement element = null;
SearchMatch[] matches = null;
SearchResultGroup group = null;
for (int index = 0; index < groups.length; index++) {
group = groups[index];
element = JavaCore.create(group.getResource());
if (element instanceof ICompilationUnit) {
matches = group.getSearchResults();
for (int offset = 0; offset < matches.length; offset++) adjustOutgoingVisibility(matches[offset], new SubProgressMonitor(monitor, 1));
}
// else if (element != null)
// fStatus.merge(RefactoringStatus.createStatus(fFailureSeverity, RefactoringCoreMessages.getFormattedString
// ("MemberVisibilityAdjustor.binary.outgoing.project", new String[] { element.getJavaProject().getElementName(), getLabel
// (fReferenced)}), null, null, RefactoringStatusEntry.NO_CODE, null)); //$NON-NLS-1$
// else if (group.getResource() != null)
// fStatus.merge(RefactoringStatus.createStatus(fFailureSeverity, RefactoringCoreMessages.getFormattedString
// ("MemberVisibilityAdjustor.binary.outgoing.resource", new String[] { group.getResource().getName(), getLabel
// (fReferenced)}), null, null, RefactoringStatusEntry.NO_CODE, null)); //$NON-NLS-1$
// TW: enable when bug 78387 is fixed
monitor.worked(1);
}
} finally {
monitor.done();
}
}
use of org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup in project che by eclipse.
the class MemberVisibilityAdjustor method adjustVisibility.
/**
* Adjusts the visibilities of the referenced and referencing elements.
*
* @param monitor the progress monitor to use
* @throws JavaModelException if an error occurs during search
*/
public final void adjustVisibility(final IProgressMonitor monitor) throws JavaModelException {
try {
//$NON-NLS-1$
monitor.beginTask("", 7);
monitor.setTaskName(RefactoringCoreMessages.MemberVisibilityAdjustor_checking);
final RefactoringSearchEngine2 engine = new RefactoringSearchEngine2(SearchPattern.createPattern(fReferenced, IJavaSearchConstants.REFERENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE));
engine.setScope(fScope);
engine.setStatus(fStatus);
engine.setOwner(fOwner);
if (fIncoming) {
// check calls to the referenced (moved) element, adjust element
// visibility if necessary.
engine.searchPattern(new SubProgressMonitor(monitor, 1));
adjustIncomingVisibility((SearchResultGroup[]) engine.getResults(), new SubProgressMonitor(monitor, 1));
engine.clearResults();
// of the type if they are accessed outside of the moved type
if (fReferenced instanceof IType) {
final IType type = (IType) fReferenced;
adjustMemberVisibility(type, new SubProgressMonitor(monitor, 1));
}
}
if (fOutgoing) {
/*
* Search for the types, fields, and methods which
* are called/acted upon inside the referenced element (the one
* to be moved) and assure that they are also visible from
* within the referencing element (the destination type (or
* package if move to new type)).
*/
engine.searchReferencedTypes(fReferenced, new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
engine.searchReferencedFields(fReferenced, new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
engine.searchReferencedMethods(fReferenced, new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
adjustOutgoingVisibility((SearchResultGroup[]) engine.getResults(), new SubProgressMonitor(monitor, 1));
}
} finally {
monitor.done();
}
}
use of org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup in project che by eclipse.
the class IntroduceFactoryRefactoring method collectAffectedUnits.
/**
* @param searchHits
* @return the set of compilation units that will be affected by this
* particular invocation of this refactoring. This in general includes
* the class containing the constructor in question, as well as all
* call sites to the constructor.
*/
private ICompilationUnit[] collectAffectedUnits(SearchResultGroup[] searchHits) {
Collection<ICompilationUnit> result = new ArrayList<ICompilationUnit>();
boolean hitInFactoryClass = false;
for (int i = 0; i < searchHits.length; i++) {
SearchResultGroup rg = searchHits[i];
ICompilationUnit icu = rg.getCompilationUnit();
result.add(icu);
if (icu.equals(fFactoryUnitHandle))
hitInFactoryClass = true;
}
if (!hitInFactoryClass)
result.add(fFactoryUnitHandle);
return result.toArray(new ICompilationUnit[result.size()]);
}
Aggregations