Skip to content

Commit

Permalink
Add plugin configuration for annotation processor arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
treblereel committed May 28, 2024
1 parent 1108fc9 commit dda703e
Show file tree
Hide file tree
Showing 18 changed files with 321 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,21 @@ public Map<String, String> getDefines() {
}, TreeMap::new));
}

@Override
public Map<String, String> getAnnotationProcessorsArgs() {
ConfigValueProvider.ConfigNode args = config.findNode("annotationProcessorsArgs");
if (args == null) {
return Collections.emptyMap();
}
return args.getChildren().stream()
// order does not matter, let's make sure the task sees them in the correct order
.sorted(Comparator.comparing(ConfigValueProvider.ConfigNode::getName))
// create a map to return - since this is sorted it should be stable both in this tracker and for the consuming task
.collect(Collectors.toMap(ConfigValueProvider.ConfigNode::getName, this::useStringConfig, (s, s2) -> {
throw new IllegalStateException("Two configs found with the same key: " + s + ", s2");
}, TreeMap::new));
}

@Override
public Map<String, String> getUsedConfigs() {
return Collections.unmodifiableMap(usedKeys);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,5 @@ public interface Config {
*/
boolean isIncrementalEnabled();

Map<String, String> getAnnotationProcessorsArgs();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<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>

<groupId>annotation-processor-in-reactor-args</groupId>
<artifactId>app</artifactId>
<version>1.0</version>
<packaging>war</packaging>

<dependencies>
<!-- Annotation processor and resources to use to generate output -->
<dependency>
<groupId>annotation-processor-in-reactor-args</groupId>
<artifactId>processor</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<!-- J2cl Test dependencies -->
<dependency>
<groupId>com.vertispan.j2cl</groupId>
<artifactId>junit-annotations</artifactId>
<version>@j2cl.version@</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.vertispan.j2cl</groupId>
<artifactId>junit-emul</artifactId>
<version>@j2cl.version@</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.vertispan.j2cl</groupId>
<artifactId>junit-emul</artifactId>
<version>@j2cl.version@</version>
<classifier>sources</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.vertispan.j2cl</groupId>
<artifactId>gwttestcase-emul</artifactId>
<version>@j2cl.version@</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
<execution>
<id>test-js</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<compilationLevel>ADVANCED</compilationLevel>
<annotationProcessorsArgs>
<optionName>my-option</optionName>
</annotationProcessorsArgs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.SOURCE)
@interface MyAnnotation {
String value() default "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example;

public class MyApp {
public static void start() {
MyOption.INSTANCE.getOption();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example;

@MyAnnotation
public interface MyOption {
MyOption INSTANCE = new MyOption_Impl();

String getOption();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<web-app></web-app>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example;

import com.google.j2cl.junit.apt.J2clTestInput;

import org.junit.Assert;
import org.junit.Test;

@J2clTestInput(OptionTest.class)
public class OptionTest {
@Test
public void resourceContents() {
// This test verifies that the resource contents were correctly read at build time
MyOption res = MyOption.INSTANCE;

Assert.assertEquals("my-option", res.getOption());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<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>

<groupId>annotation-processor-in-reactor-args</groupId>
<artifactId>annotation-processor-in-reactor</artifactId>
<version>1.0</version>
<packaging>pom</packaging>

<description>
This project tests not only if an annotation processor can be in the reactor (as a dependency of
the actual j2cl app), but also if the processor can correctly read files out of java and resources
directories of projects and dependencies.
</description>

<modules>
<module>processor</module>
<module>app</module>
</modules>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<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>annotation-processor-in-reactor-args</groupId>
<artifactId>annotation-processor-in-reactor</artifactId>
<version>1.0</version>
</parent>
<artifactId>processor</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>com.squareup</groupId>
<artifactId>javapoet</artifactId>
<version>1.13.0</version>
</dependency>
<dependency>
<groupId>com.google.auto</groupId>
<artifactId>auto-common</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<proc>none</proc>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.example;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class MyProcessor extends AbstractProcessor {
@Override
public Set<String> getSupportedAnnotationTypes() {
return Collections.singleton("com.example.MyAnnotation");
}

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
TypeElement annotationElt = processingEnv.getElementUtils().getTypeElement("com.example.MyAnnotation");
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(annotationElt);

Filer filer = processingEnv.getFiler();
elements.stream().filter(e -> e.getKind().isInterface()).forEach(type -> {
//emit a new class for that type, with no-arg string methods
try {
ClassName origClassName = ClassName.get((TypeElement) type);
TypeSpec.Builder builder = TypeSpec.classBuilder(origClassName.simpleName() + "_Impl")
.addModifiers(Modifier.PUBLIC)
.addSuperinterface(origClassName);

Map<String, String> options = processingEnv.getOptions();
CharSequence contents = options.getOrDefault("optionName", "default");
MethodSpec methodSpec = MethodSpec.methodBuilder("getOption")
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override.class)
.returns(String.class)
.addCode("return $S;", contents)
.build();

builder.addMethod(methodSpec);
JavaFile newFile = JavaFile.builder(origClassName.packageName(), builder.build()).build();
newFile.writeTo(filer);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.example.MyProcessor
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ public class BuildMojo extends AbstractBuildMojo {
@Parameter(defaultValue = "false")
protected boolean enableSourcemaps;

/**
* Arguments to pass to annotation processors, in the form of key-value pairs.
*/
@Parameter
protected Map<String, String> annotationProcessorsArgs = new TreeMap<>();

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
// pre-create the directory so it is easier to find up front, even if it starts off empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ public class TestMojo extends AbstractBuildMojo {
@Parameter
protected TranslationsFileConfig translationsFile;

/**
* Arguments to pass to annotation processors, in the form of key-value pairs.
*/
@Parameter
protected Map<String, String> annotationProcessorsArgs = new TreeMap<>();

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (skip) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Function;
Expand Down Expand Up @@ -151,6 +153,12 @@ public class WatchMojo extends AbstractBuildMojo {
@Parameter(defaultValue = "SORT_ONLY")
protected String dependencyMode;

/**
* Arguments to pass to annotation processors, in the form of key-value pairs.
*/
@Parameter
protected Map<String, String> annotationProcessorsArgs = new TreeMap<>();

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
PluginDescriptor pluginDescriptor = (PluginDescriptor) getPluginContext().get("pluginDescriptor");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -114,6 +115,7 @@ public Task resolve(Project project, Config config) {

File bootstrapClasspath = config.getBootstrapClasspath();
List<File> extraClasspath = new ArrayList<>(config.getExtraClasspath());
Map<String, String> annotationProcessorsArgs = Collections.unmodifiableMap(config.getAnnotationProcessorsArgs());
Set<String> processors = new HashSet<>();
project.getDependencies()
.stream()
Expand Down Expand Up @@ -144,7 +146,7 @@ public Task resolve(Project project, Config config) {
List<File> sourcePaths = inputDirs.getParentPaths().stream().map(Path::toFile).collect(Collectors.toUnmodifiableList());
File generatedClassesDir = getGeneratedClassesDir(context);
File classOutputDir = context.outputPath().toFile();
Javac javac = new Javac(context, generatedClassesDir, sourcePaths, classpathDirs, classOutputDir, bootstrapClasspath, aptProcessors);
Javac javac = new Javac(context, generatedClassesDir, sourcePaths, classpathDirs, classOutputDir, bootstrapClasspath, aptProcessors, annotationProcessorsArgs);

// TODO convention for mapping to original file paths, provide FileInfo out of Inputs instead of Paths,
// automatically relativized?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public Task resolve(Project project, Config config) {
).collect(Collectors.toUnmodifiableList());

List<File> sourcePaths = ownSources.getParentPaths().stream().map(Path::toFile).collect(Collectors.toUnmodifiableList());
Javac javac = new Javac(context, null, sourcePaths, classpathDirs, context.outputPath().toFile(), bootstrapClasspath, Collections.emptySet());
Javac javac = new Javac(context, null, sourcePaths, classpathDirs, context.outputPath().toFile(), bootstrapClasspath, Collections.emptySet(), Collections.emptyMap());

// TODO convention for mapping to original file paths, provide FileInfo out of Inputs instead of Paths,
// automatically relativized?
Expand Down
Loading

0 comments on commit dda703e

Please sign in to comment.