Search in sources :

Example 1 with ITypeHierarchy

use of org.eclipse.jdt.core.ITypeHierarchy in project che by eclipse.

the class RenameTypeProcessor method initializeSimilarElementsRenameProcessors.

// --------- Similar names
/**
	 * Creates and initializes the refactoring processors for similarly named elements
	 * @param progressMonitor progress monitor
	 * @param context context
	 * @return status
	 * @throws CoreException should not happen
	 */
private RefactoringStatus initializeSimilarElementsRenameProcessors(IProgressMonitor progressMonitor, CheckConditionsContext context) throws CoreException {
    Assert.isNotNull(fPreloadedElementToName);
    Assert.isNotNull(fPreloadedElementToSelection);
    final RefactoringStatus status = new RefactoringStatus();
    final Set<IMethod> handledTopLevelMethods = new HashSet<IMethod>();
    final Set<Warning> warnings = new HashSet<Warning>();
    final List<RefactoringProcessor> processors = new ArrayList<RefactoringProcessor>();
    fFinalSimilarElementToName = new HashMap<IJavaElement, String>();
    CompilationUnit currentResolvedCU = null;
    ICompilationUnit currentCU = null;
    int current = 0;
    final int max = fPreloadedElementToName.size();
    //$NON-NLS-1$
    progressMonitor.beginTask("", max * 3);
    progressMonitor.setTaskName(RefactoringCoreMessages.RenameTypeProcessor_checking_similarly_named_declarations_refactoring_conditions);
    for (Iterator<IJavaElement> iter = fPreloadedElementToName.keySet().iterator(); iter.hasNext(); ) {
        final IJavaElement element = iter.next();
        current++;
        progressMonitor.worked(3);
        // not selected? -> skip
        if (!(fPreloadedElementToSelection.get(element)).booleanValue())
            continue;
        // already registered? (may happen with overridden methods) -> skip
        if (fFinalSimilarElementToName.containsKey(element))
            continue;
        // CompilationUnit changed? (note: fPreloadedElementToName is sorted by CompilationUnit)
        ICompilationUnit newCU = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
        if (!newCU.equals(currentCU)) {
            checkCUCompleteConditions(status, currentResolvedCU, currentCU, processors);
            if (status.hasFatalError())
                return status;
            // reset values
            currentResolvedCU = null;
            currentCU = newCU;
            processors.clear();
        }
        final String newName = fPreloadedElementToName.get(element);
        RefactoringProcessor processor = null;
        if (element instanceof ILocalVariable) {
            final ILocalVariable currentLocal = (ILocalVariable) element;
            if (currentResolvedCU == null)
                currentResolvedCU = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(currentCU, true);
            processor = createLocalRenameProcessor(currentLocal, newName, currentResolvedCU);
            if (status.hasFatalError())
                return status;
            fFinalSimilarElementToName.put(currentLocal, newName);
        }
        if (element instanceof IField) {
            final IField currentField = (IField) element;
            processor = createFieldRenameProcessor(currentField, newName);
            status.merge(checkForConflictingRename(currentField, newName));
            if (status.hasFatalError())
                return status;
            fFinalSimilarElementToName.put(currentField, newName);
        }
        if (element instanceof IMethod) {
            IMethod currentMethod = (IMethod) element;
            if (MethodChecks.isVirtual(currentMethod)) {
                final IType declaringType = currentMethod.getDeclaringType();
                ITypeHierarchy hierarchy = null;
                if (!declaringType.isInterface())
                    hierarchy = declaringType.newTypeHierarchy(new NullProgressMonitor());
                final IMethod topmost = MethodChecks.getTopmostMethod(currentMethod, hierarchy, new NullProgressMonitor());
                if (topmost != null)
                    currentMethod = topmost;
                if (handledTopLevelMethods.contains(currentMethod))
                    continue;
                handledTopLevelMethods.add(currentMethod);
                final IMethod[] ripples = RippleMethodFinder2.getRelatedMethods(currentMethod, new NullProgressMonitor(), null);
                if (checkForWarnings(warnings, newName, ripples))
                    continue;
                status.merge(checkForConflictingRename(ripples, newName));
                if (status.hasFatalError())
                    return status;
                processor = createVirtualMethodRenameProcessor(currentMethod, newName, ripples, hierarchy);
                fFinalSimilarElementToName.put(currentMethod, newName);
                for (int i = 0; i < ripples.length; i++) {
                    fFinalSimilarElementToName.put(ripples[i], newName);
                }
            } else {
                status.merge(checkForConflictingRename(new IMethod[] { currentMethod }, newName));
                if (status.hasFatalError())
                    break;
                fFinalSimilarElementToName.put(currentMethod, newName);
                processor = createNonVirtualMethodRenameProcessor(currentMethod, newName);
            }
        }
        progressMonitor.subTask(Messages.format(RefactoringCoreMessages.RenameTypeProcessor_progress_current_total, new Object[] { String.valueOf(current), String.valueOf(max) }));
        status.merge(processor.checkInitialConditions(new NoOverrideProgressMonitor(progressMonitor, 1)));
        if (status.hasFatalError())
            return status;
        status.merge(processor.checkFinalConditions(new NoOverrideProgressMonitor(progressMonitor, 1), context));
        if (status.hasFatalError())
            return status;
        processors.add(processor);
        progressMonitor.worked(1);
        if (progressMonitor.isCanceled())
            throw new OperationCanceledException();
    }
    // check last CU
    checkCUCompleteConditions(status, currentResolvedCU, currentCU, processors);
    status.merge(addWarnings(warnings));
    progressMonitor.done();
    return status;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IType(org.eclipse.jdt.core.IType) ILocalVariable(org.eclipse.jdt.core.ILocalVariable) ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) IMethod(org.eclipse.jdt.core.IMethod) HashSet(java.util.HashSet) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IField(org.eclipse.jdt.core.IField) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) RefactoringProcessor(org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor)

Example 2 with ITypeHierarchy

use of org.eclipse.jdt.core.ITypeHierarchy in project che by eclipse.

the class RenameVirtualMethodProcessor method checkInitialConditions.

//------------ preconditions -------------
@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor monitor) throws CoreException {
    RefactoringStatus result = super.checkInitialConditions(monitor);
    if (result.hasFatalError())
        return result;
    try {
        //$NON-NLS-1$
        monitor.beginTask("", 3);
        if (!fActivationChecked) {
            // the following code may change the method to be changed.
            IMethod method = getMethod();
            fOriginalMethod = method;
            ITypeHierarchy hierarchy = null;
            IType declaringType = method.getDeclaringType();
            if (!declaringType.isInterface())
                hierarchy = getCachedHierarchy(declaringType, new SubProgressMonitor(monitor, 1));
            IMethod topmost = getMethod();
            if (MethodChecks.isVirtual(topmost))
                topmost = MethodChecks.getTopmostMethod(getMethod(), hierarchy, monitor);
            if (topmost != null)
                initialize(topmost);
            fActivationChecked = true;
        }
    } finally {
        monitor.done();
    }
    return result;
}
Also used : 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 3 with ITypeHierarchy

use of org.eclipse.jdt.core.ITypeHierarchy 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 4 with ITypeHierarchy

use of org.eclipse.jdt.core.ITypeHierarchy in project che by eclipse.

the class IndexSelector method getFocusedElementsAndTypes.

/*
 * Create the list of focused jars or projects.
 */
private static IJavaElement[] getFocusedElementsAndTypes(SearchPattern pattern, IJavaElement focusElement, ObjectVector superTypes) throws JavaModelException {
    if (pattern instanceof MethodPattern) {
        // For method pattern, it needs to walk along the focus type super hierarchy
        // and add jars/projects of all the encountered types.
        IType type = (IType) pattern.focus.getAncestor(IJavaElement.TYPE);
        MethodPattern methodPattern = (MethodPattern) pattern;
        String selector = new String(methodPattern.selector);
        int parameterCount = methodPattern.parameterCount;
        ITypeHierarchy superHierarchy = type.newSupertypeHierarchy(null);
        IType[] allTypes = superHierarchy.getAllSupertypes(type);
        int length = allTypes.length;
        SimpleSet focusSet = new SimpleSet(length + 1);
        if (focusElement != null)
            focusSet.add(focusElement);
        for (int i = 0; i < length; i++) {
            IMethod[] methods = allTypes[i].getMethods();
            int mLength = methods.length;
            for (int m = 0; m < mLength; m++) {
                if (parameterCount == methods[m].getNumberOfParameters() && methods[m].getElementName().equals(selector)) {
                    IPackageFragmentRoot root = (IPackageFragmentRoot) allTypes[i].getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
                    IJavaElement element = root.isArchive() ? root : root.getParent();
                    focusSet.add(element);
                    if (superTypes != null)
                        superTypes.add(allTypes[i]);
                    break;
                }
            }
        }
        // Rebuilt a contiguous array
        IJavaElement[] focuses = new IJavaElement[focusSet.elementSize];
        Object[] values = focusSet.values;
        int count = 0;
        for (int i = values.length; --i >= 0; ) {
            if (values[i] != null) {
                focuses[count++] = (IJavaElement) values[i];
            }
        }
        return focuses;
    }
    if (focusElement == null)
        return new IJavaElement[0];
    return new IJavaElement[] { focusElement };
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) SimpleSet(org.eclipse.jdt.internal.compiler.util.SimpleSet) MethodPattern(org.eclipse.jdt.internal.core.search.matching.MethodPattern) IType(org.eclipse.jdt.core.IType) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) IMethod(org.eclipse.jdt.core.IMethod)

Example 5 with ITypeHierarchy

use of org.eclipse.jdt.core.ITypeHierarchy in project che by eclipse.

the class IntroduceIndirectionRefactoring method updateTargetVisibility.

private RefactoringStatus updateTargetVisibility(IProgressMonitor monitor) throws JavaModelException, CoreException {
    RefactoringStatus result = new RefactoringStatus();
    // Adjust the visibility of the method and of the referenced type. Note that
    // the target method may not be in the target type; and in this case, the type
    // of the target method does not need a visibility adjustment.
    // This method is called after all other changes have been
    // created. Changes induced by this method will be attached to those changes.
    result.merge(adjustVisibility((IType) fIntermediaryFirstParameterType.getJavaElement(), fIntermediaryType, monitor));
    if (result.hasError())
        // binary
        return result;
    ModifierKeyword neededVisibility = getNeededVisibility(fTargetMethod, fIntermediaryType);
    if (neededVisibility != null) {
        result.merge(adjustVisibility(fTargetMethod, neededVisibility, monitor));
        if (result.hasError())
            // binary
            return result;
        // Need to adjust the overridden methods of the target method.
        ITypeHierarchy hierarchy = fTargetMethod.getDeclaringType().newTypeHierarchy(null);
        MethodOverrideTester tester = new MethodOverrideTester(fTargetMethod.getDeclaringType(), hierarchy);
        IType[] subtypes = hierarchy.getAllSubtypes(fTargetMethod.getDeclaringType());
        for (int i = 0; i < subtypes.length; i++) {
            IMethod method = tester.findOverridingMethodInType(subtypes[i], fTargetMethod);
            if (method != null && method.exists()) {
                result.merge(adjustVisibility(method, neededVisibility, monitor));
                if (monitor.isCanceled())
                    throw new OperationCanceledException();
                if (result.hasError())
                    // binary
                    return result;
            }
        }
    }
    return result;
}
Also used : ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) ModifierKeyword(org.eclipse.jdt.core.dom.Modifier.ModifierKeyword) MethodOverrideTester(org.eclipse.jdt.internal.corext.util.MethodOverrideTester) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IMethod(org.eclipse.jdt.core.IMethod) IType(org.eclipse.jdt.core.IType)

Aggregations

ITypeHierarchy (org.eclipse.jdt.core.ITypeHierarchy)36 IType (org.eclipse.jdt.core.IType)24 IMethod (org.eclipse.jdt.core.IMethod)14 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)11 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)7 ArrayList (java.util.ArrayList)6 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)5 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 ModifierKeyword (org.eclipse.jdt.core.dom.Modifier.ModifierKeyword)4 MethodOverrideTester (org.eclipse.jdt.internal.corext.util.MethodOverrideTester)4 Type (org.eclipse.che.ide.ext.java.shared.dto.model.Type)3 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 IJavaElement (org.eclipse.jdt.core.IJavaElement)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)2 RefactoringStatusContext (org.eclipse.ltk.core.refactoring.RefactoringStatusContext)2 HashMap (java.util.HashMap)1 List (java.util.List)1