Skip to content

Commit 23234fb

Browse files
committed
give example: given a method signature a, we want to find out which methods call a. We keep searching until we find the top method, and output all the links from the top method to a.
1 parent ba21ad9 commit 23234fb

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// script
2+
use coref::java::*
3+
4+
fn default_java_db() -> JavaDB {
5+
return JavaDB::load("coref_java_src.db")
6+
}
7+
8+
// Given one or more function signatures, only one is given in the current example, which can be modified
9+
// signature
10+
fn isChecked(signature: string) -> bool {
11+
[
12+
{"HelloWorld.test2:void()"},
13+
]
14+
}
15+
16+
// You can view the signature, line number, and file location of each callable by outputting the following function:
17+
fn signature_name(signature: string, line: int, fileName: string) -> bool {
18+
let (db = default_java_db()){
19+
for (callable in Callable(db)){
20+
if (signature = callable.getSignature() && fileName = callable.getLocation().getFile().getName()
21+
&& line = callable.getLocation().getStartLineNumber()) {
22+
return true
23+
}
24+
}
25+
}
26+
}
27+
28+
// Determine whether it is a callable corresponding to the function signature
29+
fn checkCallable(c: Callable)-> bool {
30+
if (isChecked(c.getSignature())) {
31+
return true
32+
}
33+
}
34+
35+
36+
// Do an upward search
37+
// Search upwards for the end condition, and restrict some properties of the end node
38+
fn callerLimit(c: Callable) -> bool {
39+
return false
40+
}
41+
fn getAncestorCallerEndWithLimit(c: Callable) -> *Callable {
42+
// If the current function has been limited
43+
if (callerLimit(c)) {
44+
yield c
45+
}
46+
if (!callerLimit(c)) {
47+
// Get the calling function of the current functio
48+
yield c.getCaller()
49+
// The current node is multiple layers above, and recursive calls are required to obtain all calling functions
50+
for (tmp in c.getCaller()) {
51+
yield getAncestorCallerEndWithLimit(tmp)
52+
}
53+
}
54+
}
55+
56+
fn getAllLimitedCallable(c:Callable)->*Callable{
57+
yield c
58+
yield getAncestorCallerEndWithLimit(c)
59+
}
60+
61+
// At the same time, output the class corresponding to callable
62+
fn getCallGraph(callMethodName:string, callClassName: string,
63+
calleeMethodName:string, calleeClassName: string) -> bool {
64+
let (db = default_java_db()){
65+
for (callable in Callable(db)){
66+
if (checkCallable(callable)) {
67+
for (call in getAllLimitedCallable(callable), callee in getAllLimitedCallable(callable)){
68+
if (call != callee && callee in call.getCallee()) {
69+
for (callMethod in Method(db), calleeMethod in Method(db)) {
70+
if (callMethod.key_eq(call) && calleeMethod.key_eq(callee)) {
71+
if (callMethodName = callMethod.getName() && callClassName = callMethod.getBelongedClass().getQualifiedName() &&
72+
calleeMethodName = callee.getName() && calleeClassName = calleeMethod.getBelongedClass().getQualifiedName()) {
73+
return true
74+
}
75+
}
76+
}
77+
}
78+
}
79+
}
80+
81+
}
82+
}
83+
}
84+
85+
fn main() {
86+
output(getCallGraph())
87+
// If you want to see the signature in the output add the following line back
88+
// output(signature_name())
89+
}

0 commit comments

Comments
 (0)