Skip to content

Commit e27a487

Browse files
committed
Add support for try-catch-finally blocks
1 parent 37d39e6 commit e27a487

File tree

4 files changed

+182
-1
lines changed

4 files changed

+182
-1
lines changed

build.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<target name="compile" description="Compile code" depends="native2ascii">
1414
<mkdir dir="${build.classes}" />
15-
<javac srcdir="${src-ascii.dir}" destdir="${build.classes}">
15+
<javac srcdir="${src-ascii.dir}" destdir="${build.classes}" source="1.6" target="1.6" compiler="javac1.6">
1616
<classpath>
1717
<fileset dir="${build.lib}">
1818
<include name="**/*.jar"/>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package org.jcodegen.java;
2+
3+
public class CatchBlock extends DeclarationWithArguments<CatchBlock> {
4+
5+
public CatchBlock(final FunctionArgument functionArgument) {
6+
this(functionArgument, 0);
7+
}
8+
9+
public CatchBlock(final String functionArgument) {
10+
this(functionArgument, 0);
11+
}
12+
13+
public CatchBlock(final FunctionArgument functionArgument, final int indentLevel) {
14+
super("catch", indentLevel, "catch");
15+
super.addArgument(functionArgument);
16+
}
17+
18+
public CatchBlock(final String functionArgument, final int indentLevel) {
19+
super("catch", indentLevel, "catch");
20+
super.addArgument(functionArgument);
21+
}
22+
23+
@Override
24+
protected CatchBlock getThis() {
25+
return this;
26+
}
27+
28+
@Override
29+
public CatchBlock addArgument(String arg) {
30+
throw new UnsupportedOperationException();
31+
}
32+
33+
@Override
34+
public CatchBlock addArguments(String... args) {
35+
throw new UnsupportedOperationException();
36+
}
37+
38+
@Override
39+
public CatchBlock addArgument(FunctionArgument arg) {
40+
throw new UnsupportedOperationException();
41+
}
42+
43+
@Override
44+
public CatchBlock addArguments(FunctionArgument... args) {
45+
throw new UnsupportedOperationException();
46+
}
47+
48+
@Override
49+
public CatchBlock addException(String ex) {
50+
throw new UnsupportedOperationException();
51+
}
52+
53+
@Override
54+
public CatchBlock addExceptions(String... exex) {
55+
throw new UnsupportedOperationException();
56+
}
57+
58+
@Override
59+
public CatchBlock visibility(Visibility visibility) {
60+
throw new UnsupportedOperationException();
61+
}
62+
63+
@Override
64+
public CatchBlock markAsAbstract() {
65+
throw new UnsupportedOperationException();
66+
}
67+
68+
@Override
69+
public CatchBlock markAsFinal() {
70+
throw new UnsupportedOperationException();
71+
}
72+
73+
@Override
74+
public CatchBlock markAsStatic() {
75+
throw new UnsupportedOperationException();
76+
}
77+
78+
@Override
79+
public CatchBlock markAsSynchronized() {
80+
throw new UnsupportedOperationException();
81+
}
82+
83+
@Override
84+
public CatchBlock annotate(String annotations) {
85+
throw new UnsupportedOperationException();
86+
}
87+
88+
@Override
89+
public String toString() {
90+
final StringBuilder buf = new StringBuilder();
91+
92+
buf.append(" catch ");
93+
94+
appendArgumentList(buf);
95+
buf.append(" ");
96+
97+
appendContent(buf);
98+
99+
buf.append("\n");
100+
101+
return buf.toString();
102+
}
103+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.jcodegen.java;
2+
3+
public class FinallyBlock extends JavaCodeBlock<FinallyBlock> {
4+
5+
public FinallyBlock() {
6+
this(0);
7+
}
8+
9+
public FinallyBlock(int indentationLevel) {
10+
super("finally", indentationLevel);
11+
}
12+
13+
@Override
14+
protected FinallyBlock getThis() {
15+
return this;
16+
}
17+
18+
@Override
19+
public String toString() {
20+
final StringBuilder buf = new StringBuilder();
21+
22+
buf.append(" finally ");
23+
appendContent(buf);
24+
25+
return buf.toString();
26+
}
27+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.jcodegen.java;
2+
3+
public class TryBlock extends JavaCodeBlock<TryBlock> {
4+
5+
private CatchBlock catchBlock;
6+
private FinallyBlock finallyBlock;
7+
8+
public TryBlock() {
9+
this(0);
10+
}
11+
12+
public TryBlock(int indentationLevel) {
13+
super("try", indentationLevel);
14+
}
15+
16+
@Override
17+
protected TryBlock getThis() {
18+
return this;
19+
}
20+
21+
public TryBlock addCatchBlock(CatchBlock catchBlock) {
22+
this.catchBlock = catchBlock;
23+
return this;
24+
}
25+
26+
public TryBlock addFinallyBlock(FinallyBlock finallyBlock) {
27+
this.finallyBlock = finallyBlock;
28+
return this;
29+
}
30+
31+
@Override
32+
public String toString() {
33+
final StringBuilder buf = new StringBuilder();
34+
35+
buf.append(getTabs()).append("try ");
36+
appendContent(buf);
37+
38+
if (catchBlock != null) {
39+
catchBlock.setIndentationLevel(getIndentationLevel());
40+
buf.append(catchBlock);
41+
}
42+
43+
if (finallyBlock != null) {
44+
finallyBlock.setIndentationLevel(getIndentationLevel());
45+
buf.append(finallyBlock);
46+
}
47+
48+
return buf.toString();
49+
}
50+
51+
}

0 commit comments

Comments
 (0)