Search in sources :

Example 21 with IMember

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

the class IntroduceIndirectionRefactoring method checkFinalConditions.

@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException {
    RefactoringStatus result = new RefactoringStatus();
    fTextChangeManager = new TextChangeManager();
    fIntermediaryFirstParameterType = null;
    fIntermediaryTypeBinding = null;
    for (Iterator<CompilationUnitRewrite> iter = fRewrites.values().iterator(); iter.hasNext(); ) iter.next().clearASTAndImportRewrites();
    int startupTicks = 5;
    int hierarchyTicks = 5;
    int visibilityTicks = 5;
    int referenceTicks = fUpdateReferences ? 30 : 5;
    int creationTicks = 5;
    //$NON-NLS-1$
    pm.beginTask("", startupTicks + hierarchyTicks + visibilityTicks + referenceTicks + creationTicks);
    pm.setTaskName(RefactoringCoreMessages.IntroduceIndirectionRefactoring_checking_conditions);
    result.merge(Checks.checkMethodName(fIntermediaryMethodName, fIntermediaryType));
    if (result.hasFatalError())
        return result;
    if (fIntermediaryType == null)
        return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_cannot_run_without_intermediary_type);
    // intermediary type is already non binary/non-enum
    CompilationUnitRewrite imRewrite = getCachedCURewrite(fIntermediaryType.getCompilationUnit());
    fIntermediaryTypeBinding = typeToBinding(fIntermediaryType, imRewrite.getRoot());
    fAdjustor = new MemberVisibilityAdjustor(fIntermediaryType, fIntermediaryType);
    fIntermediaryAdjustments = new HashMap<IMember, IncomingMemberVisibilityAdjustment>();
    // check static method in non-static nested type
    if (fIntermediaryTypeBinding.isNested() && !Modifier.isStatic(fIntermediaryTypeBinding.getModifiers()))
        return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_cannot_create_in_nested_nonstatic, JavaStatusContext.create(fIntermediaryType));
    pm.worked(startupTicks);
    if (pm.isCanceled())
        throw new OperationCanceledException();
    if (fUpdateReferences) {
        //$NON-NLS-1$
        pm.setTaskName(RefactoringCoreMessages.IntroduceIndirectionRefactoring_checking_conditions + " " + RefactoringCoreMessages.IntroduceIndirectionRefactoring_looking_for_references);
        result.merge(updateReferences(new NoOverrideProgressMonitor(pm, referenceTicks)));
        pm.setTaskName(RefactoringCoreMessages.IntroduceIndirectionRefactoring_checking_conditions);
    } else {
        // only update the declaration and/or a selected method invocation
        if (fSelectionMethodInvocation != null) {
            fIntermediaryFirstParameterType = getExpressionType(fSelectionMethodInvocation);
            final IMember enclosing = getEnclosingInitialSelectionMember();
            // create an edit for this particular call
            result.merge(updateMethodInvocation(fSelectionMethodInvocation, enclosing, getCachedCURewrite(fSelectionCompilationUnit)));
            if (!isRewriteKept(fSelectionCompilationUnit))
                createChangeAndDiscardRewrite(fSelectionCompilationUnit);
            // does call see the intermediary method?
            // => increase visibility of the type of the intermediary method.
            result.merge(adjustVisibility(fIntermediaryType, enclosing.getDeclaringType(), new NoOverrideProgressMonitor(pm, 0)));
        }
        pm.worked(referenceTicks);
    }
    if (pm.isCanceled())
        throw new OperationCanceledException();
    if (fIntermediaryFirstParameterType == null)
        fIntermediaryFirstParameterType = fTargetMethodBinding.getDeclaringClass();
    // The target type and method may have changed - update them
    IType actualTargetType = (IType) fIntermediaryFirstParameterType.getJavaElement();
    if (!fTargetMethod.getDeclaringType().equals(actualTargetType)) {
        IMethod actualTargetMethod = new MethodOverrideTester(actualTargetType, actualTargetType.newSupertypeHierarchy(null)).findOverriddenMethodInHierarchy(actualTargetType, fTargetMethod);
        fTargetMethod = actualTargetMethod;
        fTargetMethodBinding = findMethodBindingInHierarchy(fIntermediaryFirstParameterType, actualTargetMethod);
        Assert.isNotNull(fTargetMethodBinding);
    }
    result.merge(checkCanCreateIntermediaryMethod());
    createIntermediaryMethod();
    pm.worked(creationTicks);
    //$NON-NLS-1$
    pm.setTaskName(RefactoringCoreMessages.IntroduceIndirectionRefactoring_checking_conditions + " " + RefactoringCoreMessages.IntroduceIndirectionRefactoring_adjusting_visibility);
    result.merge(updateTargetVisibility(new NoOverrideProgressMonitor(pm, 0)));
    result.merge(updateIntermediaryVisibility(new NoOverrideProgressMonitor(pm, 0)));
    pm.worked(visibilityTicks);
    pm.setTaskName(RefactoringCoreMessages.IntroduceIndirectionRefactoring_checking_conditions);
    createChangeAndDiscardRewrite(fIntermediaryType.getCompilationUnit());
    result.merge(Checks.validateModifiesFiles(getAllFilesToModify(), getValidationContext()));
    pm.done();
    return result;
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) IncomingMemberVisibilityAdjustment(org.eclipse.jdt.internal.corext.refactoring.structure.MemberVisibilityAdjustor.IncomingMemberVisibilityAdjustment) MethodOverrideTester(org.eclipse.jdt.internal.corext.util.MethodOverrideTester) MemberVisibilityAdjustor(org.eclipse.jdt.internal.corext.refactoring.structure.MemberVisibilityAdjustor) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IMember(org.eclipse.jdt.core.IMember) IType(org.eclipse.jdt.core.IType) IMethod(org.eclipse.jdt.core.IMethod) TextChangeManager(org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager)

Example 22 with IMember

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

the class StubUtility2 method getNodeToInsertBefore.

/**
	 * Evaluates the insertion position of a new node.
	 *
	 * @param listRewrite The list rewriter to which the new node will be added
	 * @param sibling The Java element before which the new element should be added.
	 * @return the AST node of the list to insert before or null to insert as last.
	 * @throws JavaModelException thrown if accessing the Java element failed
	 */
public static ASTNode getNodeToInsertBefore(ListRewrite listRewrite, IJavaElement sibling) throws JavaModelException {
    if (sibling instanceof IMember) {
        ISourceRange sourceRange = ((IMember) sibling).getSourceRange();
        if (sourceRange == null) {
            return null;
        }
        int insertPos = sourceRange.getOffset();
        List<? extends ASTNode> members = listRewrite.getOriginalList();
        for (int i = 0; i < members.size(); i++) {
            ASTNode curr = members.get(i);
            if (curr.getStartPosition() >= insertPos) {
                return curr;
            }
        }
    }
    return null;
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) IMember(org.eclipse.jdt.core.IMember) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 23 with IMember

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

the class SourceMapper method getSourceRange.

/**
     * Returns the <code>SourceRange</code> for the given element, or
     * {-1, -1} if no source range is known for the element.
     */
public SourceRange getSourceRange(IJavaElement element) {
    switch(element.getElementType()) {
        case IJavaElement.METHOD:
            if (((IMember) element).isBinary()) {
                IJavaElement[] el = getUnqualifiedMethodHandle((IMethod) element, false);
                if (el[1] != null && this.sourceRanges.get(el[0]) == null) {
                    element = getUnqualifiedMethodHandle((IMethod) element, true)[0];
                } else {
                    element = el[0];
                }
            }
            break;
        case IJavaElement.TYPE_PARAMETER:
            IJavaElement parent = element.getParent();
            if (parent.getElementType() == IJavaElement.METHOD) {
                IMethod method = (IMethod) parent;
                if (method.isBinary()) {
                    IJavaElement[] el = getUnqualifiedMethodHandle(method, false);
                    if (el[1] != null && this.sourceRanges.get(el[0]) == null) {
                        method = (IMethod) getUnqualifiedMethodHandle(method, true)[0];
                    } else {
                        method = (IMethod) el[0];
                    }
                    element = method.getTypeParameter(element.getElementName());
                }
            }
            break;
        case IJavaElement.LOCAL_VARIABLE:
            LocalVariableElementKey key = new LocalVariableElementKey(element.getParent(), element.getElementName());
            SourceRange[] ranges = (SourceRange[]) this.parametersRanges.get(key);
            if (ranges == null) {
                return UNKNOWN_RANGE;
            } else {
                return ranges[0];
            }
    }
    SourceRange[] ranges = (SourceRange[]) this.sourceRanges.get(element);
    if (ranges == null) {
        return UNKNOWN_RANGE;
    } else {
        return ranges[0];
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IMethod(org.eclipse.jdt.core.IMethod) ISourceRange(org.eclipse.jdt.core.ISourceRange) SourceRange(org.eclipse.jdt.core.SourceRange) IMember(org.eclipse.jdt.core.IMember)

Example 24 with IMember

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

the class RefactoringHandleTransplanter method addElements.

private void addElements(IMember element, LinkedList<IMember> chain) {
    chain.addFirst(element);
    IJavaElement parent = element.getParent();
    if (parent instanceof IMember)
        addElements((IMember) parent, chain);
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IMember(org.eclipse.jdt.core.IMember)

Example 25 with IMember

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

the class RefactoringHandleTransplanter method convertElements.

private IMember[] convertElements(IMember[] oldElements) {
    final IMember[] newElements = new IMember[oldElements.length];
    final IMember first = oldElements[0];
    Assert.isTrue(first instanceof IType);
    if (first.equals(fOldType))
        // We renamed a top level type.
        newElements[0] = fNewType;
    else
        newElements[0] = first;
    for (int i = 1; i < oldElements.length; i++) {
        final IJavaElement newParent = newElements[i - 1];
        final IJavaElement currentElement = oldElements[i];
        switch(newParent.getElementType()) {
            case IJavaElement.TYPE:
                {
                    switch(currentElement.getElementType()) {
                        case IJavaElement.TYPE:
                            {
                                final String newName = resolveTypeName((IType) currentElement);
                                newElements[i] = ((IType) newParent).getType(newName);
                                break;
                            }
                        case IJavaElement.METHOD:
                            {
                                final String newName = resolveElementName(currentElement);
                                final String[] newParameterTypes = resolveParameterTypes((IMethod) currentElement);
                                newElements[i] = ((IType) newParent).getMethod(newName, newParameterTypes);
                                break;
                            }
                        case IJavaElement.INITIALIZER:
                            {
                                final IInitializer initializer = (IInitializer) currentElement;
                                newElements[i] = ((IType) newParent).getInitializer(initializer.getOccurrenceCount());
                                break;
                            }
                        case IJavaElement.FIELD:
                            {
                                final String newName = resolveElementName(currentElement);
                                newElements[i] = ((IType) newParent).getField(newName);
                                break;
                            }
                    }
                    break;
                }
            case IJavaElement.METHOD:
                {
                    switch(currentElement.getElementType()) {
                        case IJavaElement.TYPE:
                            {
                                newElements[i] = resolveTypeInMember((IMethod) newParent, (IType) currentElement);
                                break;
                            }
                    }
                    break;
                }
            case IJavaElement.INITIALIZER:
                {
                    switch(currentElement.getElementType()) {
                        case IJavaElement.TYPE:
                            {
                                newElements[i] = resolveTypeInMember((IInitializer) newParent, (IType) currentElement);
                                break;
                            }
                    }
                    break;
                }
            case IJavaElement.FIELD:
                {
                    switch(currentElement.getElementType()) {
                        case IJavaElement.TYPE:
                            {
                                // anonymous type in field declaration
                                newElements[i] = resolveTypeInMember((IField) newParent, (IType) currentElement);
                                break;
                            }
                    }
                    break;
                }
        }
    }
    return newElements;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IInitializer(org.eclipse.jdt.core.IInitializer) IMethod(org.eclipse.jdt.core.IMethod) IField(org.eclipse.jdt.core.IField) IMember(org.eclipse.jdt.core.IMember) IType(org.eclipse.jdt.core.IType)

Aggregations

IMember (org.eclipse.jdt.core.IMember)33 IType (org.eclipse.jdt.core.IType)15 IJavaElement (org.eclipse.jdt.core.IJavaElement)14 IMethod (org.eclipse.jdt.core.IMethod)13 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)11 JavaModelException (org.eclipse.jdt.core.JavaModelException)6 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)5 List (java.util.List)4 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)4 IField (org.eclipse.jdt.core.IField)4 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)4 CompilationUnitRewrite (org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite)4 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)3 IJavaProject (org.eclipse.jdt.core.IJavaProject)3 ISourceRange (org.eclipse.jdt.core.ISourceRange)3 ASTNode (org.eclipse.jdt.core.dom.ASTNode)3 HashMap (java.util.HashMap)2 IInitializer (org.eclipse.jdt.core.IInitializer)2