-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace
Reference.clone()
with new constructor in Java 11 migration (…
…#510) * Java 11 recipe remove ReferenceCloneMethod * udpated test * Update src/test/java/org/openrewrite/java/migrate/ReferenceCloneMethodTest.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update src/test/java/org/openrewrite/java/migrate/ReferenceCloneMethodTest.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Polish whitespace in text blocks * Clean up unnecessary type casts * Shorten all fully qualified types on replacement constructor --------- Co-authored-by: anuram <ranuradh@us.ibm.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Tim te Beek <tim@moderne.io>
- Loading branch information
1 parent
ceecc08
commit 9c99820
Showing
3 changed files
with
184 additions
and
2 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
src/main/java/org/openrewrite/java/migrate/ReferenceCloneMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* 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 lombok.EqualsAndHashCode; | ||
import lombok.Value; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Preconditions; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.JavaTemplate; | ||
import org.openrewrite.java.JavaVisitor; | ||
import org.openrewrite.java.MethodMatcher; | ||
import org.openrewrite.java.ShortenFullyQualifiedTypeReferences; | ||
import org.openrewrite.java.search.UsesMethod; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.java.tree.TypeUtils; | ||
|
||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = false) | ||
class ReferenceCloneMethod extends Recipe { | ||
private static final MethodMatcher REFERENCE_CLONE = new MethodMatcher("java.lang.ref.Reference clone()", true); | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Replace `java.lang.ref.Reference.clone()` with constructor call"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "The recipe replaces any clone calls that may resolve to a `java.lang.ref.Reference.clone()` " + | ||
"or any of its known subclasses: `java.lang.ref.PhantomReference`, `java.lang.ref.SoftReference`, and `java.lang.ref.WeakReference` " + | ||
"with a constructor call passing in the referent and reference queue as parameters."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return Preconditions.check( | ||
new UsesMethod<>(REFERENCE_CLONE), | ||
new JavaVisitor<ExecutionContext>() { | ||
private static final String REFERENCE_CLONE_REPLACED = "REFERENCE_CLONE_REPLACED"; | ||
|
||
@Override | ||
public J visitTypeCast(J.TypeCast typeCast, ExecutionContext ctx) { | ||
J j = super.visitTypeCast(typeCast, ctx); | ||
if (Boolean.TRUE.equals(getCursor().pollNearestMessage(REFERENCE_CLONE_REPLACED)) | ||
&& j instanceof J.TypeCast) { | ||
J.TypeCast tc = (J.TypeCast) j; | ||
if (TypeUtils.isOfType(tc.getType(), tc.getExpression().getType())) { | ||
return tc.getExpression(); | ||
} | ||
} | ||
return j; | ||
} | ||
|
||
@Override | ||
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { | ||
super.visitMethodInvocation(method, ctx); | ||
if (REFERENCE_CLONE.matches(method) && method.getSelect() instanceof J.Identifier) { | ||
J.Identifier methodRef = (J.Identifier) method.getSelect(); | ||
String template = "new " + methodRef.getType().toString() + "(" + methodRef.getSimpleName() + ", new ReferenceQueue<>())"; | ||
getCursor().putMessageOnFirstEnclosing(J.TypeCast.class, REFERENCE_CLONE_REPLACED, true); | ||
J replacement = JavaTemplate.builder(template) | ||
.contextSensitive() | ||
.imports("java.lang.ref.ReferenceQueue") | ||
.build().apply(getCursor(), method.getCoordinates().replace()); | ||
doAfterVisit(ShortenFullyQualifiedTypeReferences.modifyOnly(replacement)); | ||
return replacement; | ||
} | ||
return method; | ||
} | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/test/java/org/openrewrite/java/migrate/ReferenceCloneMethodTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* 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.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
|
||
class ReferenceCloneMethodTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new ReferenceCloneMethod()); | ||
} | ||
|
||
@DocumentExample | ||
@Test | ||
void referenceCloneRemoval() { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
import java.lang.ref.WeakReference; | ||
import java.lang.ref.SoftReference; | ||
import java.lang.ref.PhantomReference; | ||
class Foo { | ||
void foo() throws Exception{ | ||
WeakReference<Object> ref = new WeakReference<Object>(null); | ||
WeakReference<Object> ref1 = (WeakReference<Object>) ref.clone(); | ||
SoftReference<Object> ref3 = new SoftReference<Object>(null); | ||
SoftReference<Object> ref4 = (SoftReference<Object>) ref3.clone(); | ||
PhantomReference<Object> ref5 = new PhantomReference<Object>(null,null); | ||
PhantomReference<Object> ref6 = (PhantomReference<Object>) ref5.clone(); | ||
} | ||
} | ||
""", | ||
""" | ||
import java.lang.ref.WeakReference; | ||
import java.lang.ref.SoftReference; | ||
import java.lang.ref.PhantomReference; | ||
class Foo { | ||
void foo() throws Exception{ | ||
WeakReference<Object> ref = new WeakReference<Object>(null); | ||
WeakReference<Object> ref1 = new WeakReference<Object>(ref, new ReferenceQueue<>()); | ||
SoftReference<Object> ref3 = new SoftReference<Object>(null); | ||
SoftReference<Object> ref4 = new SoftReference<Object>(ref3, new ReferenceQueue<>()); | ||
PhantomReference<Object> ref5 = new PhantomReference<Object>(null,null); | ||
PhantomReference<Object> ref6 = new PhantomReference<Object>(ref5, new ReferenceQueue<>()); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void noCloneRemoval() { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
class ClonableClass implements Cloneable { | ||
public ClonableClass(int id) { | ||
} | ||
@Override | ||
public Object clone() throws CloneNotSupportedException { | ||
return super.clone(); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |