Search in sources :

Example 1 with ModifierKeyword

use of org.eclipse.jdt.core.dom.Modifier.ModifierKeyword 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)

Example 2 with ModifierKeyword

use of org.eclipse.jdt.core.dom.Modifier.ModifierKeyword in project che by eclipse.

the class ModifierCorrectionSubProcessor method findVisibilityModifier.

private static Modifier findVisibilityModifier(List<IExtendedModifier> modifiers) {
    for (int i = 0; i < modifiers.size(); i++) {
        IExtendedModifier curr = modifiers.get(i);
        if (curr instanceof Modifier) {
            Modifier modifier = (Modifier) curr;
            ModifierKeyword keyword = modifier.getKeyword();
            if (keyword == ModifierKeyword.PUBLIC_KEYWORD || keyword == ModifierKeyword.PROTECTED_KEYWORD || keyword == ModifierKeyword.PRIVATE_KEYWORD) {
                return modifier;
            }
        }
    }
    return null;
}
Also used : ModifierKeyword(org.eclipse.jdt.core.dom.Modifier.ModifierKeyword) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 3 with ModifierKeyword

use of org.eclipse.jdt.core.dom.Modifier.ModifierKeyword in project che by eclipse.

the class MemberVisibilityAdjustor method adjustIncomingVisibility.

/**
	 * Adjusts the visibility of the specified member.
	 *
	 * @param element the "source" point from which to calculate the visibility
	 * @param referencedMovedElement the moved element which may be adjusted in visibility
	 * @param monitor the progress monitor to use
	 * @throws JavaModelException if the visibility adjustment could not be computed
	 */
private void adjustIncomingVisibility(final IJavaElement element, IMember referencedMovedElement, final IProgressMonitor monitor) throws JavaModelException {
    final ModifierKeyword threshold = getVisibilityThreshold(element, referencedMovedElement, monitor);
    int flags = referencedMovedElement.getFlags();
    IType declaring = referencedMovedElement.getDeclaringType();
    if (declaring != null && declaring.isInterface() || referencedMovedElement instanceof IField && Flags.isEnum(referencedMovedElement.getFlags()))
        return;
    if (hasLowerVisibility(flags, threshold == null ? Modifier.NONE : threshold.toFlagValue()) && needsVisibilityAdjustment(referencedMovedElement, threshold))
        fAdjustments.put(referencedMovedElement, new IncomingMemberVisibilityAdjustment(referencedMovedElement, threshold, RefactoringStatus.createStatus(fVisibilitySeverity, Messages.format(getMessage(referencedMovedElement), new String[] { getLabel(referencedMovedElement), getLabel(threshold) }), JavaStatusContext.create(referencedMovedElement), null, RefactoringStatusEntry.NO_CODE, null)));
}
Also used : ModifierKeyword(org.eclipse.jdt.core.dom.Modifier.ModifierKeyword) IField(org.eclipse.jdt.core.IField) IType(org.eclipse.jdt.core.IType)

Example 4 with ModifierKeyword

use of org.eclipse.jdt.core.dom.Modifier.ModifierKeyword in project che by eclipse.

the class MemberVisibilityAdjustor method computeOutgoingVisibilityThreshold.

/**
	 * Computes the visibility threshold for the referenced element.
	 *
	 * @param referencing the referencing element
	 * @param referenced the referenced element
	 * @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 computeOutgoingVisibilityThreshold(final IJavaElement referencing, final IMember referenced, final IProgressMonitor monitor) throws JavaModelException {
    Assert.isTrue(referencing instanceof ICompilationUnit || referencing instanceof IType || referencing instanceof IPackageFragment);
    Assert.isTrue(referenced instanceof IType || referenced instanceof IField || referenced instanceof IMethod);
    ModifierKeyword keyword = ModifierKeyword.PUBLIC_KEYWORD;
    try {
        //$NON-NLS-1$
        monitor.beginTask("", 1);
        monitor.setTaskName(RefactoringCoreMessages.MemberVisibilityAdjustor_checking);
        final int referencingType = referencing.getElementType();
        final int referencedType = referenced.getElementType();
        switch(referencedType) {
            case IJavaElement.TYPE:
                {
                    final IType typeReferenced = (IType) referenced;
                    switch(referencingType) {
                        case IJavaElement.COMPILATION_UNIT:
                            {
                                final ICompilationUnit unit = (ICompilationUnit) referencing;
                                final ICompilationUnit referencedUnit = typeReferenced.getCompilationUnit();
                                if (referencedUnit != null && referencedUnit.equals(unit))
                                    keyword = ModifierKeyword.PRIVATE_KEYWORD;
                                else if (referencedUnit != null && referencedUnit.getParent().equals(unit.getParent()))
                                    keyword = null;
                                break;
                            }
                        case IJavaElement.TYPE:
                            {
                                keyword = thresholdTypeToType((IType) referencing, typeReferenced, monitor);
                                break;
                            }
                        case IJavaElement.PACKAGE_FRAGMENT:
                            {
                                final IPackageFragment fragment = (IPackageFragment) referencing;
                                if (typeReferenced.getPackageFragment().equals(fragment))
                                    keyword = null;
                                break;
                            }
                        default:
                            Assert.isTrue(false);
                    }
                    break;
                }
            case IJavaElement.FIELD:
                {
                    final IField fieldReferenced = (IField) referenced;
                    final ICompilationUnit referencedUnit = fieldReferenced.getCompilationUnit();
                    switch(referencingType) {
                        case IJavaElement.COMPILATION_UNIT:
                            {
                                final ICompilationUnit unit = (ICompilationUnit) referencing;
                                if (referencedUnit != null && referencedUnit.equals(unit))
                                    keyword = ModifierKeyword.PRIVATE_KEYWORD;
                                else if (referencedUnit != null && referencedUnit.getParent().equals(unit.getParent()))
                                    keyword = null;
                                break;
                            }
                        case IJavaElement.TYPE:
                            {
                                keyword = thresholdTypeToField((IType) referencing, fieldReferenced, monitor);
                                break;
                            }
                        case IJavaElement.PACKAGE_FRAGMENT:
                            {
                                final IPackageFragment fragment = (IPackageFragment) referencing;
                                if (fieldReferenced.getDeclaringType().getPackageFragment().equals(fragment))
                                    keyword = null;
                                break;
                            }
                        default:
                            Assert.isTrue(false);
                    }
                    break;
                }
            case IJavaElement.METHOD:
                {
                    final IMethod methodReferenced = (IMethod) referenced;
                    final ICompilationUnit referencedUnit = methodReferenced.getCompilationUnit();
                    switch(referencingType) {
                        case IJavaElement.COMPILATION_UNIT:
                            {
                                final ICompilationUnit unit = (ICompilationUnit) referencing;
                                if (referencedUnit != null && referencedUnit.equals(unit))
                                    keyword = ModifierKeyword.PRIVATE_KEYWORD;
                                else if (referencedUnit != null && referencedUnit.getParent().equals(unit.getParent()))
                                    keyword = null;
                                break;
                            }
                        case IJavaElement.TYPE:
                            {
                                keyword = thresholdTypeToMethod((IType) referencing, methodReferenced, monitor);
                                break;
                            }
                        case IJavaElement.PACKAGE_FRAGMENT:
                            {
                                final IPackageFragment fragment = (IPackageFragment) referencing;
                                if (methodReferenced.getDeclaringType().getPackageFragment().equals(fragment))
                                    keyword = null;
                                break;
                            }
                        default:
                            Assert.isTrue(false);
                    }
                    break;
                }
            default:
                Assert.isTrue(false);
        }
    } finally {
        monitor.done();
    }
    return keyword;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ModifierKeyword(org.eclipse.jdt.core.dom.Modifier.ModifierKeyword) IMethod(org.eclipse.jdt.core.IMethod) IField(org.eclipse.jdt.core.IField) IType(org.eclipse.jdt.core.IType)

Example 5 with ModifierKeyword

use of org.eclipse.jdt.core.dom.Modifier.ModifierKeyword in project che by eclipse.

the class MemberVisibilityAdjustor method getVisibilityThreshold.

/**
	 * Computes the visibility threshold for the referenced element.
	 *
	 * @param referencing the referencing element
	 * @param referenced the referenced element
	 * @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
	 */
public ModifierKeyword getVisibilityThreshold(final IJavaElement referencing, final IMember referenced, final IProgressMonitor monitor) throws JavaModelException {
    Assert.isTrue(!(referencing instanceof IInitializer));
    Assert.isTrue(!(referenced instanceof IInitializer));
    ModifierKeyword keyword = ModifierKeyword.PUBLIC_KEYWORD;
    try {
        //$NON-NLS-1$
        monitor.beginTask("", 1);
        monitor.setTaskName(RefactoringCoreMessages.MemberVisibilityAdjustor_checking);
        final int referencingType = referencing.getElementType();
        final int referencedType = referenced.getElementType();
        switch(referencedType) {
            case IJavaElement.TYPE:
                {
                    final IType typeReferenced = (IType) referenced;
                    final ICompilationUnit referencedUnit = typeReferenced.getCompilationUnit();
                    switch(referencingType) {
                        case IJavaElement.TYPE:
                            {
                                keyword = thresholdTypeToType((IType) referencing, typeReferenced, monitor);
                                break;
                            }
                        case IJavaElement.FIELD:
                        case IJavaElement.METHOD:
                            {
                                final IMember member = (IMember) referencing;
                                if (typeReferenced.equals(member.getDeclaringType()))
                                    keyword = ModifierKeyword.PRIVATE_KEYWORD;
                                else if (referencedUnit != null && referencedUnit.equals(member.getCompilationUnit()))
                                    keyword = ModifierKeyword.PRIVATE_KEYWORD;
                                else if (typeReferenced.getPackageFragment().equals(member.getDeclaringType().getPackageFragment()))
                                    keyword = null;
                                break;
                            }
                        case IJavaElement.PACKAGE_FRAGMENT:
                            {
                                final IPackageFragment fragment = (IPackageFragment) referencing;
                                if (typeReferenced.getPackageFragment().equals(fragment))
                                    keyword = null;
                                break;
                            }
                        default:
                            Assert.isTrue(false);
                    }
                    break;
                }
            case IJavaElement.FIELD:
                {
                    final IField fieldReferenced = (IField) referenced;
                    final ICompilationUnit referencedUnit = fieldReferenced.getCompilationUnit();
                    switch(referencingType) {
                        case IJavaElement.TYPE:
                            {
                                keyword = thresholdTypeToField((IType) referencing, fieldReferenced, monitor);
                                break;
                            }
                        case IJavaElement.FIELD:
                        case IJavaElement.METHOD:
                            {
                                final IMember member = (IMember) referencing;
                                if (fieldReferenced.getDeclaringType().equals(member.getDeclaringType()))
                                    keyword = ModifierKeyword.PRIVATE_KEYWORD;
                                else if (referencedUnit != null && referencedUnit.equals(member.getCompilationUnit()))
                                    keyword = ModifierKeyword.PRIVATE_KEYWORD;
                                else if (fieldReferenced.getDeclaringType().getPackageFragment().equals(member.getDeclaringType().getPackageFragment()))
                                    keyword = null;
                                break;
                            }
                        case IJavaElement.PACKAGE_FRAGMENT:
                            {
                                final IPackageFragment fragment = (IPackageFragment) referencing;
                                if (fieldReferenced.getDeclaringType().getPackageFragment().equals(fragment))
                                    keyword = null;
                                break;
                            }
                        default:
                            Assert.isTrue(false);
                    }
                    break;
                }
            case IJavaElement.METHOD:
                {
                    final IMethod methodReferenced = (IMethod) referenced;
                    final ICompilationUnit referencedUnit = methodReferenced.getCompilationUnit();
                    switch(referencingType) {
                        case IJavaElement.TYPE:
                            {
                                keyword = thresholdTypeToMethod((IType) referencing, methodReferenced, monitor);
                                break;
                            }
                        case IJavaElement.FIELD:
                        case IJavaElement.METHOD:
                            {
                                final IMember member = (IMember) referencing;
                                if (methodReferenced.getDeclaringType().equals(member.getDeclaringType()))
                                    keyword = ModifierKeyword.PRIVATE_KEYWORD;
                                else if (referencedUnit != null && referencedUnit.equals(member.getCompilationUnit()))
                                    keyword = ModifierKeyword.PRIVATE_KEYWORD;
                                else if (methodReferenced.getDeclaringType().getPackageFragment().equals(member.getDeclaringType().getPackageFragment()))
                                    keyword = null;
                                break;
                            }
                        case IJavaElement.PACKAGE_FRAGMENT:
                            {
                                final IPackageFragment fragment = (IPackageFragment) referencing;
                                if (methodReferenced.getDeclaringType().getPackageFragment().equals(fragment))
                                    keyword = null;
                                break;
                            }
                        default:
                            Assert.isTrue(false);
                    }
                    break;
                }
            default:
                Assert.isTrue(false);
        }
    } finally {
        monitor.done();
    }
    return keyword;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ModifierKeyword(org.eclipse.jdt.core.dom.Modifier.ModifierKeyword) 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

ModifierKeyword (org.eclipse.jdt.core.dom.Modifier.ModifierKeyword)11 IType (org.eclipse.jdt.core.IType)7 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)5 ITypeHierarchy (org.eclipse.jdt.core.ITypeHierarchy)4 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)3 IField (org.eclipse.jdt.core.IField)3 IMethod (org.eclipse.jdt.core.IMethod)3 IExtendedModifier (org.eclipse.jdt.core.dom.IExtendedModifier)3 Modifier (org.eclipse.jdt.core.dom.Modifier)3 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 IInitializer (org.eclipse.jdt.core.IInitializer)1 IMember (org.eclipse.jdt.core.IMember)1 MarkerAnnotation (org.eclipse.jdt.core.dom.MarkerAnnotation)1 Name (org.eclipse.jdt.core.dom.Name)1 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)1 SimpleName (org.eclipse.jdt.core.dom.SimpleName)1 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)1 MethodOverrideTester (org.eclipse.jdt.internal.corext.util.MethodOverrideTester)1