use of org.eclipse.jdt.core.IMethod in project che by eclipse.
the class RenameVirtualMethodProcessor method doCheckFinalConditions.
@Override
protected RefactoringStatus doCheckFinalConditions(IProgressMonitor pm, CheckConditionsContext checkContext) throws CoreException {
try {
//$NON-NLS-1$
pm.beginTask("", 9);
RefactoringStatus result = new RefactoringStatus();
result.merge(super.doCheckFinalConditions(new SubProgressMonitor(pm, 7), checkContext));
if (result.hasFatalError())
return result;
final IMethod method = getMethod();
final IType declaring = method.getDeclaringType();
final ITypeHierarchy hierarchy = getCachedHierarchy(declaring, new SubProgressMonitor(pm, 1));
final String name = getNewElementName();
if (declaring.isInterface()) {
if (isSpecialCase())
result.addError(RefactoringCoreMessages.RenameMethodInInterfaceRefactoring_special_case);
pm.worked(1);
IMethod[] relatedMethods = relatedTypeDeclaresMethodName(new SubProgressMonitor(pm, 1), method, name);
for (int i = 0; i < relatedMethods.length; i++) {
IMethod relatedMethod = relatedMethods[i];
RefactoringStatusContext context = JavaStatusContext.create(relatedMethod);
result.addError(RefactoringCoreMessages.RenameMethodInInterfaceRefactoring_already_defined, context);
}
} else {
if (classesDeclareOverridingNativeMethod(hierarchy.getAllSubtypes(declaring))) {
result.addError(Messages.format(RefactoringCoreMessages.RenameVirtualMethodRefactoring_requieres_renaming_native, //$NON-NLS-1$
new String[] { BasicElementLabels.getJavaElementName(method.getElementName()), "UnsatisfiedLinkError" }));
}
IMethod[] hierarchyMethods = hierarchyDeclaresMethodName(new SubProgressMonitor(pm, 1), hierarchy, method, name);
for (int i = 0; i < hierarchyMethods.length; i++) {
IMethod hierarchyMethod = hierarchyMethods[i];
RefactoringStatusContext context = JavaStatusContext.create(hierarchyMethod);
if (Checks.compareParamTypes(method.getParameterTypes(), hierarchyMethod.getParameterTypes())) {
result.addError(Messages.format(RefactoringCoreMessages.RenameVirtualMethodRefactoring_hierarchy_declares2, BasicElementLabels.getJavaElementName(name)), context);
} else {
result.addWarning(Messages.format(RefactoringCoreMessages.RenameVirtualMethodRefactoring_hierarchy_declares1, BasicElementLabels.getJavaElementName(name)), context);
}
}
}
fCachedHierarchy = null;
return result;
} finally {
pm.done();
}
}
use of org.eclipse.jdt.core.IMethod 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.jdt.core.IMethod 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.jdt.core.IMethod in project che by eclipse.
the class RenameTypeParameterProcessor method checkNewElementName.
public RefactoringStatus checkNewElementName(String name) throws CoreException {
Assert.isNotNull(name);
RefactoringStatus result = Checks.checkTypeParameterName(name, fTypeParameter);
if (Checks.startsWithLowerCase(name))
result.addWarning(RefactoringCoreMessages.RenameTypeParameterRefactoring_should_start_lowercase);
if (Checks.isAlreadyNamed(fTypeParameter, name))
result.addFatalError(RefactoringCoreMessages.RenameTypeParameterRefactoring_another_name);
IMember member = fTypeParameter.getDeclaringMember();
if (member instanceof IType) {
IType type = (IType) member;
if (type.getTypeParameter(name).exists())
result.addFatalError(RefactoringCoreMessages.RenameTypeParameterRefactoring_class_type_parameter_already_defined);
} else if (member instanceof IMethod) {
IMethod method = (IMethod) member;
if (method.getTypeParameter(name).exists())
result.addFatalError(RefactoringCoreMessages.RenameTypeParameterRefactoring_method_type_parameter_already_defined);
} else {
//$NON-NLS-1$
JavaPlugin.logErrorMessage("Unexpected sub-type of IMember: " + member.getClass().getName());
Assert.isTrue(false);
}
return result;
}
use of org.eclipse.jdt.core.IMethod in project che by eclipse.
the class MethodChecks method checkIfOverridesAnother.
public static RefactoringStatus checkIfOverridesAnother(IMethod method, ITypeHierarchy hierarchy) throws JavaModelException {
IMethod overrides = MethodChecks.overridesAnotherMethod(method, hierarchy);
if (overrides == null)
return null;
RefactoringStatusContext context = JavaStatusContext.create(overrides);
String message = Messages.format(RefactoringCoreMessages.MethodChecks_overrides, new String[] { JavaElementUtil.createMethodSignature(overrides), JavaElementLabels.getElementLabel(overrides.getDeclaringType(), JavaElementLabels.ALL_FULLY_QUALIFIED) });
return RefactoringStatus.createStatus(RefactoringStatus.FATAL, message, context, Corext.getPluginId(), RefactoringStatusCodes.OVERRIDES_ANOTHER_METHOD, overrides);
}
Aggregations