Closed sviperll closed 9 years ago
As a workaround, you can use a direct statement. Note that if the block ends up empty then it probably won't print right and thus cause a compile error.
method.body().directStatement("synchronized(lock)");
JBlock synchronizedBlock = method.body().block();
Actually, I've done it like this:
public static JBlock addSynchronizedBlock(JBlock body, final IJExpression lock) {
/*
* HACK:
* jcodemodel currently provides no support for synchronized blocks.
* Implement synchronized block right here:
*/
final JBlock synchronizedBlock = new JBlock(true, true) {
};
body.add(new IJStatement() {
@Override
public void state(JFormatter f) {
f.print("synchronized (").generable(lock).print(") ").generable(synchronizedBlock).newline();
}
});
return synchronizedBlock;
}
I will add a nicer method to do it. Stay tuned
Besides
JBlock synchronizedBlock = method.body().block();
doesn't work. Created block is rendered without parenthesis and indentation no matter what.
Now contained in the trunk. method.body.synchronizedBlock (expr)
.
Implementation class is JSynchronizedBlock
.
I've found no way to generate
synchronized
block like this: