|
1 | 1 | package liquidjava.utils; |
2 | 2 |
|
| 3 | +import java.util.List; |
3 | 4 | import java.util.Map; |
4 | 5 | import java.util.Set; |
5 | 6 | import java.util.stream.Stream; |
|
22 | 23 | public class Utils { |
23 | 24 |
|
24 | 25 | private static final Set<String> DEFAULT_NAMES = Set.of("old", "length", "addToIndex", "getFromIndex"); |
| 26 | + private static final List<String> REFINEMENT_KEYS = List.of("value", "to", "from"); |
25 | 27 |
|
26 | 28 | public static CtTypeReference<?> getType(String type, Factory factory) { |
27 | 29 | // TODO: complete with other types |
@@ -54,28 +56,42 @@ public static String getFile(CtElement element) { |
54 | 56 | return pos.getFile().getAbsolutePath(); |
55 | 57 | } |
56 | 58 |
|
57 | | - public static SourcePosition getLJAnnotationPosition(CtElement element, String refinement) { |
58 | | - return element.getAnnotations().stream() |
59 | | - .filter(a -> isLiquidJavaAnnotation(a) && hasRefinementValue(a, "\"" + refinement + "\"")).findFirst() |
60 | | - .map(CtElement::getPosition).orElse(element.getPosition()); |
| 59 | + // Get the position of the annotation with the given value |
| 60 | + public static SourcePosition getLJAnnotationPosition(CtElement element, String value) { |
| 61 | + String quotedValue = "\"" + value + "\""; |
| 62 | + return getLiquidJavaAnnotations(element) |
| 63 | + .flatMap(annotation -> getLJAnnotationValues(annotation) |
| 64 | + .filter(expr -> quotedValue.equals(expr.toString())) |
| 65 | + .map(expr -> expr.getPosition() != null ? expr.getPosition() : annotation.getPosition())) |
| 66 | + .findFirst().orElse(element.getPosition()); |
61 | 67 | } |
62 | 68 |
|
| 69 | + // Get the position of the first LJ annotation on the element |
63 | 70 | public static SourcePosition getFirstLJAnnotationPosition(CtElement element) { |
64 | | - return element.getAnnotations().stream().filter(Utils::isLiquidJavaAnnotation).map(CtElement::getPosition) |
65 | | - .min((p1, p2) -> { |
66 | | - if (p1.getLine() != p2.getLine()) |
67 | | - return Integer.compare(p1.getLine(), p2.getLine()); |
68 | | - return Integer.compare(p1.getColumn(), p2.getColumn()); |
69 | | - }).orElse(element.getPosition()); |
| 71 | + return getLiquidJavaAnnotations(element).map(CtAnnotation::getPosition).filter(pos -> pos != null).findFirst() |
| 72 | + .orElse(element.getPosition()); |
| 73 | + } |
| 74 | + |
| 75 | + // Get the position of the first value of the first LJ annotation on the element |
| 76 | + public static SourcePosition getFirstLJAnnotationValuePosition(CtElement element) { |
| 77 | + return getLiquidJavaAnnotations(element) |
| 78 | + .map(annotation -> getLJAnnotationValues(annotation).map(CtElement::getPosition) |
| 79 | + .filter(pos -> pos != null).findFirst() |
| 80 | + .orElse(annotation.getPosition() != null ? annotation.getPosition() : element.getPosition())) |
| 81 | + .findFirst().orElse(element.getPosition()); |
| 82 | + } |
| 83 | + |
| 84 | + private static Stream<CtAnnotation<?>> getLiquidJavaAnnotations(CtElement element) { |
| 85 | + return element.getAnnotations().stream().filter(Utils::isLiquidJavaAnnotation); |
70 | 86 | } |
71 | 87 |
|
72 | 88 | private static boolean isLiquidJavaAnnotation(CtAnnotation<?> annotation) { |
73 | 89 | return annotation.getAnnotationType().getQualifiedName().startsWith("liquidjava.specification"); |
74 | 90 | } |
75 | 91 |
|
76 | | - private static boolean hasRefinementValue(CtAnnotation<?> annotation, String refinement) { |
77 | | - Map<String, ?> values = annotation.getValues(); |
78 | | - return Stream.of("value", "to", "from").anyMatch(key -> refinement.equals(String.valueOf(values.get(key)))); |
| 92 | + private static Stream<CtElement> getLJAnnotationValues(CtAnnotation<?> annotation) { |
| 93 | + Map<String, ?> values = annotation.getAllValues(); |
| 94 | + return REFINEMENT_KEYS.stream().map(values::get).filter(CtElement.class::isInstance).map(CtElement.class::cast); |
79 | 95 | } |
80 | 96 |
|
81 | 97 | public static SourcePosition getRealPosition(CtElement element) { |
|
0 commit comments