Skip to content

[kotlin] AnnotationFqnAnnotator: @TypeName not set on UnescapedAnnotation nodes #6891

Closed

Description

@stokpop

Bug

AnnotationFqnAnnotator.collectAnnotationNodes() (introduced in #6795) always returns an empty list, so @TypeName is never set on KtUnescapedAnnotation / KtSingleAnnotation nodes for any declaration type.

Root cause

The refactored traversal uses KtModifier (singular) as the first step:

return declNode
        .children(KtModifier.class)      // ← wrong
        .children(KtAnnotation.class)
        .children()
        .children(KtUnescapedAnnotation.class)
        .toList();

The Kotlin grammar rule modifiers → (annotation | modifier)* produces two distinct node types:

Node Grammar rule Children
KtModifiers modifiers (the container) KtAnnotation, KtModifier, …
KtModifier modifier (single item: private, override, etc.) visibility/inheritance/… keywords

A declaration like @Deprecated fun foo() has this AST structure:

FunctionDeclaration
  └── Modifiers        ← KtModifiers  (container)
       └── Annotation  ← KtAnnotation
            └── SingleAnnotation  ← KtSingleAnnotation
                 └── UnescapedAnnotation  ← KtUnescapedAnnotation

KtModifier (singular) is never a parent of KtAnnotation, so the chain finds nothing.

Affected declarations

All four places that call AnnotationFqnAnnotator.setAnnotationFqns:

  • visitFunctionDeclaration
  • visitClassDeclaration
  • visitPropertyDeclaration
  • visitClassParameter

Reproducer test

Add to KotlinTypeAnnotationVisitorTest:

@Test
void unescapedAnnotationOnFunctionHasTypeName() {
    KtKotlinFile root = PARSER.parse("@Deprecated(\"use X\") fun foo(): String = \"\"");
    KtUnescapedAnnotation ann = root.descendants(KtUnescapedAnnotation.class).first();
    assertNotNull(ann);
    assertEquals("kotlin.Deprecated", KotlinNodeTypeData.getTypeName(ann));
}

Fix

import net.sourceforge.pmd.lang.kotlin.ast.KotlinParser.KtModifiers;

private static List<KtUnescapedAnnotation> collectAnnotationNodes(KotlinNode declNode) {
    return declNode
            .children(KtModifiers.class)   // container, not individual modifier
            .children(KtAnnotation.class)
            .children()
            .children(KtUnescapedAnnotation.class)
            .toList();
}

Activity

  1. added 3 commits that reference this issue on Jul 20, 2026
    9f23a7a
    48f95c9
    61bb626
  2. changed the title [-][kotlin] AnnotationFqnAnnotator: @TypeName not set on UnescapedAnnotation nodes (KtModifier vs KtModifiers)[/-] [+][kotlin] AnnotationFqnAnnotator: @TypeName not set on UnescapedAnnotation nodes[/+] on Jul 21, 2026
  3. added this to the 7.27.0 milestone on Jul 21, 2026
  4. added a commit that references this issue on Jul 21, 2026
    d04ce54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions