[Bug]: Javadoc not attached to class in some cases
- Dominant language
- Java
- Stars
- 2k
- Forks
- 392
- Avg merge
- 11h 24m
- Merged PRs (30d)
- 36
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
```Java
/**
*
* @author uxxxx
*/
public class Test {}
```
### Source code for your Spoon processing
```Java
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> 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> 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
```Java
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
```Java
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
Contributor guide
Research direction
Run the supplied Main reproducer with the Java source shown in the issue and inspect Spoon's parsing and comment-attachment path for top-level classes. Confirm the behavior for the provided source, including the package and indentation variations mentioned. Done means ctType.getComments() contains the Javadoc for the unindented, package-less class, with regression coverage for the reproduced case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100