SpoonLabs / astor

Automatic program repair for Java with generate-and-validate techniques :v::v:: jGenProg (2014) - jMutRepair (2016) - jKali (2016) - DeepRepair (2017) - Cardumen (2018) - 3sfix (2018)
https://hal.archives-ouvertes.fr/hal-01321615/document
GNU General Public License v2.0
208 stars 106 forks source link

missing comments on patches #231

Open martinezmatias opened 4 years ago

martinezmatias commented 4 years ago

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.

martinezmatias commented 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

martinezmatias commented 4 years ago

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)
martinezmatias commented 4 years ago

Solution: diff context = 0

JakkuSakura commented 3 years ago

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?

martinezmatias commented 3 years ago

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

martinezmatias commented 3 years ago

PRs are welcome @qiujiangkun , you are welcome if you 'd try to implement this feature. We can help you :)

JakkuSakura commented 3 years ago

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 image

martinezmatias commented 3 years ago

That code could be very useful for future research. Do you have a pointer?

JakkuSakura commented 3 years ago

Yes, I have modified my previous comment to include link to that code