|
12 | 12 | import liquidjava.diagnostics.errors.*; |
13 | 13 | import liquidjava.diagnostics.categories.ErrorCategory; |
14 | 14 | import liquidjava.diagnostics.hints.Hint; |
| 15 | +import liquidjava.diagnostics.warnings.CustomWarning; |
15 | 16 | import liquidjava.diagnostics.warnings.UnsatisfiableRefinementWarning; |
16 | 17 | import liquidjava.processor.context.AliasWrapper; |
17 | 18 | import liquidjava.processor.context.Context; |
@@ -230,8 +231,10 @@ public void registerGhostsAndStateSets(CtElement element) throws LJError { |
230 | 231 | } |
231 | 232 | // checked once every set and ghost of the class exists, so the order of the annotations does not matter; a |
232 | 233 | // generated name such as state1 can clash too |
233 | | - for (Map.Entry<GhostFunction, SourcePosition> stateSet : stateSets.entrySet()) |
| 234 | + for (Map.Entry<GhostFunction, SourcePosition> stateSet : stateSets.entrySet()) { |
234 | 235 | checkStateSetNameIsFree(stateSet.getKey(), stateSet.getValue()); |
| 236 | + warnIfStateSetNameIsAMethod(stateSet.getKey(), stateSet.getValue()); |
| 237 | + } |
235 | 238 | } |
236 | 239 |
|
237 | 240 | /** |
@@ -285,6 +288,28 @@ private void checkStateSetNameIsFree(GhostFunction set, SourcePosition position) |
285 | 288 | set.getParentClassName()), position); |
286 | 289 | } |
287 | 290 |
|
| 291 | + /** |
| 292 | + * Warns about a named state set whose name is also the name of a method of its class, or of the class an external |
| 293 | + * spec refines: in a message such as {@code undo(x) == undoable} the set reads like a call to that method |
| 294 | + */ |
| 295 | + private void warnIfStateSetNameIsAMethod(GhostFunction set, SourcePosition position) { |
| 296 | + String name = set.getName(); |
| 297 | + if (name.matches(Formats.STATE_SET_NAMES)) |
| 298 | + return; // a set without a name, whose name the user did not choose |
| 299 | + CtType<?> type = factory.Type().createReference(set.getPrefix()).getTypeDeclaration(); |
| 300 | + // a private method of a superclass is not a method of this class |
| 301 | + if (type == null || type.getAllMethods().stream() |
| 302 | + .noneMatch(m -> m.getSimpleName().equals(name) && (m.getDeclaringType() == type || !m.isPrivate()))) |
| 303 | + return; |
| 304 | + String state = context.getGhostState(set.getParentClassName()).stream().filter(gs -> gs.getParent() == set) |
| 305 | + .map(GhostState::getName).findFirst().orElse("..."); |
| 306 | + diagnostics.add(new CustomWarning(position, |
| 307 | + String.format( |
| 308 | + "State set name '%s' is also the name of a method of %s, so %s(x) == %s reads like a " |
| 309 | + + "call to %s(). Choose another name, such as %sState", |
| 310 | + name, set.getParentClassName(), name, state, name, name))); |
| 311 | + } |
| 312 | + |
288 | 313 | private GhostFunction createStateSet(CtNewArray<String> e, String name, CtElement element) throws LJError { |
289 | 314 |
|
290 | 315 | // if any of the states starts with uppercase, throw error (reserved for alias) |
|
0 commit comments