Skip to content

Commit d7ce256

Browse files
Do not suggest constructors that cannot be called
A state machine left out constructors only when it could not model them, so hints could suggest creating an abstract class, an interface, an enum or a class whose constructor is private. Constructors of such types are now left out of the machine. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 359d0e9 commit d7ce256

9 files changed

Lines changed: 183 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package testSuite;
2+
3+
import liquidjava.specification.StateRefinement;
4+
import liquidjava.specification.StateSet;
5+
6+
// The constructor creates the needed state, but the class is abstract, so no new object can be suggested
7+
@StateSet({"fresh", "ready"})
8+
public abstract class ErrorStateHintUnknownAbstract {
9+
10+
@StateRefinement(to = "ready(this)")
11+
public ErrorStateHintUnknownAbstract() {}
12+
13+
@StateRefinement(from = "ready(this)", to = "fresh(this)")
14+
public void use() {}
15+
16+
static void fail(ErrorStateHintUnknownAbstract value) {
17+
value.use(); // Expect: State Refinement Error
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package testSuite;
2+
3+
import liquidjava.specification.StateRefinement;
4+
import liquidjava.specification.StateSet;
5+
6+
// The same class as ErrorStateHintUnknownAbstract, but concrete: the constructor is suggested
7+
@StateSet({"fresh", "ready"})
8+
public class ErrorStateHintUnknownConcrete {
9+
10+
@StateRefinement(to = "ready(this)")
11+
public ErrorStateHintUnknownConcrete() {}
12+
13+
@StateRefinement(from = "ready(this)", to = "fresh(this)")
14+
public void use() {}
15+
16+
static void fail(ErrorStateHintUnknownConcrete value) {
17+
value.use(); // Expect: State Refinement Error
18+
}
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package testSuite;
2+
3+
import liquidjava.specification.StateRefinement;
4+
import liquidjava.specification.StateSet;
5+
6+
// The constructor creates the needed state, but it is private, so no new object can be suggested
7+
@StateSet({"fresh", "ready"})
8+
public class ErrorStateHintUnknownPrivateConstructor {
9+
10+
@StateRefinement(to = "ready(this)")
11+
private ErrorStateHintUnknownPrivateConstructor() {}
12+
13+
@StateRefinement(from = "ready(this)", to = "fresh(this)")
14+
public void use() {}
15+
}
16+
17+
class ErrorStateHintUnknownPrivateConstructorUser {
18+
static void fail(ErrorStateHintUnknownPrivateConstructor value) {
19+
value.use(); // Expect: State Refinement Error
20+
}
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package testSuite.classes.state_hint_abstract_spec_error;
2+
3+
import liquidjava.specification.ExternalRefinementsFor;
4+
import liquidjava.specification.StateRefinement;
5+
import liquidjava.specification.StateSet;
6+
7+
// A spec for an abstract JDK class: its constructor creates the needed state, but no client can call new InputStream()
8+
@ExternalRefinementsFor("java.io.InputStream")
9+
@StateSet({"open", "closed"})
10+
public interface InputStreamRefinements {
11+
12+
@StateRefinement(to = "open(this)")
13+
void InputStream();
14+
15+
@StateRefinement(from = "open(this)")
16+
int read();
17+
18+
@StateRefinement(from = "open(this)", to = "closed(this)")
19+
void close();
20+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package testSuite.classes.state_hint_abstract_spec_error;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
6+
public class Use {
7+
8+
static int first(InputStream in) throws IOException {
9+
return in.read(); // Expect: State Refinement Error
10+
}
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package testSuite.classes.state_hint_interface_spec_error;
2+
3+
import liquidjava.specification.ExternalRefinementsFor;
4+
import liquidjava.specification.StateRefinement;
5+
import liquidjava.specification.StateSet;
6+
7+
// A spec for an interface: it has no constructor, so no new object is suggested
8+
@ExternalRefinementsFor("java.util.concurrent.RunnableFuture")
9+
@StateSet({"pending", "done"})
10+
public interface RunnableFutureRefinements {
11+
12+
@StateRefinement(from = "pending(this)", to = "done(this)")
13+
void run();
14+
15+
@StateRefinement(from = "done(this)")
16+
boolean isDone();
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package testSuite.classes.state_hint_interface_spec_error;
2+
3+
import java.util.concurrent.RunnableFuture;
4+
5+
public class Use {
6+
7+
static boolean finished(RunnableFuture<String> task) {
8+
return task.isDone(); // Expect: State Refinement Error
9+
}
10+
}

‎liquidjava-verifier/src/main/java/liquidjava/processor/state_machine/StateMachineBuilder.java‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
import liquidjava.rj_language.Predicate;
2020
import liquidjava.utils.Utils;
2121
import liquidjava.utils.constants.Keys;
22+
import spoon.reflect.declaration.CtClass;
2223
import spoon.reflect.declaration.CtElement;
24+
import spoon.reflect.declaration.CtModifiable;
25+
import spoon.reflect.declaration.CtType;
2326
import spoon.reflect.reference.CtExecutableReference;
2427
import 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))

‎liquidjava-verifier/src/test/java/liquidjava/api/tests/TestStateHints.java‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,37 @@ void thisReceiverIsNotToldToCreateANewObject() {
181181
new Hint(Kind.NEEDED_STATE, "The state of this is unknown here, and this call needs it to be open"));
182182
}
183183

184+
@Test
185+
void abstractClassIsNotToldToCreateANewObject() {
186+
assertHints("ErrorStateHintUnknownAbstract.java",
187+
new Hint(Kind.NEEDED_STATE, "The state of value is unknown here, and this call needs it to be ready"));
188+
}
189+
190+
@Test
191+
void abstractClassWithAnExternalSpecIsNotToldToCreateANewObject() {
192+
assertHints("classes/state_hint_abstract_spec_error",
193+
new Hint(Kind.NEEDED_STATE, "The state of in is unknown here, and this call needs it to be open"));
194+
}
195+
196+
@Test
197+
void interfaceWithAnExternalSpecIsNotToldToCreateANewObject() {
198+
assertHints("classes/state_hint_interface_spec_error",
199+
new Hint(Kind.NEEDED_STATE, "The state of task is unknown here, and this call needs it to be done"));
200+
}
201+
202+
@Test
203+
void privateConstructorIsNotSuggested() {
204+
assertHints("ErrorStateHintUnknownPrivateConstructor.java",
205+
new Hint(Kind.NEEDED_STATE, "The state of value is unknown here, and this call needs it to be ready"));
206+
}
207+
208+
@Test
209+
void concreteClassIsToldToCreateANewObject() {
210+
assertHints("ErrorStateHintUnknownConcrete.java",
211+
new Hint(Kind.NEEDED_STATE, "The state of value is unknown here, and this call needs it to be ready. "
212+
+ "Objects created with new ErrorStateHintUnknownConcrete() start in such a state"));
213+
}
214+
184215
@Test
185216
void stateCheckedByAnEarlierCallIsNotCalledUnknown() {
186217
assertHints("classes/state_hint_checked_state_error", new Hint(Kind.NEEDED_STATE,

0 commit comments

Comments
 (0)