From 44d85d4cf50b472baf635ccf23db4340a9e18538 Mon Sep 17 00:00:00 2001 From: Daniel Mensinger Date: Mon, 4 Nov 2024 11:01:25 +0100 Subject: [PATCH 1/2] [MCOMPILER-538] Add test case --- .../code/MyAnnotatedClass.bad.java | 29 ++++++ .../code/MyAnnotatedClass.good.java | 29 ++++++ src/it/MCOMPILER-538/code/pom.xml | 93 +++++++++++++++++++ .../code/src/main/java/bar/.gitignore | 1 + .../code/src/main/java/bar/package-info.java | 20 ++++ src/it/MCOMPILER-538/invoker.properties | 23 +++++ src/it/MCOMPILER-538/pom.xml | 74 +++++++++++++++ src/it/MCOMPILER-538/processor/pom.xml | 43 +++++++++ .../src/main/java/foo/MyAnnotation.java | 28 ++++++ .../src/main/java/foo/MyProcessor.java | 69 ++++++++++++++ .../javax.annotation.processing.Processor | 18 ++++ 11 files changed, 427 insertions(+) create mode 100644 src/it/MCOMPILER-538/code/MyAnnotatedClass.bad.java create mode 100644 src/it/MCOMPILER-538/code/MyAnnotatedClass.good.java create mode 100644 src/it/MCOMPILER-538/code/pom.xml create mode 100644 src/it/MCOMPILER-538/code/src/main/java/bar/.gitignore create mode 100644 src/it/MCOMPILER-538/code/src/main/java/bar/package-info.java create mode 100644 src/it/MCOMPILER-538/invoker.properties create mode 100644 src/it/MCOMPILER-538/pom.xml create mode 100644 src/it/MCOMPILER-538/processor/pom.xml create mode 100644 src/it/MCOMPILER-538/processor/src/main/java/foo/MyAnnotation.java create mode 100644 src/it/MCOMPILER-538/processor/src/main/java/foo/MyProcessor.java create mode 100644 src/it/MCOMPILER-538/processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor diff --git a/src/it/MCOMPILER-538/code/MyAnnotatedClass.bad.java b/src/it/MCOMPILER-538/code/MyAnnotatedClass.bad.java new file mode 100644 index 00000000..765cd64c --- /dev/null +++ b/src/it/MCOMPILER-538/code/MyAnnotatedClass.bad.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 bar; + +import foo.MyAnnotation; + +@MyAnnotation +public class MyAnnotatedClass { + MyGeneratedClass generatedClass = new MyGeneratedClass(false); + + // foo + +} diff --git a/src/it/MCOMPILER-538/code/MyAnnotatedClass.good.java b/src/it/MCOMPILER-538/code/MyAnnotatedClass.good.java new file mode 100644 index 00000000..cc4126d8 --- /dev/null +++ b/src/it/MCOMPILER-538/code/MyAnnotatedClass.good.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 bar; + +import foo.MyAnnotation; + +@MyAnnotation +public class MyAnnotatedClass { + MyGeneratedClass generatedClass = new MyGeneratedClass(); + + // foo + +} diff --git a/src/it/MCOMPILER-538/code/pom.xml b/src/it/MCOMPILER-538/code/pom.xml new file mode 100644 index 00000000..2dd9744f --- /dev/null +++ b/src/it/MCOMPILER-538/code/pom.xml @@ -0,0 +1,93 @@ + + + + 4.0.0 + + foo + bar + ${revision} + + ${revision} + jar + code + + + + foo + processor + ${revision} + provided + + + + + + + com.kohlschutter.mavenplugins + copy-rename-maven-plugin + 2.0.0 + + + set-source + generate-sources + + copy + + + + + ${basedir}/MyAnnotatedClass.${it.type}.java + ${basedir}/src/main/java/bar/MyAnnotatedClass.java + + + + + + + + + org.apache.maven.plugins + maven-clean-plugin + @version.maven-clean-plugin@ + + + clean-source-file + validate + + clean + + + true + + + src/main/java + + package-info.java + + + + + + + + + + diff --git a/src/it/MCOMPILER-538/code/src/main/java/bar/.gitignore b/src/it/MCOMPILER-538/code/src/main/java/bar/.gitignore new file mode 100644 index 00000000..ebce081a --- /dev/null +++ b/src/it/MCOMPILER-538/code/src/main/java/bar/.gitignore @@ -0,0 +1 @@ +MyAnnotatedClass.java diff --git a/src/it/MCOMPILER-538/code/src/main/java/bar/package-info.java b/src/it/MCOMPILER-538/code/src/main/java/bar/package-info.java new file mode 100644 index 00000000..0dde32fa --- /dev/null +++ b/src/it/MCOMPILER-538/code/src/main/java/bar/package-info.java @@ -0,0 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 bar; diff --git a/src/it/MCOMPILER-538/invoker.properties b/src/it/MCOMPILER-538/invoker.properties new file mode 100644 index 00000000..0a3163aa --- /dev/null +++ b/src/it/MCOMPILER-538/invoker.properties @@ -0,0 +1,23 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +# NOTE: The first time, we run up to "integration-test" phase which includes the AntRun execution which saves the +# timestamp of the first JAR for comparison with the timestamp of the JAR from the final "package" invocation. +# Note: +invoker.goals.1 = clean +invoker.goals.2 = compile -Dit.type=bad -Dmaven.compiler.failOnError=false +invoker.goals.3 = compile -Dit.type=good diff --git a/src/it/MCOMPILER-538/pom.xml b/src/it/MCOMPILER-538/pom.xml new file mode 100644 index 00000000..3ef45a7b --- /dev/null +++ b/src/it/MCOMPILER-538/pom.xml @@ -0,0 +1,74 @@ + + + + 4.0.0 + foo + bar + ${revision} + pom + + + 1.0-SNAPSHOT + + UTF-8 + + + + processor + code + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + @project.version@ + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + UTF-8 + 17 + 17 + true + true + true + false + + -Xlint:all,-processing + -Xdoclint:all,-missing,-accessibility + -Xmaxerrs + 2147483647 + -Xmaxwarns + 2147483647 + + + + + + diff --git a/src/it/MCOMPILER-538/processor/pom.xml b/src/it/MCOMPILER-538/processor/pom.xml new file mode 100644 index 00000000..a666cad9 --- /dev/null +++ b/src/it/MCOMPILER-538/processor/pom.xml @@ -0,0 +1,43 @@ + + + + 4.0.0 + + foo + bar + ${revision} + + ${revision} + processor + jar + + + + + org.apache.maven.plugins + maven-compiler-plugin + + none + + + + + diff --git a/src/it/MCOMPILER-538/processor/src/main/java/foo/MyAnnotation.java b/src/it/MCOMPILER-538/processor/src/main/java/foo/MyAnnotation.java new file mode 100644 index 00000000..88431aef --- /dev/null +++ b/src/it/MCOMPILER-538/processor/src/main/java/foo/MyAnnotation.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 foo; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.SOURCE) +@Target(ElementType.TYPE) +public @interface MyAnnotation {} diff --git a/src/it/MCOMPILER-538/processor/src/main/java/foo/MyProcessor.java b/src/it/MCOMPILER-538/processor/src/main/java/foo/MyProcessor.java new file mode 100644 index 00000000..b000e2de --- /dev/null +++ b/src/it/MCOMPILER-538/processor/src/main/java/foo/MyProcessor.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 foo; + +import javax.annotation.processing.*; +import javax.lang.model.SourceVersion; +import javax.lang.model.element.TypeElement; +import javax.tools.Diagnostic; +import javax.tools.JavaFileObject; + +import java.io.IOException; +import java.io.Writer; +import java.util.Set; + +@SupportedSourceVersion(SourceVersion.RELEASE_17) +@SupportedAnnotationTypes("foo.MyAnnotation") +public class MyProcessor extends AbstractProcessor { + + private int procRun = 0; + + @Override + public boolean process(Set annotations, RoundEnvironment roundEnv) { + try { + generate(annotations, roundEnv); + } catch (IOException e) { + throw new RuntimeException(e); + } + + return false; + } + + private void generate(Set annotations, RoundEnvironment roundEnv) throws IOException { + Messager msg = processingEnv.getMessager(); + msg.printMessage(Diagnostic.Kind.NOTE, String.format("[%d]: Hello Info", procRun)); + msg.printMessage(Diagnostic.Kind.WARNING, String.format("[%d]: Hello Warning", procRun)); + procRun++; + + if (procRun > 1) { + return; + } + + JavaFileObject file = processingEnv.getFiler().createSourceFile("bar.MyGeneratedClass"); + try (Writer writer = file.openWriter()) { + writer.write( + """ + package bar; + + public class MyGeneratedClass { + } + """); + } + } +} diff --git a/src/it/MCOMPILER-538/processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor b/src/it/MCOMPILER-538/processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor new file mode 100644 index 00000000..678af7ba --- /dev/null +++ b/src/it/MCOMPILER-538/processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor @@ -0,0 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +foo.MyProcessor From 9b8693970631d8051f672a34cafeb59bf159851c Mon Sep 17 00:00:00 2001 From: Daniel Mensinger Date: Mon, 4 Nov 2024 14:12:36 +0100 Subject: [PATCH 2/2] [MCOMPILER-538] Do not add generatedSourcesDirectory to the source roots --- .../SourcePathReadGoal.java | 9 ++++++-- .../plugin/compiler/AbstractCompilerMojo.java | 22 ------------------- 2 files changed, 7 insertions(+), 24 deletions(-) diff --git a/src/it/setup_annotation-verify-plugin/src/main/java/org.apache.maven.plugins.compiler.it/SourcePathReadGoal.java b/src/it/setup_annotation-verify-plugin/src/main/java/org.apache.maven.plugins.compiler.it/SourcePathReadGoal.java index 46b7d2b6..d3691fa2 100644 --- a/src/it/setup_annotation-verify-plugin/src/main/java/org.apache.maven.plugins.compiler.it/SourcePathReadGoal.java +++ b/src/it/setup_annotation-verify-plugin/src/main/java/org.apache.maven.plugins.compiler.it/SourcePathReadGoal.java @@ -47,12 +47,17 @@ public class SourcePathReadGoal extends AbstractMojo { public void execute() throws MojoExecutionException, MojoFailureException { if (sourceClass != null) { getLog().info("Checking compile source roots for: '" + sourceClass + "'"); - assertGeneratedSourceFileFor(sourceClass, project.getCompileSourceRoots()); + List roots = project.getCompileSourceRoots(); + roots.add(project.getModel().getBuild().getOutputDirectory() + "/../generated-sources/annotations"); + assertGeneratedSourceFileFor(sourceClass, roots); } if (testSourceClass != null) { getLog().info("Checking test-compile source roots for: '" + testSourceClass + "'"); - assertGeneratedSourceFileFor(testSourceClass, project.getTestCompileSourceRoots()); + List roots = project.getTestCompileSourceRoots(); + roots.add( + project.getModel().getBuild().getOutputDirectory() + "/../generated-test-sources/test-annotations"); + assertGeneratedSourceFileFor(testSourceClass, roots); } } diff --git a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java index 2229dd8b..9b63153a 100644 --- a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java +++ b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java @@ -821,28 +821,6 @@ public void execute() throws MojoExecutionException, CompilationFailureException if (!generatedSourcesDirectory.exists()) { generatedSourcesDirectory.mkdirs(); } - - String generatedSourcesPath = generatedSourcesDirectory.getAbsolutePath(); - - compileSourceRoots.add(generatedSourcesPath); - - if (isTestCompile()) { - getLog().debug("Adding " + generatedSourcesPath + " to test-compile source roots:\n " - + StringUtils.join(project.getTestCompileSourceRoots().iterator(), "\n ")); - - project.addTestCompileSourceRoot(generatedSourcesPath); - - getLog().debug("New test-compile source roots:\n " - + StringUtils.join(project.getTestCompileSourceRoots().iterator(), "\n ")); - } else { - getLog().debug("Adding " + generatedSourcesPath + " to compile source roots:\n " - + StringUtils.join(project.getCompileSourceRoots().iterator(), "\n ")); - - project.addCompileSourceRoot(generatedSourcesPath); - - getLog().debug("New compile source roots:\n " - + StringUtils.join(project.getCompileSourceRoots().iterator(), "\n ")); - } } compilerConfiguration.setSourceLocations(compileSourceRoots);