Skip to content

Commit 3efa684

Browse files
committed
Fix: annotation processor not always detected when specified with <type>processor</type>.
Replace some relatively verbose "is null or empty" checks by `isAbsent(…)` methods.
1 parent 8dd8f36 commit 3efa684

File tree

17 files changed

+453
-56
lines changed

17 files changed

+453
-56
lines changed

src/it/MCOMPILER-272/processor1/src/main/resources/META-INF/services/javax.annotation.processing.Processor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
# to you under the Apache License, Version 2.0 (the
66
# "License"); you may not use this file except in compliance
77
# with the License. You may obtain a copy of the License at
8-
#
8+
#
99
# http://www.apache.org/licenses/LICENSE-2.0
10-
#
10+
#
1111
# Unless required by applicable law or agreed to in writing,
1212
# software distributed under the License is distributed on an
1313
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY

src/it/MCOMPILER-272/processor2/src/main/resources/META-INF/services/javax.annotation.processing.Processor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
# to you under the Apache License, Version 2.0 (the
66
# "License"); you may not use this file except in compliance
77
# with the License. You may obtain a copy of the License at
8-
#
8+
#
99
# http://www.apache.org/licenses/LICENSE-2.0
10-
#
10+
#
1111
# Unless required by applicable law or agreed to in writing,
1212
# software distributed under the License is distributed on an
1313
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
<project xmlns="http://maven.apache.org/POM/4.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.1.0 http://maven.apache.org/xsd/maven-4.1.0.xsd">
21+
<modelVersion>4.1.0</modelVersion>
22+
<parent>
23+
<groupId>org.apache.maven.plugins.compiler.it</groupId>
24+
<artifactId>processor-type</artifactId>
25+
<version>1.0-SNAPSHOT</version>
26+
</parent>
27+
<artifactId>annotation-processor-dep</artifactId>
28+
</project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 dependency;
20+
21+
public class AnnotationProcessorDependency {
22+
public static void foo() {}
23+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
<project xmlns="http://maven.apache.org/POM/4.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.1.0 http://maven.apache.org/xsd/maven-4.1.0.xsd">
21+
<modelVersion>4.1.0</modelVersion>
22+
<parent>
23+
<groupId>org.apache.maven.plugins.compiler.it</groupId>
24+
<artifactId>processor-type</artifactId>
25+
<version>1.0-SNAPSHOT</version>
26+
</parent>
27+
<artifactId>annotation-processor</artifactId>
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.apache.maven.plugins.compiler.it</groupId>
31+
<artifactId>annotation-processor-dep</artifactId>
32+
<version>1.0-SNAPSHOT</version>
33+
</dependency>
34+
</dependencies>
35+
</project>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 processor;
20+
21+
import javax.annotation.processing.AbstractProcessor;
22+
import javax.annotation.processing.RoundEnvironment;
23+
import javax.annotation.processing.SupportedAnnotationTypes;
24+
import javax.annotation.processing.SupportedSourceVersion;
25+
import javax.lang.model.SourceVersion;
26+
import javax.lang.model.element.Element;
27+
import javax.lang.model.element.Name;
28+
import javax.lang.model.element.TypeElement;
29+
import javax.tools.FileObject;
30+
import javax.tools.StandardLocation;
31+
32+
import java.io.IOException;
33+
import java.io.UncheckedIOException;
34+
import java.io.Writer;
35+
import java.util.Set;
36+
37+
@SupportedSourceVersion(SourceVersion.RELEASE_17)
38+
@SupportedAnnotationTypes("user.SimpleAnnotation")
39+
public class SimpleAnnotationProcessor extends AbstractProcessor {
40+
public SimpleAnnotationProcessor() {}
41+
42+
@Override
43+
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
44+
// Verifies that transitive dependencies worked.
45+
// TODO: pending https://github.com/apache/maven/pull/11373
46+
// dependency.AnnotationProcessorDependency.foo();
47+
48+
boolean claimed = false;
49+
for (TypeElement annotation : annotations) {
50+
for (Element element : roundEnv.getElementsAnnotatedWith(annotation)) {
51+
Name packageName =
52+
processingEnv.getElementUtils().getPackageOf(element).getQualifiedName();
53+
Name name = element.getSimpleName();
54+
try {
55+
FileObject resource = processingEnv
56+
.getFiler()
57+
.createResource(StandardLocation.SOURCE_OUTPUT, packageName, name + ".txt", element);
58+
try (Writer writer = resource.openWriter()) {
59+
writer.write(name.toString());
60+
}
61+
} catch (IOException e) {
62+
throw new UncheckedIOException(e);
63+
}
64+
claimed = true;
65+
}
66+
}
67+
return claimed;
68+
}
69+
}
Lines changed: 18 additions & 0 deletions
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+
processor.SimpleAnnotationProcessor
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
<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">
21+
<modelVersion>4.0.0</modelVersion>
22+
23+
<parent>
24+
<groupId>org.apache.maven.plugins.compiler.it</groupId>
25+
<artifactId>processor-type</artifactId>
26+
<version>1.0-SNAPSHOT</version>
27+
</parent>
28+
29+
<artifactId>annotation-user</artifactId>
30+
31+
<dependencies>
32+
<dependency>
33+
<groupId>org.apache.maven.plugins.compiler.it</groupId>
34+
<artifactId>annotation-processor</artifactId>
35+
<version>1.0-SNAPSHOT</version>
36+
<type>processor</type>
37+
</dependency>
38+
</dependencies>
39+
40+
<build>
41+
<plugins>
42+
<plugin>
43+
<groupId>org.apache.maven.plugins.compiler.it</groupId>
44+
<artifactId>annotation-verify-plugin</artifactId>
45+
<version>1.0.0-SNAPSHOT</version>
46+
<executions>
47+
<execution>
48+
<id>verify-annotations</id>
49+
<goals>
50+
<goal>read-source</goal>
51+
</goals>
52+
<phase>process-test-classes</phase>
53+
<configuration>
54+
<sourceClass>user.SimpleObject</sourceClass>
55+
<testSourceClass>user.SimpleTestObject</testSourceClass>
56+
</configuration>
57+
</execution>
58+
</executions>
59+
</plugin>
60+
</plugins>
61+
</build>
62+
</project>
Lines changed: 28 additions & 0 deletions
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 user;
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 {}
Lines changed: 22 additions & 0 deletions
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 user;
20+
21+
@SimpleAnnotation
22+
public class SimpleObject {}

0 commit comments

Comments
 (0)