|
| 1 | +package com.garciat.typeclasses.processor; |
| 2 | + |
| 3 | +import com.garciat.typeclasses.api.TypeClass; |
| 4 | +import com.garciat.typeclasses.api.hkt.TApp; |
| 5 | +import com.garciat.typeclasses.api.hkt.TPar; |
| 6 | +import com.garciat.typeclasses.api.hkt.TagBase; |
| 7 | +import com.garciat.typeclasses.impl.utils.Lists; |
| 8 | +import com.garciat.typeclasses.types.Maybe; |
| 9 | +import com.garciat.typeclasses.types.Pair; |
| 10 | +import java.util.List; |
| 11 | +import java.util.function.Function; |
| 12 | +import java.util.stream.Stream; |
| 13 | +import javax.lang.model.element.ExecutableElement; |
| 14 | +import javax.lang.model.element.Modifier; |
| 15 | +import javax.lang.model.element.TypeElement; |
| 16 | +import javax.lang.model.element.VariableElement; |
| 17 | +import javax.lang.model.type.*; |
| 18 | + |
| 19 | +public class StaticWitnessSystem { |
| 20 | + private static final Class<?> TAG_BASE_CLASS = TagBase.class; |
| 21 | + private static final Class<?> TAPP_CLASS = TApp.class; |
| 22 | + private static final Class<?> TPAR_CLASS = TPar.class; |
| 23 | + |
| 24 | + public StaticWitnessSystem() {} |
| 25 | + |
| 26 | + public List<WitnessConstructor> findRules(ParsedType target) { |
| 27 | + return switch (target) { |
| 28 | + case ParsedType.App(var fun, var arg) -> Lists.concat(findRules(fun), findRules(arg)); |
| 29 | + case ParsedType.Const(var java) -> |
| 30 | + java.asElement().getEnclosedElements().stream() |
| 31 | + .flatMap(isInstanceOf(ExecutableElement.class)) |
| 32 | + .flatMap(method -> parseWitnessConstructor(method).stream()) |
| 33 | + .toList(); |
| 34 | + case ParsedType.Var(var ignore) -> List.of(); |
| 35 | + case ParsedType.ArrayOf(var ignore) -> List.of(); |
| 36 | + case ParsedType.Primitive(var ignore) -> List.of(); |
| 37 | + }; |
| 38 | + } |
| 39 | + |
| 40 | + private Maybe<WitnessConstructor> parseWitnessConstructor(ExecutableElement method) { |
| 41 | + if (method.getModifiers().contains(Modifier.PUBLIC) |
| 42 | + && method.getModifiers().contains(Modifier.STATIC) |
| 43 | + && method.getAnnotation(TypeClass.Witness.class) instanceof TypeClass.Witness witnessAnn) { |
| 44 | + return Maybe.just( |
| 45 | + new WitnessConstructor( |
| 46 | + method, |
| 47 | + witnessAnn.overlap(), |
| 48 | + method.getParameters().stream() |
| 49 | + .map(VariableElement::asType) |
| 50 | + .map(this::parse) |
| 51 | + .toList(), |
| 52 | + parse(method.getReturnType()))); |
| 53 | + |
| 54 | + } else { |
| 55 | + return Maybe.nothing(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public ParsedType parse(TypeMirror type) { |
| 60 | + return switch (type) { |
| 61 | + case TypeVariable tv -> new ParsedType.Var(tv); |
| 62 | + case ArrayType at -> new ParsedType.ArrayOf(parse(at.getComponentType())); |
| 63 | + // Store primitive as its boxed type representation, just to have a DeclaredType. |
| 64 | + case PrimitiveType pt -> new ParsedType.Primitive(pt); |
| 65 | + case DeclaredType dt |
| 66 | + when parseTagType(dt) instanceof Maybe.Just<DeclaredType>(var realType) -> |
| 67 | + new ParsedType.Const(realType); |
| 68 | + case DeclaredType dt when dt.getTypeArguments().isEmpty() -> new ParsedType.Const(dt); |
| 69 | + case DeclaredType dt |
| 70 | + when parseAppType(dt) |
| 71 | + instanceof |
| 72 | + Maybe.Just<Pair<TypeMirror, TypeMirror>>( |
| 73 | + Pair<TypeMirror, TypeMirror>(var fun, var arg)) -> |
| 74 | + new ParsedType.App(parse(fun), parse(arg)); |
| 75 | + case DeclaredType dt -> |
| 76 | + dt.getTypeArguments().stream() |
| 77 | + .map(this::parse) |
| 78 | + .reduce(new ParsedType.Const(erasure(dt)), ParsedType.App::new); |
| 79 | + case WildcardType wt -> |
| 80 | + throw new IllegalArgumentException("Cannot parse wildcard type: " + wt); |
| 81 | + default -> throw new IllegalArgumentException("Unsupported type: " + type); |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + private static Maybe<DeclaredType> parseTagType(DeclaredType t) { |
| 86 | + if (t.asElement() instanceof TypeElement tag |
| 87 | + && tag.getEnclosingElement() instanceof TypeElement enclosing |
| 88 | + && enclosing.asType() instanceof DeclaredType enclosingType |
| 89 | + && tag.getSuperclass() instanceof DeclaredType tagSuperType |
| 90 | + && tagSuperType.asElement() instanceof TypeElement tagSuper |
| 91 | + && tagSuper.getQualifiedName().contentEquals(TAG_BASE_CLASS.getName())) { |
| 92 | + return Maybe.just(enclosingType); |
| 93 | + } else { |
| 94 | + return Maybe.nothing(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private Maybe<Pair<TypeMirror, TypeMirror>> parseAppType(DeclaredType t) { |
| 99 | + return t.getTypeArguments().size() == 2 && isAppType(erasure(t)) |
| 100 | + ? Maybe.just(new Pair<>(t.getTypeArguments().get(0), t.getTypeArguments().get(1))) |
| 101 | + : Maybe.nothing(); |
| 102 | + } |
| 103 | + |
| 104 | + private boolean isAppType(TypeMirror erasure) { |
| 105 | + return erasure instanceof DeclaredType dt |
| 106 | + && dt.asElement() instanceof TypeElement te |
| 107 | + && (te.getQualifiedName().contentEquals(TAPP_CLASS.getName()) |
| 108 | + || te.getQualifiedName().contentEquals(TPAR_CLASS.getName())); |
| 109 | + } |
| 110 | + |
| 111 | + private DeclaredType erasure(DeclaredType t) { |
| 112 | + return t.asElement().asType() instanceof DeclaredType typeCtor ? typeCtor : t; |
| 113 | + } |
| 114 | + |
| 115 | + private static <T extends U, U> Function<U, Stream<T>> isInstanceOf(Class<T> cls) { |
| 116 | + return u -> cls.isInstance(u) ? Stream.of(cls.cast(u)) : Stream.empty(); |
| 117 | + } |
| 118 | +} |
0 commit comments