Skip to content

Commit

Permalink
Merge branch 'main' into recipe_removedSecurityManagerMethods
Browse files Browse the repository at this point in the history
  • Loading branch information
ranuradh authored Apr 21, 2024
2 parents bf92b45 + 7b936c0 commit c116b27
Show file tree
Hide file tree
Showing 6 changed files with 445 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.TypeUtils;

import static org.openrewrite.java.tree.J.ClassDeclaration.Kind.Type.Interface;

@Value
@EqualsAndHashCode(callSuper = false)
public class AddMissingMethodImplementation extends Recipe {

@Option(displayName = "Fully Qualified Class Name",
description = "A fully qualified class being implemented with missing method.",
example = "com.yourorg.FooBar")
Expand All @@ -49,7 +52,7 @@ public class AddMissingMethodImplementation extends Recipe {

@Override
public String getDisplayName() {
return "Adds missing method implementations.";
return "Adds missing method implementations";
}

@Override
Expand All @@ -63,6 +66,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
}

public class ClassImplementationVisitor extends JavaIsoVisitor<ExecutionContext> {

private final JavaTemplate methodTemplate = JavaTemplate.builder(methodTemplateString).build();
private final MethodMatcher methodMatcher = new MethodMatcher(methodPattern, true);

Expand All @@ -71,8 +75,8 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration cs, Execution
// need to make sure we handle sub-classes
J.ClassDeclaration classDecl = super.visitClassDeclaration(cs, ctx);

// No need to make changes to abstract classes; only change concrete classes.
if (classDecl.hasModifier(J.Modifier.Type.Abstract)) {
// No need to make changes to abstract classes or interfaces; only change concrete classes.
if (classDecl.hasModifier(J.Modifier.Type.Abstract) || classDecl.getKind() == Interface) {
return classDecl;
}
// Don't make changes to classes that don't match the fully qualified name
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/META-INF/rewrite/java-version-11.yml
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,19 @@ recipeList:
newGroupId: jakarta.xml.ws
newArtifactId: jakarta.xml.ws-api
newVersion: 2.3.x
# Add the jakarta JAXB artifact if it is missing but a project uses types in java.xml.bind
# Add the jakarta JAXB artifact if it is missing but a project uses types in either javax.jws or javax.xml.ws
- org.openrewrite.java.dependencies.AddDependency:
groupId: jakarta.xml.ws
artifactId: jakarta.xml.ws-api
version: 2.3.x
onlyIfUsing: javax.jws..*
acceptTransitive: true
- org.openrewrite.java.dependencies.AddDependency:
groupId: jakarta.xml.ws
artifactId: jakarta.xml.ws-api
version: 2.3.x
onlyIfUsing: javax.xml.ws..*
acceptTransitive: true
# If a project already had the jakarta api, make sure it is at the latest version.
- org.openrewrite.java.dependencies.UpgradeDependencyVersion:
groupId: jakarta.xml.ws
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/rewrite/java-version-7.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ recipeList:
methodTemplateString: "public java.lang.String getSchema() { \n\t// TODO Auto-generated method stub\n return null; }"
- org.openrewrite.java.migrate.AddMissingMethodImplementation:
fullyQualifiedClassName: java.sql.Connection
methodPattern: "*..* setNetworkTimeout(java.util.concurrent.Executor)"
methodPattern: "*..* setNetworkTimeout(java.util.concurrent.Executor, int)"
methodTemplateString: "public void setNetworkTimeout(java.util.concurrent.Executor executor, int milliseconds) { \n\t// TODO Auto-generated method stub\n }"
- org.openrewrite.java.migrate.AddMissingMethodImplementation:
fullyQualifiedClassName: java.sql.Connection
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* 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.java.migrate;

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

import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.java.Assertions.javaVersion;

class AddMissingMethodImplementationTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(
new AddMissingMethodImplementation("I1", "*..* m1()",
"public void m1() { System.out.println(\"m1\"); }"))
.allSources(src -> src.markers(javaVersion(21)));
}

@Test
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/459")
void skipInterfaces() {
//language=java
rewriteRun(
java(
"""
interface I1 {}
interface I2 extends I1 {}
"""
)
);
}

@Test
void skipAbstractClasses() {
//language=java
rewriteRun(
java(
"""
interface I1 {}
abstract class AC2 implements I1 {}
"""
)
);
}

@DocumentExample
@Test
void happyPath() {
//language=java
rewriteRun(
java(
"""
interface I1 {}
class C2 implements I1 {}
""",
"""
interface I1 {}
class C2 implements I1 {
public void m1() {
System.out.println("m1");
}}
"""
)
);
}

@Test
void methodExists() {
//language=java
rewriteRun(
java(
"""
interface I1 {}
class C2 implements I1 {
public void m1() {
System.out.println("m1");
}
}
"""
)
);
}

@Test
void methodExistsDiffImpl() {
//language=java
rewriteRun(
java(
"""
interface I1 {}
class C2 implements I1 {
public void m1() {
System.out.println("m1 diff");
}
}
"""
)
);
}

@Test
void methodExistsDiffSignature() {
//language=java
rewriteRun(
java(
"""
interface I1 {}
class C2 implements I1 {
protected void m1() {
System.out.println("m1");
}
}
"""
)
);
}

}
Loading

0 comments on commit c116b27

Please sign in to comment.