Skip to content
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,84 @@
/*
* Copyright 2026 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license
* <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.testing.mockito;

import lombok.Getter;
import org.jspecify.annotations.Nullable;
import org.openrewrite.Cursor;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;

import java.util.List;

import static org.openrewrite.java.VariableNameUtils.GenerationStrategy.INCREMENT_NUMBER;
import static org.openrewrite.java.VariableNameUtils.generateVariableName;

public class PowerMockWhiteboxGetInternalStateToJavaReflection extends Recipe {

private static final MethodMatcher GET_INTERNAL_STATE =
new MethodMatcher("org.powermock.reflect.Whitebox getInternalState(java.lang.Object, java.lang.String)");

@Getter
final String displayName = "Replace PowerMock `Whitebox.getInternalState()` with Java reflection";

@Getter
final String description = "Replace `Whitebox.getInternalState(Object, String)` with `java.lang.reflect.Field` " +
"access, casting to the declared result type where needed. The field lookup uses `getDeclaredField` on " +
"the target object's class, which differs from PowerMock's class-hierarchy traversal for fields " +
"inherited from a superclass.";

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new GetInternalStateVisitor().withPrecondition();
}

private static class GetInternalStateVisitor extends WhiteboxToReflectionVisitor {

GetInternalStateVisitor() {
super("java.lang.reflect.Field", GET_INTERNAL_STATE);
}

@Override
@Nullable String buildTemplate(J.MethodInvocation mi, ResultSink sink, Cursor scope,
JavaType.@Nullable Method resolvedMethod) {
String fieldName = extractStringLiteral(mi.getArguments().get(1));
if (fieldName == null) {
return null;
}
String varName = generateVariableName(fieldName + "Field", scope, INCREMENT_NUMBER);
String prefix = fieldLookupPrefix(varName);
if (sink.varName != null) {
if (isNonObjectCast(sink.castType)) {
return prefix + sink.castType + " " + sink.varName + " = (" + sink.castType + ") " + varName + ".get(#{any(java.lang.Object)});";
}
return prefix + "Object " + sink.varName + " = " + varName + ".get(#{any(java.lang.Object)});";
}
return prefix + varName + ".get(#{any(java.lang.Object)});";
Comment on lines +70 to +74

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why does this one use concatenation but the next file use StringBuilder?

}

@Override
Object[] buildArgs(J.MethodInvocation mi, JavaType.@Nullable Method resolvedMethod) {
List<Expression> args = mi.getArguments();
// target, fieldName, target
return new Object[]{args.get(0), args.get(1), args.get(0)};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
/*
* Copyright 2026 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license
* <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.testing.mockito;

import lombok.Getter;
import org.jspecify.annotations.Nullable;
import org.openrewrite.Cursor;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;

import java.util.ArrayList;
import java.util.List;

import static org.openrewrite.java.VariableNameUtils.GenerationStrategy.INCREMENT_NUMBER;
import static org.openrewrite.java.VariableNameUtils.generateVariableName;

public class PowerMockWhiteboxInvokeMethodToJavaReflection extends Recipe {

private static final MethodMatcher INVOKE_METHOD =
new MethodMatcher("org.powermock.reflect.Whitebox invokeMethod(java.lang.Object, java.lang.String, ..)");

@Getter
final String displayName = "Replace PowerMock `Whitebox.invokeMethod()` with Java reflection";

@Getter
final String description = "Replace `Whitebox.invokeMethod(Object, String, ..)` with `java.lang.reflect.Method` " +
"lookup and `invoke()`. Parameter types are taken from the unambiguously resolved target method, " +
"falling back to each argument's compile-time class.";

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new InvokeMethodVisitor().withPrecondition();
}

private static class InvokeMethodVisitor extends WhiteboxToReflectionVisitor {

InvokeMethodVisitor() {
super("java.lang.reflect.Method", INVOKE_METHOD);
}

@Override
JavaType.@Nullable Method resolve(J.MethodInvocation mi) {
return resolveTargetMethod(mi.getArguments());
}

@Override
@Nullable String buildTemplate(J.MethodInvocation mi, ResultSink sink, Cursor scope,
JavaType.@Nullable Method resolvedMethod) {
List<Expression> args = mi.getArguments();
String methodName = extractStringLiteral(args.get(1));
if (methodName == null) {
return null;
}
String varName = generateVariableName(methodName + "Method", scope, INCREMENT_NUMBER);

// getDeclaredMethod line
StringBuilder sb = new StringBuilder();
sb.append("Method ").append(varName).append(" = #{any(java.lang.Object)}.getClass().getDeclaredMethod(#{any(java.lang.String)}");
for (int i = 2; i < args.size(); i++) {
String classLiteral = getParamClassLiteral(args, i, resolvedMethod);
if (classLiteral != null) {
sb.append(", ").append(classLiteral);
} else {
sb.append(", #{any(java.lang.Object)}.getClass()");
}
}
sb.append(");\n");

// setAccessible line
sb.append(varName).append(".setAccessible(true);\n");

// invoke line
if (sink.varName != null) {
if (isNonObjectCast(sink.castType)) {
sb.append(sink.castType).append(" ").append(sink.varName).append(" = (").append(sink.castType).append(") ");
} else {
sb.append("Object ").append(sink.varName).append(" = ");
}
}
sb.append(varName).append(".invoke(#{any(java.lang.Object)}");
for (int i = 2; i < args.size(); i++) {
sb.append(", #{any(java.lang.Object)}");
}
sb.append(");");

return sb.toString();
}

@Override
Object[] buildArgs(J.MethodInvocation mi, JavaType.@Nullable Method resolvedMethod) {
List<Expression> args = mi.getArguments();
List<Object> result = new ArrayList<>();
result.add(args.get(0)); // target for getDeclaredMethod
result.add(args.get(1)); // methodName
for (int i = 2; i < args.size(); i++) {
if (getParamClassLiteral(args, i, resolvedMethod) == null) {
result.add(args.get(i)); // arg.getClass() fallback for getDeclaredMethod
}
}
result.add(args.get(0)); // target for invoke
for (int i = 2; i < args.size(); i++) {
result.add(args.get(i)); // arg for invoke
}
return result.toArray();
}

/**
* Get the class literal for a parameter at the given argument index. Prefers the resolved
* method's declared parameter type, falls back to the argument's compile-time type, and
* returns null if neither is available.
*/
private @Nullable String getParamClassLiteral(List<Expression> args, int argIndex,
JavaType.@Nullable Method resolvedMethod) {
if (resolvedMethod != null) {
String literal = classLiteralFromType(resolvedMethod.getParameterTypes().get(argIndex - 2));
if (literal != null) {
return literal;
}
}
return classLiteralFromType(args.get(argIndex).getType());
}

/**
* Resolve the target method from the first argument's type and the method name, walking the
* supertype chain. Returns null if the method cannot be unambiguously resolved (not found,
* overloaded, or missing type information).
*/
private JavaType.@Nullable Method resolveTargetMethod(List<Expression> args) {
if (args.size() <= 2) {
return null;
}
String methodName = extractStringLiteral(args.get(1));
if (methodName == null) {
return null;
}
JavaType targetType = args.get(0).getType();
if (!(targetType instanceof JavaType.FullyQualified)) {
return null;
}
int expectedParamCount = args.size() - 2;
JavaType.Method match = null;
for (JavaType.FullyQualified current = (JavaType.FullyQualified) targetType;
current != null; current = current.getSupertype()) {
for (JavaType.Method method : current.getMethods()) {
if (method.getName().equals(methodName) &&
method.getParameterTypes().size() == expectedParamCount) {
if (match != null) {
return null; // ambiguous overload
}
match = method;
}
}
}
return match;
}

private @Nullable String classLiteralFromType(@Nullable JavaType type) {
if (type instanceof JavaType.Primitive) {
return ((JavaType.Primitive) type).getKeyword() + ".class";
}
if (type instanceof JavaType.FullyQualified) {
return ((JavaType.FullyQualified) type).getClassName() + ".class";
}
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2026 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license
* <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.testing.mockito;

import lombok.Getter;
import org.jspecify.annotations.Nullable;
import org.openrewrite.Cursor;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;

import java.util.List;

import static org.openrewrite.java.VariableNameUtils.GenerationStrategy.INCREMENT_NUMBER;
import static org.openrewrite.java.VariableNameUtils.generateVariableName;

public class PowerMockWhiteboxSetInternalStateToJavaReflection extends Recipe {

private static final MethodMatcher SET_INTERNAL_STATE =
new MethodMatcher("org.powermock.reflect.Whitebox setInternalState(java.lang.Object, java.lang.String, java.lang.Object)");
private static final MethodMatcher SET_INTERNAL_STATE_WHERE =
new MethodMatcher("org.powermock.reflect.Whitebox setInternalState(java.lang.Object, java.lang.String, java.lang.Object, java.lang.Class)");

@Getter
final String displayName = "Replace PowerMock `Whitebox.setInternalState()` with Java reflection";

@Getter
final String description = "Replace `Whitebox.setInternalState(Object, String, Object)` and " +
"`Whitebox.setInternalState(Object, String, Object, Class)` with `java.lang.reflect.Field` access. " +
"The 3-arg overload looks up the field on the target's class; the 4-arg where-overload uses the " +
"supplied Class to resolve fields declared on a superclass.";

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new SetInternalStateVisitor().withPrecondition();
}

private static class SetInternalStateVisitor extends WhiteboxToReflectionVisitor {

SetInternalStateVisitor() {
super("java.lang.reflect.Field", SET_INTERNAL_STATE, SET_INTERNAL_STATE_WHERE);
}

@Override
@Nullable String buildTemplate(J.MethodInvocation mi, ResultSink sink, Cursor scope,
JavaType.@Nullable Method resolvedMethod) {
String fieldName = extractStringLiteral(mi.getArguments().get(1));
if (fieldName == null) {
return null;
}
String varName = generateVariableName(fieldName + "Field", scope, INCREMENT_NUMBER);
String prefix = mi.getArguments().size() == 4
? fieldLookupPrefixWhere(varName)
: fieldLookupPrefix(varName);
return prefix +
varName + ".set(#{any(java.lang.Object)}, #{any(java.lang.Object)});";
}

@Override
Object[] buildArgs(J.MethodInvocation mi, JavaType.@Nullable Method resolvedMethod) {
List<Expression> args = mi.getArguments();
if (args.size() == 4) {
// whereClass, fieldName, target, value
return new Object[]{args.get(3), args.get(1), args.get(0), args.get(2)};
}
// target, fieldName, target, value
return new Object[]{args.get(0), args.get(1), args.get(0), args.get(2)};
}
}
}
Loading
Loading