|
| 1 | +package liquidjava.processor.refinement_checker; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Optional; |
| 6 | +import java.util.stream.Collectors; |
| 7 | + |
| 8 | +import liquidjava.diagnostics.categories.ErrorCategory; |
| 9 | +import liquidjava.diagnostics.errors.RefinementError; |
| 10 | +import liquidjava.processor.context.Context; |
| 11 | +import liquidjava.processor.context.Variable; |
| 12 | +import spoon.reflect.declaration.CtConstructor; |
| 13 | +import spoon.reflect.declaration.CtElement; |
| 14 | +import spoon.reflect.declaration.CtExecutable; |
| 15 | +import spoon.reflect.declaration.CtMethod; |
| 16 | +import spoon.reflect.declaration.CtParameter; |
| 17 | +import spoon.reflect.declaration.CtType; |
| 18 | +import liquidjava.utils.Utils; |
| 19 | + |
| 20 | +/** |
| 21 | + * The notes of a refinement error that say where the values its expected refinement is about come from. Each is given |
| 22 | + * only when it restates what the verifier used, and is left out otherwise |
| 23 | + */ |
| 24 | +final class FromNotes { |
| 25 | + |
| 26 | + private FromNotes() { |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * When nothing is known about the values expected constrains and each is a parameter of the enclosing method that |
| 31 | + * is declared with no refinement, a note per parameter saying so. Nothing is said about the callers |
| 32 | + */ |
| 33 | + static List<String> unrefinedParameters(RefinementError error, CtElement checked, Context context) { |
| 34 | + Optional<CtExecutable<?>> method = enclosingMethod(checked); |
| 35 | + List<String> variables = error.getExpected().getVariableNames().stream().distinct().toList(); |
| 36 | + if (error.getCategory().filter(ErrorCategory.UNKNOWN::equals).isEmpty() || method.isEmpty() |
| 37 | + || variables.isEmpty()) |
| 38 | + return List.of(); |
| 39 | + |
| 40 | + List<String> notes = new ArrayList<>(); |
| 41 | + for (String variable : variables) { |
| 42 | + Optional<CtParameter<?>> parameter = method.get().getParameters().stream() |
| 43 | + .filter(p -> p.getSimpleName().equals(variable)).findFirst(); |
| 44 | + // a written parameter has an instance, which expected would name instead |
| 45 | + if (parameter.isEmpty() || !parameter.get().getPosition().isValidPosition() |
| 46 | + || !(context.getVariableByName(variable)instanceof Variable declared) |
| 47 | + || !declared.getMainRefinement().isBooleanTrue() || isRefined(parameter.get(), context)) |
| 48 | + return List.of(); |
| 49 | + notes.add(String.format("From: %s is a parameter of %s (line %d) with no refinement", |
| 50 | + error.getDisplayNames().get(variable), signature(method.get()), |
| 51 | + parameter.get().getPosition().getLine())); |
| 52 | + } |
| 53 | + return notes; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Whether the parameter is written with a refinement, even {@code true}, or a specification of its method refines |
| 58 | + * it, such as an external one, which the method's body is not checked against |
| 59 | + */ |
| 60 | + private static boolean isRefined(CtParameter<?> parameter, Context context) { |
| 61 | + if (parameter.getAnnotations().stream() |
| 62 | + .anyMatch(a -> a.getAnnotationType().getQualifiedName().equals("liquidjava.specification.Refinement"))) |
| 63 | + return true; |
| 64 | + CtExecutable<?> method = parameter.getParent(); |
| 65 | + int index = method.getParameters().indexOf(parameter); |
| 66 | + String owner = method.getParent(CtType.class).getQualifiedName(); |
| 67 | + String name = method instanceof CtConstructor<?> ? Utils.getSimpleName(owner) : method.getSimpleName(); |
| 68 | + List<String> types = method.getParameters().stream().map(p -> p.getType().getQualifiedName()).toList(); |
| 69 | + return context.getCtxFunctions().stream() |
| 70 | + .filter(f -> owner.equals(f.getTargetClass()) && name.equals(Utils.getSimpleName(f.getName())) |
| 71 | + && types.equals(f.getArguments().stream() |
| 72 | + .map(a -> a.getType() == null ? null : a.getType().getQualifiedName()).toList())) |
| 73 | + .anyMatch(f -> !f.getArguments().get(index).getMainRefinement().isBooleanTrue()); |
| 74 | + } |
| 75 | + |
| 76 | + private static Optional<CtExecutable<?>> enclosingMethod(CtElement element) { |
| 77 | + CtElement parent = element.getParent(); |
| 78 | + while (parent != null && !(parent instanceof CtMethod<?> || parent instanceof CtConstructor<?>)) |
| 79 | + parent = parent.getParent(); |
| 80 | + return Optional.ofNullable((CtExecutable<?>) parent); |
| 81 | + } |
| 82 | + |
| 83 | + /** The method as {@code name(ParameterType, ...)}, a constructor by its class name */ |
| 84 | + private static String signature(CtExecutable<?> method) { |
| 85 | + String name = method instanceof CtConstructor<?> constructor ? constructor.getDeclaringType().getSimpleName() |
| 86 | + : method.getSimpleName(); |
| 87 | + return name + method.getParameters().stream().map(p -> p.getType().getSimpleName()) |
| 88 | + .collect(Collectors.joining(", ", "(", ")")); |
| 89 | + } |
| 90 | +} |
0 commit comments