Search in sources :

Example 76 with SubProgressMonitor

use of org.eclipse.core.runtime.SubProgressMonitor in project che by eclipse.

the class MemberVisibilityAdjustor method adjustVisibility.

/**
	 * Adjusts the visibilities of the referenced and referencing elements.
	 *
	 * @param monitor the progress monitor to use
	 * @throws JavaModelException if an error occurs during search
	 */
public final void adjustVisibility(final IProgressMonitor monitor) throws JavaModelException {
    try {
        //$NON-NLS-1$
        monitor.beginTask("", 7);
        monitor.setTaskName(RefactoringCoreMessages.MemberVisibilityAdjustor_checking);
        final RefactoringSearchEngine2 engine = new RefactoringSearchEngine2(SearchPattern.createPattern(fReferenced, IJavaSearchConstants.REFERENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE));
        engine.setScope(fScope);
        engine.setStatus(fStatus);
        engine.setOwner(fOwner);
        if (fIncoming) {
            // check calls to the referenced (moved) element, adjust element
            // visibility if necessary.
            engine.searchPattern(new SubProgressMonitor(monitor, 1));
            adjustIncomingVisibility((SearchResultGroup[]) engine.getResults(), new SubProgressMonitor(monitor, 1));
            engine.clearResults();
            // of the type if they are accessed outside of the moved type
            if (fReferenced instanceof IType) {
                final IType type = (IType) fReferenced;
                adjustMemberVisibility(type, new SubProgressMonitor(monitor, 1));
            }
        }
        if (fOutgoing) {
            /*
				 * Search for the types, fields, and methods which
				 * are called/acted upon inside the referenced element (the one
				 * to be moved) and assure that they are also visible from
				 * within the referencing element (the destination type (or
				 * package if move to new type)).
				 */
            engine.searchReferencedTypes(fReferenced, new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
            engine.searchReferencedFields(fReferenced, new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
            engine.searchReferencedMethods(fReferenced, new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
            adjustOutgoingVisibility((SearchResultGroup[]) engine.getResults(), new SubProgressMonitor(monitor, 1));
        }
    } finally {
        monitor.done();
    }
}
Also used : SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) RefactoringSearchEngine2(org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine2) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IType(org.eclipse.jdt.core.IType)

Example 77 with SubProgressMonitor

use of org.eclipse.core.runtime.SubProgressMonitor in project che by eclipse.

the class MemberVisibilityAdjustor method thresholdTypeToType.

/**
	 * Returns the visibility threshold from a type to another type.
	 *
	 * @param referencing the referencing type
	 * @param referenced the referenced type
	 * @param monitor the progress monitor to use
	 * @return the visibility keyword corresponding to the threshold, or <code>null</code> for default visibility
	 * @throws JavaModelException if the java elements could not be accessed
	 */
private ModifierKeyword thresholdTypeToType(final IType referencing, final IType referenced, final IProgressMonitor monitor) throws JavaModelException {
    ModifierKeyword keyword = ModifierKeyword.PUBLIC_KEYWORD;
    final ICompilationUnit referencedUnit = referenced.getCompilationUnit();
    if (referencing.equals(referenced.getDeclaringType()))
        keyword = ModifierKeyword.PRIVATE_KEYWORD;
    else {
        final ITypeHierarchy hierarchy = getTypeHierarchy(referencing, new SubProgressMonitor(monitor, 1));
        final IType[] types = hierarchy.getSupertypes(referencing);
        IType superType = null;
        for (int index = 0; index < types.length; index++) {
            superType = types[index];
            if (superType.equals(referenced)) {
                keyword = null;
                return keyword;
            }
        }
    }
    final ICompilationUnit typeUnit = referencing.getCompilationUnit();
    if (referencedUnit != null && referencedUnit.equals(typeUnit)) {
        if (referenced.getDeclaringType() != null)
            keyword = null;
        else
            keyword = ModifierKeyword.PRIVATE_KEYWORD;
    } else if (referencedUnit != null && typeUnit != null && referencedUnit.getParent().equals(typeUnit.getParent()))
        keyword = null;
    return keyword;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) ModifierKeyword(org.eclipse.jdt.core.dom.Modifier.ModifierKeyword) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IType(org.eclipse.jdt.core.IType)

Example 78 with SubProgressMonitor

use of org.eclipse.core.runtime.SubProgressMonitor in project che by eclipse.

the class IntroduceFactoryRefactoring method findNonPrimaryType.

private IType findNonPrimaryType(String fullyQualifiedName, IProgressMonitor pm, RefactoringStatus status) throws JavaModelException {
    SearchPattern p = SearchPattern.createPattern(fullyQualifiedName, IJavaSearchConstants.TYPE, IJavaSearchConstants.DECLARATIONS, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
    final RefactoringSearchEngine2 engine = new RefactoringSearchEngine2(p);
    engine.setFiltering(true, true);
    engine.setScope(RefactoringScopeFactory.create(fCtorBinding.getJavaElement().getJavaProject()));
    engine.setStatus(status);
    engine.searchPattern(new SubProgressMonitor(pm, 1));
    SearchResultGroup[] groups = (SearchResultGroup[]) engine.getResults();
    if (groups.length != 0) {
        for (int i = 0; i < groups.length; i++) {
            SearchMatch[] matches = groups[i].getSearchResults();
            for (int j = 0; j < matches.length; j++) {
                if (matches[j].getAccuracy() == SearchMatch.A_ACCURATE)
                    return (IType) matches[j].getElement();
            }
        }
    }
    return null;
}
Also used : SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) RefactoringSearchEngine2(org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine2) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Example 79 with SubProgressMonitor

use of org.eclipse.core.runtime.SubProgressMonitor in project che by eclipse.

the class IntroduceFactoryRefactoring method searchForCallsTo.

/**
	 * Search for all calls to the given <code>IMethodBinding</code> in the project
	 * that contains the compilation unit <code>fCUHandle</code>.
	 * @param methodBinding
	 * @param pm
	 * @param status
	 * @return an array of <code>SearchResultGroup</code>'s that identify the search matches
	 * @throws JavaModelException
	 */
private SearchResultGroup[] searchForCallsTo(IMethodBinding methodBinding, IProgressMonitor pm, RefactoringStatus status) throws JavaModelException {
    IMethod method = (IMethod) methodBinding.getJavaElement();
    final RefactoringSearchEngine2 engine = new RefactoringSearchEngine2(createSearchPattern(method, methodBinding));
    engine.setFiltering(true, true);
    engine.setScope(createSearchScope(method, methodBinding));
    engine.setStatus(status);
    engine.searchPattern(new SubProgressMonitor(pm, 1));
    return (SearchResultGroup[]) engine.getResults();
}
Also used : IMethod(org.eclipse.jdt.core.IMethod) RefactoringSearchEngine2(org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine2) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Example 80 with SubProgressMonitor

use of org.eclipse.core.runtime.SubProgressMonitor in project che by eclipse.

the class IntroduceIndirectionRefactoring method adjustVisibility.

private RefactoringStatus adjustVisibility(IMember whoToAdjust, ModifierKeyword neededVisibility, boolean alsoIncreaseEnclosing, IProgressMonitor monitor) throws CoreException {
    Map<IMember, IncomingMemberVisibilityAdjustment> adjustments;
    if (isRewriteKept(whoToAdjust.getCompilationUnit()))
        adjustments = fIntermediaryAdjustments;
    else
        adjustments = new HashMap<IMember, IncomingMemberVisibilityAdjustment>();
    int existingAdjustments = adjustments.size();
    addAdjustment(whoToAdjust, neededVisibility, adjustments);
    if (alsoIncreaseEnclosing)
        while (whoToAdjust.getDeclaringType() != null) {
            whoToAdjust = whoToAdjust.getDeclaringType();
            addAdjustment(whoToAdjust, neededVisibility, adjustments);
        }
    boolean hasNewAdjustments = (adjustments.size() - existingAdjustments) > 0;
    if (hasNewAdjustments && ((whoToAdjust.isReadOnly() || whoToAdjust.isBinary())))
        return RefactoringStatus.createErrorStatus(Messages.format(RefactoringCoreMessages.IntroduceIndirectionRefactoring_cannot_update_binary_target_visibility, new String[] { JavaElementLabels.getElementLabel(whoToAdjust, JavaElementLabels.ALL_DEFAULT) }), JavaStatusContext.create(whoToAdjust));
    RefactoringStatus status = new RefactoringStatus();
    // Don't create a rewrite if it is not necessary
    if (!hasNewAdjustments)
        return status;
    try {
        monitor.beginTask(RefactoringCoreMessages.MemberVisibilityAdjustor_adjusting, 2);
        Map<ICompilationUnit, CompilationUnitRewrite> rewrites;
        if (!isRewriteKept(whoToAdjust.getCompilationUnit())) {
            CompilationUnitRewrite rewrite = new CompilationUnitRewrite(whoToAdjust.getCompilationUnit());
            rewrite.setResolveBindings(false);
            rewrites = new HashMap<ICompilationUnit, CompilationUnitRewrite>();
            rewrites.put(whoToAdjust.getCompilationUnit(), rewrite);
            status.merge(rewriteVisibility(adjustments, rewrites, new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)));
            rewrite.attachChange((CompilationUnitChange) fTextChangeManager.get(whoToAdjust.getCompilationUnit()), true, new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
        }
    } finally {
        monitor.done();
    }
    return status;
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) IncomingMemberVisibilityAdjustment(org.eclipse.jdt.internal.corext.refactoring.structure.MemberVisibilityAdjustor.IncomingMemberVisibilityAdjustment) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) HashMap(java.util.HashMap) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IMember(org.eclipse.jdt.core.IMember) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Aggregations

SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)217 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)54 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)53 CoreException (org.eclipse.core.runtime.CoreException)40 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)39 ArrayList (java.util.ArrayList)36 IFile (org.eclipse.core.resources.IFile)33 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)31 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)30 IOException (java.io.IOException)23 IType (org.eclipse.jdt.core.IType)19 HashSet (java.util.HashSet)17 IPath (org.eclipse.core.runtime.IPath)17 IResource (org.eclipse.core.resources.IResource)16 IProject (org.eclipse.core.resources.IProject)15 File (java.io.File)14 List (java.util.List)13 IMethod (org.eclipse.jdt.core.IMethod)13 InvocationTargetException (java.lang.reflect.InvocationTargetException)12 HashMap (java.util.HashMap)12