Skip to content

Commit

Permalink
Renamed FileSuffix marker to AnnotationCallSite.
Browse files Browse the repository at this point in the history
Added support for AnnotationCallSite property getter.
fixes #80
  • Loading branch information
traceyyoshima committed Apr 7, 2023
1 parent 909dc18 commit c1cc224
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static java.lang.Math.max;
import static java.util.Collections.*;
Expand Down Expand Up @@ -184,7 +183,12 @@ public J visitAnnotationCall(FirAnnotationCall annotationCall, ExecutionContext
skip("@");
if (annotationCall.getUseSiteTarget() == AnnotationUseSiteTarget.FILE) {
skip("file");
markers = markers.addIfAbsent(new FileSuffix(randomId(), sourceBefore(":")));
markers = markers.addIfAbsent(new AnnotationCallSite(randomId(), "file", sourceBefore(":")));
}

if (annotationCall.getUseSiteTarget() == AnnotationUseSiteTarget.PROPERTY_GETTER) {
skip("get");
markers = markers.addIfAbsent(new AnnotationCallSite(randomId(), "get", sourceBefore(":")));
}

J.Identifier name = (J.Identifier) visitElement(annotationCall.getCalleeReference(), ctx);
Expand Down Expand Up @@ -1507,7 +1511,15 @@ public J visitProperty(FirProperty property, ExecutionContext ctx) {
Markers markers = Markers.EMPTY;

List<J> modifiers = emptyList();
List<J.Annotation> annotations = mapModifiers(property.getAnnotations(), property.getName().asString());
List<FirAnnotation> mapAnnotations = new ArrayList<>(property.getAnnotations());
// Note: it might be possible to have annotations on both the property associated to the getter
// AND the getter itself. In that case, we to use a single list to avoid duplicates and add
// the target annotations to the appropriate LST element.
if (property.getGetter() != null && !property.getGetter().getAnnotations().isEmpty()) {
mapAnnotations.addAll(property.getGetter().getAnnotations());
}

List<J.Annotation> annotations = mapModifiers(mapAnnotations, property.getName().asString());

JRightPadded<J.VariableDeclarations.NamedVariable> receiver = null;
if (property.getReceiverTypeRef() != null) {
Expand Down Expand Up @@ -1699,7 +1711,7 @@ public J visitPropertyAccessor(FirPropertyAccessor propertyAccessor, ExecutionCo
if (propertyAccessor.isGetter()) {
Markers markers = Markers.EMPTY;
List<J> modifiers = emptyList();
List<J.Annotation> annotations = mapAnnotations(propertyAccessor.getAnnotations());
List<J.Annotation> annotations = emptyList();

JRightPadded<J.VariableDeclarations.NamedVariable> infixReceiver = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,10 @@ public J visitAnnotation(J.Annotation annotation, PrintOutputCapture<P> p) {
p.append("@");
}

FileSuffix suffix = annotation.getMarkers().findFirst(FileSuffix.class).orElse(null);
if (suffix != null) {
p.append("file");
KotlinPrinter.this.visitSpace(suffix.getSuffix(), KSpace.Location.FILE_ANNOTATION_SUFFIX, p);
AnnotationCallSite callSite = annotation.getMarkers().findFirst(AnnotationCallSite.class).orElse(null);
if (callSite != null) {
p.append(callSite.getName());
KotlinPrinter.this.visitSpace(callSite.getSuffix(), KSpace.Location.FILE_ANNOTATION_SUFFIX, p);
p.append(":");
}
visit(annotation.getAnnotationType(), p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
@Incubating(since = "0.0")
@Value
@With
public class FileSuffix implements Marker {
public class AnnotationCallSite implements Marker {
UUID id;
String name;
Space suffix;
}
16 changes: 16 additions & 0 deletions src/test/java/org/openrewrite/kotlin/tree/AnnotationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.kotlin.tree;

import org.junit.jupiter.api.Test;
import org.openrewrite.Issue;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.kotlin.tree.ParserAssertions.kotlin;
Expand Down Expand Up @@ -66,4 +67,19 @@ annotation class Test ( val values : Array < String > ) {
)
);
}

@Issue("https://github.com/openrewrite/rewrite-kotlin/issues/80")
@Test
void jvmNameAnnotation() {
rewriteRun(
kotlin(
"""
import kotlin.jvm.JvmName
@get : JvmName ( "getCount" )
val count : Int ?
get ( ) = 1
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,10 @@ void genericIntersectionType() {
rewriteRun(
kotlin(
"""
val first: String = "1"
val second: Int = 2
val first : String = "1"
val second : Int = 2
val l = listOf("foo" to first, "bar" to second)
val l = listOf ( "foo" to first , "bar" to second )
"""
)
);
Expand Down

0 comments on commit c1cc224

Please sign in to comment.