-
Notifications
You must be signed in to change notification settings - Fork 89
Added recipe to remove @VisibleForTesting
annotation when used from production code
#689
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
Draft
JohannisK
wants to merge
22
commits into
main
Choose a base branch
from
224-remove-visiblefortesting-if-variable-is-accessed-from-production-code
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
8638eb4
Added recipe to remove @VisibleForTesting annotation when used from p…
JohannisK 6610f25
Added license
JohannisK 03926fa
Renamed executionContext to ctx
JohannisK 5ea0619
Improvements based on review
JohannisK 76e54c2
Removed JetBrains annotations
JohannisK 47b9f7d
Removed unused import
JohannisK 918489b
Review, TestCases generics and method references, Fix method references
JohannisK 7eac321
rename to ctx
JohannisK 68ac454
Added generic test
JohannisK 83a021e
Added tests proving scope of support for generics, added scope to des…
JohannisK 9efb086
Merge branch 'main' into 224-remove-visiblefortesting-if-variable-is-…
timtebeek ab231a0
Review
JohannisK 0005511
remove newline
JohannisK 8e076d8
removed import
JohannisK f63c13b
Remove `forEach` and `stream()` to avoid object allocations
timtebeek bd2d2bf
Add UsesType precondition on `getVisitor`
timtebeek 6973da1
Verify that we remove the import when last annotation removed
timtebeek 7832d93
Added maybeRemoveImport
JohannisK d0413dd
Removed wrong test
JohannisK f53a28b
Run `RemoveVisibleForTestingAnnotationWhenUsedInProduction` as part o…
timtebeek c11c00a
Merge branch 'main' into 224-remove-visiblefortesting-if-variable-is-…
timtebeek d11ef63
Added private filter
JohannisK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
191 changes: 191 additions & 0 deletions
191
...enrewrite/java/testing/cleanup/RemoveVisibleForTestingAnnotationWhenUsedInProduction.java
This file contains hidden or 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,191 @@ | ||
/* | ||
* Copyright 2025 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.cleanup; | ||
|
||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Preconditions; | ||
import org.openrewrite.ScanningRecipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.JavaIsoVisitor; | ||
import org.openrewrite.java.RemoveAnnotation; | ||
import org.openrewrite.java.search.IsLikelyNotTest; | ||
import org.openrewrite.java.search.UsesType; | ||
import org.openrewrite.java.tree.Flag; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.java.tree.JavaType; | ||
import org.openrewrite.java.tree.TypeUtils; | ||
|
||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public class RemoveVisibleForTestingAnnotationWhenUsedInProduction extends ScanningRecipe<RemoveVisibleForTestingAnnotationWhenUsedInProduction.VisibleForTesting> { | ||
|
||
public static class VisibleForTesting { | ||
Set<String> methodPatterns = new HashSet<>(); | ||
Set<String> fieldPatterns = new HashSet<>(); | ||
Set<String> classPatterns = new HashSet<>(); | ||
Set<String> fullyQualifiedVisibleForTestingAnnotationPatterns = new HashSet<>(); | ||
} | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Remove `@VisibleForTesting` annotation when target is used in production"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "The `@VisibleForTesting` annotation marks a method or field that is intentionally made more accessible (e.g., changing its visibility from private or package-private to public or protected) solely for testing purposes. " + | ||
"The annotation serves as an indicator that the increased visibility is not part of the intended public API but exists only to support testability. " + | ||
"This recipe removes the annotation where such an element is used from production classes. It identifies production classes as classes in `src/main` and test classes as classes in `src/test`. " + | ||
"It will remove the `@VisibleForTesting` from methods, fields (both member fields and constants), constructors and inner classes. " + | ||
"It does not support generic methods (e.g. `<T> T method(T);`. " + | ||
"This recipe should not be used in an environment where QA tooling acts on the `@VisibleForTesting` annotation."; | ||
} | ||
|
||
@Override | ||
public VisibleForTesting getInitialValue(ExecutionContext ctx) { | ||
return new VisibleForTesting(); | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getScanner(VisibleForTesting acc) { | ||
JavaIsoVisitor<ExecutionContext> scanningVisitor = new JavaIsoVisitor<ExecutionContext>() { | ||
|
||
@Override | ||
public J.FieldAccess visitFieldAccess(J.FieldAccess fa, ExecutionContext ctx) { | ||
if (fa.getTarget().getType() instanceof JavaType.Class && !((JavaType.Class) fa.getTarget().getType()).hasFlags(Flag.Private)) { | ||
checkAndRegister(acc.classPatterns, fa.getTarget().getType()); | ||
} | ||
if (fa.getName().getFieldType() != null && !fa.getName().getFieldType().hasFlags(Flag.Private)) { | ||
checkAndRegister(acc.fieldPatterns, fa.getName().getFieldType()); | ||
} | ||
return super.visitFieldAccess(fa, ctx); | ||
} | ||
|
||
@Override | ||
public J.MemberReference visitMemberReference(J.MemberReference mr, ExecutionContext ctx) { | ||
if (mr.getMethodType() != null && !mr.getMethodType().hasFlags(Flag.Private)) { | ||
checkAndRegister(acc.methodPatterns, mr.getMethodType()); | ||
} | ||
return super.visitMemberReference(mr, ctx); | ||
} | ||
|
||
@Override | ||
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation mi, ExecutionContext ctx) { | ||
if (mi.getMethodType() != null && !mi.getMethodType().hasFlags(Flag.Private)) { | ||
checkAndRegister(acc.methodPatterns, mi.getMethodType()); | ||
} | ||
return super.visitMethodInvocation(mi, ctx); | ||
} | ||
|
||
@Override | ||
public J.NewClass visitNewClass(J.NewClass nc, ExecutionContext ctx) { | ||
if (nc.getConstructorType() != null && !nc.getConstructorType().hasFlags(Flag.Private)) { | ||
checkAndRegister(acc.methodPatterns, nc.getConstructorType()); | ||
} | ||
if (nc.getClazz() != null && nc.getClazz().getType() instanceof JavaType.Class && !((JavaType.Class) nc.getClazz().getType()).hasFlags(Flag.Private)) { | ||
checkAndRegister(acc.classPatterns, nc.getClazz().getType()); | ||
} | ||
return super.visitNewClass(nc, ctx); | ||
} | ||
|
||
private void checkAndRegister(Set<String> target, JavaType type) { | ||
String typeString = TypeUtils.toString(type); | ||
if (!target.contains(typeString)) { | ||
for (JavaType.FullyQualified annotation : getAnnotations(type)) { | ||
if ("VisibleForTesting".equals(annotation.getClassName())) { | ||
target.add(typeString); | ||
acc.fullyQualifiedVisibleForTestingAnnotationPatterns.add(annotation.getFullyQualifiedName()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private List<JavaType.FullyQualified> getAnnotations(JavaType type) { | ||
if (type instanceof JavaType.Class) { | ||
return ((JavaType.Class) type).getAnnotations(); | ||
} else if (type instanceof JavaType.Variable) { | ||
return ((JavaType.Variable) type).getAnnotations(); | ||
} else if (type instanceof JavaType.Method) { | ||
return ((JavaType.Method) type).getAnnotations(); | ||
} | ||
return Collections.emptyList(); | ||
} | ||
}; | ||
return Preconditions.check(new IsLikelyNotTest().getVisitor(), scanningVisitor); | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor(VisibleForTesting acc) { | ||
JavaIsoVisitor<ExecutionContext> visitor = new JavaIsoVisitor<ExecutionContext>() { | ||
@Override | ||
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations variableDeclarations, ExecutionContext ctx) { | ||
J.VariableDeclarations vd = super.visitVariableDeclarations(variableDeclarations, ctx); | ||
if (!vd.getVariables().isEmpty()) { | ||
// if none of the variables in the declaration are used from production code, the annotation should be kept | ||
for (J.VariableDeclarations.NamedVariable elem : vd.getVariables()) { | ||
if (elem.getVariableType() != null) { | ||
if (acc.fieldPatterns.contains(TypeUtils.toString(elem.getVariableType()))) { | ||
J.VariableDeclarations newVd = (J.VariableDeclarations) new RemoveAnnotation("@*..VisibleForTesting").getVisitor() | ||
.visitNonNull(vd, ctx, getCursor().getParentOrThrow()); | ||
if (vd != newVd) { | ||
acc.fullyQualifiedVisibleForTestingAnnotationPatterns.forEach(this::maybeRemoveImport); | ||
} | ||
return newVd; | ||
} | ||
} | ||
} | ||
} | ||
return vd; | ||
} | ||
|
||
@Override | ||
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDeclaration, ExecutionContext ctx) { | ||
J.MethodDeclaration md = super.visitMethodDeclaration(methodDeclaration, ctx); | ||
if (md.getMethodType() != null && acc.methodPatterns.contains(TypeUtils.toString(md.getMethodType()))) { | ||
J.MethodDeclaration newMd = (J.MethodDeclaration) new RemoveAnnotation("@*..VisibleForTesting").getVisitor() | ||
.visitNonNull(md, ctx, getCursor().getParentOrThrow()); | ||
if (md != newMd) { | ||
acc.fullyQualifiedVisibleForTestingAnnotationPatterns.forEach(this::maybeRemoveImport); | ||
} | ||
return newMd; | ||
} | ||
return md; | ||
} | ||
|
||
@Override | ||
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDeclaration, ExecutionContext ctx) { | ||
J.ClassDeclaration cd = super.visitClassDeclaration(classDeclaration, ctx); | ||
if (cd.getType() != null && acc.classPatterns.contains(TypeUtils.toString(cd.getType()))) { | ||
J.ClassDeclaration newCd = (J.ClassDeclaration) new RemoveAnnotation("@*..VisibleForTesting").getVisitor() | ||
.visitNonNull(cd, ctx, getCursor().getParentOrThrow()); | ||
if (cd != newCd) { | ||
acc.fullyQualifiedVisibleForTestingAnnotationPatterns.forEach(this::maybeRemoveImport); | ||
} | ||
return newCd; | ||
} | ||
return cd; | ||
} | ||
}; | ||
return Preconditions.check( | ||
Preconditions.and( | ||
new IsLikelyNotTest().getVisitor(), | ||
new UsesType<>("*..VisibleForTesting", true)), | ||
visitor); | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.