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
@@ -112,6 +115,8 @@ private StateMachine build(String className, CtTypeReference<?> type) throws LJE
112115 if (!className .equals (function .getTargetClass ()) || !function .hasStateChange ())
113116 continue ;
114117 if (CtExecutableReference .CONSTRUCTOR_NAME .equals (function .getName ())) {
118+ if (abstractType (type ) || privateConstructor (function , type ))
119+ continue ; // cannot be suggested as a new object
115120 Optional <Set <State >> created = createdStates (function );
116121 created .ifPresent (s -> constructors .put (function , s ));
117122 continue ;
@@ -135,6 +140,36 @@ private boolean relatedClassChangesState(String className, CtTypeReference<?> ty
135140 .anyMatch (other -> other .isSubtypeOf (type ) || type .isSubtypeOf (other ));
136141 }
137142
143+ private static boolean abstractType (CtTypeReference <?> type ) {
144+ CtType <?> declaration = type .getTypeDeclaration ();
145+ return declaration == null || declaration .isInterface () || declaration .isAbstract ();
146+ }
147+
148+ /**
149+ * Whether {@code constructor} cannot be called from outside its class: it is private, or the class is an enum. A
150+ * constructor that is not found in the class's declaration is assumed callable. The machine is shared by every
151+ * caller, so a private constructor is left out even for calls inside its class.
152+ */
153+ private static boolean privateConstructor (RefinedFunction constructor , CtTypeReference <?> type ) {
154+ try {
155+ CtType <?> declaration = type .getTypeDeclaration ();
156+ if (declaration .isEnum ())
157+ return true ;
158+ if (!(declaration instanceof CtClass <?> declared ))
159+ return false ;
160+ List <String > parameters = constructor .getArguments ().stream ().map (a -> typeName (a .getType ())).toList ();
161+ return declared .getConstructors ().stream ()
162+ .filter (c -> c .getParameters ().stream ().map (p -> typeName (p .getType ())).toList ().equals (parameters ))
163+ .anyMatch (CtModifiable ::isPrivate );
164+ } catch (RuntimeException e ) {
165+ return false ; // a declaration that cannot be built is not evidence that the constructor is private
166+ }
167+ }
168+
169+ private static String typeName (CtTypeReference <?> type ) {
170+ return type == null ? "" : type .getQualifiedName ();
171+ }
172+
138173 /** The transitions of {@code method}, or nothing if they depend on more than the state of {@code this} */
139174 private Optional <List <Transition >> transitionsOf (RefinedFunction method ) throws LJError {
140175 if (!canModel (method ))
0 commit comments