Wednesday 15 September 2010

java - Find out if one method could call another -



java - Find out if one method could call another -

i trying figure out how take java pojo, , analyze method other methods , function call. example, here hardcoded illustration of output. how can create general? need analyze java objects programmatically determine methods phone call if executed. example:

package com.example.analyze; public class main { private static class foo { public void foo(int value, bar bar) { if(value > 5) bar.gaz(); } } private static class bar { public void gaz() { system.out.println("gaz"); } } private static class analyzer { public void analyze(object object){ system.out.println("object method foo phone call bar method gaz"); } } public static void main(string[] args) { foo foo = new foo(); analyzer analyzer = new analyzer(); analyzer.analyze(foo); } }

what need build phone call graph, , inquire if 2 nodes (a caller , callee) connected in phone call graph. isn't easy task.

what need do:

parse source code making application. java parsers relatively easy find. java 1.8 parsers, not easy there's 1 hiding in java compiler can use, , in eclipse jdt; company provides 1 our dms toolkit. build abstract syntax trees same; need code structures. java compiler, jdt, , dms can this. perform name , type resolution. need know definition of every symbol means. java compiler 1 compilation unit @ time. jdt may many files; don't have lot of experience this. dms can big sets of java source files @ once. now need (object) points-to analysis: want know, (object-valued) field, specific instance objects might point-to; tell methods might used trigger. info task inspecting asts , symbol table definitions tell each symbol means. if see x.f=new foo; know f in x can point foo, basic fact. generics , type erasure create messy. if see y.g=z.h, know g in y can point h in z can point to; of course of study z might class inherits z. if see y.g=a[...], know g in y can point object might have been assigned array a. if see y.g=bar(...) know g in y can point bar might return; unfortunately, need phone call graph reply question narrowly. can approximate in various ways conservative answer. know how values related 1 another, have take transitive closure on set, thought of each g in each y can point-to. can more precise reply if take business relationship command , info flow of individual methods, that's more machinery construct. (here more details on points-to analysis.) java compiler computes of info when compiling, not entire scheme of source files; remember processing source files 1 @ time. don't think jdt attempts @ all. our dms doesn't (yet) this, have done systems of c code of 26 1000000 lines; arguably harder problem because people kinds of abusive things pointers including casts lie. finally can build phone call graph. each method, build phone call graph node. each phone call site in method, determine set of callees , link calling node called node. previous step has collected info needed provide these links.

[you might able avoid parsing/name-type resolution part of above using wala, constructed doing of above].

with phone call graph, if want know if can phone call b, find node in phone call graph, , see if there path b.

another note here suggests 6 month task compiler class. think 6 months experienced compiler person, or more (and haven't addressed nasty problems such class loaders , reflective calls).

i think improve off finding solution this, else has built. has; not found or wants part it. might find implementations done in univerisities; there kinds of papers written academics (and supported prototype) compute object-graphs. downwards side systems are prototypes, , beingness build small, unpaid teams of graduates, don't handle border cases allow lone latest version of java (lambdas, anyone?)

java static-analysis

No comments:

Post a Comment