Skip to content
This repository was archived by the owner on May 12, 2025. It is now read-only.

Commit a2ea744

Browse files
Fixed some annotation parsing. (#554)
1 parent 3804358 commit a2ea744

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ public J visitParameter(KtParameter parameter, ExecutionContext data) {
912912
}
913913

914914
if (parameter.getValOrVarKeyword() != null) {
915-
modifiers.add(mapModifier(parameter.getValOrVarKeyword(), Collections.emptyList(), consumedSpaces));
915+
modifiers.add(mapModifier(parameter.getValOrVarKeyword(), lastAnnotations, consumedSpaces));
916916
}
917917
}
918918

@@ -1027,9 +1027,9 @@ public J visitPrimaryConstructor(KtPrimaryConstructor constructor, ExecutionCont
10271027
public J visitPropertyAccessor(KtPropertyAccessor accessor, ExecutionContext data) {
10281028
Markers markers = Markers.EMPTY;
10291029
List<J.Annotation> leadingAnnotations = new ArrayList<>();
1030-
List<J.Modifier> modifiers = mapModifiers(accessor.getModifierList(), leadingAnnotations, emptyList(), data);
1031-
TypeTree returnTypeExpression = null;
10321030
List<J.Annotation> lastAnnotations = new ArrayList<>();
1031+
List<J.Modifier> modifiers = mapModifiers(accessor.getModifierList(), leadingAnnotations, lastAnnotations, data);
1032+
TypeTree returnTypeExpression = null;
10331033
JContainer<Statement> params;
10341034
J.Block body = null;
10351035
Set<PsiElement> consumedSpaces = preConsumedInfix(accessor);
@@ -1164,7 +1164,7 @@ public J visitSecondaryConstructor(KtSecondaryConstructor constructor, Execution
11641164
List<J.Annotation> leadingAnnotations = new ArrayList<>();
11651165
List<J.Annotation> lastAnnotations = new ArrayList<>();
11661166
List<J.Modifier> modifiers = mapModifiers(constructor.getModifierList(), leadingAnnotations, lastAnnotations, data);
1167-
modifiers.add(mapModifier(constructor.getConstructorKeyword(), emptyList(), preConsumedInfix(constructor)));
1167+
modifiers.add(mapModifier(constructor.getConstructorKeyword(), lastAnnotations, preConsumedInfix(constructor)));
11681168

11691169
JavaType.Method type = methodDeclarationType(constructor);
11701170
J.Identifier name = createIdentifier(requireNonNull(constructor.getName()), prefix(constructor.getConstructorKeyword()), type)
@@ -2609,7 +2609,7 @@ private J visitNamedFunction0(KtNamedFunction function, ExecutionContext data) {
26092609
modifiers.add(buildFinalModifier().withPrefix(Space.EMPTY));
26102610
}
26112611

2612-
modifiers.add(new J.Modifier(randomId(), prefix(function.getFunKeyword(), prefixConsumedSet), Markers.EMPTY, "fun", J.Modifier.Type.LanguageExtension, emptyList()));
2612+
modifiers.add(new J.Modifier(randomId(), prefix(function.getFunKeyword(), prefixConsumedSet), Markers.EMPTY, "fun", J.Modifier.Type.LanguageExtension, lastAnnotations));
26132613
J.Identifier name;
26142614

26152615
JavaType.Method type = methodDeclarationType(function);

src/test/java/org/openrewrite/kotlin/tree/AnnotationTest.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,4 +690,61 @@ annotation class Ann2
690690
)
691691
);
692692
}
693+
694+
@Issue("https://github.com/openrewrite/rewrite-kotlin/issues/552")
695+
@Test
696+
void trailingAnnotationOnSecondaryConstructor() {
697+
rewriteRun(
698+
kotlin(
699+
"""
700+
annotation class A1
701+
class Test ( val answer : Int ) {
702+
private @A1 constructor ( ) : this ( 42 )
703+
}
704+
"""
705+
)
706+
);
707+
}
708+
709+
@Issue("https://github.com/openrewrite/rewrite-kotlin/issues/552")
710+
@Test
711+
void trailingAnnotationOnPropertyAccessor() {
712+
rewriteRun(
713+
kotlin(
714+
"""
715+
annotation class Ann
716+
var s : String = ""
717+
internal @Ann set ( value ) {
718+
field = value
719+
}
720+
"""
721+
)
722+
);
723+
}
724+
725+
@Issue("https://github.com/openrewrite/rewrite-kotlin/issues/552")
726+
@Test
727+
void trailingAnnotationOnTypeReference() {
728+
rewriteRun(
729+
kotlin(
730+
"""
731+
annotation class Anno
732+
class Example ( public @Anno val quux : String )
733+
"""
734+
)
735+
);
736+
}
737+
738+
@Issue("https://github.com/openrewrite/rewrite-kotlin/issues/552")
739+
@Test
740+
void trailingAnnotationNamedFunction() {
741+
rewriteRun(
742+
kotlin(
743+
"""
744+
annotation class Anno
745+
internal @Anno fun method() {}
746+
"""
747+
)
748+
);
749+
}
693750
}

0 commit comments

Comments
 (0)