Skip to content

Commit 86b9f59

Browse files
psirokygnodet
andauthored
[MCOMPILER-395] Allow dependency exclusions for 'annotationProcessorPaths' (#173)
Co-authored-by: Guillaume Nodet <[email protected]>
1 parent e304ceb commit 86b9f59

File tree

13 files changed

+537
-46
lines changed

13 files changed

+537
-46
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one
4+
~ or more contributor license agreements. See the NOTICE file
5+
~ distributed with this work for additional information
6+
~ regarding copyright ownership. The ASF licenses this file
7+
~ to you under the Apache License, Version 2.0 (the
8+
~ "License"); you may not use this file except in compliance
9+
~ with the License. You may obtain a copy of the License at
10+
~
11+
~ http://www.apache.org/licenses/LICENSE-2.0
12+
~
13+
~ Unless required by applicable law or agreed to in writing,
14+
~ software distributed under the License is distributed on an
15+
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
~ KIND, either express or implied. See the License for the
17+
~ specific language governing permissions and limitations
18+
~ under the License.
19+
-->
20+
21+
<project xmlns="http://maven.apache.org/POM/4.0.0"
22+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
<modelVersion>4.0.0</modelVersion>
25+
26+
<parent>
27+
<groupId>org.apache.maven.plugins.compiler.it</groupId>
28+
<artifactId>mcompiler395-test</artifactId>
29+
<version>1.0.0-SNAPSHOT</version>
30+
</parent>
31+
32+
<artifactId>mcompiler395-annotation-processor-dep</artifactId>
33+
34+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package mcompiler395;
20+
21+
public class AnnotationProcessorDependency {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one
4+
~ or more contributor license agreements. See the NOTICE file
5+
~ distributed with this work for additional information
6+
~ regarding copyright ownership. The ASF licenses this file
7+
~ to you under the Apache License, Version 2.0 (the
8+
~ "License"); you may not use this file except in compliance
9+
~ with the License. You may obtain a copy of the License at
10+
~
11+
~ http://www.apache.org/licenses/LICENSE-2.0
12+
~
13+
~ Unless required by applicable law or agreed to in writing,
14+
~ software distributed under the License is distributed on an
15+
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
~ KIND, either express or implied. See the License for the
17+
~ specific language governing permissions and limitations
18+
~ under the License.
19+
-->
20+
21+
<project xmlns="http://maven.apache.org/POM/4.0.0"
22+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
<modelVersion>4.0.0</modelVersion>
25+
26+
<parent>
27+
<groupId>org.apache.maven.plugins.compiler.it</groupId>
28+
<artifactId>mcompiler395-test</artifactId>
29+
<version>1.0.0-SNAPSHOT</version>
30+
</parent>
31+
32+
<artifactId>mcompiler395-annotation-processor</artifactId>
33+
34+
<dependencies>
35+
<dependency>
36+
<groupId>org.apache.maven.plugins.compiler.it</groupId>
37+
<artifactId>mcompiler395-annotation-processor-dep</artifactId>
38+
<version>1.0.0-SNAPSHOT</version>
39+
</dependency>
40+
</dependencies>
41+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package mcompiler395;
20+
21+
import javax.annotation.processing.AbstractProcessor;
22+
import javax.annotation.processing.Filer;
23+
import javax.annotation.processing.RoundEnvironment;
24+
import javax.annotation.processing.SupportedAnnotationTypes;
25+
import javax.annotation.processing.SupportedSourceVersion;
26+
import javax.lang.model.SourceVersion;
27+
import javax.lang.model.element.Element;
28+
import javax.lang.model.element.Name;
29+
import javax.lang.model.element.PackageElement;
30+
import javax.lang.model.element.TypeElement;
31+
import javax.lang.model.util.Elements;
32+
import javax.tools.FileObject;
33+
import javax.tools.StandardLocation;
34+
35+
import java.io.IOException;
36+
import java.io.Writer;
37+
import java.util.Set;
38+
39+
@SupportedSourceVersion(SourceVersion.RELEASE_6)
40+
@SupportedAnnotationTypes("mcompiler395.SimpleAnnotation")
41+
public class SimpleAnnotationProcessor extends AbstractProcessor {
42+
43+
@Override
44+
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
45+
if (annotations.isEmpty()) {
46+
return true;
47+
}
48+
49+
// assert that mcompiler395-annotation-processor-dep is NOT on the processorpath, since it is excluded
50+
// in the plugin configuration
51+
try {
52+
getClass().getClassLoader().loadClass("mcompiler395.AnnotationProcessorDependency");
53+
throw new RuntimeException("Expected a ClassNotFoundException, because "
54+
+ "mcompiler395.AnnotationProcessorDependency is not supposed to be on the processorpath.");
55+
} catch (ClassNotFoundException expected) {
56+
// expected
57+
}
58+
59+
Filer filer = processingEnv.getFiler();
60+
61+
Elements elementUtils = processingEnv.getElementUtils();
62+
63+
Set<? extends Element> elements =
64+
roundEnv.getElementsAnnotatedWith(annotations.iterator().next());
65+
66+
for (Element element : elements) {
67+
Name name = element.getSimpleName();
68+
69+
PackageElement packageElement = elementUtils.getPackageOf(element);
70+
71+
try {
72+
Name packageName = packageElement.getQualifiedName();
73+
FileObject resource =
74+
filer.createResource(StandardLocation.SOURCE_OUTPUT, packageName, name + ".txt", element);
75+
76+
Writer writer = resource.openWriter();
77+
writer.write(name.toString());
78+
writer.close();
79+
} catch (IOException e) {
80+
throw new RuntimeException(e);
81+
}
82+
}
83+
return !elements.isEmpty();
84+
}
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one
4+
~ or more contributor license agreements. See the NOTICE file
5+
~ distributed with this work for additional information
6+
~ regarding copyright ownership. The ASF licenses this file
7+
~ to you under the Apache License, Version 2.0 (the
8+
~ "License"); you may not use this file except in compliance
9+
~ with the License. You may obtain a copy of the License at
10+
~
11+
~ http://www.apache.org/licenses/LICENSE-2.0
12+
~
13+
~ Unless required by applicable law or agreed to in writing,
14+
~ software distributed under the License is distributed on an
15+
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
~ KIND, either express or implied. See the License for the
17+
~ specific language governing permissions and limitations
18+
~ under the License.
19+
-->
20+
21+
<project xmlns="http://maven.apache.org/POM/4.0.0"
22+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
<modelVersion>4.0.0</modelVersion>
25+
26+
<parent>
27+
<groupId>org.apache.maven.plugins.compiler.it</groupId>
28+
<artifactId>mcompiler395-test</artifactId>
29+
<version>1.0.0-SNAPSHOT</version>
30+
</parent>
31+
32+
<artifactId>mcompiler395-annotation-user</artifactId>
33+
34+
<build>
35+
<plugins>
36+
<plugin>
37+
<artifactId>maven-compiler-plugin</artifactId>
38+
<configuration>
39+
<annotationProcessors>
40+
<annotationProcessor>mcompiler395.SimpleAnnotationProcessor</annotationProcessor>
41+
</annotationProcessors>
42+
<annotationProcessorPaths>
43+
<path>
44+
<groupId>org.apache.maven.plugins.compiler.it</groupId>
45+
<artifactId>mcompiler395-annotation-processor</artifactId>
46+
<version>1.0.0-SNAPSHOT</version>
47+
<exclusions>
48+
<exclusion>
49+
<groupId>org.apache.maven.plugins.compiler.it</groupId>
50+
<artifactId>mcompiler395-annotation-processor-dep</artifactId>
51+
</exclusion>
52+
</exclusions>
53+
</path>
54+
</annotationProcessorPaths>
55+
</configuration>
56+
</plugin>
57+
<plugin>
58+
<groupId>org.apache.maven.plugins.compiler.it</groupId>
59+
<artifactId>annotation-verify-plugin</artifactId>
60+
<version>1.0.0-SNAPSHOT</version>
61+
<executions>
62+
<execution>
63+
<id>verify-annotations</id>
64+
<phase>process-test-classes</phase>
65+
<goals>
66+
<goal>read-source</goal>
67+
</goals>
68+
<configuration>
69+
<sourceClass>mcompiler395.SimpleObject</sourceClass>
70+
<testSourceClass>mcompiler395.SimpleTestObject</testSourceClass>
71+
</configuration>
72+
</execution>
73+
</executions>
74+
</plugin>
75+
</plugins>
76+
</build>
77+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package mcompiler395;
20+
21+
import java.lang.annotation.ElementType;
22+
import java.lang.annotation.Retention;
23+
import java.lang.annotation.RetentionPolicy;
24+
import java.lang.annotation.Target;
25+
26+
@Target(ElementType.TYPE)
27+
@Retention(RetentionPolicy.SOURCE)
28+
public @interface SimpleAnnotation {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package mcompiler395;
20+
21+
@SimpleAnnotation
22+
public class SimpleObject {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package mcompiler395;
20+
21+
@SimpleAnnotation
22+
public class SimpleTestObject {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
invoker.goals=process-test-classes

0 commit comments

Comments
 (0)