Search in sources :

Example 1 with MethodOverrideTester

use of org.eclipse.jdt.internal.corext.util.MethodOverrideTester in project che by eclipse.

the class JavadocContentAccess2 method createSuperMethodReferences.

private static StringBuffer createSuperMethodReferences(final IMethod method, String urlPrefix) throws JavaModelException {
    IType type = method.getDeclaringType();
    ITypeHierarchy hierarchy = SuperTypeHierarchyCache.getTypeHierarchy(type);
    final MethodOverrideTester tester = SuperTypeHierarchyCache.getMethodOverrideTester(type);
    final ArrayList<IMethod> superInterfaceMethods = new ArrayList<IMethod>();
    final IMethod[] superClassMethod = { null };
    new InheritDocVisitor() {

        @Override
        public Object visit(IType currType) throws JavaModelException {
            IMethod overridden = tester.findOverriddenMethodInType(currType, method);
            if (overridden == null)
                return InheritDocVisitor.CONTINUE;
            if (currType.isInterface())
                superInterfaceMethods.add(overridden);
            else
                superClassMethod[0] = overridden;
            return STOP_BRANCH;
        }
    }.visitInheritDoc(type, hierarchy);
    boolean hasSuperInterfaceMethods = superInterfaceMethods.size() != 0;
    if (!hasSuperInterfaceMethods && superClassMethod[0] == null)
        return null;
    StringBuffer buf = new StringBuffer();
    //$NON-NLS-1$
    buf.append("<div>");
    if (hasSuperInterfaceMethods) {
        //$NON-NLS-1$
        buf.append("<b>");
        buf.append(JavaDocMessages.JavaDoc2HTMLTextReader_specified_by_section);
        //$NON-NLS-1$
        buf.append("</b> ");
        for (Iterator<IMethod> iter = superInterfaceMethods.iterator(); iter.hasNext(); ) {
            IMethod overridden = iter.next();
            buf.append(createMethodInTypeLinks(overridden, urlPrefix));
            if (iter.hasNext())
                buf.append(JavaElementLabels.COMMA_STRING);
        }
    }
    if (superClassMethod[0] != null) {
        if (hasSuperInterfaceMethods)
            buf.append(JavaElementLabels.COMMA_STRING);
        //$NON-NLS-1$
        buf.append("<b>");
        buf.append(JavaDocMessages.JavaDoc2HTMLTextReader_overrides_section);
        //$NON-NLS-1$
        buf.append("</b> ");
        buf.append(createMethodInTypeLinks(superClassMethod[0], urlPrefix));
    }
    //$NON-NLS-1$
    buf.append("</div>");
    return buf;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) MethodOverrideTester(org.eclipse.jdt.internal.corext.util.MethodOverrideTester) ArrayList(java.util.ArrayList) IType(org.eclipse.jdt.core.IType) ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) IMethod(org.eclipse.jdt.core.IMethod)

Example 2 with MethodOverrideTester

use of org.eclipse.jdt.internal.corext.util.MethodOverrideTester in project che by eclipse.

the class JavadocContentAccess2 method findAttachedDocInHierarchy.

/**
     * Finds the first available attached Javadoc in the hierarchy of the given method.
     *
     * @param method
     *         the method
     * @return the inherited Javadoc from the Javadoc attachment, or <code>null</code> if none
     * @throws org.eclipse.jdt.core.JavaModelException
     *         unexpected problem
     */
private static String findAttachedDocInHierarchy(final IMethod method) throws JavaModelException {
    IType type = method.getDeclaringType();
    ITypeHierarchy hierarchy = SuperTypeHierarchyCache.getTypeHierarchy(type);
    final MethodOverrideTester tester = SuperTypeHierarchyCache.getMethodOverrideTester(type);
    return (String) new InheritDocVisitor() {

        @Override
        public Object visit(IType currType) throws JavaModelException {
            IMethod overridden = tester.findOverriddenMethodInType(currType, method);
            if (overridden == null)
                return InheritDocVisitor.CONTINUE;
            if (overridden.getOpenable().getBuffer() == null) {
                // only if no source available
                //TODO: BaseURL for method can be wrong for attached Javadoc from overridden
                // (e.g. when overridden is from rt.jar). Fix would be to add baseURL here.
                String attachedJavadoc = overridden.getAttachedJavadoc(null);
                if (attachedJavadoc != null)
                    return attachedJavadoc;
            }
            return CONTINUE;
        }
    }.visitInheritDoc(type, hierarchy);
}
Also used : ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) MethodOverrideTester(org.eclipse.jdt.internal.corext.util.MethodOverrideTester) IMethod(org.eclipse.jdt.core.IMethod) IType(org.eclipse.jdt.core.IType)

Example 3 with MethodOverrideTester

use of org.eclipse.jdt.internal.corext.util.MethodOverrideTester 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 4 with MethodOverrideTester

use of org.eclipse.jdt.internal.corext.util.MethodOverrideTester in project xtext-eclipse by eclipse.

the class CombinedJvmJdtRenameContextFactory method addDeclaringMethod.

protected void addDeclaringMethod(JvmIdentifiableElement jvmElement, IJavaElement javaElement, Map<URI, IJavaElement> jvm2javaElement) {
    try {
        IType declaringType = ((IMethod) javaElement).getDeclaringType();
        ITypeHierarchy typeHierarchy = declaringType.newSupertypeHierarchy(new NullProgressMonitor());
        MethodOverrideTester methodOverrideTester = new MethodOverrideTester(declaringType, typeHierarchy);
        IMethod declaringMethod = methodOverrideTester.findDeclaringMethod((IMethod) javaElement, true);
        if (declaringMethod != null)
            jvm2javaElement.put(EcoreUtil.getURI(jvmElement), declaringMethod);
        else
            jvm2javaElement.put(EcoreUtil.getURI(jvmElement), javaElement);
    } catch (JavaModelException e) {
        throw new RuntimeException(e);
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) JavaModelException(org.eclipse.jdt.core.JavaModelException) MethodOverrideTester(org.eclipse.jdt.internal.corext.util.MethodOverrideTester) IMethod(org.eclipse.jdt.core.IMethod) IType(org.eclipse.jdt.core.IType)

Example 5 with MethodOverrideTester

use of org.eclipse.jdt.internal.corext.util.MethodOverrideTester in project che by eclipse.

the class MethodChecks method overridesAnotherMethod.

public static IMethod overridesAnotherMethod(IMethod method, ITypeHierarchy hierarchy) throws JavaModelException {
    MethodOverrideTester tester = new MethodOverrideTester(method.getDeclaringType(), hierarchy);
    IMethod found = tester.findDeclaringMethod(method, true);
    boolean overrides = (found != null && !found.equals(method) && (!JdtFlags.isStatic(found)) && (!JdtFlags.isPrivate(found)));
    if (overrides)
        return found;
    else
        return null;
}
Also used : MethodOverrideTester(org.eclipse.jdt.internal.corext.util.MethodOverrideTester) IMethod(org.eclipse.jdt.core.IMethod)

Aggregations

IMethod (org.eclipse.jdt.core.IMethod)8 MethodOverrideTester (org.eclipse.jdt.internal.corext.util.MethodOverrideTester)8 IType (org.eclipse.jdt.core.IType)7 ITypeHierarchy (org.eclipse.jdt.core.ITypeHierarchy)6 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)2 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 Type (org.eclipse.che.ide.ext.java.shared.dto.model.Type)1 IMember (org.eclipse.jdt.core.IMember)1 ModifierKeyword (org.eclipse.jdt.core.dom.Modifier.ModifierKeyword)1 CompilationUnitRewrite (org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite)1 MemberVisibilityAdjustor (org.eclipse.jdt.internal.corext.refactoring.structure.MemberVisibilityAdjustor)1 IncomingMemberVisibilityAdjustment (org.eclipse.jdt.internal.corext.refactoring.structure.MemberVisibilityAdjustor.IncomingMemberVisibilityAdjustment)1 TextChangeManager (org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager)1