From 6c543506edc68a47b16caa60ecccbf2d93e01fba Mon Sep 17 00:00:00 2001 From: Samu Lang Date: Sun, 6 Aug 2023 17:44:28 +0200 Subject: [PATCH] Generate files that compile for three annotations --- declarative/processor/pom.xml | 5 + .../declarative/processor/Processor.java | 130 +++++++++++++++--- 2 files changed, 117 insertions(+), 18 deletions(-) diff --git a/declarative/processor/pom.xml b/declarative/processor/pom.xml index e69167d20..9ac62fb2b 100644 --- a/declarative/processor/pom.xml +++ b/declarative/processor/pom.xml @@ -24,6 +24,11 @@ inrupt-rdf-wrapping-declarative-annotations ${project.version} + + com.inrupt.rdf + inrupt-rdf-wrapping-jena + ${project.version} + org.junit.jupiter diff --git a/declarative/processor/src/main/java/com/inrupt/rdf/wrapping/declarative/processor/Processor.java b/declarative/processor/src/main/java/com/inrupt/rdf/wrapping/declarative/processor/Processor.java index b8eb6c8e5..bc7ee40a8 100644 --- a/declarative/processor/src/main/java/com/inrupt/rdf/wrapping/declarative/processor/Processor.java +++ b/declarative/processor/src/main/java/com/inrupt/rdf/wrapping/declarative/processor/Processor.java @@ -83,24 +83,20 @@ public boolean process(final Set annotations, final Round out.println(); } - out.println("import javax.annotation.processing.Generated;"); - out.println(); - - out.println("/**"); - out.println(" * Warning this class consists of generated code."); - out.println(" */"); - - out.print("@Generated(value = \""); - out.print(this.getClass().getName()); - out.print("\", date = \""); - out.print(Instant.now()); - out.println("\")"); - - out.print("public class "); - out.print(implementationClassName); - out.print(" implements "); - out.print(originalInterfaceName); - out.println(" {}"); + switch (annotation.getQualifiedName().toString()) { + case "com.inrupt.rdf.wrapping.declarative.annotations.Dataset": + printDataset(originalInterfaceName, implementationClassName, out); + break; + case "com.inrupt.rdf.wrapping.declarative.annotations.Graph": + printGraph(originalInterfaceName, implementationClassName, out); + break; + case "com.inrupt.rdf.wrapping.declarative.annotations.Resource": + printResource(originalInterfaceName, implementationClassName, out); + break; + + } + + } catch (IOException e) { throw new RuntimeException("could not open writer", e); } @@ -109,4 +105,102 @@ public boolean process(final Set annotations, final Round return true; } + + private void printDataset(final String originalInterfaceName, final String implementationClassName, final PrintWriter out) { + out.println("import javax.annotation.processing.Generated;"); + out.println(); + out.println("import org.apache.jena.sparql.core.DatasetGraph;"); + out.println("import org.apache.jena.sparql.core.DatasetImpl;"); + out.println(); + + printJavadoc(out); + printGenerated(out); + + out.print("public class "); + out.print(implementationClassName); + out.print(" extends DatasetImpl implements "); + out.print(originalInterfaceName); + out.println(" {"); + + out.print(" protected "); + out.print(implementationClassName); + out.println("(final DatasetGraph original) {"); + out.println(" super(original);"); + out.println(" }"); + + out.println("}"); + } + + private void printGraph(final String originalInterfaceName, final String implementationClassName, final PrintWriter out) { + out.println("import javax.annotation.processing.Generated;"); + out.println(); + out.println("import org.apache.jena.graph.Graph;"); + out.println("import org.apache.jena.rdf.model.impl.ModelCom;"); + out.println(); + + printJavadoc(out); + printGenerated(out); + + out.print("public class "); + out.print(implementationClassName); + out.print(" extends ModelCom implements "); + out.print(originalInterfaceName); + out.println(" {"); + + out.print(" protected "); + out.print(implementationClassName); + out.println("(final Graph original) {"); + out.println(" super(original);"); + out.println(" }"); + + out.println("}"); + } + + private void printResource(final String originalInterfaceName, final String implementationClassName, final PrintWriter out) { + out.println("import com.inrupt.rdf.wrapping.jena.UriOrBlankFactory;"); + out.println("import com.inrupt.rdf.wrapping.jena.WrapperResource;"); + out.println(); + out.println("import javax.annotation.processing.Generated;"); + out.println(); + out.println("import org.apache.jena.enhanced.EnhGraph;"); + out.println("import org.apache.jena.enhanced.Implementation;"); + out.println("import org.apache.jena.graph.Node;"); + out.println(); + + printJavadoc(out); + printGenerated(out); + + out.print("public class "); + out.print(implementationClassName); + out.print(" extends WrapperResource implements "); + out.print(originalInterfaceName); + out.println(" {"); + + out.print(" static final Implementation factory = new UriOrBlankFactory("); + out.print(implementationClassName); + out.println("::new);"); + out.println(); + + out.print(" protected "); + out.print(implementationClassName); + out.println("(final Node node, final EnhGraph graph) {"); + out.println(" super(node, graph);"); + out.println(" }"); + + out.println("}"); + } + + private void printGenerated(final PrintWriter out) { + out.print("@Generated(value = \""); + out.print(this.getClass().getName()); + out.print("\", date = \""); + out.print(Instant.now()); + out.println("\")"); + } + + private static void printJavadoc(final PrintWriter out) { + out.println("/**"); + out.println(" * Warning this class consists of generated code."); + out.println(" */"); + } }