Search code examples
javaeclipseeclipse-jdt

Finding IMethod from IType by method name


Let's say I have Hello class that has hello method.

public class Hello {
public int hello(int x, int y)

I need to get the IMethod reference of "hello" method. This is the code that I could get IType (Hello).

IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
// 1. The name of the project in the workspace
IProject project = root.getProject("Hello");
project.open(null /* IProgressMonitor */);

IJavaProject javaProject = JavaCore.create(project);
// 2. The name of the Type (including the namespace)
IType itype = javaProject.findType("smcho.HelloRenameMethod");

IMethod method = itype.findMethod() // ???

I googled IType#findMethod(), but the input parameter for this method is an IMethod instance, not a string.

How can I get IMethod from IType with the method name? Or, how can I use the IType#findMethod() to get IMethod?


Solution

  • I needed to use getMethod to get IMethod with method name.

    IMethod method = itype.getMethod("hello", new String[] {"I", "I"});