|
7 | 7 | import java.util.LinkedHashMap; |
8 | 8 | import java.util.List; |
9 | 9 | import java.util.Map; |
| 10 | +import java.util.Objects; |
10 | 11 | import java.util.Optional; |
11 | 12 | import java.util.Set; |
12 | 13 | import java.util.stream.Collectors; |
@@ -216,7 +217,7 @@ private StateMachine build(CtTypeReference<?> type, List<CtTypeReference<?>> sup |
216 | 217 | continue; |
217 | 218 | if (overridden(function, className)) |
218 | 219 | 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)) { |
220 | 221 | complete = false; |
221 | 222 | continue; |
222 | 223 | } |
@@ -285,6 +286,26 @@ private boolean resolvesTo(RefinedFunction method, CtTypeReference<?> type, List |
285 | 286 | return false; |
286 | 287 | } |
287 | 288 |
|
| 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 | + |
288 | 309 | private static boolean abstractType(CtTypeReference<?> type) { |
289 | 310 | CtType<?> declaration = type.getTypeDeclaration(); |
290 | 311 | return declaration == null || declaration.isInterface() || declaration.isAbstract(); |
|
0 commit comments