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

Completes LogRecord.getLongThreadID recipe #295

Merged
merged 4 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright 2023 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.Option;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.TypeUtils;
import org.openrewrite.marker.Markers;

import static java.util.Collections.emptyList;

@Value
@EqualsAndHashCode(callSuper = true)
public class ChangeMethodInvocationReturnType extends Recipe {

@Option(displayName = "Method pattern",
description = "A method pattern that is used to find matching method declarations/invocations.",
example = "org.mockito.Matchers anyVararg()")
String methodPattern;

@Option(displayName = "New method invocation return type",
description = "The return return type of method invocation.",
example = "long")
String newReturnType;

@Override
public String getDisplayName() {
return "Change method invocation return type";
}

@Override
public String getDescription() {
return "Changes the return type of a method invocation.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {
private final MethodMatcher methodMatcher = new MethodMatcher(methodPattern, false);

private boolean methodUpdated;

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);
JavaType.Method type = m.getMethodType();
if (methodMatcher.matches(method) && type != null && !newReturnType.equals(type.getReturnType().toString())) {
type = type.withReturnType(JavaType.buildType(newReturnType));
m = m.withMethodType(type);
methodUpdated = true;
}
return m;
}

@Override
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
methodUpdated = false;
JavaType.FullyQualified originalType = multiVariable.getTypeAsFullyQualified();
J.VariableDeclarations mv = super.visitVariableDeclarations(multiVariable, ctx);

if (methodUpdated) {
JavaType newType = JavaType.buildType(newReturnType);
JavaType.FullyQualified newFieldType = TypeUtils.asFullyQualified(newType);

maybeAddImport(newFieldType);
maybeRemoveImport(originalType);

mv = mv.withTypeExpression(mv.getTypeExpression() == null ?
null :
new J.Identifier(mv.getTypeExpression().getId(),
mv.getTypeExpression().getPrefix(),
Markers.EMPTY,
emptyList(),
newReturnType,
newType,
null
)
);

mv = mv.withVariables(ListUtils.map(mv.getVariables(), var -> {
JavaType.FullyQualified varType = TypeUtils.asFullyQualified(var.getType());
if (varType != null && !varType.equals(newType)) {
return var.withType(newType).withName(var.getName().withType(newType));
}
return var;
}));
}

return mv;
}
};
}
}
10 changes: 6 additions & 4 deletions src/main/resources/META-INF/rewrite/java-version-17.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ description: Avoid using the deprecated methods in `java.util.logging.LogRecord`
tags:
- java17
recipeList:
# Disabled for now, as the new methods returns a long instead of an int, which causes compilation errors
#- org.openrewrite.java.ChangeMethodName:
# methodPattern: java.util.logging.LogRecord getThreadID()
# newMethodName: getLongThreadID
- org.openrewrite.java.ChangeMethodName:
methodPattern: java.util.logging.LogRecord getThreadID()
newMethodName: getLongThreadID
- org.openrewrite.java.migrate.ChangeMethodInvocationReturnType:
methodPattern: java.util.logging.LogRecord getLongThreadID()
newReturnType: long
- org.openrewrite.java.ChangeMethodName:
methodPattern: java.util.logging.LogRecord setThreadID(int)
newMethodName: setLongThreadID
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2023 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 ChangeMethodInvocationReturnTypeTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new ChangeMethodInvocationReturnType("java.lang.Integer parseInt(String)", "long"));
}

@Test
@DocumentExample
void replaceVariableAssignment() {
rewriteRun(
//language=java
java(
"""
class Foo {
void bar() {
int one = Integer.parseInt("1");
}
}
""",
"""
class Foo {
void bar() {
long one = Integer.parseInt("1");
}
}
"""
)
);
}

@Test
void shouldOnlyChangeTargetMethodAssignments() {
rewriteRun(
//language=java
java(
"""
class Foo {
void bar() {
int zero = Integer.valueOf("0");
int one = Integer.parseInt("1");
int two = Integer.valueOf("2");
}
}
""",
"""
class Foo {
void bar() {
int zero = Integer.valueOf("0");
long one = Integer.parseInt("1");
int two = Integer.valueOf("2");
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ void replaceLogRecordSetThreadID() {

class Foo {
void bar(LogRecord record) {
int threadID = record.getThreadID();
record.setThreadID(1);
}
}
Expand All @@ -280,6 +281,7 @@ void bar(LogRecord record) {

class Foo {
void bar(LogRecord record) {
long threadID = record.getLongThreadID();
record.setLongThreadID(1);
}
}
Expand Down
Loading