55 */
66public 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}
0 commit comments