Skip to content

Commit

Permalink
refactor processor
Browse files Browse the repository at this point in the history
  • Loading branch information
tonivade committed Feb 11, 2024
1 parent ee2d275 commit 0475b33
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ subprojects {
destinationFile = file("${buildDir}/jacoco/test.exec")
}

reports.html.required = false
reports.html.required = true
}

tasks.withType(JavaCompile).each {
Expand Down
2 changes: 2 additions & 0 deletions processor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ dependencies {
test {
useJUnitPlatform()

reports.html.required = true

jvmArgs([
'--add-opens=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED',
'--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) 2018-2023, Antonio Gabriel Muñoz Conejo <antoniogmc at gmail dot com>
* Distributed under the terms of the MIT License
*/
package com.github.tonivade.purefun;
package com.github.tonivade.purefun.processor;

import static java.lang.String.format;
import static java.util.stream.Collectors.joining;
Expand All @@ -20,10 +20,12 @@
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;

import com.github.tonivade.purefun.HigherKind;

@SupportedAnnotationTypes("com.github.tonivade.purefun.HigherKind")
public class HigherKindProcessor extends AbstractProcessor {

private static final String GENERATED = "@Generated(\"com.github.tonivade.purefun.HigherKindProcessor\")";
private static final String GENERATED = "@Generated(\"com.github.tonivade.purefun.processor.HigherKindProcessor\")";

private static final String JAVAX_ANNOTATION_GENERATED = "javax.annotation.Generated";
private static final String JAVAX_ANNOTATION_PROCESSING_GENERATED = "javax.annotation.processing.Generated";
Expand Down Expand Up @@ -59,9 +61,9 @@ private void generate(TypeElement element) throws IOException {
int lastIndexOf = qualifiedName.lastIndexOf('.');
String packageName;
String className;

HigherKind annotation = element.getAnnotation(HigherKind.class);

if (lastIndexOf > 0) {
packageName = qualifiedName.substring(0, lastIndexOf);
className = qualifiedName.substring(lastIndexOf + 1);
Expand Down Expand Up @@ -215,23 +217,23 @@ private static void narrowK2(PrintWriter writer, String className, String aType,
private static void narrowK3(PrintWriter writer, String className, String aType, String bType, String cType, String hkt) {
narrowK(writer, "<" + aType + ", " + bType + ", " + cType + ">", className + "<A, B, C>", hkt);
}

private static void narrowK(PrintWriter writer, String types, String returnType, String param) {
writer.println(" @SuppressWarnings(\"unchecked\")");
writer.println(" static " + types + " " + returnType + " narrowK(" + param + " hkt) {");
writer.println(" return (" + returnType + ") hkt;");
writer.println(" }");
writer.println();
}

private static void toTypeOf1(PrintWriter writer, String className, String aType, String hkt, String typeOf) {
toTypeOf(writer, "<" + aType + ">", className + "<A>", hkt, typeOf, className);
}

private static void toTypeOf2(PrintWriter writer, String className, String aType, String bType, String hkt, String typeOf) {
toTypeOf(writer, "<" + aType + ", " + bType + ">", className + "<A, B>", hkt, typeOf, className);
}

private static void toTypeOf3(PrintWriter writer, String className, String aType, String bType, String cType, String hkt, String typeOf) {
toTypeOf(writer, "<" + aType + ", " + bType + ", " + cType + ">", className + "<A, B, C>", hkt, typeOf, className);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
com.github.tonivade.purefun.HigherKindProcessor
com.github.tonivade.purefun.processor.HigherKindProcessor
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) 2018-2023, Antonio Gabriel Muñoz Conejo <antoniogmc at gmail dot com>
* Distributed under the terms of the MIT License
*/
package com.github.tonivade.purefun;
package com.github.tonivade.purefun.processor;

import static com.google.common.truth.Truth.assert_;
import static com.google.testing.compile.JavaFileObjects.forSourceLines;
Expand All @@ -18,7 +18,7 @@ public class HigherKindProcessorTest {
"import com.github.tonivade.purefun.Witness;",
"import javax.annotation.processing.Generated;",

"@Generated(\"com.github.tonivade.purefun.HigherKindProcessor\")",
"@Generated(\"com.github.tonivade.purefun.processor.HigherKindProcessor\")",
"public final class Foo_ implements Witness {",
"private Foo_() {}",
"}");
Expand All @@ -41,7 +41,7 @@ public void compilesKind1() {
"import com.github.tonivade.purefun.Fixer;",
"import javax.annotation.processing.Generated;",

"@Generated(\"com.github.tonivade.purefun.HigherKindProcessor\")",
"@Generated(\"com.github.tonivade.purefun.processor.HigherKindProcessor\")",
"public sealed interface FooOf<A> extends Kind<Foo_, A> permits Foo {",

"@SuppressWarnings(\"unchecked\")",
Expand Down Expand Up @@ -74,7 +74,7 @@ public void compilesKind1NoPackage() {
"import com.github.tonivade.purefun.Fixer;",
"import javax.annotation.processing.Generated;",

"@Generated(\"com.github.tonivade.purefun.HigherKindProcessor\")",
"@Generated(\"com.github.tonivade.purefun.processor.HigherKindProcessor\")",
"public sealed interface FooOf<A> extends Kind<Foo_, A> permits Foo {",

"@SuppressWarnings(\"unchecked\")",
Expand All @@ -92,7 +92,7 @@ public void compilesKind1NoPackage() {
"import com.github.tonivade.purefun.Witness;",
"import javax.annotation.processing.Generated;",

"@Generated(\"com.github.tonivade.purefun.HigherKindProcessor\")",
"@Generated(\"com.github.tonivade.purefun.processor.HigherKindProcessor\")",
"public final class Foo_ implements Witness {",
"private Foo_() {}",
"}");
Expand All @@ -119,7 +119,7 @@ public void compilesKind1Bounds() {
"import com.github.tonivade.purefun.Fixer;",
"import javax.annotation.processing.Generated;",

"@Generated(\"com.github.tonivade.purefun.HigherKindProcessor\")",
"@Generated(\"com.github.tonivade.purefun.processor.HigherKindProcessor\")",
"public sealed interface FooOf<A extends java.lang.String> extends Kind<Foo_, A> permits Foo {",

"@SuppressWarnings(\"unchecked\")",
Expand Down Expand Up @@ -188,14 +188,14 @@ public void compilesKind2() {
"import com.github.tonivade.purefun.Fixer;",
"import javax.annotation.processing.Generated;",

"@Generated(\"com.github.tonivade.purefun.HigherKindProcessor\")",
"@Generated(\"com.github.tonivade.purefun.processor.HigherKindProcessor\")",
"public sealed interface FooOf<A, B> extends Kind<Kind<Foo_, A>, B> permits Foo {",

"@SuppressWarnings(\"unchecked\")",
"static <A, B> Foo<A, B> narrowK(Kind<Kind<Foo_, A>, ? extends B> hkt) {",
"return (Foo<A, B>) hkt;",
"}",

"static <A, B> Fixer<Kind<Kind<Foo_, A>, B>, Foo<A, B>> toFoo() {",
"return FooOf::narrowK;",
"}",
Expand Down Expand Up @@ -225,14 +225,14 @@ public void compilesKind3() {
"import com.github.tonivade.purefun.Fixer;",
"import javax.annotation.processing.Generated;",

"@Generated(\"com.github.tonivade.purefun.HigherKindProcessor\")",
"@Generated(\"com.github.tonivade.purefun.processor.HigherKindProcessor\")",
"public sealed interface FooOf<A, B, C> extends Kind<Kind<Kind<Foo_, A>, B>, C> permits Foo {",

"@SuppressWarnings(\"unchecked\")",
"static <A, B, C> Foo<A, B, C> narrowK(Kind<Kind<Kind<Foo_, A>, B>, ? extends C> hkt) {",
"return (Foo<A, B, C>) hkt;",
"}",

"static <A, B, C> Fixer<Kind<Kind<Kind<Foo_, A>, B>, C>, Foo<A, B, C>> toFoo() {",
"return FooOf::narrowK;",
"}",
Expand Down

0 comments on commit 0475b33

Please sign in to comment.