Sample for extensions from AnnotationProcessor

This commit is contained in:
Johannes Frohnmeyer 2022-11-23 18:25:37 +01:00
parent 848fbbafb5
commit 579eb3a1f4
Signed by: Johannes
GPG Key ID: E76429612C2929F4
12 changed files with 62 additions and 265 deletions

View File

@ -1,5 +1,5 @@
plugins {
id("java")
`java-library`
}
group = "io.gitlab.jfronny"
@ -7,10 +7,16 @@ version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
maven("https://maven.frohnmeyer-wds.de/artifacts")
}
dependencies {
val manifoldVersion = "2022.1.23"
val manifoldVersion = "2022.1.27"
val gsonCompileVersion = "1.1-SNAPSHOT"
api("io.gitlab.jfronny.gson:gson-compile-core:$gsonCompileVersion")
compileOnly("io.gitlab.jfronny.gson:gson-compile-annotations:$gsonCompileVersion")
annotationProcessor("io.gitlab.jfronny.gson:gson-compile-processor:$gsonCompileVersion")
implementation("systems.manifold:manifold-props-rt:$manifoldVersion")

29
module2/build.gradle.kts Normal file
View File

@ -0,0 +1,29 @@
plugins {
`java-library`
}
repositories {
mavenCentral()
maven("https://maven.frohnmeyer-wds.de/artifacts")
}
dependencies {
val manifoldVersion = "2022.1.27"
implementation(rootProject)
implementation("systems.manifold:manifold-ext-rt:$manifoldVersion")
testAnnotationProcessor(annotationProcessor("systems.manifold:manifold-ext:$manifoldVersion")!!)
}
if (sourceSets.main.get().allJava.files.any {it.name == "module-info.java"}) {
tasks.withType<JavaCompile> {
options.compilerArgs.addAll(arrayOf("-Xplugin:Manifold no-bootstrap", "--module-path", classpath.asPath))
}
} else {
tasks.withType<JavaCompile> {
options.compilerArgs.addAll(arrayOf("-Xplugin:Manifold no-bootstrap"))
}
}

View File

@ -0,0 +1,12 @@
package io.gitlab.jfronny;
import io.gitlab.jfronny.inceptum.ExampleRecord;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
// This works
System.out.println(new ExampleRecord("aaee").toJson());
}
}

View File

@ -1,2 +1,2 @@
rootProject.name = "manifold-example"
include("module2")

View File

@ -1,9 +0,0 @@
package extensions.java.lang.Iterable;
import manifold.ext.rt.api.Extension;
import manifold.ext.rt.api.Structural;
@Extension
@Structural
public class StructuralIterable {
}

View File

@ -1,14 +0,0 @@
package extensions.org.w3c.dom.Node;
import manifold.ext.rt.api.Extension;
import manifold.ext.rt.api.This;
import org.w3c.dom.Node;
@Extension
public class NodeExt {
public static boolean isWhitespace(@This Node thiz) {
if (thiz.nodeType == Node.TEXT_NODE && thiz.textContent.isBlank()) return true;
if (thiz.nodeType == Node.COMMENT_NODE) return true;
return false;
}
}

View File

@ -1,20 +0,0 @@
package extensions.org.w3c.dom.NodeList;
import io.gitlab.jfronny.inceptum.common.dom.NodeListIterator;
import manifold.ext.rt.api.Extension;
import manifold.ext.rt.api.This;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.util.Iterator;
@Extension
public abstract class NodeListExt implements Iterable<Node> {
public static Iterator<Node> iterator(@This NodeList thiz) {
return new NodeListIterator(thiz);
}
public static Node get(@This NodeList thiz, int index) {
return thiz.item(index);
}
}

View File

@ -0,0 +1,9 @@
package io.gitlab.jfronny.inceptum;
import io.gitlab.jfronny.gson.compile.annotations.GSerializable;
// An extension class called "GC_ExampleRecord" is generated through a javax.annotation.processing.Processor through javax.annotation.processing.AbstractProcessor
@GSerializable
public record ExampleRecord(String someValue) {
}

View File

@ -1,85 +1,10 @@
package io.gitlab.jfronny.inceptum;
import io.gitlab.jfronny.inceptum.common.model.maven.Pom;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import java.io.IOException;
import java.io.InputStream;
public class Main {
private static final DocumentBuilder FACTORY;
static {
try {
FACTORY = DocumentBuilderFactory.newInstance().newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new RuntimeException("Could not create document builder", e);
}
}
public static void main(String[] args) throws IOException, SAXException {
Pom result;
try (InputStream is = Main.class.getResourceAsStream("/ExamplePom.xml")) {
Document doc = FACTORY.parse(is);
doc.documentElement.normalize();
result = new Pom();
if (!"project".equals(doc.documentElement.nodeName)) throw new IOException("Illegal document name");
boolean hasModelVersion = false;
boolean hasGroupId = false;
boolean hasArtifactId = false;
boolean hasVersion = false;
for (Node node : doc.documentElement.childNodes) {
switch (node.nodeName) {
case "modelVersion" -> {
hasModelVersion = true;
result.modelVersion = node.textContent;
}
case "parent" -> {
// Dirty hack to get slf4j working: simply assume the groupId and version of the parent is also the groupId of this
if (!hasGroupId) {
for (Node child : node.childNodes) {
switch (child.nodeName) {
case "groupId" -> {
if (!hasGroupId) {
hasGroupId = true;
result.groupId = node.textContent;
}
}
case "version" -> {
if (!hasVersion) {
hasVersion = true;
result.version = node.textContent;
}
}
}
}
}
}
case "groupId" -> {
hasGroupId = true;
result.groupId = node.textContent;
}
case "artifactId" -> {
hasArtifactId = true;
result.artifactId = node.textContent;
}
case "version" -> {
hasVersion = true;
result.version = node.textContent;
}
case "packaging" -> result.packaging = node.textContent;
case "classifier" -> result.classifier = node.textContent;
default -> {}
}
}
if (!hasModelVersion) throw new IOException("Pom lacks modelVersion");
if (!hasGroupId) throw new IOException("Pom lacks groupId");
if (!hasArtifactId) throw new IOException("Pom lacks artifactId");
if (!hasVersion) throw new IOException("Pom lacks version");
}
System.out.println("Parsed: $result");
public static void main(String[] args) throws IOException {
// This doesn't
System.out.println(new ExampleRecord("aaee").toJson());
}
}

View File

@ -1,30 +0,0 @@
package io.gitlab.jfronny.inceptum.common.dom;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class NodeListIterator implements Iterator<Node> {
private final NodeList list;
private int index = 0;
public NodeListIterator(NodeList list) {
this.list = list;
}
@Override
public boolean hasNext() {
while (index < list.length && list[index].isWhitespace()) {
index++;
}
return index < list.length;
}
@Override
public Node next() {
if (!hasNext()) throw new NoSuchElementException();
return list[index++];
}
}

View File

@ -1,26 +0,0 @@
package io.gitlab.jfronny.inceptum.common.model.maven;
import java.util.List;
public class Pom {
public String modelVersion;
public String groupId;
public String artifactId;
public String version;
public String classifier;
public String snapshotVersion;
public String packaging;
@Override
public String toString() {
return "Pom{" +
"modelVersion='" + modelVersion + '\'' +
", groupId='" + groupId + '\'' +
", artifactId='" + artifactId + '\'' +
", version='" + version + '\'' +
", classifier='" + classifier + '\'' +
", snapshotVersion='" + snapshotVersion + '\'' +
", packaging='" + packaging + '\'' +
'}';
}
}

View File

@ -1,85 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-parent</artifactId>
<version>1.7.36</version>
</parent>
<artifactId>slf4j-api</artifactId>
<packaging>jar</packaging>
<name>SLF4J API Module</name>
<description>The slf4j API</description>
<url>http://www.slf4j.org</url>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>animal-sniffer-maven-plugin</artifactId>
<configuration>
<!-- Signatures cannot be determined and will error unless excluded. This is isolated to
code otherwise already marked for removal in the module artifact. -->
<ignores>
<ignore>org.slf4j.impl.StaticMDCBinder</ignore>
<ignore>org.slf4j.impl.StaticLoggerBinder</ignore>
<ignore>org.slf4j.impl.StaticMarkerBinder</ignore>
</ignores>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkMode>once</forkMode>
<reportFormat>plain</reportFormat>
<trimStackTrace>false</trimStackTrace>
<excludes>
<exclude>**/AllTest.java</exclude>
<exclude>**/PackageTest.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>bundle-test-jar</id>
<phase>package</phase>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<configuration>
<tasks>
<echo>Removing slf4j-api's dummy StaticLoggerBinder and StaticMarkerBinder</echo>
<delete dir="target/classes/org/slf4j/impl"/>
</tasks>
</configuration>
</plugin>
</plugins>
</build>
</project>