use of org.eclipse.ltk.core.refactoring.RefactoringStatus in project che by eclipse.
the class RenameTypeProcessor method addWarnings.
private RefactoringStatus addWarnings(final Set<Warning> warnings) {
RefactoringStatus status = new RefactoringStatus();
// Remove deleted ripple methods from user selection and add warnings
for (Iterator<Warning> iter = warnings.iterator(); iter.hasNext(); ) {
final Warning warning = iter.next();
final IMethod[] elements = warning.getRipple();
if (warning.isSelectionWarning()) {
String message = Messages.format(RefactoringCoreMessages.RenameTypeProcessor_deselected_method_is_overridden, new String[] { JavaElementLabels.getElementLabel(elements[0], JavaElementLabels.ALL_DEFAULT), JavaElementLabels.getElementLabel(elements[0].getDeclaringType(), JavaElementLabels.ALL_DEFAULT) });
status.addWarning(message);
}
if (warning.isNameWarning()) {
String message = Messages.format(RefactoringCoreMessages.RenameTypeProcessor_renamed_method_is_overridden, new String[] { JavaElementLabels.getElementLabel(elements[0], JavaElementLabels.ALL_DEFAULT), JavaElementLabels.getElementLabel(elements[0].getDeclaringType(), JavaElementLabels.ALL_DEFAULT) });
status.addWarning(message);
}
for (int i = 0; i < elements.length; i++) fPreloadedElementToSelection.put(elements[i], Boolean.FALSE);
}
return status;
}
use of org.eclipse.ltk.core.refactoring.RefactoringStatus in project che by eclipse.
the class RenameTypeProcessor method initializeReferences.
/**
* Initializes the references to the type and the similarly named elements. This
* method creates both the fReferences and the fPreloadedElementToName
* fields.
*
* May be called from the UI.
* @param monitor progress monitor
* @return initialization status
* @throws JavaModelException some fundamental error with the underlying model
* @throws OperationCanceledException if user canceled the task
*
*/
public RefactoringStatus initializeReferences(IProgressMonitor monitor) throws JavaModelException, OperationCanceledException {
Assert.isNotNull(fType);
Assert.isNotNull(getNewElementName());
if (fReferences != null && (getNewElementName().equals(fCachedNewName)) && (fCachedRenameSimilarElements == getUpdateSimilarDeclarations()) && (fCachedRenamingStrategy == fRenamingStrategy))
return fCachedRefactoringStatus;
fCachedNewName = getNewElementName();
fCachedRenameSimilarElements = fUpdateSimilarElements;
fCachedRenamingStrategy = fRenamingStrategy;
fCachedRefactoringStatus = new RefactoringStatus();
try {
SearchPattern pattern = SearchPattern.createPattern(fType, IJavaSearchConstants.REFERENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
String binaryRefsDescription = Messages.format(RefactoringCoreMessages.ReferencesInBinaryContext_ref_in_binaries_description, BasicElementLabels.getJavaElementName(fType.getElementName()));
ReferencesInBinaryContext binaryRefs = new ReferencesInBinaryContext(binaryRefsDescription);
fReferences = RefactoringSearchEngine.search(pattern, RefactoringScopeFactory.create(fType, true, false), new TypeOccurrenceCollector(fType, binaryRefs), monitor, fCachedRefactoringStatus);
binaryRefs.addErrorIfNecessary(fCachedRefactoringStatus);
fReferences = Checks.excludeCompilationUnits(fReferences, fCachedRefactoringStatus);
fPreloadedElementToName = new LinkedHashMap<IJavaElement, String>();
fPreloadedElementToSelection = new HashMap<IJavaElement, Boolean>();
final String unQualifiedTypeName = fType.getElementName();
//$NON-NLS-1$
monitor.beginTask("", fReferences.length);
if (getUpdateSimilarDeclarations()) {
RenamingNameSuggestor sugg = new RenamingNameSuggestor(fRenamingStrategy);
for (int i = 0; i < fReferences.length; i++) {
final ICompilationUnit cu = fReferences[i].getCompilationUnit();
if (cu == null)
continue;
final SearchMatch[] results = fReferences[i].getSearchResults();
for (int j = 0; j < results.length; j++) {
if (!(results[j] instanceof TypeReferenceMatch))
continue;
final TypeReferenceMatch match = (TypeReferenceMatch) results[j];
final List<IJavaElement> matches = new ArrayList<IJavaElement>();
if (match.getLocalElement() != null) {
if (match.getLocalElement() instanceof ILocalVariable) {
matches.add(match.getLocalElement());
}
// else don't update (e.g. match in type parameter, annotation, ...)
} else {
matches.add((IJavaElement) match.getElement());
}
final IJavaElement[] others = match.getOtherElements();
if (others != null)
matches.addAll(Arrays.asList(others));
for (Iterator<IJavaElement> iter = matches.iterator(); iter.hasNext(); ) {
final IJavaElement element = iter.next();
if (!(element instanceof IMethod) && !(element instanceof IField) && !(element instanceof ILocalVariable))
continue;
if (!isInDeclaredType(match.getOffset(), element))
continue;
if (element instanceof IField) {
final IField currentField = (IField) element;
final String newFieldName = sugg.suggestNewFieldName(currentField.getJavaProject(), currentField.getElementName(), Flags.isStatic(currentField.getFlags()), unQualifiedTypeName, getNewElementName());
if (newFieldName != null)
fPreloadedElementToName.put(currentField, newFieldName);
} else if (element instanceof IMethod) {
final IMethod currentMethod = (IMethod) element;
addMethodRename(unQualifiedTypeName, sugg, currentMethod);
} else if (element instanceof ILocalVariable) {
final ILocalVariable currentLocal = (ILocalVariable) element;
final boolean isParameter;
if (currentLocal.isParameter()) {
addMethodRename(unQualifiedTypeName, sugg, (IMethod) currentLocal.getParent());
isParameter = true;
} else
isParameter = false;
final String newLocalName = sugg.suggestNewLocalName(currentLocal.getJavaProject(), currentLocal.getElementName(), isParameter, unQualifiedTypeName, getNewElementName());
if (newLocalName != null)
fPreloadedElementToName.put(currentLocal, newLocalName);
}
}
}
if (monitor.isCanceled())
throw new OperationCanceledException();
}
}
for (Iterator<IJavaElement> iter = fPreloadedElementToName.keySet().iterator(); iter.hasNext(); ) {
IJavaElement element = iter.next();
fPreloadedElementToSelection.put(element, Boolean.TRUE);
}
fPreloadedElementToNameDefault = new LinkedHashMap<IJavaElement, String>(fPreloadedElementToName);
} catch (OperationCanceledException e) {
fReferences = null;
fPreloadedElementToName = null;
throw new OperationCanceledException();
}
return fCachedRefactoringStatus;
}
use of org.eclipse.ltk.core.refactoring.RefactoringStatus in project che by eclipse.
the class RenameTypeProcessor method analyseEnclosedTypes.
private RefactoringStatus analyseEnclosedTypes() throws CoreException {
final ISourceRange typeRange = fType.getSourceRange();
final RefactoringStatus result = new RefactoringStatus();
CompilationUnit cuNode = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(fType.getCompilationUnit(), false);
cuNode.accept(new ASTVisitor() {
@Override
public boolean visit(TypeDeclaration node) {
// enums and annotations can't be local
if (node.getStartPosition() <= typeRange.getOffset())
return true;
if (node.getStartPosition() > typeRange.getOffset() + typeRange.getLength())
return true;
if (getNewElementName().equals(node.getName().getIdentifier())) {
RefactoringStatusContext context = JavaStatusContext.create(fType.getCompilationUnit(), node);
String msg = null;
if (node.isLocalTypeDeclaration()) {
msg = Messages.format(RefactoringCoreMessages.RenameTypeRefactoring_local_type, new String[] { JavaElementUtil.createSignature(fType), getNewElementLabel() });
} else if (node.isMemberTypeDeclaration()) {
msg = Messages.format(RefactoringCoreMessages.RenameTypeRefactoring_member_type, new String[] { JavaElementUtil.createSignature(fType), getNewElementLabel() });
}
if (msg != null)
result.addError(msg, context);
}
MethodDeclaration[] methods = node.getMethods();
for (int i = 0; i < methods.length; i++) {
if (Modifier.isNative(methods[i].getModifiers())) {
RefactoringStatusContext context = JavaStatusContext.create(fType.getCompilationUnit(), methods[i]);
String msg = Messages.format(RefactoringCoreMessages.RenameTypeRefactoring_enclosed_type_native, BasicElementLabels.getJavaElementName(node.getName().getIdentifier()));
result.addWarning(msg, context);
}
}
return true;
}
});
return result;
}
use of org.eclipse.ltk.core.refactoring.RefactoringStatus in project che by eclipse.
the class RenameCompilationUnitProcessor method checkNewElementName.
public RefactoringStatus checkNewElementName(String newName) throws CoreException {
//$NON-NLS-1$
Assert.isNotNull(newName, "new name");
String typeName = removeFileNameExtension(newName);
RefactoringStatus result = Checks.checkCompilationUnitName(newName, fCu);
if (fWillRenameType)
result.merge(fRenameTypeProcessor.checkNewElementName(typeName));
if (Checks.isAlreadyNamed(fCu, newName))
result.addFatalError(RefactoringCoreMessages.RenameCompilationUnitRefactoring_same_name);
return result;
}
use of org.eclipse.ltk.core.refactoring.RefactoringStatus in project che by eclipse.
the class RenameCompilationUnitProcessor method doCheckFinalConditions.
@Override
protected RefactoringStatus doCheckFinalConditions(IProgressMonitor pm, CheckConditionsContext context) throws CoreException {
try {
if (fWillRenameType && (!fCu.isStructureKnown())) {
RefactoringStatus result1 = new RefactoringStatus();
RefactoringStatus result2 = new RefactoringStatus();
result2.merge(Checks.checkCompilationUnitNewName(fCu, removeFileNameExtension(getNewElementName())));
if (result2.hasFatalError())
result1.addError(Messages.format(RefactoringCoreMessages.RenameCompilationUnitRefactoring_not_parsed_1, BasicElementLabels.getFileName(fCu)));
else
result1.addError(Messages.format(RefactoringCoreMessages.RenameCompilationUnitRefactoring_not_parsed, BasicElementLabels.getFileName(fCu)));
result1.merge(result2);
}
if (fWillRenameType) {
return fRenameTypeProcessor.checkFinalConditions(pm, context);
} else {
return Checks.checkCompilationUnitNewName(fCu, removeFileNameExtension(getNewElementName()));
}
} finally {
pm.done();
}
}
Aggregations