Search in sources :

Example 16 with IMember

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

the class IntroduceIndirectionRefactoring method updateReferences.

private RefactoringStatus updateReferences(IProgressMonitor monitor) throws CoreException {
    RefactoringStatus result = new RefactoringStatus();
    //$NON-NLS-1$
    monitor.beginTask("", 90);
    if (monitor.isCanceled())
        throw new OperationCanceledException();
    IMethod[] ripple = RippleMethodFinder2.getRelatedMethods(fTargetMethod, false, new NoOverrideProgressMonitor(monitor, 10), null);
    if (monitor.isCanceled())
        throw new OperationCanceledException();
    SearchResultGroup[] references = Checks.excludeCompilationUnits(getReferences(ripple, new NoOverrideProgressMonitor(monitor, 10), result), result);
    if (result.hasFatalError())
        return result;
    result.merge(Checks.checkCompileErrorsInAffectedFiles(references));
    if (monitor.isCanceled())
        throw new OperationCanceledException();
    int ticksPerCU = references.length == 0 ? 0 : 70 / references.length;
    for (int i = 0; i < references.length; i++) {
        SearchResultGroup group = references[i];
        SearchMatch[] searchResults = group.getSearchResults();
        CompilationUnitRewrite currentCURewrite = getCachedCURewrite(group.getCompilationUnit());
        for (int j = 0; j < searchResults.length; j++) {
            SearchMatch match = searchResults[j];
            if (match.isInsideDocComment())
                continue;
            IMember enclosingMember = (IMember) match.getElement();
            ASTNode target = getSelectedNode(group.getCompilationUnit(), currentCURewrite.getRoot(), match.getOffset(), match.getLength());
            if (target instanceof SuperMethodInvocation) {
                // Cannot retarget calls to super - add a warning
                result.merge(createWarningAboutCall(enclosingMember, target, RefactoringCoreMessages.IntroduceIndirectionRefactoring_call_warning_super_keyword));
                continue;
            }
            //$NON-NLS-1$
            Assert.isTrue(target instanceof MethodInvocation, "Element of call should be a MethodInvocation.");
            MethodInvocation invocation = (MethodInvocation) target;
            ITypeBinding typeBinding = getExpressionType(invocation);
            if (fIntermediaryFirstParameterType == null) {
                // no highest type yet
                fIntermediaryFirstParameterType = typeBinding.getTypeDeclaration();
            } else {
                // check if current type is higher
                result.merge(findCommonParent(typeBinding.getTypeDeclaration()));
            }
            if (result.hasFatalError())
                return result;
            // create an edit for this particular call
            result.merge(updateMethodInvocation(invocation, enclosingMember, currentCURewrite));
            // does call see the intermediary method?
            // => increase visibility of the type of the intermediary method.
            result.merge(adjustVisibility(fIntermediaryType, enclosingMember.getDeclaringType(), new NoOverrideProgressMonitor(monitor, 0)));
            if (monitor.isCanceled())
                throw new OperationCanceledException();
        }
        if (!isRewriteKept(group.getCompilationUnit()))
            createChangeAndDiscardRewrite(group.getCompilationUnit());
        monitor.worked(ticksPerCU);
    }
    monitor.done();
    return result;
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) IMember(org.eclipse.jdt.core.IMember) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IMethod(org.eclipse.jdt.core.IMethod)

Example 17 with IMember

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

the class RefactoringAvailabilityTester method isPullUpAvailable.

public static boolean isPullUpAvailable(final IStructuredSelection selection) throws JavaModelException {
    if (!selection.isEmpty()) {
        if (selection.size() == 1) {
            if (selection.getFirstElement() instanceof ICompilationUnit)
                // Do not force opening
                return true;
            final IType type = getSingleSelectedType(selection);
            if (type != null)
                return Checks.isAvailable(type) && isPullUpAvailable(new IType[] { type });
        }
        for (final Iterator<?> iterator = selection.iterator(); iterator.hasNext(); ) {
            if (!(iterator.next() instanceof IMember))
                return false;
        }
        final Set<IMember> members = new HashSet<IMember>();
        @SuppressWarnings("unchecked") List<IMember> selectionList = (List<IMember>) (List<?>) Arrays.asList(selection.toArray());
        members.addAll(selectionList);
        return isPullUpAvailable(members.toArray(new IMember[members.size()]));
    }
    return false;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) List(java.util.List) ArrayList(java.util.ArrayList) IMember(org.eclipse.jdt.core.IMember) IType(org.eclipse.jdt.core.IType) HashSet(java.util.HashSet)

Example 18 with IMember

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

the class RefactoringAvailabilityTester method isExtractSupertypeAvailable.

public static boolean isExtractSupertypeAvailable(final IStructuredSelection selection) throws JavaModelException {
    if (!selection.isEmpty()) {
        if (selection.size() == 1) {
            if (selection.getFirstElement() instanceof ICompilationUnit)
                // Do not force opening
                return true;
            final IType type = getSingleSelectedType(selection);
            if (type != null)
                return Checks.isAvailable(type) && isExtractSupertypeAvailable(new IType[] { type });
        }
        for (final Iterator<?> iterator = selection.iterator(); iterator.hasNext(); ) {
            if (!(iterator.next() instanceof IMember))
                return false;
        }
        final Set<IMember> members = new HashSet<IMember>();
        @SuppressWarnings("unchecked") List<IMember> selectionList = (List<IMember>) (List<?>) Arrays.asList(selection.toArray());
        members.addAll(selectionList);
        return isExtractSupertypeAvailable(members.toArray(new IMember[members.size()]));
    }
    return false;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) List(java.util.List) ArrayList(java.util.ArrayList) IMember(org.eclipse.jdt.core.IMember) IType(org.eclipse.jdt.core.IType) HashSet(java.util.HashSet)

Example 19 with IMember

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

the class RefactoringAvailabilityTester method isInlineMethodAvailable.

public static boolean isInlineMethodAvailable(final JavaTextSelection selection) throws JavaModelException {
    final IJavaElement[] elements = selection.resolveElementAtOffset();
    if (elements.length != 1) {
        IJavaElement enclosingElement = selection.resolveEnclosingElement();
        if (!(enclosingElement instanceof IMember))
            return false;
        ITypeRoot typeRoot = ((IMember) enclosingElement).getTypeRoot();
        CompilationUnit compilationUnit = selection.resolvePartialAstAtOffset();
        if (compilationUnit == null)
            return false;
        return getInlineableMethodNode(typeRoot, compilationUnit, selection.getOffset(), selection.getLength()) != null;
    }
    IJavaElement element = elements[0];
    if (!(element instanceof IMethod))
        return false;
    IMethod method = (IMethod) element;
    if (!isInlineMethodAvailable((method)))
        return false;
    // in binary class, only activate for method declarations
    IJavaElement enclosingElement = selection.resolveEnclosingElement();
    if (enclosingElement == null || enclosingElement.getAncestor(IJavaElement.CLASS_FILE) == null)
        return true;
    if (!(enclosingElement instanceof IMethod))
        return false;
    IMethod enclosingMethod = (IMethod) enclosingElement;
    if (enclosingMethod.isConstructor())
        return false;
    int nameOffset = enclosingMethod.getNameRange().getOffset();
    int nameLength = enclosingMethod.getNameRange().getLength();
    return (nameOffset <= selection.getOffset()) && (selection.getOffset() + selection.getLength() <= nameOffset + nameLength);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) IMethod(org.eclipse.jdt.core.IMethod) IMember(org.eclipse.jdt.core.IMember)

Example 20 with IMember

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

the class JavaElementLabelComposer method appendTypeParameterLabel.

/**
	 * Appends the styled label for a type parameter.
	 *
	 * @param typeParameter the element to render
	 * @param flags the rendering flags. Flags with names starting with 'T_' are considered.
	 */
public void appendTypeParameterLabel(ITypeParameter typeParameter, long flags) {
    try {
        appendTypeParameterWithBounds(typeParameter, flags);
        // post qualification
        if (getFlag(flags, JavaElementLabels.TP_POST_QUALIFIED)) {
            fBuffer.append(JavaElementLabels.CONCAT_STRING);
            IMember declaringMember = typeParameter.getDeclaringMember();
            appendElementLabel(declaringMember, JavaElementLabels.M_PARAMETER_TYPES | JavaElementLabels.M_FULLY_QUALIFIED | JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
        }
    } catch (JavaModelException e) {
        // NotExistsException will not reach this point
        JavaPlugin.log(e);
    }
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IMember(org.eclipse.jdt.core.IMember)

Aggregations

IMember (org.eclipse.jdt.core.IMember)34 IType (org.eclipse.jdt.core.IType)16 IJavaElement (org.eclipse.jdt.core.IJavaElement)14 IMethod (org.eclipse.jdt.core.IMethod)13 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)12 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