1111import com .github .javaparser .ast .nodeTypes .NodeWithName ;
1212import com .github .javaparser .ast .nodeTypes .NodeWithSimpleName ;
1313import com .github .javaparser .ast .stmt .BlockStmt ;
14+ import com .github .javaparser .ast .stmt .ExpressionStmt ;
15+ import com .github .javaparser .ast .stmt .IfStmt ;
1416import com .github .javaparser .ast .stmt .ReturnStmt ;
1517import com .github .javaparser .ast .type .ClassOrInterfaceType ;
1618import com .github .javaparser .ast .type .PrimitiveType ;
@@ -130,6 +132,12 @@ static void addHashCode(ClassOrInterfaceDeclaration target) {
130132 return ;
131133 }
132134
135+ FieldDeclaration field = target .addField (Integer .class , "hashCode" , PRIVATE );
136+ final var variable = field .getVariables ().getFirst ();
137+ field .addAnnotation ("EqEx" );
138+ field .addAnnotation ("Nullable" );
139+ field .addAnnotation ("Internal" );
140+
133141 MethodDeclaration hashCode = target .addMethod ("hashCode" , PUBLIC );
134142 //hashCode.addModifier(FINAL);
135143 hashCode .addAnnotation (Override .class );
@@ -144,9 +152,14 @@ static void addHashCode(ClassOrInterfaceDeclaration target) {
144152
145153 if (args .length == 0 )
146154 assert false : "No defined fields" ;
147- else
148- hashCode .getBody ().get ().addStatement (new ReturnStmt (
149- callObjects ("hash" , args )));
155+ else {
156+ final Expression compute = callObjects ("hash" , args );
157+ final Expression hashCodeIsNull = new BinaryExpr (variable .getNameAsExpression (), new NullLiteralExpr (), BinaryExpr .Operator .EQUALS );
158+ final var setHashCode = new ExpressionStmt (new AssignExpr (variable .getNameAsExpression (), compute , AssignExpr .Operator .ASSIGN ));
159+ hashCode .getBody ().get ().addStatement (
160+ new IfStmt (hashCodeIsNull , setHashCode , null ));
161+ hashCode .getBody ().get ().addStatement (new ReturnStmt (variable .getNameAsExpression ()));
162+ }
150163 }
151164
152165 static void ToString (ClassOrInterfaceDeclaration clazz ) {
@@ -170,6 +183,32 @@ static void ToString(ClassOrInterfaceDeclaration clazz) {
170183 new MethodCallExpr (new StringLiteralExpr (sb ), "formatted" , new NodeList <>(args ))));
171184 }
172185
186+ static void handleRoot (ClassOrInterfaceDeclaration clazz ) {
187+ if (isRoot (clazz )) {
188+ clazz .setInterface (false );
189+ clazz .addModifier (PUBLIC , ABSTRACT );
190+ clazz .getExtendedTypes ().clear ();
191+
192+ for (var field : clazz .getMethods ()) {
193+ field .addModifier (PUBLIC , ABSTRACT );
194+ }
195+ } else if (isNonTerminal (clazz )) {
196+
197+ }
198+ }
199+
200+ static boolean isRoot (ClassOrInterfaceDeclaration clazz ) {
201+ return clazz .getAnnotationByName ("Root" ).isPresent ();
202+ }
203+
204+ static boolean isNonTerminal (ClassOrInterfaceDeclaration clazz ) {
205+ return isRoot (clazz ) || clazz .isInterface ();
206+ }
207+
208+ static boolean isTerminal (ClassOrInterfaceDeclaration clazz ) {
209+ return !isNonTerminal (clazz );
210+ }
211+
173212 private static Expression callObjects (String method , Expression ... args ) {
174213 return new MethodCallExpr (new NameExpr ("Objects" ), method , new NodeList <>(args ));
175214 }
@@ -390,6 +429,8 @@ static void setPackage(ClassOrInterfaceDeclaration target) {
390429 for (var s : permittedTypes .get (target .getNameAsString ())) {
391430 target .getPermittedTypes ().add (new ClassOrInterfaceType (null , s ));
392431 }
432+ //target.setExtendedTypes(new NodeList<>());
433+ target .getMethods ().forEach (it -> it .addModifier (DEFAULT ));
393434 } else {
394435 target .addModifier (FINAL );
395436 target .setImplementedTypes (target .getExtendedTypes ());
@@ -549,7 +590,7 @@ private static boolean isList(VariableDeclarator type) {
549590 }
550591
551592 public static void enforceHierarchy (ClassOrInterfaceDeclaration decl ) {
552- if (decl . getExtendedTypes (). isEmpty ( )) {
593+ if (isTerminal ( decl )) {
553594 decl .addExtendedType ("JavaSourceElement" );
554595 }
555596 }
0 commit comments