You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 */publicclassTest {}
Source code for your Spoon processing
importspoon.Launcher;
importspoon.compiler.Environment;
importspoon.processing.Processor;
importspoon.reflect.CtModel;
importspoon.reflect.declaration.CtElement;
importspoon.reflect.declaration.CtType;
importspoon.reflect.visitor.DefaultImportComparator;
importspoon.reflect.visitor.DefaultJavaPrettyPrinter;
importspoon.reflect.visitor.ForceImportProcessor;
importspoon.reflect.visitor.ImportCleaner;
importspoon.reflect.visitor.ImportConflictDetector;
importspoon.reflect.visitor.filter.TypeFilter;
importspoon.support.compiler.VirtualFile;
importjava.util.List;
publicfinalclassMain {
privateMain() {
}
privatestaticCtModelbuildModel() {
Launcherlauncher = newLauncher();
Environmentenvironment = launcher.getEnvironment();
environment.setPrettyPrinterCreator(() -> newDefaultJavaPrettyPrinter(environment) {
{
// copy-pasted from StandardEnvironment#createPrettyPrinterAutoImportList<Processor<CtElement>> preprocessors = List.of(
// try to import as many types as possiblenewForceImportProcessor(),
// remove unused imports first. Do not add new imports at a time when conflicts are not resolvednewImportCleaner().setCanAddImports(false),
// solve conflicts, the current imports are relevant toonewImportConflictDetector(),
// compute final importsnewImportCleaner().setImportComparator(newDefaultImportComparator())
);
this.setIgnoreImplicit(false);
this.setPreprocessors(preprocessors);
this.setMinimizeRoundBrackets(true);
}
});
environment.setComplianceLevel(21);
environment.setShouldCompile(true);
launcher.addInputResource(newVirtualFile(
""" /** * * @author uxxxx */ public class Test {} """,
"Test"
));
returnlauncher.buildModel();
}
publicstaticvoidmain(String[] args) {
CtModelctModel = buildModel();
List<CtType<?>> types = ctModel.getElements(newTypeFilter<>(CtType.class));
if (types.size() != 1) {
thrownewIllegalStateException("No types found");
}
CtType<?> ctType = types.get(0);
if (ctType.getComments().isEmpty()) {
thrownewIllegalStateException("No javadoc found");
}
}
}
Actual output
SLF4J: Failedtoloadclass"org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaultingtono-operation (NOP) loggerimplementationSLF4J: Seehttp://www.slf4j.org/codes.html#StaticLoggerBinder for further details.Exceptioninthread"main"java.lang.IllegalStateException: Nojavadocfoundatorg.example.Main.main(Main.java:75)
Processfinishedwithexitcode1
Expected output
SLF4J: Failedtoloadclass"org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaultingtono-operation (NOP) loggerimplementationSLF4J: Seehttp://www.slf4j.org/codes.html#StaticLoggerBinder for further details.Processfinishedwithexitcode0
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
The text was updated successfully, but these errors were encountered:
The root cause seems to be that the class starts after the comment, if the comment is the first thing in the file. This causes the comment builder to use the compilation unit as the target, attaching the comment to it. If you provide e.g. a package, the class position is extended to encompass the comment (and not just the modifiers), avoiding this.
Spoon could rely on the .javadoc property to properly find the targeted element, at least in the case of javadoc.
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
Source code for your Spoon processing
Actual output
Expected output
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
The text was updated successfully, but these errors were encountered: