Search in sources :

Example 51 with IMethod

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

the class MethodOverrideTester method findOverriddenMethodInType.

/**
	 * Finds an overridden method in a type. With generics it is possible that 2 methods in the same type are overridden at the same time.
	 * In that case the first overridden method found is returned.
	 * @param overriddenType The type to find methods in
	 * @param overriding The overriding method
	 * @return The first overridden method or <code>null</code> if no method is overridden
	 * @throws JavaModelException if a problem occurs
	 */
public IMethod findOverriddenMethodInType(IType overriddenType, IMethod overriding) throws JavaModelException {
    int flags = overriding.getFlags();
    if (Flags.isPrivate(flags) || Flags.isStatic(flags) || overriding.isConstructor())
        return null;
    IMethod[] overriddenMethods = overriddenType.getMethods();
    for (int i = 0; i < overriddenMethods.length; i++) {
        IMethod overridden = overriddenMethods[i];
        flags = overridden.getFlags();
        if (Flags.isPrivate(flags) || Flags.isStatic(flags) || overridden.isConstructor())
            continue;
        if (isSubsignature(overriding, overridden)) {
            return overridden;
        }
    }
    return null;
}
Also used : IMethod(org.eclipse.jdt.core.IMethod)

Example 52 with IMethod

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

the class RefactoringTest method getMethods.

public static IMethod[] getMethods(IType type, String[] names, String[][] signatures) {
    if (names == null || signatures == null)
        return new IMethod[0];
    List methods = new ArrayList(names.length);
    for (int i = 0; i < names.length; i++) {
        IMethod method = type.getMethod(names[i], signatures[i]);
        assertTrue("method " + method.getElementName() + " does not exist", method.exists());
        if (!methods.contains(method))
            methods.add(method);
    }
    return (IMethod[]) methods.toArray(new IMethod[methods.size()]);
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) IMethod(org.eclipse.jdt.core.IMethod)

Example 53 with IMethod

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

the class RenameMethodInInterfaceTest method helper1_0.

private void helper1_0(String methodName, String newMethodName, String[] signatures) throws Exception {
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), "A");
    IType interfaceI = getType(cu, "I");
    IMethod method = interfaceI.getMethod(methodName, signatures);
    RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_METHOD);
    descriptor.setJavaElement(method);
    descriptor.setUpdateReferences(true);
    descriptor.setNewName(newMethodName);
    RefactoringStatus result = performRefactoring(descriptor);
    assertNotNull("precondition was supposed to fail", result);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IMethod(org.eclipse.jdt.core.IMethod) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) IType(org.eclipse.jdt.core.IType)

Example 54 with IMethod

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

the class RenameMethodInInterfaceTest method helper2_0.

private void helper2_0(String methodName, String newMethodName, String[] signatures, boolean shouldPass, boolean updateReferences, boolean createDelegate) throws Exception {
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), "A");
    IType interfaceI = getType(cu, "I");
    IMethod method = interfaceI.getMethod(methodName, signatures);
    RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_METHOD);
    descriptor.setJavaElement(method);
    descriptor.setUpdateReferences(updateReferences);
    descriptor.setNewName(newMethodName);
    descriptor.setKeepOriginal(createDelegate);
    descriptor.setDeprecateDelegate(true);
    assertEquals("was supposed to pass", null, performRefactoring(descriptor));
    if (!shouldPass) {
        assertTrue("incorrect renaming because of a java model bug", !getFileContents(getOutputTestFileName("A")).equals(cu.getSource()));
        return;
    }
    assertEqualLines("incorrect renaming", getFileContents(getOutputTestFileName("A")), cu.getSource());
    assertTrue("anythingToUndo", RefactoringCore.getUndoManager().anythingToUndo());
    assertTrue("! anythingToRedo", !RefactoringCore.getUndoManager().anythingToRedo());
    //assertEquals("1 to undo", 1, Refactoring.getUndoManager().getRefactoringLog().size());
    RefactoringCore.getUndoManager().performUndo(null, new NullProgressMonitor());
    assertEqualLines("invalid undo", getFileContents(getInputTestFileName("A")), cu.getSource());
    assertTrue("! anythingToUndo", !RefactoringCore.getUndoManager().anythingToUndo());
    assertTrue("anythingToRedo", RefactoringCore.getUndoManager().anythingToRedo());
    //assertEquals("1 to redo", 1, Refactoring.getUndoManager().getRedoStack().size());
    RefactoringCore.getUndoManager().performRedo(null, new NullProgressMonitor());
    assertEqualLines("invalid redo", getFileContents(getOutputTestFileName("A")), cu.getSource());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IMethod(org.eclipse.jdt.core.IMethod) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) IType(org.eclipse.jdt.core.IType)

Example 55 with IMethod

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

the class SearchTestHelper method runMethodRefQuery.

static JavaSearchQuery runMethodRefQuery(String TypeName, String methodName, String[] parameterTypes) throws JavaModelException {
    IMethod method = getMethod(TypeName, methodName, parameterTypes);
    JavaSearchQuery query = new JavaSearchQuery(new ElementQuerySpecification(method, IJavaSearchConstants.REFERENCES, JavaSearchScopeFactory.getInstance().createWorkspaceScope(true), "workspace scope"));
    NewSearchUI.runQueryInForeground(null, query);
    return query;
}
Also used : JavaSearchQuery(org.eclipse.jdt.internal.ui.search.JavaSearchQuery) ElementQuerySpecification(org.eclipse.jdt.ui.search.ElementQuerySpecification) IMethod(org.eclipse.jdt.core.IMethod)

Aggregations

IMethod (org.eclipse.jdt.core.IMethod)217 IType (org.eclipse.jdt.core.IType)112 IJavaElement (org.eclipse.jdt.core.IJavaElement)56 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)37 ArrayList (java.util.ArrayList)35 JavaModelException (org.eclipse.jdt.core.JavaModelException)32 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)29 IField (org.eclipse.jdt.core.IField)27 Test (org.junit.Test)22 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)19 ITypeHierarchy (org.eclipse.jdt.core.ITypeHierarchy)19 IMember (org.eclipse.jdt.core.IMember)15 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)14 RenameJavaElementDescriptor (org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)13 IJavaProject (org.eclipse.jdt.core.IJavaProject)12 HashSet (java.util.HashSet)11 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)10 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)10 ISourceRange (org.eclipse.jdt.core.ISourceRange)9 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)9