Open martinezmatias opened 4 years ago
Here, we disable before creating the model: https://github.com/SpoonLabs/astor/blob/b11cd3d74525cc2ed00c32119e90533fd5599920/src/main/java/fr/inria/astor/core/manipulation/MutationSupporter.java#L69
Actually, if we enable the inclusion of comments
actory.getEnvironment().setCommentEnabled(true);
we have:
ava.lang.UnsupportedOperationException: PartialSourcePosition only contains a CompilationUnit
at spoon.reflect.cu.position.NoSourcePosition.getSourceEnd(NoSourcePosition.java:58)
at spoon.reflect.visitor.ElementPrinterHelper.getComments(ElementPrinterHelper.java:417)
at spoon.reflect.visitor.ElementPrinterHelper.writeComment(ElementPrinterHelper.java:408)
at spoon.reflect.visitor.ElementPrinterHelper.writeHeader(ElementPrinterHelper.java:351)
at spoon.reflect.visitor.DefaultJavaPrettyPrinter.writeHeader(DefaultJavaPrettyPrinter.java:1957)
at spoon.reflect.visitor.DefaultJavaPrettyPrinter.calculate(DefaultJavaPrettyPrinter.java:1983)
at spoon.support.JavaOutputProcessor.createJavaFile(JavaOutputProcessor.java:114)
at spoon.support.JavaOutputProcessor.process(JavaOutputProcessor.java:150)
at fr.inria.astor.core.manipulation.bytecode.OutputWritter.saveSourceCode(OutputWritter.java:57)
at fr.inria.astor.core.manipulation.MutationSupporter.generateSourceCodeFromCtClass(MutationSupporter.java:141)
at fr.inria.astor.core.manipulation.MutationSupporter.saveSourceCodeOnDiskProgramVariant(MutationSupporter.java:121)
at fr.inria.astor.util.PatchDiffCalculator.getDiff(PatchDiffCalculator.java:57)
at fr.inria.astor.core.solutionsearch.AstorCoreEngine.computePatchDiff(AstorCoreEngine.java:229)
at fr.inria.astor.core.solutionsearch.AstorCoreEngine.atEnd(AstorCoreEngine.java:186)
Solution: diff context = 0
In the generated result file, I don't have comments either. Has this been fixed? Could you explain a bit how to keep the comments?
Hi @qiujiangkun
To include comments, we should include them in the generated model. if I am not wrong, by default, those are not included. https://github.com/SpoonLabs/astor/blob/b11cd3d74525cc2ed00c32119e90533fd5599920/src/main/java/fr/inria/astor/core/manipulation/MutationSupporter.java#L69
I'd say that would be simple to modify astor to include them: add an option that indicates whether comments should be included or not, then use the value (e.g., read it using the ConfigurationProperties) when Astor creates the model here
PRs are welcome @qiujiangkun , you are welcome if you 'd try to implement this feature. We can help you :)
Thanks for the solution. I've implemented a very primitive algorithm based on Intellij IDEA's great PSI model, to match patch against original code structure as much as possible. https://github.com/bc2021-program-repair/intellij-idea-astor-plugin/blob/main/src/main/kotlin/org/bytecamp/program_repair/astor_plugin/code/CodeImporter.kt
That code could be very useful for future research. Do you have a pointer?
Yes, I have modified my previous comment to include link to that code
The modified version of the patched file does not include comments, even it keeps the original line number of elements. (it leaves empty lines). Reported by @tdurieux.