Skip to content

Commit c435513

Browse files
committed
Better support for prefix and postfix increment and decrement operators (++/--). The primary gap was in the ability to use ++var or --var as pure statements rather than just expressions.
1 parent e31da7d commit c435513

File tree

5 files changed

+74
-11
lines changed

5 files changed

+74
-11
lines changed

codemodel/src/main/java/com/sun/codemodel/JExpr.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,42 @@ public static JExpression assignPlus(JAssignmentTarget lhs, JExpression rhs) {
5959
return new JAssignment(lhs, rhs, "+");
6060
}
6161

62+
public static JStatement incr(final JExpression expression) {
63+
return new JStatement() {
64+
public void state(JFormatter f) {
65+
JOp.incr(expression).generate(f);
66+
f.p(';').nl();
67+
}
68+
};
69+
}
70+
71+
public static JStatement preincr(final JExpression expression) {
72+
return new JStatement() {
73+
public void state(JFormatter f) {
74+
JOp.preincr(expression).generate(f);
75+
f.p(';').nl();
76+
}
77+
};
78+
}
79+
80+
public static JStatement decr(final JExpression expression) {
81+
return new JStatement() {
82+
public void state(JFormatter f) {
83+
JOp.decr(expression).generate(f);
84+
f.p(';').nl();
85+
}
86+
};
87+
}
88+
89+
public static JStatement predecr(final JExpression expression) {
90+
return new JStatement() {
91+
public void state(JFormatter f) {
92+
JOp.predecr(expression).generate(f);
93+
f.p(';').nl();
94+
}
95+
};
96+
}
97+
6298
public static JInvocation _new(JClass c) {
6399
return new JInvocation(c);
64100
}
@@ -179,7 +215,7 @@ public static JExpression _null() {
179215
public static final JExpression FALSE = new JAtom("false");
180216

181217
public static JExpression lit(boolean b) {
182-
return b?TRUE:FALSE;
218+
return b ? TRUE : FALSE;
183219
}
184220

185221
public static JExpression lit(int n) {
@@ -297,7 +333,7 @@ public static JExpression lit(String s) {
297333
public static JExpression direct( final String source ) {
298334
return new JExpressionImpl(){
299335
public void generate( JFormatter f ) {
300-
f.p('(').p(source).p(')');
336+
f.p('(').p(source).p(')');
301337
}
302338
};
303339
}

codemodel/src/main/java/com/sun/codemodel/JExpression.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@
4545
* A Java expression.
4646
*
4747
* <p>
48-
* Unlike most of CodeModel, JExpressions are built bottom-up (
49-
* meaning you start from leaves and then gradually build compliated expressions
50-
* by combining them.)
48+
* Unlike most of CodeModel, JExpressions are built bottom-up
49+
* (meaning you start from leaves and then gradually build complicated expressions
50+
* by combining them).
5151
*
5252
* <p>
5353
* {@link JExpression} defines a series of composer methods,
@@ -76,11 +76,21 @@ public interface JExpression extends JGenerable {
7676
*/
7777
JExpression incr();
7878

79+
/**
80+
* Returns "++[this]" from "[this]".
81+
*/
82+
JExpression preincr();
83+
7984
/**
8085
* Returns "[this]--" from "[this]".
8186
*/
8287
JExpression decr();
8388

89+
/**
90+
* Returns "--[this]" from "[this]".
91+
*/
92+
JExpression predecr();
93+
8494
/**
8595
* Returns "[this]+[right]"
8696
*/

codemodel/src/main/java/com/sun/codemodel/JExpressionImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,18 @@ public final JExpression incr() {
6969
return JOp.incr(this);
7070
}
7171

72+
public final JExpression preincr() {
73+
return JOp.preincr(this);
74+
}
75+
7276
public final JExpression decr() {
7377
return JOp.decr(this);
7478
}
7579

80+
public final JExpression predecr() {
81+
return JOp.predecr(this);
82+
}
83+
7684
public final JExpression plus(JExpression right) {
7785
return JOp.plus(this, right);
7886
}

codemodel/src/main/java/com/sun/codemodel/JFormatter.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ private boolean needSpace(char c1, char c2) {
184184
switch (c2) {
185185
case '{':
186186
case '}':
187-
case '+':
188187
case '>':
189188
case '@':
190189
return true;

codemodel/src/main/java/com/sun/codemodel/JOp.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@
4747

4848
abstract public class JOp {
4949

50-
private JOp() {
51-
}
50+
private JOp() { }
5251

5352

5453
/**
@@ -65,11 +64,12 @@ static private class UnaryOp extends JExpressionImpl {
6564

6665
protected String op;
6766
protected JExpression e;
68-
protected boolean opFirst = true;
67+
protected final boolean opFirst;
6968

7069
UnaryOp(String op, JExpression e) {
7170
this.op = op;
7271
this.e = e;
72+
opFirst = true;
7373
}
7474

7575
UnaryOp(JExpression e, String op) {
@@ -84,7 +84,6 @@ public void generate(JFormatter f) {
8484
else
8585
f.p('(').g(e).p(op).p(')');
8686
}
87-
8887
}
8988

9089
public static JExpression minus(JExpression e) {
@@ -110,23 +109,34 @@ static private class TightUnaryOp extends UnaryOp {
110109
super(e, op);
111110
}
112111

112+
TightUnaryOp(String op, JExpression e) {
113+
super(op, e);
114+
}
115+
113116
public void generate(JFormatter f) {
114117
if (opFirst)
115118
f.p(op).g(e);
116119
else
117120
f.g(e).p(op);
118121
}
119-
120122
}
121123

122124
public static JExpression incr(JExpression e) {
123125
return new TightUnaryOp(e, "++");
124126
}
125127

128+
public static JExpression preincr(JExpression e) {
129+
return new TightUnaryOp("++", e);
130+
}
131+
126132
public static JExpression decr(JExpression e) {
127133
return new TightUnaryOp(e, "--");
128134
}
129135

136+
public static JExpression predecr(JExpression e) {
137+
return new TightUnaryOp("--", e);
138+
}
139+
130140

131141
/* -- Binary operators -- */
132142

0 commit comments

Comments
 (0)