3

I've been trying to learn how to use Suns codemodel library and I'm absolutely stumped with generating for loops and if-else blocks. I'm struggling with how to generate the conditions for the if-else blocks and the for loops, but also on how to generate the body of these as well.

For example:

if (condition) { //How is this condition generated?
     //How is this body filled?
} else {

} 

For the loop:

for(condition) {  //How is this condition generated?
   //How is this body filled?
}
Dexygen
  • 12,287
  • 13
  • 80
  • 147
  • for is unrelated to if and else. It's unclear what you want to do here. If you want to continue to do something while a condition is true, then use while(condition). If you want to either do one thing or another thing depending on the truth of a condition, just use if and else. Bodies are just a series of statements, same as wherever you're putting your if/else. – Vitruvie Nov 25 '13 at 04:06
  • Sorry I was unclear in what I wrote. I can't figure out how to write the body of the if-else blocks as well as how to write the condition for the for loop and its body as well. – user3027681 Nov 25 '13 at 04:10
  • To anyone as confused by this question as I was, Sun Codemodel is apparently a Java library for generating Java code, so the question is not "How do I program?" but "How does this particular library work?" – Boann Nov 25 '13 at 04:24

1 Answers1

4

Im going to assume you already have a class and method defined.

To write a condition if/else statement you need to use the _if() and _else() methods on the JBody class. This adds the statement to the body of the method you have defined. from these methods you can reference and add to their bodies by calling the _then() method on the _if() or _else() returns the JBody directly. Here's an example:

JConditional condition = body._if(input.lt(JExpr.lit(42)));
condition._then().add(
    codeModel.ref(System.class).staticRef("out").invoke("println").arg(JExpr.lit("hello"))); 
condition._else().add(
    codeModel.ref(System.class).staticRef("out").invoke("println").arg(JExpr.lit("world")));

which outputs:

if (input< 42) {
    System.out.println("hello");
} else {
    System.out.println("world");
}

To write a for loop, there are a couple of options. The traditional for loop is written using the _for() method on the JBlock, which allows you to chain the init(), test() and update() methods corresponding to the various parts of the for loop declaration:

JForLoop forLoop = body._for();
JVar ivar = forLoop.init(codeModel.INT, "i", JExpr.lit(0));
forLoop.test(ivar.lt(JExpr.lit(42)));
forLoop.update(ivar.assignPlus(JExpr.lit(1)));

forLoop.body().add(
    codeModel.ref(System.class).staticRef("out").invoke("println").arg(ivar));

which outputs:

for (int i = 0; (i< 42); i += 1) {
    System.out.println(i);
}

For kicks, here's a working example: https://gist.github.com/johncarl81/7647146

John Ericksen
  • 10,995
  • 4
  • 45
  • 75
  • This is exactly the answer I was looking for, thank you so much! – user3027681 Nov 25 '13 at 21:03
  • There is no JBody class on [CodeModel](https://codemodel.java.net/nonav/apidocs/)? Am I missing something? – Utku Jun 28 '16 at 09:13
  • No, it is returned when defining a block. For instance when calling] [method.body()](https://codemodel.java.net/nonav/apidocs/com/sun/codemodel/JMethod.html#body()) – John Ericksen Jun 28 '16 at 14:02