1919import liquidjava .rj_language .Predicate ;
2020import liquidjava .utils .Utils ;
2121import liquidjava .utils .constants .Keys ;
22+ import spoon .reflect .declaration .CtClass ;
2223import spoon .reflect .declaration .CtElement ;
24+ import spoon .reflect .declaration .CtModifiable ;
25+ import spoon .reflect .declaration .CtType ;
2326import spoon .reflect .reference .CtExecutableReference ;
2427import spoon .reflect .reference .CtTypeReference ;
2528
@@ -131,6 +134,8 @@ private StateMachine build(String className, CtTypeReference<?> type) throws LJE
131134 if (!className .equals (function .getTargetClass ()) || !function .hasStateChange ())
132135 continue ;
133136 if (CtExecutableReference .CONSTRUCTOR_NAME .equals (function .getName ())) {
137+ if (abstractType (type ) || privateConstructor (function , type ))
138+ continue ; // cannot be suggested as a new object
134139 Optional <Set <State >> created = createdStates (function );
135140 created .ifPresent (s -> constructors .put (function , s ));
136141 continue ;
@@ -154,6 +159,36 @@ private boolean relatedClassChangesState(String className, CtTypeReference<?> ty
154159 .anyMatch (other -> other .isSubtypeOf (type ) || type .isSubtypeOf (other ));
155160 }
156161
162+ private static boolean abstractType (CtTypeReference <?> type ) {
163+ CtType <?> declaration = type .getTypeDeclaration ();
164+ return declaration == null || declaration .isInterface () || declaration .isAbstract ();
165+ }
166+
167+ /**
168+ * Whether {@code constructor} cannot be called from outside its class: it is private, or the class is an enum. A
169+ * constructor that is not found in the class's declaration is assumed callable. The machine is shared by every
170+ * caller, so a private constructor is left out even for calls inside its class.
171+ */
172+ private static boolean privateConstructor (RefinedFunction constructor , CtTypeReference <?> type ) {
173+ try {
174+ CtType <?> declaration = type .getTypeDeclaration ();
175+ if (declaration .isEnum ())
176+ return true ;
177+ if (!(declaration instanceof CtClass <?> declared ))
178+ return false ;
179+ List <String > parameters = constructor .getArguments ().stream ().map (a -> typeName (a .getType ())).toList ();
180+ return declared .getConstructors ().stream ()
181+ .filter (c -> c .getParameters ().stream ().map (p -> typeName (p .getType ())).toList ().equals (parameters ))
182+ .anyMatch (CtModifiable ::isPrivate );
183+ } catch (RuntimeException e ) {
184+ return false ; // a declaration that cannot be built is not evidence that the constructor is private
185+ }
186+ }
187+
188+ private static String typeName (CtTypeReference <?> type ) {
189+ return type == null ? "" : type .getQualifiedName ();
190+ }
191+
157192 /** The transitions of {@code method}, or nothing if they depend on more than the state of {@code this} */
158193 private Optional <List <Transition >> transitionsOf (RefinedFunction method ) throws LJError {
159194 if (!canModel (method ))
0 commit comments