diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/ChangeDependency.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/ChangeDependency.java index 7e79261c09..33a1bdce31 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/ChangeDependency.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/ChangeDependency.java @@ -30,7 +30,6 @@ import org.openrewrite.groovy.tree.G; import org.openrewrite.internal.ListUtils; import org.openrewrite.internal.StringUtils; -import org.openrewrite.java.InlineMe; import org.openrewrite.java.JavaIsoVisitor; import org.openrewrite.java.tree.Expression; import org.openrewrite.java.tree.J; @@ -132,7 +131,6 @@ public ChangeDependency(String oldGroupId, String oldArtifactId, @Nullable Strin this.changeManagedDependency = changeManagedDependency; } - @InlineMe(replacement = "this(oldGroupId, oldArtifactId, newGroupId, newArtifactId, newVersion, versionPattern, overrideManagedVersion, true)") public ChangeDependency(String oldGroupId, String oldArtifactId, @Nullable String newGroupId, @Nullable String newArtifactId, @Nullable String newVersion, @Nullable String versionPattern, @Nullable Boolean overrideManagedVersion) { this(oldGroupId, oldArtifactId, newGroupId, newArtifactId, newVersion, versionPattern, overrideManagedVersion, true); } diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/trait/Traits.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/trait/Traits.java index e95ba81d1d..f98c569e22 100644 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/trait/Traits.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/trait/Traits.java @@ -15,8 +15,6 @@ */ package org.openrewrite.gradle.trait; -import org.openrewrite.java.InlineMe; - /** * @deprecated Use specific matchers like {@link GradleDependency.Matcher} or {@link JvmTestSuite.Matcher} instead. */ @@ -29,7 +27,6 @@ private Traits() { * @deprecated Use {@link GradleDependency.Matcher} instead. */ @Deprecated - @InlineMe(replacement = "new GradleDependency.Matcher()", imports = "org.openrewrite.gradle.GradleDependency") public static GradleDependency.Matcher gradleDependency() { return new GradleDependency.Matcher(); } @@ -38,7 +35,6 @@ public static GradleDependency.Matcher gradleDependency() { * @deprecated Use {@link JvmTestSuite.Matcher} instead. */ @Deprecated - @InlineMe(replacement = "new JvmTestSuite.Matcher()", imports = "org.openrewrite.gradle.JvmTestSuite") public static JvmTestSuite.Matcher jvmTestSuite() { return new JvmTestSuite.Matcher(); } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/ChangeMethodTargetToStatic.java b/rewrite-java/src/main/java/org/openrewrite/java/ChangeMethodTargetToStatic.java index efd2c5c170..cd6d481f8c 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/ChangeMethodTargetToStatic.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/ChangeMethodTargetToStatic.java @@ -68,7 +68,6 @@ public class ChangeMethodTargetToStatic extends Recipe { @Nullable Boolean matchUnknownTypes; - @InlineMe(replacement = "this(methodPattern, fullyQualifiedTargetTypeName, returnType, matchOverrides, false)") public ChangeMethodTargetToStatic(String methodPattern, String fullyQualifiedTargetTypeName, @Nullable String returnType, @Nullable Boolean matchOverrides) { this(methodPattern, fullyQualifiedTargetTypeName, returnType, matchOverrides, false); diff --git a/rewrite-java/src/main/java/org/openrewrite/java/InlineMe.java b/rewrite-java/src/main/java/org/openrewrite/java/InlineMe.java deleted file mode 100644 index ef1b223d23..0000000000 --- a/rewrite-java/src/main/java/org/openrewrite/java/InlineMe.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2025 the original author or authors. - *

- * 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 - *

- * https://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 org.openrewrite.java; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Target; - -/** - * Indicates that calls to the annotated method or constructor can be replaced with the provided replacement template. - *

- * This annotation is typically used to mark methods or constructors that are deprecated and have a preferred alternative - * implementation. The replacement template should provide a way to achieve the same functionality as the annotated - * method or constructor, but using a different approach. - */ -@Documented -@Target({ElementType.METHOD, ElementType.CONSTRUCTOR}) -public @interface InlineMe { - /** - * A Java template that can be used to replace calls to the annotated method. Typically starts with {@code this.} - * for method invocations in the same class, or {@code this(}, for constructor invocations in the same class. - *

- * Replacement templates may refer to elements from other classes, provided the necessary imports are passed in. - *

- * Replacement templates can reference method parameters by name, and can also reference {@code this}. - * - * @return a Java template that can be used to replace calls to the annotated method. - */ - String replacement(); - - /** - * Optional imports required by the replacement template. - * - * @return imports to be used when compiling the replacement template, and added to the source file if necessary. - */ - String[] imports() default {}; - - /** - * Optional static imports required by the replacement template. - * - * @return static imports to be used when compiling the replacement template, and added to the source file if necessary. - */ - String[] staticImports() default {}; -} diff --git a/rewrite-java/src/main/java/org/openrewrite/java/internal/parser/InlineMethodCallsRecipeGenerator.java b/rewrite-java/src/main/java/org/openrewrite/java/internal/parser/InlineMethodCallsRecipeGenerator.java deleted file mode 100644 index 11e4749ab8..0000000000 --- a/rewrite-java/src/main/java/org/openrewrite/java/internal/parser/InlineMethodCallsRecipeGenerator.java +++ /dev/null @@ -1,324 +0,0 @@ -/* - * Copyright 2025 the original author or authors. - *

- * 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 - *

- * https://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 org.openrewrite.java.internal.parser; - -import lombok.AllArgsConstructor; -import org.jspecify.annotations.Nullable; -import org.openrewrite.InMemoryExecutionContext; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.time.Year; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.stream.Stream; -import java.util.zip.GZIPInputStream; - -import static java.lang.String.format; -import static java.util.Objects.requireNonNull; -import static java.util.stream.Collectors.groupingBy; - -public class InlineMethodCallsRecipeGenerator { - - public static void main(String[] args) { - if (args.length < 3) { - System.err.println("Usage: InlineMethodCallsRecipeGenerator "); - System.exit(1); - } - - Path inputPath = Paths.get(args[0]); - Path outputPath = Paths.get(args[1]); - String recipeName = args[2]; - - generate(inputPath, outputPath, recipeName); - } - - static void generate(Path tsvFile, Path outputPath, String recipeName) { - List inlineMethods = new ArrayList<>(); - - TypeTable.Reader reader = new TypeTable.Reader(new InMemoryExecutionContext()); - try (InputStream is = Files.newInputStream(tsvFile); InputStream inflate = new GZIPInputStream(is)) { - reader.parseTsvAndProcess(inflate, TypeTable.Reader.Options.matchAll(), (gav, classes, nestedTypes) -> { - if (gav == null) { - return; - } - - // Process each class in this GAV - for (TypeTable.ClassDefinition classDef : classes.values()) { - // Process each member (method/constructor) in the class - for (TypeTable.Member member : classDef.getMembers()) { - // Check if member has @InlineMe annotation - String annotations = member.getAnnotations(); - if (annotations != null && annotations.contains("InlineMe")) { - InlineMeMethod inlineMethod = extractInlineMeMethod(gav, classDef, member); - if (inlineMethod != null) { - inlineMethods.add(inlineMethod); - } - } - } - } - }); - - // Generate YAML recipes - generateYamlRecipes(inlineMethods, outputPath, recipeName); - - System.out.println("Generated " + inlineMethods.size() + " inline recipes to " + outputPath); - - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - private static @Nullable InlineMeMethod extractInlineMeMethod(TypeTable.GroupArtifactVersion gav, - TypeTable.ClassDefinition classDef, - TypeTable.Member member) { - try { - // Parse the annotations to find @InlineMe - List annotations = - AnnotationDeserializer.parseAnnotations(requireNonNull(member.getAnnotations())); - for (AnnotationDeserializer.AnnotationInfo annotation : annotations) { - if (!annotation.getDescriptor().endsWith("/InlineMe;")) { - continue; - } - - List attributes = annotation.getAttributes(); - if (attributes == null) { - continue; - } - - // Extract annotation values - String replacement = null; - List imports = new ArrayList<>(); - List staticImports = new ArrayList<>(); - - for (AnnotationDeserializer.AttributeInfo attr : attributes) { - switch (attr.getName()) { - case "replacement": - replacement = (String) attr.getValue(); - break; - case "imports": - if (attr.getValue() instanceof Object[]) { - for (Object imp : (Object[]) attr.getValue()) { - imports.add((String) imp); - } - } - break; - case "staticImports": - if (attr.getValue() instanceof Object[]) { - for (Object imp : (Object[]) attr.getValue()) { - staticImports.add((String) imp); - } - } - break; - } - } - - if (replacement != null) { - // Build the method pattern - String methodPattern = buildMethodPattern(classDef, member); - - return new InlineMeMethod( - gav, - methodPattern, - replacement, - imports, - staticImports, - gav.getArtifactId() + "-" + gav.getVersion() - ); - } - } - } catch (Exception e) { - System.err.println("Failed to parse annotations for " + classDef.getName() + "." + member.getName() + ": " + e.getMessage()); - } - - return null; - } - - private static String buildMethodPattern(TypeTable.ClassDefinition classDef, TypeTable.Member member) { - String className = classDef.getName().replace('/', '.'); - String methodName = member.getName(); - - // For constructors, use the class name - if ("".equals(methodName)) { - methodName = className.substring(className.lastIndexOf('.') + 1); - } - - // Parse method descriptor to extract parameter types - String descriptor = member.getDescriptor(); - String paramPattern = parseMethodParameters(descriptor); - - return className + " " + methodName + paramPattern; - } - - private static String parseMethodParameters(String descriptor) { - if (!descriptor.startsWith("(")) { - return "()"; - } - - List paramTypes = new ArrayList<>(); - int i = 1; // Skip opening '(' - while (i < descriptor.length() && descriptor.charAt(i) != ')') { - String type = parseType(descriptor, i); - paramTypes.add(type); - i += getTypeLength(descriptor, i); - } - - if (paramTypes.isEmpty()) { - return "()"; - } - return "(" + String.join(", ", paramTypes) + ")"; - } - - private static String parseType(String descriptor, int start) { - char c = descriptor.charAt(start); - switch (c) { - case 'B': - return "byte"; - case 'C': - return "char"; - case 'D': - return "double"; - case 'F': - return "float"; - case 'I': - return "int"; - case 'J': - return "long"; - case 'S': - return "short"; - case 'Z': - return "boolean"; - case 'V': - return "void"; - case 'L': - // Object type - extract class name - int semicolon = descriptor.indexOf(';', start); - String className = descriptor.substring(start + 1, semicolon); - return className.replace('/', '.'); - case '[': - // Array type - String elementType = parseType(descriptor, start + 1); - return elementType + "[]"; - default: - return "Object"; // Fallback - } - } - - private static int getTypeLength(String descriptor, int start) { - char c = descriptor.charAt(start); - switch (c) { - case 'B': - case 'C': - case 'D': - case 'F': - case 'I': - case 'J': - case 'S': - case 'Z': - case 'V': - return 1; - case 'L': - // Object type - find the semicolon - return descriptor.indexOf(';', start) - start + 1; - case '[': - // Array type - recurse for element type - return 1 + getTypeLength(descriptor, start + 1); - default: - return 1; - } - } - - private static void generateYamlRecipes(List methods, Path outputPath, String recipeName) throws IOException { - StringBuilder yaml = new StringBuilder(); - Path licenseHeader = Paths.get("gradle/licenseHeader.txt"); - if (Files.isRegularFile(licenseHeader)) { - try (Stream lines = Files.lines(licenseHeader)) { - lines.forEach(line -> yaml - .append("# ") - .append(line.replace("${year}", String.valueOf(Year.now().getValue()))) - .append("\n")); - } - } - - yaml.append("#\n"); - yaml.append("# Generated InlineMe recipes from TypeTable\n"); - yaml.append("#\n\n"); - - yaml.append("type: specs.openrewrite.org/v1beta/recipe\n"); - yaml.append(format("name: %s\n", recipeName)); - yaml.append("displayName: Inline methods annotated with `@InlineMe`\n"); - yaml.append("description: >-\n"); - yaml.append(" Automatically generated recipes to inline method calls based on `@InlineMe` annotations\n"); - yaml.append(" discovered in the type table.\n"); - yaml.append("recipeList:\n"); - - // Group methods by GAV for better organization - Map> methodsByGav = - methods.stream().collect(groupingBy(m -> m.gav)); - - for (Map.Entry> entry : methodsByGav.entrySet()) { - TypeTable.GroupArtifactVersion gav = entry.getKey(); - List gavMethods = entry.getValue(); - - yaml.append("\n # From ").append(gav.getGroupId()).append(":").append(gav.getArtifactId()) - .append(":").append(gav.getVersion()).append("\n"); - - for (InlineMeMethod method : gavMethods) { - yaml.append(" - org.openrewrite.java.InlineMethodCalls:\n"); - yaml.append(" methodPattern: '").append(escapeYaml(method.methodPattern)).append("'\n"); - yaml.append(" replacement: '").append(escapeYaml(method.replacement)).append("'\n"); - - if (!method.imports.isEmpty()) { - yaml.append(" imports:\n"); - for (String imp : method.imports) { - yaml.append(" - '").append(escapeYaml(imp)).append("'\n"); - } - } - - if (!method.staticImports.isEmpty()) { - yaml.append(" staticImports:\n"); - for (String imp : method.staticImports) { - yaml.append(" - '").append(escapeYaml(imp)).append("'\n"); - } - } - - yaml.append(" classpathFromResources:\n"); - yaml.append(" - '").append(escapeYaml(method.classpathResource)).append("'\n"); - } - } - - Files.write(outputPath, yaml.toString().getBytes()); - } - - private static String escapeYaml(String value) { - // Escape single quotes by doubling them - return value.replace("'", "''"); - } - - @AllArgsConstructor - private static class InlineMeMethod { - final TypeTable.GroupArtifactVersion gav; - final String methodPattern; - final String replacement; - final List imports; - final List staticImports; - final String classpathResource; - } -} diff --git a/rewrite-java/src/main/java/org/openrewrite/java/trait/Traits.java b/rewrite-java/src/main/java/org/openrewrite/java/trait/Traits.java index 1b80892c11..51ec420c6c 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/trait/Traits.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/trait/Traits.java @@ -16,7 +16,6 @@ package org.openrewrite.java.trait; import org.openrewrite.java.AnnotationMatcher; -import org.openrewrite.java.InlineMe; import org.openrewrite.java.MethodMatcher; /** @@ -32,7 +31,6 @@ private Traits() { * @deprecated Use {@link Literal.Matcher} instead. */ @Deprecated - @InlineMe(replacement = "new Literal.Matcher()", imports = "org.openrewrite.java.Literal") public static Literal.Matcher literal() { return new Literal.Matcher(); } @@ -41,7 +39,6 @@ public static Literal.Matcher literal() { * @deprecated Use {@link VariableAccess.Matcher} instead. */ @Deprecated - @InlineMe(replacement = "new VariableAccess.Matcher()", imports = "org.openrewrite.java.VariableAccess") public static VariableAccess.Matcher variableAccess() { return new VariableAccess.Matcher(); } @@ -50,7 +47,6 @@ public static VariableAccess.Matcher variableAccess() { * @deprecated Use {@link MethodAccess.Matcher} instead. */ @Deprecated - @InlineMe(replacement = "new MethodAccess.Matcher()", imports = "org.openrewrite.java.MethodAccess") public static MethodAccess.Matcher methodAccess(MethodMatcher matcher) { return new MethodAccess.Matcher(matcher); } @@ -59,7 +55,6 @@ public static MethodAccess.Matcher methodAccess(MethodMatcher matcher) { * @deprecated Use {@link MethodAccess.Matcher} instead. */ @Deprecated - @InlineMe(replacement = "new MethodAccess.Matcher(signature)", imports = "org.openrewrite.java.MethodAccess") public static MethodAccess.Matcher methodAccess(String signature) { return new MethodAccess.Matcher(signature); } @@ -68,7 +63,6 @@ public static MethodAccess.Matcher methodAccess(String signature) { * @deprecated Use {@link MethodAccess.Matcher} instead. */ @Deprecated - @InlineMe(replacement = "new MethodAccess.Matcher(declaringType, methodName, args)", imports = "org.openrewrite.java.MethodAccess") public static Annotated.Matcher annotated(AnnotationMatcher matcher) { return new Annotated.Matcher(matcher); } @@ -77,7 +71,6 @@ public static Annotated.Matcher annotated(AnnotationMatcher matcher) { * @deprecated Use {@link Annotated.Matcher} instead. */ @Deprecated - @InlineMe(replacement = "new Annotated.Matcher(signature)", imports = "org.openrewrite.java.Annotated") public static Annotated.Matcher annotated(String signature) { return new Annotated.Matcher(signature); } @@ -86,7 +79,6 @@ public static Annotated.Matcher annotated(String signature) { * @deprecated Use {@link Annotated.Matcher} instead. */ @Deprecated - @InlineMe(replacement = "new Annotated.Matcher(annotationType)", imports = "org.openrewrite.java.Annotated") public static Annotated.Matcher annotated(Class annotationType) { return new Annotated.Matcher(annotationType); } diff --git a/rewrite-java/src/test/java/org/openrewrite/java/internal/parser/InlineMethodCallsRecipeGeneratorTest.java b/rewrite-java/src/test/java/org/openrewrite/java/internal/parser/InlineMethodCallsRecipeGeneratorTest.java deleted file mode 100644 index 90fafb13f3..0000000000 --- a/rewrite-java/src/test/java/org/openrewrite/java/internal/parser/InlineMethodCallsRecipeGeneratorTest.java +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Copyright 2025 the original author or authors. - *

- * 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 - *

- * https://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 org.openrewrite.java.internal.parser; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; - -import java.io.OutputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.zip.GZIPOutputStream; - -import static org.assertj.core.api.Assertions.assertThat; - -class InlineMethodCallsRecipeGeneratorTest { - - @Test - void generateRecipesFromTypeTable(@TempDir Path tempDir) throws Exception { - // Create a test TypeTable TSV file - Path inputTsv = tempDir.resolve("test.tsv.gz"); - String tsvContent = """ - groupId\tartifactId\tversion\tclassAccess\tclassName\tclassSignature\tclassSuperclassSignature\tclassSuperinterfaceSignatures\taccess\tname\tdescriptor\tsignature\tparameterNames\texceptions\telementAnnotations\tparameterAnnotations\ttypeAnnotations\tconstantValue - com.example\ttest-lib\t1.0.0\t1\tcom/example/TestClass\t\tjava/lang/Object\t\t-1\t\t\t\t\t\t\t\t\t - com.example\ttest-lib\t1.0.0\t1\tcom/example/TestClass\t\tjava/lang/Object\t\t1\toldMethod\t(Ljava/lang/String;)V\t\tinput\t\t@Lorg/openrewrite/java/InlineMe;(replacement=s"newMethod(input)",imports=[s"java.util.List"])\t\t\t - """; - try (OutputStream os = Files.newOutputStream(inputTsv); - GZIPOutputStream gzos = new GZIPOutputStream(os)) { - gzos.write(tsvContent.getBytes()); - } - - // Run the generator - Path outputYaml = tempDir.resolve("output.yaml"); - InlineMethodCallsRecipeGenerator.generate(inputTsv, outputYaml, "org.openrewrite.java.InlineMethodCallsGenerated"); - - // Read and verify the content - //language=yaml - assertThat(outputYaml).hasContent(""" - # - # Generated InlineMe recipes from TypeTable - # - - type: specs.openrewrite.org/v1beta/recipe - name: org.openrewrite.java.InlineMethodCallsGenerated - displayName: Inline methods annotated with `@InlineMe` - description: >- - Automatically generated recipes to inline method calls based on `@InlineMe` annotations - discovered in the type table. - recipeList: - - # From com.example:test-lib:1.0.0 - - org.openrewrite.java.InlineMethodCalls: - methodPattern: 'com.example.TestClass oldMethod(java.lang.String)' - replacement: 'newMethod(input)' - imports: - - 'java.util.List' - classpathFromResources: - - 'test-lib-1.0.0' - """); - } - - @Test - void handleMultipleAnnotatedMethods(@TempDir Path tempDir) throws Exception { - // Create a test TypeTable TSV file with multiple annotated methods - Path inputTsv = tempDir.resolve("test.tsv.gz"); - String tsvContent = """ - groupId\tartifactId\tversion\tclassAccess\tclassName\tclassSignature\tclassSuperclassSignature\tclassSuperinterfaceSignatures\taccess\tname\tdescriptor\tsignature\tparameterNames\texceptions\telementAnnotations\tparameterAnnotations\ttypeAnnotations\tconstantValue - com.example\ttest-lib\t1.0.0\t1\tcom/example/TestClass\t\tjava/lang/Object\t\t-1\t\t\t\t\t\t\t\t\t - com.example\ttest-lib\t1.0.0\t1\tcom/example/TestClass\t\tjava/lang/Object\t\t1\tmethod1\t()V\t\t\t\t@Lorg/openrewrite/java/InlineMe;(replacement=s"replacement1()")\t\t\t - com.example\ttest-lib\t1.0.0\t1\tcom/example/TestClass\t\tjava/lang/Object\t\t1\tmethod2\t(I)V\t\tnum\t\t@Lorg/openrewrite/java/InlineMe;(replacement=s"replacement2(num)",staticImports=[s"java.util.Collections.emptyList"])\t\t\t - """; - try (OutputStream os = Files.newOutputStream(inputTsv); - GZIPOutputStream gzos = new GZIPOutputStream(os)) { - gzos.write(tsvContent.getBytes()); - } - - // Run the generator - Path outputYaml = tempDir.resolve("output.yaml"); - InlineMethodCallsRecipeGenerator.generate(inputTsv, outputYaml, "org.openrewrite.java.InlineMethodCallsGenerated"); - - // Read and verify the content - //language=yaml - assertThat(outputYaml).hasContent(""" - # - # Generated InlineMe recipes from TypeTable - # - - type: specs.openrewrite.org/v1beta/recipe - name: org.openrewrite.java.InlineMethodCallsGenerated - displayName: Inline methods annotated with `@InlineMe` - description: >- - Automatically generated recipes to inline method calls based on `@InlineMe` annotations - discovered in the type table. - recipeList: - - # From com.example:test-lib:1.0.0 - - org.openrewrite.java.InlineMethodCalls: - methodPattern: 'com.example.TestClass method1()' - replacement: 'replacement1()' - classpathFromResources: - - 'test-lib-1.0.0' - - org.openrewrite.java.InlineMethodCalls: - methodPattern: 'com.example.TestClass method2(int)' - replacement: 'replacement2(num)' - staticImports: - - 'java.util.Collections.emptyList' - classpathFromResources: - - 'test-lib-1.0.0' - """); - } - - @Test - void skipMethodsWithoutInlineMeAnnotation(@TempDir Path tempDir) throws Exception { - // Create a test TypeTable TSV file with a mix of annotated and non-annotated methods - Path inputTsv = tempDir.resolve("test.tsv.gz"); - String tsvContent = """ - groupId\tartifactId\tversion\tclassAccess\tclassName\tclassSignature\tclassSuperclassSignature\tclassSuperinterfaceSignatures\taccess\tname\tdescriptor\tsignature\tparameterNames\texceptions\telementAnnotations\tparameterAnnotations\ttypeAnnotations\tconstantValue - com.example\ttest-lib\t1.0.0\t1\tcom/example/TestClass\t\tjava/lang/Object\t\t-1\t\t\t\t\t\t\t\t\t - com.example\ttest-lib\t1.0.0\t1\tcom/example/TestClass\t\tjava/lang/Object\t\t1\tregularMethod\t()V\t\t\t\t\t\t\t - com.example\ttest-lib\t1.0.0\t1\tcom/example/TestClass\t\tjava/lang/Object\t\t1\tannotatedMethod\t()V\t\t\t\t@Lorg/openrewrite/java/InlineMe;(replacement=s"replacement()")\t\t\t - """; - try (OutputStream os = Files.newOutputStream(inputTsv); - GZIPOutputStream gzos = new GZIPOutputStream(os)) { - gzos.write(tsvContent.getBytes()); - } - - // Run the generator - Path outputYaml = tempDir.resolve("output.yaml"); - InlineMethodCallsRecipeGenerator.generate(inputTsv, outputYaml, "org.openrewrite.java.InlineMethodCallsGenerated"); - - // Read and verify the content - //language=yaml - assertThat(outputYaml).hasContent(""" - # - # Generated InlineMe recipes from TypeTable - # - - type: specs.openrewrite.org/v1beta/recipe - name: org.openrewrite.java.InlineMethodCallsGenerated - displayName: Inline methods annotated with `@InlineMe` - description: >- - Automatically generated recipes to inline method calls based on `@InlineMe` annotations - discovered in the type table. - recipeList: - - # From com.example:test-lib:1.0.0 - - org.openrewrite.java.InlineMethodCalls: - methodPattern: 'com.example.TestClass annotatedMethod()' - replacement: 'replacement()' - classpathFromResources: - - 'test-lib-1.0.0' - """); - } -} diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/AddManagedDependency.java b/rewrite-maven/src/main/java/org/openrewrite/maven/AddManagedDependency.java index 5189a07ff0..a3e7e62f83 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/AddManagedDependency.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/AddManagedDependency.java @@ -21,7 +21,6 @@ import org.jspecify.annotations.Nullable; import org.openrewrite.*; import org.openrewrite.internal.StringUtils; -import org.openrewrite.java.InlineMe; import org.openrewrite.maven.table.MavenMetadataFailures; import org.openrewrite.maven.trait.MavenDependency; import org.openrewrite.maven.tree.*; @@ -114,7 +113,6 @@ public class AddManagedDependency extends ScanningRecipe