Skip to content

Commit 86e7ded

Browse files
Leave out inherited methods the receiver cannot call
A private method is not inherited, nor is a package-private one by a class in another package, so suggesting it gives a call that does not compile. Such methods leave the machine incomplete instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7537f62 commit 86e7ded

4 files changed

Lines changed: 72 additions & 1 deletion

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package testSuite.classes.state_hints_inaccessible_inherited_error;
2+
3+
import liquidjava.specification.StateRefinement;
4+
import liquidjava.specification.StateSet;
5+
6+
@StateSet({"closed", "open"})
7+
public class Hatch {
8+
9+
@StateRefinement(to = "closed(this)")
10+
public Hatch() {}
11+
12+
@StateRefinement(from = "closed(this)", to = "open(this)")
13+
private void open() {}
14+
15+
@StateRefinement(from = "closed(this)", to = "open(this)")
16+
void openInPackage() {}
17+
18+
@StateRefinement(from = "open(this)")
19+
public void use() {}
20+
}
21+
22+
class SamePackageHatch extends Hatch {
23+
24+
static void useClosed() {
25+
SamePackageHatch hatch = new SamePackageHatch();
26+
hatch.use(); // Expect: State Refinement Error
27+
}
28+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package testSuite.classes.state_hints_inaccessible_inherited_error.elsewhere;
2+
3+
import testSuite.classes.state_hints_inaccessible_inherited_error.Hatch;
4+
5+
public class OtherPackageHatch extends Hatch {
6+
7+
static void useClosed() {
8+
OtherPackageHatch hatch = new OtherPackageHatch();
9+
hatch.use(); // Expect: State Refinement Error
10+
}
11+
}

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.LinkedHashMap;
88
import java.util.List;
99
import java.util.Map;
10+
import java.util.Objects;
1011
import java.util.Optional;
1112
import java.util.Set;
1213
import java.util.stream.Collectors;
@@ -216,7 +217,7 @@ private StateMachine build(CtTypeReference<?> type, List<CtTypeReference<?>> sup
216217
continue;
217218
if (overridden(function, className))
218219
continue; // the class's own refinement for it is the one in the machine
219-
if (!resolvesTo(function, type, supertypes)) {
220+
if (!resolvesTo(function, type, supertypes) || !inheritedBy(function, type)) {
220221
complete = false;
221222
continue;
222223
}
@@ -285,6 +286,26 @@ private boolean resolvesTo(RefinedFunction method, CtTypeReference<?> type, List
285286
return false;
286287
}
287288

289+
/**
290+
* Whether objects of {@code type} inherit {@code method}, so it can be called on them: a private method is not
291+
* inherited, nor is a package-private one by a class in another package. Methods are matched by name and number of
292+
* parameters, so an inaccessible overload also leaves the method out, which only leaves the machine incomplete.
293+
*/
294+
private boolean inheritedBy(RefinedFunction method, CtTypeReference<?> type) {
295+
CtType<?> declaring = tc.getFactory().Type().createReference(method.getTargetClass()).getTypeDeclaration();
296+
CtType<?> declaration = type.getTypeDeclaration();
297+
if (declaring == null || declaration == null)
298+
return false; // cannot tell whether it is inherited
299+
if (declaring.isInterface())
300+
return true;
301+
String name = Utils.getSimpleName(method.getName());
302+
int arity = method.getArguments().size();
303+
boolean samePackage = Objects.equals(declaring.getPackage(), declaration.getPackage());
304+
return declaring.getMethods().stream()
305+
.filter(m -> m.getSimpleName().equals(name) && m.getParameters().size() == arity)
306+
.noneMatch(m -> m.isPrivate() || (!samePackage && !m.isPublic() && !m.isProtected()));
307+
}
308+
288309
private static boolean abstractType(CtTypeReference<?> type) {
289310
CtType<?> declaration = type.getTypeDeclaration();
290311
return declaration == null || declaration.isInterface() || declaration.isAbstract();

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,17 @@ void onlyConstructorsThatCanBeCalledAreSuggested() {
212212
new Hint(Kind.NEEDED_STATE, "The state of value is unknown here, and this call needs it to be ready"));
213213
}
214214

215+
@Test
216+
void inheritedMethodsTheReceiverCannotCallAreNotSuggested() {
217+
List<StateRefinementError> errors = errors("classes/state_hints_inaccessible_inherited_error", 2);
218+
assertHints(errors.get(0),
219+
new Hint(Kind.CALL_SEQUENCE,
220+
"Call hatch.openInPackage() before this call to move hatch from closed to open"),
221+
new Hint(Kind.AVAILABLE_CALLS,
222+
"In state closed, hatch can call: openInPackage() (and possibly others)"));
223+
assertHints(errors.get(1));
224+
}
225+
215226
@Test
216227
void argumentThatDecidedTheStateIsShownWithAValueThatWouldWork() {
217228
assertHints("ErrorArgumentPicksTheState.java", new Hint(Kind.DECIDING_ARGUMENT,

0 commit comments

Comments
 (0)