detecting reachability of statement - Entry and exit points of a method

23 views
Skip to first unread message

Alibek

unread,
Dec 19, 2010, 12:46:33 PM12/19/10
to JavaParser
Hello !

can someone help me - is there easy way to detect whether last "}"
statement is reachable?

I would like to add extra calls before every normal exit points of a
method, which are either "return" or last "}".
It seems that I can relatively easily detect each "return" by
iterating thru all statements and checking whether statement is
instance of ReturnStmt. But, I cannot just simply go for last "}",
because it can result in unreachable code error in some cases.

regards,
Alibek

Ahti Legonkov

unread,
Dec 19, 2010, 7:20:34 PM12/19/10
to JavaParser


On Dec 19, 7:46 pm, Alibek <alibek.jor...@gmail.com> wrote:
> Hello !
>
> can someone help me - is there easy way to detect whether last "}"
> statement is reachable?

There is no easy way to do reachability analysis. Search for "code
reachability analysis" and you'll get a lot of articles that will get
you started with this.
But there is an easier way to achieve the desired effect. Generate
code that wraps the method body in try...catch...finally, initialize a
flag before try, modify it in catch, an rethrow, in finally check the
flag and have the code you'd want yo run on each return.

--
lego
Reply all
Reply to author
Forward
0 new messages