Skip to content

Commit b995fd6

Browse files
committed
Added support for synchronized keyword in some Declaration subclasses.
1 parent bd659e4 commit b995fd6

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

src/org/jcodegen/java/ConstructorDeclaration.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public ConstructorDeclaration markAsAbstract() {
3434
throw new UnsupportedOperationException("Constructors cannot be abstract.");
3535
}
3636

37+
@Override
38+
public ConstructorDeclaration markAsSynchronized() {
39+
throw new UnsupportedOperationException("Constructors cannot be synchronized.");
40+
}
41+
3742

3843
@Override
3944
public String toString() {

src/org/jcodegen/java/Declaration.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
*/
66
public abstract class Declaration<T extends Declaration<T>> extends JavaCodeBlock<T> {
77

8+
// TODO :
9+
// revisit subclass hierarchy to prevent creation of illegal constructs in java,
10+
// like synchronized constructors or abstract fields,
11+
// at compile time instead of throwing UnsupportedOperationExceptions
12+
813
private final String name;
914
//private final String codeEntity;
1015

@@ -13,6 +18,7 @@ public abstract class Declaration<T extends Declaration<T>> extends JavaCodeBloc
1318
private boolean isAbstract = false;
1419
private boolean isFinal = false;
1520
private boolean isStatic = false;
21+
private boolean isSynchronized = false;
1622

1723
private String annotations = null;
1824

@@ -49,12 +55,20 @@ public T markAsFinal() {
4955

5056
public T markAsStatic() {
5157
if (isAbstract)
52-
throw new IllegalArgumentException(getAbstractAndFinalErrorMessage());
58+
throw new IllegalArgumentException(getAbstractAndStaticErrorMessage());
5359

5460
isStatic = true;
5561
return getThis();
5662
}
5763

64+
public T markAsSynchronized() {
65+
if (isAbstract)
66+
throw new IllegalArgumentException(getAbstractAndSynchronizedErrorMessage());
67+
68+
isSynchronized = true;
69+
return getThis();
70+
}
71+
5872
public T annotate(final String annotations) {
5973
this.annotations = annotations;
6074
return getThis();
@@ -73,6 +87,10 @@ protected String getAbstractAndStaticErrorMessage() {
7387
return getKeyword() + " cannot be abstract AND static";
7488
}
7589

90+
protected String getAbstractAndSynchronizedErrorMessage() {
91+
return getKeyword() + " cannot be abstract AND synchronized";
92+
}
93+
7694
protected void appendAnnotations(final StringBuilder buf) {
7795
if (annotations != null) {
7896
buf.append(getTabs());
@@ -93,5 +111,7 @@ protected void appendDeclarationStart(final StringBuilder buf) {
93111
buf.append("static ");
94112
if (isFinal)
95113
buf.append("final ");
114+
if (isSynchronized)
115+
buf.append("synchronized ");
96116
}
97117
}

src/org/jcodegen/java/JavaClass.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ public JavaClass implementsGenericInterface(final String implementedInterface, f
4242
}
4343

4444

45+
@Override
46+
public JavaClass markAsSynchronized() {
47+
throw new UnsupportedOperationException("Class cannot be synchronized.");
48+
}
49+
50+
4551
@Override
4652
public String toString() {
4753
final StringBuilder buf = new StringBuilder();

src/org/jcodegen/java/VarDeclaration.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ protected VarDeclaration getThis() {
5252
return this;
5353
}
5454

55+
@Override
56+
public VarDeclaration markAsAbstract() {
57+
throw new UnsupportedOperationException("Field or variable cannot be abstract.");
58+
}
59+
60+
@Override
61+
public VarDeclaration markAsSynchronized() {
62+
throw new UnsupportedOperationException("Field or variable cannot be synchronized.");
63+
}
64+
5565

5666
@Override
5767
public String toString() {

0 commit comments

Comments
 (0)