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 AddAnnotationProcessor recipe #4694

Merged
merged 10 commits into from
Nov 25, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.openrewrite.maven;

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Option;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.xml.XmlIsoVisitor;
import org.openrewrite.xml.tree.Xml;

import static org.openrewrite.maven.trait.Traits.mavenPlugin;

@Value
@EqualsAndHashCode(callSuper = false)
public class AddAnnotationProcessor extends Recipe {
private static final String MAVEN_COMPILER_PLUGIN_GROUP_ID = "org.apache.maven.plugins";
private static final String MAVEN_COMPILER_PLUGIN_ARTIFACT_ID = "maven-compiler-plugin";

@Option(displayName = "Group",
description = "The first part of the coordinate 'org.projectlombok:lombok-mapstruct-binding:0.2.0' of the processor to add.",
example = "org.projectlombok")
String groupId;

@Option(displayName = "Artifact",
description = "The second part of a coordinate 'org.projectlombok:lombok-mapstruct-binding:0.2.0' of the processor to add.",
example = "lombok-mapstruct-binding")
String artifactId;

@Option(displayName = "Version",
description = "The third part of a coordinate 'org.projectlombok:lombok-mapstruct-binding:0.2.0' of the processor to add. " +
"Note that an exact version is expected",
example = "0.2.0")
String version;

@Override
public String getDisplayName() {
return "Add an annotation processor to `maven-compiler-plugin`";
}

@Override
public String getDescription() {
return "Add an annotation processor to the maven compiler plugin. Will not do anything if it already exists. " +
"Also doesn't add anything when no other annotation processors are defined yet. " +
"(Perhaps `ChangePluginConfiguration` can be used).";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new MavenVisitor<ExecutionContext>() {
@Override
public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
Xml.Tag plugins = (Xml.Tag) super.visitTag(tag, ctx);
plugins = (Xml.Tag) mavenPlugin().asVisitor(plugin -> {
if (MAVEN_COMPILER_PLUGIN_GROUP_ID.equals(plugin.getGroupId())
&& MAVEN_COMPILER_PLUGIN_ARTIFACT_ID.equals(plugin.getArtifactId())) {
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
return new XmlIsoVisitor<ExecutionContext>() {
@Override
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
Xml.Tag tg = super.visitTag(tag, ctx);
if ("annotationProcessorPaths".equals(tg.getName())) {
for (Xml.Tag child : tg.getChildren()) {
if (groupId.equals(child.getChildValue("groupId").orElse(null)) &&
artifactId.equals(child.getChildValue("artifactId").orElse(null))) {
return tg;
}
}
return tg.withContent(ListUtils.concat(tg.getChildren(), Xml.Tag.build(String.format(
"<path>\n<groupId>%s</groupId>\n<artifactId>%s</artifactId>\n<version>%s</version>\n</path>",
groupId, artifactId, version))));
}
return tg;
}
}.visitTag(plugin.getTree(), ctx);
}
return plugin.getTree();
}).visitNonNull(plugins, 0);
if (plugins != tag) {
plugins = autoFormat(plugins, ctx);
}
return plugins;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.Cursor;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.trait.Trait;
import org.openrewrite.xml.XPathMatcher;
import org.openrewrite.xml.tree.Xml;
Expand All @@ -29,6 +30,7 @@ public class MavenPlugin implements Trait<Xml.Tag> {
static final XPathMatcher PLUGIN_MATCHER = new XPathMatcher("//plugins/plugin");

Cursor cursor;

String groupId;

@Nullable
Expand Down Expand Up @@ -59,10 +61,11 @@ public static class Matcher extends MavenTraitMatcher<MavenPlugin> {

private Optional<String> getProperty(Cursor cursor, String property) {
Xml.Tag tag = cursor.getValue();
if (getResolutionResult(cursor).getPom().getProperties() != null) {
MavenResolutionResult resolutionResult = getResolutionResult(cursor);
if (resolutionResult != null && resolutionResult.getPom().getProperties() != null) {
if (tag.getChildValue(property).isPresent() && tag.getChildValue(property).get().trim().startsWith("${")) {
String propertyKey = tag.getChildValue(property).get().trim();
return Optional.ofNullable(getResolutionResult(cursor).getPom().getValue(propertyKey));
return Optional.ofNullable(resolutionResult.getPom().getValue(propertyKey));
}
}
return tag.getChildValue(property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.openrewrite.maven.trait;

import org.jspecify.annotations.Nullable;
import org.openrewrite.Cursor;
import org.openrewrite.SourceFile;
import org.openrewrite.maven.MavenVisitor;
Expand All @@ -27,7 +28,7 @@

public abstract class MavenTraitMatcher<U extends Trait<?>> extends SimpleTraitMatcher<U> {

protected MavenResolutionResult getResolutionResult(Cursor cursor) {
protected @Nullable MavenResolutionResult getResolutionResult(Cursor cursor) {
AtomicReference<MavenResolutionResult> mrr = new AtomicReference<>();
new MavenVisitor<Integer>() {
@Override
Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Elsewhere we have similar classes in src/main as well; makes for nicer usage to move this up now as well.

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.openrewrite.maven;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.maven.Assertions.pomXml;

class AddAnnotationProcessorTest implements RewriteTest {

@DocumentExample
@Test
void addAnnotationProcessor() {
rewriteRun(
spec -> spec.recipe(new AddAnnotationProcessor(
"org.projectlombok",
"lombok-mapstruct-binding",
"0.2.0"
)),
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
""",
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
"""
)
);
}

@Test
void shouldNotAddProcessorAlreadyPresent() {
rewriteRun(
spec -> spec.recipe(new AddAnnotationProcessor(
"org.projectlombok",
"lombok-mapstruct-binding",
"0.2.0"
)),
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.1.0</version>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd intentionally changed this to use a mismatched version; should we perhaps override any existing version here? Or only when lower than the version we're intended to add?

</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
"""
)
);
}
}
Loading