Skip to content

[Bug]: Javadoc not attached to class in some cases #6106

Open
@Luro02

Description

Describe the bug

In some cases, the javadoc is not attached to the test class, like in the example source code (ctClass.getComments() would then be empty).

Note: if there is a package declaration or the javadoc is indented, it will attach the javadoc to the test class.

Source code you are trying to analyze/transform

/**
 *
 * @author uxxxx
 */
public class Test {}

Source code for your Spoon processing

import spoon.Launcher;
import spoon.compiler.Environment;
import spoon.processing.Processor;
import spoon.reflect.CtModel;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtType;
import spoon.reflect.visitor.DefaultImportComparator;
import spoon.reflect.visitor.DefaultJavaPrettyPrinter;
import spoon.reflect.visitor.ForceImportProcessor;
import spoon.reflect.visitor.ImportCleaner;
import spoon.reflect.visitor.ImportConflictDetector;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.compiler.VirtualFile;

import java.util.List;

public final class Main {
    private Main() {
    }

    private static CtModel buildModel() {
        Launcher launcher = new Launcher();

        Environment environment = launcher.getEnvironment();
        environment.setPrettyPrinterCreator(() -> new DefaultJavaPrettyPrinter(environment) {
            {
                // copy-pasted from StandardEnvironment#createPrettyPrinterAutoImport
                List<Processor<CtElement>> preprocessors = List.of(
                    // try to import as many types as possible
                    new ForceImportProcessor(),
                    // remove unused imports first. Do not add new imports at a time when conflicts are not resolved
                    new ImportCleaner().setCanAddImports(false),
                    // solve conflicts, the current imports are relevant too
                    new ImportConflictDetector(),
                    // compute final imports
                    new ImportCleaner().setImportComparator(new DefaultImportComparator())
                );
                this.setIgnoreImplicit(false);
                this.setPreprocessors(preprocessors);
                this.setMinimizeRoundBrackets(true);
            }
        });

        environment.setComplianceLevel(21);
        environment.setShouldCompile(true);

        launcher.addInputResource(new VirtualFile(
            """
            /**
             *
             * @author uxxxx
             */
            public class Test {}
            """,
            "Test"
        ));

        return launcher.buildModel();
    }


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

        List<CtType<?>> types = ctModel.getElements(new TypeFilter<>(CtType.class));

        if (types.size() != 1) {
            throw new IllegalStateException("No types found");
        }

        CtType<?> ctType = types.get(0);
        if (ctType.getComments().isEmpty()) {
            throw new IllegalStateException("No javadoc found");
        }
    }
}

Actual output

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.IllegalStateException: No javadoc found
	at org.example.Main.main(Main.java:75)

Process finished with exit code 1

Expected output

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

Process finished with exit code 0

Spoon Version

11.1.1-beta-18

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 11

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions