Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plugin configuration for annotation processor arguments #263

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,28 @@
/*
* Copyright © 2022 j2cl-maven-plugin authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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,23 @@
/*
* Copyright © 2022 j2cl-maven-plugin authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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,25 @@
/*
* Copyright © 2022 j2cl-maven-plugin authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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,33 @@
/*
* Copyright © 2022 j2cl-maven-plugin authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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,80 @@
/*
* Copyright © 2022 j2cl-maven-plugin authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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
Loading
Loading