Skip to content

Commit

Permalink
Use AnnotationService rather than getAllAnnotations() methods
Browse files Browse the repository at this point in the history
Replaces the use of deprecated `getAllAnnotations()` methods with `AnnotationService`.
  • Loading branch information
knutwannheden committed Dec 28, 2023
1 parent 4348159 commit a5dc2f7
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.service.AnnotationService;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;

Expand Down Expand Up @@ -60,6 +61,7 @@ public ChangeCovariantEqualsMethodVisitor(J.ClassDeclaration enclosingClass) {
@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, P p) {
J.MethodDeclaration m = super.visitMethodDeclaration(method, p);
updateCursor(m);

/*
* Looking for "public boolean equals(EnclosingClassType)" as the method signature match.
Expand All @@ -76,7 +78,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, P
JavaType.Primitive.Boolean.equals(m.getReturnTypeExpression().getType()) &&
new MethodMatcher(ecfqn + " equals(" + ecfqn + ")").matches(m, enclosingClass)) {

if (m.getAllAnnotations().stream().noneMatch(OVERRIDE_ANNOTATION::matches)) {
if (!service(AnnotationService.class).matches(getCursor(), OVERRIDE_ANNOTATION)) {
m = JavaTemplate.builder("@Override").build()
.apply(updateCursor(m),
m.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.AnnotationMatcher;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.service.AnnotationService;
import org.openrewrite.java.style.ExplicitInitializationStyle;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.Space;
import org.openrewrite.java.tree.TypeUtils;

import java.util.Iterator;

@Value
@EqualsAndHashCode(callSuper = true)
public class ExplicitInitializationVisitor<P> extends JavaIsoVisitor<P> {
Expand All @@ -53,15 +56,15 @@ public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations
return v;
}
}
J.ClassDeclaration clz = getCursor().firstEnclosing(J.ClassDeclaration.class);
if (clz != null && clz.getAllAnnotations().stream().anyMatch(LOMBOK_VALUE::matches)) {
Iterator<Cursor> clz = getCursor().getPathAsCursors(c -> c.getValue() instanceof J.ClassDeclaration);
if (clz.hasNext() && service(AnnotationService.class).matches(clz.next(), LOMBOK_VALUE)) {
return v;
}
JavaType.Primitive primitive = TypeUtils.asPrimitive(variable.getType());
JavaType.Array array = TypeUtils.asArray(variable.getType());

J.VariableDeclarations variableDecls = variableDeclsCursor.getValue();
if (variableDecls.getAllAnnotations().stream().anyMatch(LOMBOK_BUILDER_DEFAULT::matches)) {
if (service(AnnotationService.class).matches(variableDeclsCursor, LOMBOK_BUILDER_DEFAULT)) {
return v;
}
J.Literal literalInit = variable.getInitializer() instanceof J.Literal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/
package org.openrewrite.staticanalysis;

import org.openrewrite.Cursor;
import org.openrewrite.Incubating;
import org.openrewrite.java.*;
import org.openrewrite.java.service.AnnotationService;
import org.openrewrite.java.style.HideUtilityClassConstructorStyle;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
Expand Down Expand Up @@ -66,7 +68,7 @@ public HideUtilityClassConstructorVisitor(HideUtilityClassConstructorStyle style
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, P p) {
J.ClassDeclaration c = super.visitClassDeclaration(classDecl, p);
if (!EXCLUDE_CLASS_TYPES.contains(c.getKind()) && !c.hasModifier(J.Modifier.Type.Abstract) && utilityClassMatcher.isRefactorableUtilityClass(c)) {
if (!EXCLUDE_CLASS_TYPES.contains(c.getKind()) && !c.hasModifier(J.Modifier.Type.Abstract) && utilityClassMatcher.isRefactorableUtilityClass(getCursor())) {
/*
* Note, it's a deliberate choice to have these be their own respective visitors rather than putting
* all the logic in one visitor. It's conceptually easier to distinguish what each are doing.
Expand All @@ -79,7 +81,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, P
* But, first and foremost, the main rationale is because it's hopefully conceptually easier to distinguish the steps
* required for HideUtilityClassConstructorVisitor to work.
*/
c = (J.ClassDeclaration) new UtilityClassWithImplicitDefaultConstructorVisitor<>().visit(c, p, getCursor().getParentOrThrow());
c = (J.ClassDeclaration) new UtilityClassWithImplicitDefaultConstructorVisitor().visit(c, p, getCursor().getParentOrThrow());
c = (J.ClassDeclaration) new UtilityClassWithExposedConstructorInspectionVisitor<>(c).visit(c, p, getCursor().getParentOrThrow());
}
return c;
Expand All @@ -88,11 +90,11 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, P
/**
* Adds an empty private constructor if the class has zero explicit constructors. This hides the default implicit constructor.
*/
private static class UtilityClassWithImplicitDefaultConstructorVisitor<P> extends JavaIsoVisitor<P> {
private class UtilityClassWithImplicitDefaultConstructorVisitor extends JavaIsoVisitor<P> {

@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, P p) {
if (UtilityClassMatcher.hasImplicitDefaultConstructor(classDecl) &&
if (utilityClassMatcher.hasImplicitDefaultConstructor(classDecl) &&
!J.ClassDeclaration.Kind.Type.Enum.equals(classDecl.getKind())) {
classDecl = JavaTemplate.builder("private #{}() {}")
.contextSensitive()
Expand Down Expand Up @@ -155,7 +157,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, P
* there are other cleanup rules which could benefit from these, such as "make utility class final", etc.
* Until then, however, we'll keep this private and unexposed.
*/
private static final class UtilityClassMatcher {
private final class UtilityClassMatcher {
private final Collection<AnnotationMatcher> ignorableAnnotations;

private UtilityClassMatcher(Collection<String> ignorableAnnotations) {
Expand All @@ -165,18 +167,17 @@ private UtilityClassMatcher(Collection<String> ignorableAnnotations) {
}
}

boolean hasIgnorableAnnotation(J.ClassDeclaration c) {
for (J.Annotation classAnn : c.getAllAnnotations()) {
for (AnnotationMatcher ignorableAnn : ignorableAnnotations) {
if (ignorableAnn.matches(classAnn)) {
return true;
}
boolean hasIgnorableAnnotation(Cursor cursor) {
AnnotationService service = service(AnnotationService.class);
for (AnnotationMatcher ignorableAnn : ignorableAnnotations) {
if (service.matches(cursor, ignorableAnn)) {
return true;
}
}
return false;
}

static boolean hasMainMethod(J.ClassDeclaration c) {
boolean hasMainMethod(J.ClassDeclaration c) {
if (c.getType() == null) {
return false;
}
Expand Down Expand Up @@ -207,7 +208,7 @@ static boolean hasMainMethod(J.ClassDeclaration c) {
*
* @return true if there are zero explicit constructors, meaning the Class has an implicit default constructor.
*/
static boolean hasImplicitDefaultConstructor(J.ClassDeclaration c) {
boolean hasImplicitDefaultConstructor(J.ClassDeclaration c) {
for (Statement statement : c.getBody().getStatements()) {
if (statement instanceof J.MethodDeclaration) {
J.MethodDeclaration methodDeclaration = (J.MethodDeclaration) statement;
Expand All @@ -219,13 +220,14 @@ static boolean hasImplicitDefaultConstructor(J.ClassDeclaration c) {
return true;
}

boolean isRefactorableUtilityClass(J.ClassDeclaration c) {
return UtilityClassMatcher.isUtilityClass(c) &&
!hasIgnorableAnnotation(c) &&
!UtilityClassMatcher.hasMainMethod(c);
boolean isRefactorableUtilityClass(Cursor cursor) {
J.ClassDeclaration c = cursor.getValue();
return isUtilityClass(c) &&
!hasIgnorableAnnotation(cursor) &&
!hasMainMethod(c);
}

static boolean isUtilityClass(J.ClassDeclaration c) {
boolean isUtilityClass(J.ClassDeclaration c) {
if (c.getImplements() != null || c.getExtends() != null) {
return false;
}
Expand All @@ -246,7 +248,7 @@ static boolean isUtilityClass(J.ClassDeclaration c) {
/**
* @return -1 if a non-static field is found, else the count of non-private static fields.
*/
private static int countStaticFields(J.ClassDeclaration classDeclaration) {
private int countStaticFields(J.ClassDeclaration classDeclaration) {
int count = 0;

for (Statement statement : classDeclaration.getBody().getStatements()) {
Expand All @@ -269,7 +271,7 @@ private static int countStaticFields(J.ClassDeclaration classDeclaration) {
/**
* @return -1 if a non-static method is found, else the count of non-private static methods.
*/
private static int countStaticMethods(J.ClassDeclaration classDeclaration) {
private int countStaticMethods(J.ClassDeclaration classDeclaration) {
int count = 0;
for (Statement statement : classDeclaration.getBody().getStatements()) {
if (!(statement instanceof J.MethodDeclaration)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.openrewrite.java.AnnotationMatcher;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.service.AnnotationService;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.TypeUtils;

Expand Down Expand Up @@ -77,7 +78,7 @@ private Cursor getCursorToParentScope(Cursor cursor) {
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
if (!method.hasModifier(J.Modifier.Type.Static)
&& !method.isConstructor()
&& method.getAllAnnotations().stream().noneMatch(OVERRIDE_ANNOTATION::matches)
&& !service(AnnotationService.class).matches(getCursor(), OVERRIDE_ANNOTATION)
&& TypeUtils.isOverride(method.getMethodType())
&& !(Boolean.TRUE.equals(ignoreAnonymousClassMethods)
&& getCursorToParentScope(getCursor()).getValue() instanceof J.NewClass)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.service.AnnotationService;
import org.openrewrite.java.tree.*;

import java.time.Duration;
Expand Down Expand Up @@ -152,7 +153,7 @@ public Statement visitStatement(Statement statement, ExecutionContext ctx) {

@Override
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
if (!multiVariable.getAllAnnotations().isEmpty()) {
if (!service(AnnotationService.class).getAllAnnotations(getCursor()).isEmpty()) {
return multiVariable;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.NoMissingTypes;
import org.openrewrite.java.service.AnnotationService;
import org.openrewrite.java.tree.*;

import java.time.Duration;
Expand Down Expand Up @@ -58,7 +59,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
JavaType.Method methodType = method.getMethodType();
if (methodType != null && methodType.hasFlags(Flag.Private) &&
!method.isConstructor() &&
method.getAllAnnotations().isEmpty()) {
service(AnnotationService.class).getAllAnnotations(getCursor()).isEmpty()) {

J.ClassDeclaration classDeclaration = getCursor().firstEnclosing(J.ClassDeclaration.class);
if (classDeclaration == null) {
Expand Down

0 comments on commit a5dc2f7

Please sign in to comment.