Skip to content

Commit

Permalink
[fix] Check for existence of CU befor getting comment, fix #134
Browse files Browse the repository at this point in the history
  • Loading branch information
slarse committed May 24, 2020
1 parent 4c0190f commit 58f0ff6
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/main/java/se/kth/spork/spoon/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -89,10 +91,7 @@ private static CtModule parse(Consumer<Launcher> addResource) {

CtModule module = model.getUnnamedModule();

// FIXME This is an ugly workaround for merging compliation unit comments
List<CtComment> 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<CtImport> imports = new ArrayList<>(parseImportStatements(model));
Expand All @@ -102,6 +101,13 @@ private static CtModule parse(Consumer<Launcher> 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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

}
Original file line number Diff line number Diff line change
@@ -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 {

}
Original file line number Diff line number Diff line change
@@ -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 {

}
Original file line number Diff line number Diff line change
@@ -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 {

}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Main {
public static void main(String[] args) {
int a = 2;
int b = 2;
System.out.println(a + b);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Main {
public static void main(String[] args) {
int a = 2;
int b = 2;
System.out.println(a + b);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Main {
public static void main(String[] args) {
int a = 2;
int b = 2;
System.out.println(a + b);
}
}

0 comments on commit 58f0ff6

Please sign in to comment.