From 58f0ff6b5e8a9e191d8f34ad601131655d074229 Mon Sep 17 00:00:00 2001 From: slarse Date: Sun, 24 May 2020 22:22:13 +0200 Subject: [PATCH] [fix] Check for existence of CU befor getting comment, fix #134 --- src/main/java/se/kth/spork/spoon/Parser.java | 14 ++++++++++---- .../compilation_unit_comment/Base.java | 12 ++++++++++++ .../compilation_unit_comment/Expected.java | 16 ++++++++++++++++ .../compilation_unit_comment/Left.java | 14 ++++++++++++++ .../compilation_unit_comment/Right.java | 14 ++++++++++++++ .../clean/both_modified/two_way_merge/Base.java | 0 .../both_modified/two_way_merge/Expected.java | 7 +++++++ .../clean/both_modified/two_way_merge/Left.java | 7 +++++++ .../clean/both_modified/two_way_merge/Right.java | 7 +++++++ 9 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 src/test/resources/clean/both_modified/compilation_unit_comment/Base.java create mode 100644 src/test/resources/clean/both_modified/compilation_unit_comment/Expected.java create mode 100644 src/test/resources/clean/both_modified/compilation_unit_comment/Left.java create mode 100644 src/test/resources/clean/both_modified/compilation_unit_comment/Right.java create mode 100644 src/test/resources/clean/both_modified/two_way_merge/Base.java create mode 100644 src/test/resources/clean/both_modified/two_way_merge/Expected.java create mode 100644 src/test/resources/clean/both_modified/two_way_merge/Left.java create mode 100644 src/test/resources/clean/both_modified/two_way_merge/Right.java diff --git a/src/main/java/se/kth/spork/spoon/Parser.java b/src/main/java/se/kth/spork/spoon/Parser.java index 26fea2b6..1586d810 100644 --- a/src/main/java/se/kth/spork/spoon/Parser.java +++ b/src/main/java/se/kth/spork/spoon/Parser.java @@ -8,6 +8,7 @@ import spoon.compiler.Environment; import spoon.reflect.CtModel; import spoon.reflect.code.CtComment; +import spoon.reflect.cu.CompilationUnit; import spoon.reflect.declaration.*; import spoon.support.compiler.FileSystemFile; import spoon.support.compiler.VirtualFile; @@ -17,6 +18,7 @@ import java.nio.file.Path; import java.util.*; import java.util.function.Consumer; +import java.util.stream.Stream; /** * A class for dealing with parsing. @@ -89,10 +91,7 @@ private static CtModule parse(Consumer addResource) { CtModule module = model.getUnnamedModule(); - // FIXME This is an ugly workaround for merging compliation unit comments - List cuComments = module.getFactory().CompilationUnit().getMap().values().iterator().next().getComments(); - String cuComment = cuComments.isEmpty() ? "" : cuComments.get(0).getRawContent(); - module.putMetadata(COMPILATION_UNIT_COMMENT, cuComment); + module.putMetadata(COMPILATION_UNIT_COMMENT, getCuComment(module)); // TODO preserve order of import statements List imports = new ArrayList<>(parseImportStatements(model)); @@ -102,6 +101,13 @@ private static CtModule parse(Consumer addResource) { return module; } + private static String getCuComment(CtModule module) { + // FIXME This is an ugly workaround for merging compliation unit comments + return module.getFactory().CompilationUnit().getMap().values().stream() + .findFirst().map(cu -> cu.getComments().stream() + ).flatMap(Stream::findFirst).map(CtComment::getRawContent).orElse(""); + } + /** * Parse unique import statements from all types of the given model. * diff --git a/src/test/resources/clean/both_modified/compilation_unit_comment/Base.java b/src/test/resources/clean/both_modified/compilation_unit_comment/Base.java new file mode 100644 index 00000000..c05ff6df --- /dev/null +++ b/src/test/resources/clean/both_modified/compilation_unit_comment/Base.java @@ -0,0 +1,12 @@ +/* + * This comment will be attached to the compilation unit. + * We want to merge it textually, separately from all other merging. + */ +package se.kth.spork; + +/** + * And this is the normal Javadoc comment. + */ +public class Cls { + +} \ No newline at end of file diff --git a/src/test/resources/clean/both_modified/compilation_unit_comment/Expected.java b/src/test/resources/clean/both_modified/compilation_unit_comment/Expected.java new file mode 100644 index 00000000..8b680564 --- /dev/null +++ b/src/test/resources/clean/both_modified/compilation_unit_comment/Expected.java @@ -0,0 +1,16 @@ +/* + * Add some stuff in the left revision! + * + * This comment will be attached to the compilation unit. + * We want to merge it textually, separately from all other merging. + * + * And add some stuff in the right revision! + */ +package se.kth.spork; + +/** + * And this is the normal Javadoc comment. + */ +public class Cls { + +} \ No newline at end of file diff --git a/src/test/resources/clean/both_modified/compilation_unit_comment/Left.java b/src/test/resources/clean/both_modified/compilation_unit_comment/Left.java new file mode 100644 index 00000000..68452662 --- /dev/null +++ b/src/test/resources/clean/both_modified/compilation_unit_comment/Left.java @@ -0,0 +1,14 @@ +/* + * Add some stuff in the left revision! + * + * This comment will be attached to the compilation unit. + * We want to merge it textually, separately from all other merging. + */ +package se.kth.spork; + +/** + * And this is the normal Javadoc comment. + */ +public class Cls { + +} \ No newline at end of file diff --git a/src/test/resources/clean/both_modified/compilation_unit_comment/Right.java b/src/test/resources/clean/both_modified/compilation_unit_comment/Right.java new file mode 100644 index 00000000..e3c56bbf --- /dev/null +++ b/src/test/resources/clean/both_modified/compilation_unit_comment/Right.java @@ -0,0 +1,14 @@ +/* + * This comment will be attached to the compilation unit. + * We want to merge it textually, separately from all other merging. + * + * And add some stuff in the right revision! + */ +package se.kth.spork; + +/** + * And this is the normal Javadoc comment. + */ +public class Cls { + +} \ No newline at end of file diff --git a/src/test/resources/clean/both_modified/two_way_merge/Base.java b/src/test/resources/clean/both_modified/two_way_merge/Base.java new file mode 100644 index 00000000..e69de29b diff --git a/src/test/resources/clean/both_modified/two_way_merge/Expected.java b/src/test/resources/clean/both_modified/two_way_merge/Expected.java new file mode 100644 index 00000000..e420782f --- /dev/null +++ b/src/test/resources/clean/both_modified/two_way_merge/Expected.java @@ -0,0 +1,7 @@ +class Main { + public static void main(String[] args) { + int a = 2; + int b = 2; + System.out.println(a + b); + } +} \ No newline at end of file diff --git a/src/test/resources/clean/both_modified/two_way_merge/Left.java b/src/test/resources/clean/both_modified/two_way_merge/Left.java new file mode 100644 index 00000000..e420782f --- /dev/null +++ b/src/test/resources/clean/both_modified/two_way_merge/Left.java @@ -0,0 +1,7 @@ +class Main { + public static void main(String[] args) { + int a = 2; + int b = 2; + System.out.println(a + b); + } +} \ No newline at end of file diff --git a/src/test/resources/clean/both_modified/two_way_merge/Right.java b/src/test/resources/clean/both_modified/two_way_merge/Right.java new file mode 100644 index 00000000..e420782f --- /dev/null +++ b/src/test/resources/clean/both_modified/two_way_merge/Right.java @@ -0,0 +1,7 @@ +class Main { + public static void main(String[] args) { + int a = 2; + int b = 2; + System.out.println(a + b); + } +} \ No newline at end of file