我使用JExpr.plus()
方法,形成一个String和语法是正确的,但它有很多括号。 例如:
JExpr.lit("ONE").plus(JExpr.lit("TWO")).plus(JExpr.lit("THREE"))
回报
(("ONE" + "TWO") + "THREE")
我想它是
"ONE" + "TWO" + "THREE"
我使用JExpr.plus()
方法,形成一个String和语法是正确的,但它有很多括号。 例如:
JExpr.lit("ONE").plus(JExpr.lit("TWO")).plus(JExpr.lit("THREE"))
回报
(("ONE" + "TWO") + "THREE")
我想它是
"ONE" + "TWO" + "THREE"
它看起来像codemodel现在你不能避免加括号的。 加号(+)被认为是一个BinaryOp,其与下面的类生成它的代码:
在com.sun.codemodel.JOp
:
static private class BinaryOp extends JExpressionImpl {
String op;
JExpression left;
JGenerable right;
BinaryOp(String op, JExpression left, JGenerable right) {
this.left = left;
this.op = op;
this.right = right;
}
public void generate(JFormatter f) {
f.p('(').g(left).p(op).g(right).p(')');
}
}
注意fp('(')
和.p(')')
添加括号的烘焙成的代码和无法避免。 这就是说,你可以改变codemodel做你所需要的,因为它是开源的。 就个人而言,我不认为有必要为括号是在其他情况下是有用的。