Search in sources :

Example 21 with IMethod

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();
    }
}
Also used : RefactoringStatusContext(org.eclipse.ltk.core.refactoring.RefactoringStatusContext) ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IMethod(org.eclipse.jdt.core.IMethod) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IType(org.eclipse.jdt.core.IType)

Example 22 with IMethod

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;
}
Also used : RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IMethod(org.eclipse.jdt.core.IMethod)

Example 23 with IMethod

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;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IField(org.eclipse.jdt.core.IField) ILocalVariable(org.eclipse.jdt.core.ILocalVariable) ReferencesInBinaryContext(org.eclipse.jdt.internal.corext.refactoring.base.ReferencesInBinaryContext) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IMethod(org.eclipse.jdt.core.IMethod) TypeReferenceMatch(org.eclipse.jdt.core.search.TypeReferenceMatch)

Example 24 with IMethod

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;
}
Also used : RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IMethod(org.eclipse.jdt.core.IMethod) IMember(org.eclipse.jdt.core.IMember) IType(org.eclipse.jdt.core.IType)

Example 25 with IMethod

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);
}
Also used : RefactoringStatusContext(org.eclipse.ltk.core.refactoring.RefactoringStatusContext) IMethod(org.eclipse.jdt.core.IMethod)

Aggregations

IMethod (org.eclipse.jdt.core.IMethod)160 IType (org.eclipse.jdt.core.IType)87 IJavaElement (org.eclipse.jdt.core.IJavaElement)36 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)29 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)28 ArrayList (java.util.ArrayList)24 Test (org.junit.Test)22 IField (org.eclipse.jdt.core.IField)21 ITypeHierarchy (org.eclipse.jdt.core.ITypeHierarchy)14 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)13 IMember (org.eclipse.jdt.core.IMember)13 JavaModelException (org.eclipse.jdt.core.JavaModelException)12 RenameJavaElementDescriptor (org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)12 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)11 HashSet (java.util.HashSet)8 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)8 ILocalVariable (org.eclipse.jdt.core.ILocalVariable)7 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)7 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)7 RenameRefactoring (org.eclipse.ltk.core.refactoring.participants.RenameRefactoring)7