Search code examples
javaeclipsepluginseclipse-jdt

Get IMethod from the method name in java


I'm looking for a way to find the IMethod, given the method name as input for developing my eclipse plugin further.

Couldn't figure out a way to do so.

can someone please direct me in the right path.


Solution

  • There can be two approaches:

    1. You can use the ASTVisitor pattern to visit the MethodDeclaration nodes, do a check for name and arguments, and get IMethod from them by resolving the binding. Refer the below posts:

    2. Get the ITypes from the compilation unit and loop through the IMethods, do check for name and arguments to find the required one.

      IType [] typeDeclarationList = unit.getTypes();
      for (IType typeDeclaration : typeDeclarationList) {
         // Get methods under each type declaration.
         IMethod [] methodList = typeDeclaration.getMethods();
         for (IMethod method : methodList) {
            // Logic here.
         }
      }