List of all unrealized methods invoked by the method

We have a huge project in which many methods were announced in advance, and implementations are carried out. All declared methods have a body that simply throws an exception, say, UnimplException. Now, since the methods have been declared and the actual (compiled) body has been provided, they can be called from other methods.

Now the question is, is there a way to list all such unrealized (having only a compiled body, throwing a specific exception) methods defined by a particular method?

To illustrate more (code should convey an idea, not a strictly compiler):

class A {
    methA () {
        throw new UnimplException();
    }
}

class B {
    methB () {
        // proper body
        // and calls methA
        A.methA();
        // does something else
        // and returns.
    }
}

class C {
    methC () {
        // proper body
        // calls methB
        B.methB();
    }
}

, , , methC, , methA, methC methB ( , ), methA , , . , , .

JavaAssist, , , , , , , .

:)

+4
1

: https://github.com/gousiosg/java-callgraph? , , Java, jar. , , .

- :

  • callgraph .
  • -.
  • , .

, , 1 - :

A:methA -> UnimplException:<init>
B:methB -> A:methA
C:methC -> B:methB

Multimap :

// this is populated from the output of the callgraph code
com.google.common.collect.Multimap<String, String> methodMap;

void checkAllMethods() {
  for (String method : methodMap.keySet()) {
    List<String> callStack = new ArrayList<>();
    if (doesMethodThrowUnimplException(method, callStack)) {
      System.out.println(method);
      // can print callStack too if interested
    }
  }
}  

boolean doesMethodThrowUnimplException(String method, List<String> callStack) {
  for (String child : methodMap.get(method)) {
    // have to check the exact method name from callgraph
    if (child.equals("UnimplException:<init>")) {
      return true;
    }
    // recurse into child if not already seen
    if (!callStack.contains(child)) {
      callStack.add(child);
      if (doesMethodThrowUnimplException(child, callStack)) {
        return true;
      }
      callStack.remove(callStack.size() - 1);
    }
  }
  return false;
}

, , UnimplException, , , , .

- - / , , , , .

+2

Source: https://habr.com/ru/post/1607547/


All Articles