Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2005

Tip: Looking for answers? Try searching our database.

[BCEL] If then else construct - again

Thread view: 
Thomas Zangl - 13 May 2005 07:52 GMT
Hi,

Quote:
I have some piece of code which I want to convert to Java using the
BCEL. Methodcalls etc work fine but now I am stuck with the "if then
else" structure.

Can someone please post an example for something simple like:

if y > x then
  Foo()
else
  Bar()
-- End of Quote

I have already posted this and now I am stuck with the following.
I use the InstructionFactory from BCEL to generate a "IFEQ" branch
insttruction:

aIh = some intruction handle. the target as far as I can figure out.
Must this be some single instruction or can I have multiple ones? (Like
in java if I have an "if" with braces containing multiple statements.)

aBranch = factory_.createBranchInstruction(Constants.IFEQ,aIh);

How can I generate the "else" statement? I thought of adding an
"inverse" branche (IFNE) with the code in the "else" branch but is there
something easier and more elegant?

Maybe somebody can post a small example how to generate this if then
else snippet to BCEL (Using InstructionList, InstructionFactory):

if ( dummyFunction(x) > 0 ) {
  foo(x-1)
} else {
  return 0;
}

TIA!

Best regards,
Signature

----------------------------------------------------------------
,yours Thomas Zangl -thomas@tzi.dhs.org- -TZ1-6BONE-
-http://tzi.dhs.org - http://www.borg-kindberg.ac.at
Use YAMC! now! Get it at http://www.borg-kindberg.ac.at/yamc/

Arnaud Berger - 13 May 2005 08:00 GMT
Hi,

Sure :

if (y > x){
   Foo();
}
else{
  Bar();
}

(forget about the "then" keyword)

Regards,

Arnaud

> Hi,
>
[quoted text clipped - 42 lines]
> -http://tzi.dhs.org - http://www.borg-kindberg.ac.at
> Use YAMC! now! Get it at http://www.borg-kindberg.ac.at/yamc/
Thomas Zangl - 13 May 2005 09:29 GMT
Hi,

> Sure :
>
[quoted text clipped - 6 lines]
>
> (forget about the "then" keyword)

Are you sure you understood my question ... ? I asked about the BCEL
syntax for that piece of code. I found a nice utily (BCELifer) that
helps me a bit.

Best regards,
Signature

----------------------------------------------------------------
,yours Thomas Zangl -thomas@tzi.dhs.org- -TZ1-6BONE-
-http://tzi.dhs.org - http://www.borg-kindberg.ac.at
Use YAMC! now! Get it at http://www.borg-kindberg.ac.at/yamc/

Daniel Sjöblom - 13 May 2005 14:07 GMT
> Can someone please post an example for something simple like:
>
[quoted text clipped - 17 lines]
> "inverse" branche (IFNE) with the code in the "else" branch but is there
> something easier and more elegant?

I already posted a reply to your original post, but you did not reply.
To turn an if/else statement into bytecode the following idiom is used:

java:

if (cond)
{
    code;
}
else
{
    other code;
}

code not in if/else;

bytecode:

    if_invert_cond ELSE
        ...
    code
        ...
    goto ENDIF
ELSE:   ...
        other code
        ...
ENDIF:  code not in if/else

where ELSE and ENDIF are labels. In BCEL you use instruction handles,
which function somewhat like labels, so in BCEL, ELSE == first
instruction handle in else block code, ENDIF == first instruction handle
 in code outside the if/else statement.

Hope that helps.

PS. I'm not sure if this needs to be pointed out, but the if_xxx
instructions work like this: if condition is true, continue execution at
specified adress, else continue execution at next instruction.

Signature

Daniel Sjöblom
Remove _NOSPAM to reply by mail

Daniel Sjöblom - 13 May 2005 14:13 GMT
>     if_invert_cond ELSE
>         ...
[quoted text clipped - 5 lines]
>         ...
> ENDIF:  code not in if/else

The indentation is a bit messed up. More like this (hope it works this
time):

    if_invert_cond ELSE
    ...
    code
    ...
    goto ENDIF
ELSE:
    ...
    other code
    ...
ENDIF:
    code not in if/else

Signature

Daniel Sjöblom
Remove _NOSPAM to reply by mail



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.