0

I am looking for a way to extract all the methods calling printc(String) method from a separate main program.

In IntelliJ IDE I can do so by go to the method void printc(String txt) and click Ctrl+Alt+H to get the expected results like this:

callers of a given method]([Groovy-java project)

but I would like to implement such functionality in my own program (without use of an IDE)

For example:

package com.controller;


import com.mgr.ServiceBean;
import com.ser.B;
**//Java file**
public class A extends ServiceBean {
    void a() {
    }

    void a1() {
    super.printc("calle of c");
    }

    void a2(B boo) {
        super.printc("calle of c");
    }


}

======

package com.ser

import com.mgr.C
import com.mgr.ServiceBean
**//Groovy file**
public class B extends ServiceBean {
    void b1() {
    }

    void b2() {
        super.printc("calle of c")
    }
}

=====

package com.mgr
**//Groovy file**
trait C
{
    String test() {
        return "test"
    }

    boolean printc(String methodName)  {
        if(methodName!=null)
            return true
        false
    }
}
=========
package com.mgr
**//Groovy file**
class ServiceBean implements C {

}

============

//Need logic which accepts 
class Main{
    public static void main(String args[]){
        Main m=new Main();
        m.FindUsage("C","void printc(String)")
    }
    Collection FindUsage(String ClassName, String methodname){
        // XXX Logic missing
        return **“[A.a1(),A.a2(Integer),B.b2()]”**
    }
}

NOTE: i can’t use stack trace to get the results as the A, B, C classes are completely different src and I am not running them, main is a separate program.

How can I find all the methods that call a given method in Java? --> doesn't include caller of Groovy classes

siddique
  • 13
  • 1
  • 5
  • 1
    Check out this, I believe this answers your query. https://stackoverflow.com/questions/2650844/getting-call-hierarchy-of-a-method-using-reflections – BHAWANI SINGH Dec 23 '19 at 16:01
  • As i have mentioned i am looking for a separate main program. I cant use StackTrace as it's not in the same program and I am not running A, B, C classes. – siddique Dec 23 '19 at 16:35
  • So you just want a code analyzer? Have you checked if codenarc can help you with that? – cfrick Dec 23 '19 at 17:36

3 Answers3

0

I think you look for a find usage function, take a look at this, it can help you https://www.jetbrains.com/help/idea/find-usages-method-options.html

oumina
  • 31
  • 6
  • Oumina, I am looking for a code to implement to get a List of calle methods like https://i.stack.imgur.com/aff5l.png – siddique Dec 23 '19 at 16:04
0

If all the classes which may come in the desired call hierarchy, are available at run time, then you can traverse over each class and it's methods and create a topological sorted graph, from which you can traverse back the call hierarchy, from the concerned method.

BHAWANI SINGH
  • 729
  • 4
  • 8
0

How can I find all the methods that call a given method in Java? provides code for byte code scanning but it fails for the Groovy files, I end up doing a mixture of by reading each and every line in my source code as a string and then parsing them along with ASM bytecode analysis to extract the needed information of java files. Note: ASM doesn't work for the groovy files as byte code for java file and groovy file are different, in Groovy file you get a larger .class file compared to java's

Sample code:

//stringValue =entire text
//charopen example: { or (
//char close: } or )
private String getFullMethod(String stringValue, String chartOpen, String charClose) {
        String methodName = ''
        int breaceStackIn = 0
        boolean read = true, readOpen = true
        if (stringValue != null) {
            stringValue.eachWithIndex { it, index ->
                if (it.equalsIgnoreCase(chartOpen) && readOpen) {
                    breaceStackIn += 1
                    read = true
                } else if (it.equalsIgnoreCase(charClose) && readOpen) {
                    breaceStackIn -= 1
                    methodName += charClose
                    read = false
                }
                if (read && readOpen) {
                    methodName += it
                }
                if (breaceStackIn == 0 && read == false && readOpen) {
                    readOpen = false
                }
            }
        }
        methodName
    }
siddique
  • 13
  • 1
  • 5