Skip to content

Commit a4539c1

Browse files
committed
[MCOMPILER-391] Use dep mgmt when resolving annotation processors and their deps
1 parent b225b1c commit a4539c1

File tree

23 files changed

+998
-4
lines changed

23 files changed

+998
-4
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>mcompiler391-test</artifactId>
29+
<version>1.0.0-SNAPSHOT</version>
30+
</parent>
31+
32+
<artifactId>mcompiler391-annotation-api</artifactId>
33+
34+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 mcompiler391;
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 {
29+
/**
30+
* Specifies which classes (FQCNs) are supposed to be on classpath during annotation processing.
31+
* Used to check if the dependency resolution mechanism of maven-compiler-plugin is constructing
32+
* the classpath (processorpath) correctly, based on the plugin configuration and dependency management.
33+
*/
34+
String[] onClasspath() default "";
35+
}
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>mcompiler391-test</artifactId>
29+
<version>1.0.0-SNAPSHOT</version>
30+
</parent>
31+
32+
<artifactId>mcompiler391-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 mcompiler391;
20+
21+
public class AnnotationProcessorDependencyV1 {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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>mcompiler391-test</artifactId>
29+
<version>1.0.0-SNAPSHOT</version>
30+
</parent>
31+
32+
<!-- Same groupId + artifactId as for v1 (1.0.0-SNAPSHOT), but different version to test version overrides
33+
via dependency management -->
34+
<artifactId>mcompiler391-annotation-processor-dep</artifactId>
35+
<version>2.0.0-SNAPSHOT</version>
36+
37+
</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 mcompiler391;
20+
21+
public class AnnotationProcessorDependencyV2 {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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>mcompiler391-test</artifactId>
29+
<version>1.0.0-SNAPSHOT</version>
30+
</parent>
31+
32+
<artifactId>mcompiler391-annotation-processor</artifactId>
33+
34+
<dependencies>
35+
<dependency>
36+
<groupId>org.apache.maven.plugins.compiler.it</groupId>
37+
<artifactId>mcompiler391-annotation-api</artifactId>
38+
<version>1.0.0-SNAPSHOT</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.apache.maven.plugins.compiler.it</groupId>
42+
<artifactId>mcompiler391-annotation-processor-dep</artifactId>
43+
<version>1.0.0-SNAPSHOT</version>
44+
</dependency>
45+
</dependencies>
46+
47+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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 mcompiler391;
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("mcompiler391.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+
Filer filer = processingEnv.getFiler();
50+
51+
Elements elementUtils = processingEnv.getElementUtils();
52+
53+
Set<? extends Element> elements =
54+
roundEnv.getElementsAnnotatedWith(annotations.iterator().next());
55+
56+
for (Element element : elements) {
57+
Name name = element.getSimpleName();
58+
PackageElement packageElement = elementUtils.getPackageOf(element);
59+
60+
SimpleAnnotation annotation = element.getAnnotation(SimpleAnnotation.class);
61+
assertClassesAreOnClasspath(annotation.onClasspath());
62+
63+
try {
64+
Name packageName = packageElement.getQualifiedName();
65+
FileObject resource =
66+
filer.createResource(StandardLocation.SOURCE_OUTPUT, packageName, name + ".txt", element);
67+
68+
Writer writer = resource.openWriter();
69+
writer.write(name.toString());
70+
writer.close();
71+
} catch (IOException e) {
72+
throw new RuntimeException(e);
73+
}
74+
}
75+
return !elements.isEmpty();
76+
}
77+
78+
private void assertClassesAreOnClasspath(String[] fqcns) {
79+
for (String fqcn : fqcns) {
80+
try {
81+
getClass().getClassLoader().loadClass(fqcn);
82+
} catch (ClassNotFoundException expected) {
83+
throw new RuntimeException(String.format(
84+
"Expected class '%s' to be on the classpath (processorpath), " + "but it wasn't.", fqcn));
85+
}
86+
}
87+
}
88+
}

0 commit comments

Comments
 (0)