org.openrewrite.java.service.AnnotationService Maven / Gradle / Ivy
Show all versions of rewrite-java Show documentation
/*
* Copyright 2023 the original author or authors.
*
* 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
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* 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.service;
import org.openrewrite.Cursor;
import org.openrewrite.Incubating;
import org.openrewrite.java.AnnotationMatcher;
import org.openrewrite.java.tree.J;
import java.util.ArrayList;
import java.util.List;
import static java.util.Collections.emptyList;
@Incubating(since = "8.12.0")
public class AnnotationService {
public boolean matches(Cursor cursor, AnnotationMatcher matcher) {
for (J.Annotation annotation : getAllAnnotations(cursor)) {
if (matcher.matches(annotation)) {
return true;
}
}
return false;
}
@SuppressWarnings("deprecation")
public List getAllAnnotations(Cursor cursor) {
J j = cursor.getValue();
if (j instanceof J.VariableDeclarations) {
return getAllAnnotations((J.VariableDeclarations) j);
} else if (j instanceof J.MethodDeclaration) {
return ((J.MethodDeclaration) j).getAllAnnotations();
} else if (j instanceof J.ClassDeclaration) {
return ((J.ClassDeclaration) j).getAllAnnotations();
} else if (j instanceof J.TypeParameter) {
return ((J.TypeParameter) j).getAnnotations();
} else if (j instanceof J.TypeParameters) {
return ((J.TypeParameters) j).getAnnotations();
} else if (j instanceof J.Package) {
return ((J.Package) j).getAnnotations();
} else if (j instanceof J.AnnotatedType) {
return getAllAnnotations((J.AnnotatedType) j);
} else if (j instanceof J.ArrayType) {
return getAllAnnotations((J.ArrayType) j);
} else if (j instanceof J.FieldAccess) {
return getAllAnnotations((J.FieldAccess) j);
} else if (j instanceof J.Identifier) {
return getAllAnnotations((J.Identifier) j);
}
return emptyList();
}
private List getAllAnnotations(J j) {
if (j instanceof J.AnnotatedType) {
return getAllAnnotations((J.AnnotatedType) j);
} else if (j instanceof J.ArrayType) {
return getAllAnnotations((J.ArrayType) j);
} else if (j instanceof J.Identifier) {
return getAllAnnotations((J.Identifier) j);
} else if (j instanceof J.FieldAccess) {
return getAllAnnotations((J.FieldAccess) j);
} else if (j instanceof J.VariableDeclarations) {
return getAllAnnotations((J.VariableDeclarations) j);
}
return emptyList();
}
private List getAllAnnotations(J.VariableDeclarations variableDeclarations) {
List allAnnotations = new ArrayList<>(variableDeclarations.getLeadingAnnotations());
for (J.Modifier modifier : variableDeclarations.getModifiers()) {
allAnnotations.addAll(modifier.getAnnotations());
}
if (variableDeclarations.getTypeExpression() instanceof J.AnnotatedType) {
allAnnotations.addAll(getAllAnnotations(((J.AnnotatedType) variableDeclarations.getTypeExpression())));
}
return allAnnotations;
}
private List getAllAnnotations(J.AnnotatedType annotatedType) {
List targetAnnotations = getAllAnnotations(annotatedType.getTypeExpression());
if (targetAnnotations.isEmpty()) {
return annotatedType.getAnnotations();
}
List annotations = new ArrayList<>(annotatedType.getAnnotations().size() + targetAnnotations.size());
annotations.addAll(annotatedType.getAnnotations());
annotations.addAll(targetAnnotations);
return annotations;
}
private List getAllAnnotations(J.ArrayType arrayType) {
if (arrayType.getAnnotations() != null) {
return arrayType.getAnnotations();
}
return emptyList();
}
private List getAllAnnotations(J.FieldAccess fieldAccess) {
return getAllAnnotations(fieldAccess.getName());
}
private List getAllAnnotations(J.Identifier identifier) {
return identifier.getAnnotations();
}
}