INRIA / spoon

Spoon is a metaprogramming library to analyze and transform Java source code. :spoon: is made with :heart:, :beers: and :sparkles:. It parses source files to build a well-designed AST with powerful analysis and transformation API.
http://spoon.gforge.inria.fr/
Other
1.74k stars 346 forks source link

[Bug]: Javadoc of class missing #5757

Open Luro02 opened 5 months ago

Luro02 commented 5 months ago

Describe the bug

The javadoc of the class vanishes when it is the first thing in the file. Adding a single space in front of the /** solves the issue.

Source code you are trying to analyze/transform

/**
 * This {@link RunCommand} executes a run on the target.
 * if (a) {
 * print(a);
 * }
 */
public class RunCommand {
    // some comment
}

Source code for your Spoon processing

import spoon.Launcher;
import spoon.reflect.CtModel;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.compiler.VirtualFile;

public final class Main {
    private static CtModel buildModel() {
        Launcher launcher = new Launcher();

        launcher.addInputResource(new VirtualFile(
            """
                /**
                 * This {@link RunCommand} executes a run on the target.
                 * if (a) {
                 * print(a);
                 * }
                 */
                public class RunCommand {
                    // some comment
                }
                """,
            "RunCommand"
        ));

        return launcher.buildModel();
    }

    public static void main(String[] args) {
        CtModel ctModel = buildModel();

        System.out.println(ctModel.getElements(new TypeFilter<>(CtClass.class)));
    }
}

Actual output

[// some comment
public class RunCommand {}]

Process finished with exit code 0

Expected output

[/**
 * This {@link RunCommand} executes a run on the target.
 * if (a) {
 * print(a);
 * }
 */
// some comment
public class RunCommand {}]

Process finished with exit code 0

Spoon Version

11.0.0

JVM Version

openjdk version "21.0.2" 2024-01-16 LTS OpenJDK Runtime Environment Zulu21.32+17-CA (build 21.0.2+13-LTS) OpenJDK 64-Bit Server VM Zulu21.32+17-CA (build 21.0.2+13-LTS, mixed mode, sharing)

What operating system are you using?

Windows

tenax66 commented 4 months ago

This seems to be related to the specification of org.eclipse.jdt.internal.compiler.parser.Scanner.

If there is a whitespace at the beginning of the target file, it is consumed before proceeding to Javadoc comment, and Javadoc comment is interpreted as the start of the class definition, so to speak. However, if there are no spaces at the beginning, Javadoc comment is processed alonely.