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

AnnotationMatcher will match annotation signature. #468

Closed
wants to merge 2 commits into from
Closed
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
Expand Up @@ -68,7 +68,7 @@
/**
* PSI based parser
*/
@SuppressWarnings({"DataFlowIssue", "ConstantValue"})
@SuppressWarnings("ConstantValue")
public class KotlinTreeParserVisitor extends KtVisitor<J, ExecutionContext> {
private final KotlinSource kotlinSource;
private final PsiElementAssociations psiElementAssociations;
Expand Down Expand Up @@ -268,7 +268,7 @@ public J visitCallableReferenceExpression(KtCallableReferenceExpression expressi
FirResolvedCallableReference reference;
if (!(firElement instanceof FirResolvedCallableReference)) {
throw new UnsupportedOperationException(String.format("Unsupported callable reference: fir: %s, psi : %s with code: %s | sub-psi : %s | sub-fir : %s",
firElement.getClass().getName(),
firElement == null ? "null" : firElement.getClass().getName(),
expression.getClass().getName(),
expression.getText(),
PsiTreePrinter.print(expression),
Expand Down Expand Up @@ -1739,13 +1739,13 @@ public J visitAnnotationEntry(@NotNull KtAnnotationEntry annotationEntry, Execut
}
}

return new J.Annotation(
return mapType(new J.Annotation(
randomId(),
deepPrefix(annotationEntry),
markers,
nameTree,
args
);
));
}

@Override
Expand Down Expand Up @@ -3357,6 +3357,25 @@ private J.If.Else buildIfElsePart(KtIfExpression expression) {
/*====================================================================
* Type related methods
* ====================================================================*/
private <T extends J> T mapType(T tree) {
return mapType(tree, null);
}

@SuppressWarnings("unchecked")
private <T extends J> T mapType(T tree, @Nullable JavaType type) {
T updated = tree;
/* Java trees */
if (updated instanceof J.Annotation) {
J.Annotation a = (J.Annotation) updated;
if (a.getAnnotationType() instanceof J.Identifier && a.getAnnotationType().getType() instanceof JavaType.Method) {
a = a.withAnnotationType(((J.Identifier) a.getAnnotationType()).withType(((JavaType.Method) a.getAnnotationType().getType()).getReturnType()));
}
updated = (T) a;
}
/* Kotlin trees */
return updated;
}

@Nullable
private JavaType type(@Nullable KtElement psi) {
if (psi == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ class PsiElementAssociations(val typeMapping: KotlinTypeMapping, val file: FirFi
}.visitFile(file, elementMap)
}

fun type(psiElement: PsiElement, owner: FirElement?): JavaType? {
fun type(psiElement: PsiElement?, owner: FirElement?): JavaType? {
val fir = primary(psiElement)
return if (fir != null) typeMapping.type(fir, owner) else null
}

fun primary(psiElement: PsiElement) =
fun primary(psiElement: PsiElement?) =
fir(psiElement) { it.source is KtRealPsiSourceElement }

fun methodDeclarationType(psi: PsiElement): JavaType.Method? {
Expand Down
39 changes: 39 additions & 0 deletions src/test/java/org/openrewrite/kotlin/AnnotationMatcherTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.openrewrite.kotlin;

import org.junit.jupiter.api.Test;
import org.openrewrite.java.AnnotationMatcher;
import org.openrewrite.java.tree.J;
import org.openrewrite.test.RewriteTest;

import java.util.concurrent.atomic.AtomicBoolean;

import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.kotlin.Assertions.kotlin;

public class AnnotationMatcherTest implements RewriteTest {

@Test
void matchAnnotation() {
rewriteRun(
kotlin(
"""
@Deprecated("")
class A
""", spec -> spec.afterRecipe(cu -> {
AtomicBoolean found = new AtomicBoolean(false);
AnnotationMatcher matcher = new AnnotationMatcher("@kotlin.Deprecated");
new KotlinIsoVisitor<AtomicBoolean>() {
@Override
public J.Annotation visitAnnotation(J.Annotation annotation, AtomicBoolean atomicBoolean) {
if (matcher.matches(annotation)) {
found.set(true);
}
return super.visitAnnotation(annotation, atomicBoolean);
}
}.visit(cu, found);
assertThat(found.get()).isTrue();
})
)
);
}
}