-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add plugin configuration for annotation processor arguments
- Loading branch information
1 parent
1108fc9
commit dda703e
Showing
18 changed files
with
321 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
j2cl-maven-plugin/src/it/annotation-processor-in-reactor-args/app/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
12 changes: 12 additions & 0 deletions
12
...c/it/annotation-processor-in-reactor-args/app/src/main/java/com/example/MyAnnotation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ""; | ||
} |
7 changes: 7 additions & 0 deletions
7
...ugin/src/it/annotation-processor-in-reactor-args/app/src/main/java/com/example/MyApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...n/src/it/annotation-processor-in-reactor-args/app/src/main/java/com/example/MyOption.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...en-plugin/src/it/annotation-processor-in-reactor-args/app/src/main/webapp/WEB-INF/web.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<web-app></web-app> |
17 changes: 17 additions & 0 deletions
17
...src/it/annotation-processor-in-reactor-args/app/src/test/java/com/example/OptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
j2cl-maven-plugin/src/it/annotation-processor-in-reactor-args/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
37 changes: 37 additions & 0 deletions
37
j2cl-maven-plugin/src/it/annotation-processor-in-reactor-args/processor/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
64 changes: 64 additions & 0 deletions
64
...annotation-processor-in-reactor-args/processor/src/main/java/com/example/MyProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...args/processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.example.MyProcessor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.