Skip to content

Commit 5070273

Browse files
committed
Simplify some bits
1 parent 1d658ad commit 5070273

2 files changed

Lines changed: 17 additions & 28 deletions

File tree

src/main/java/com/garciat/typeclasses/processor/StaticWitnessSystem.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,13 @@
1515
import javax.lang.model.element.TypeElement;
1616
import javax.lang.model.element.VariableElement;
1717
import javax.lang.model.type.*;
18-
import javax.lang.model.util.Types;
1918

2019
public class StaticWitnessSystem {
2120
private static final Class<?> TAG_BASE_CLASS = TagBase.class;
2221
private static final Class<?> TAPP_CLASS = TApp.class;
2322
private static final Class<?> TPAR_CLASS = TPar.class;
2423

25-
private final Types types;
26-
27-
public StaticWitnessSystem(Types types) {
28-
this.types = types;
29-
}
24+
public StaticWitnessSystem() {}
3025

3126
public List<WitnessConstructor> findRules(ParsedType target) {
3227
return switch (target) {
@@ -42,7 +37,7 @@ public List<WitnessConstructor> findRules(ParsedType target) {
4237
};
4338
}
4439

45-
public Maybe<WitnessConstructor> parseWitnessConstructor(ExecutableElement method) {
40+
private Maybe<WitnessConstructor> parseWitnessConstructor(ExecutableElement method) {
4641
if (method.getModifiers().contains(Modifier.PUBLIC)
4742
&& method.getModifiers().contains(Modifier.STATIC)
4843
&& method.getAnnotation(TypeClass.Witness.class) instanceof TypeClass.Witness witnessAnn) {
@@ -61,10 +56,6 @@ public Maybe<WitnessConstructor> parseWitnessConstructor(ExecutableElement metho
6156
}
6257
}
6358

64-
public List<ParsedType> parseAll(List<? extends TypeMirror> types) {
65-
return types.stream().map(this::parse).toList();
66-
}
67-
6859
public ParsedType parse(TypeMirror type) {
6960
return switch (type) {
7061
case TypeVariable tv -> new ParsedType.Var(tv);
@@ -82,8 +73,9 @@ when parseAppType(dt)
8273
Pair<TypeMirror, TypeMirror>(var fun, var arg)) ->
8374
new ParsedType.App(parse(fun), parse(arg));
8475
case DeclaredType dt ->
85-
parseAll(dt.getTypeArguments()).stream()
86-
.reduce(parse(types.erasure(dt)), ParsedType.App::new);
76+
dt.getTypeArguments().stream()
77+
.map(this::parse)
78+
.reduce(new ParsedType.Const(erasure(dt)), ParsedType.App::new);
8779
case WildcardType wt ->
8880
throw new IllegalArgumentException("Cannot parse wildcard type: " + wt);
8981
default -> throw new IllegalArgumentException("Unsupported type: " + type);
@@ -104,7 +96,7 @@ private static Maybe<DeclaredType> parseTagType(DeclaredType t) {
10496
}
10597

10698
private Maybe<Pair<TypeMirror, TypeMirror>> parseAppType(DeclaredType t) {
107-
return t.getTypeArguments().size() == 2 && isAppType(types.erasure(t))
99+
return t.getTypeArguments().size() == 2 && isAppType(erasure(t))
108100
? Maybe.just(new Pair<>(t.getTypeArguments().get(0), t.getTypeArguments().get(1)))
109101
: Maybe.nothing();
110102
}
@@ -116,6 +108,10 @@ private boolean isAppType(TypeMirror erasure) {
116108
|| te.getQualifiedName().contentEquals(TPAR_CLASS.getName()));
117109
}
118110

111+
private DeclaredType erasure(DeclaredType t) {
112+
return t.asElement().asType() instanceof DeclaredType typeCtor ? typeCtor : t;
113+
}
114+
119115
private static <T extends U, U> Function<U, Stream<T>> isInstanceOf(Class<T> cls) {
120116
return u -> cls.isInstance(u) ? Stream.of(cls.cast(u)) : Stream.empty();
121117
}

src/main/java/com/garciat/typeclasses/processor/WitnessResolutionChecker.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import javax.lang.model.element.TypeElement;
1313
import javax.lang.model.type.DeclaredType;
1414
import javax.lang.model.type.TypeMirror;
15-
import javax.lang.model.util.Types;
1615
import javax.tools.Diagnostic;
1716

1817
public final class WitnessResolutionChecker implements Plugin {
@@ -45,29 +44,23 @@ public void finished(TaskEvent e) {
4544
return;
4645
}
4746

48-
Trees trees = Trees.instance(task);
49-
new WitnessCallScanner(trees, task.getTypes()).scan(e.getCompilationUnit(), trees);
47+
new WitnessCallScanner(Trees.instance(task)).scan(e.getCompilationUnit(), null);
5048
}
5149
});
5250
}
5351

5452
/** Scanner that finds calls to TypeClasses.witness() and validates them. */
55-
private static class WitnessCallScanner extends TreePathScanner<Void, Trees> {
53+
private static class WitnessCallScanner extends TreePathScanner<Void, Void> {
5654
private final Trees trees;
5755
private final StaticWitnessSystem system;
5856

59-
WitnessCallScanner(Trees trees, Types types) {
57+
private WitnessCallScanner(Trees trees) {
6058
this.trees = trees;
61-
this.system = new StaticWitnessSystem(types);
59+
this.system = new StaticWitnessSystem();
6260
}
6361

6462
@Override
65-
public Void visitClass(ClassTree node, Trees trees) {
66-
return super.visitClass(node, trees);
67-
}
68-
69-
@Override
70-
public Void visitMethodInvocation(MethodInvocationTree node, Trees trees) {
63+
public Void visitMethodInvocation(MethodInvocationTree node, Void arg) {
7164
Element element = trees.getElement(getCurrentPath());
7265

7366
if (isMethodCall(WITNESS_METHOD, element)) {
@@ -95,7 +88,7 @@ public Void visitMethodInvocation(MethodInvocationTree node, Trees trees) {
9588
this.trees.printMessage(
9689
Diagnostic.Kind.ERROR,
9790
"Failed to resolve witness for type: "
98-
+ target.format()
91+
+ witnessTypeMirror
9992
+ "\nReason: "
10093
+ error.format(),
10194
getCurrentPath().getLeaf(),
@@ -108,7 +101,7 @@ public Void visitMethodInvocation(MethodInvocationTree node, Trees trees) {
108101
}
109102
}
110103

111-
return super.visitMethodInvocation(node, trees);
104+
return super.visitMethodInvocation(node, arg);
112105
}
113106
}
114107

0 commit comments

Comments
 (0)