Skip to content

Commit

Permalink
fix: CompilationUnit has lineSeparatorPositions even for empty class (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pvojtechovsky authored and monperrus committed Dec 8, 2018
1 parent 120d94c commit 5da7f8e
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public class JDTCommentBuilder {
this.sourceUnit = declarationUnit.compilationResult.compilationUnit;
this.contents = sourceUnit.getContents();
this.filePath = CharOperation.charToString(sourceUnit.getFileName());
this.spoonUnit = factory.CompilationUnit().getOrCreate(filePath);
this.spoonUnit = JDTTreeBuilder.getOrCreateCompilationUnit(declarationUnit, factory);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class JDTImportBuilder {
this.sourceUnit = declarationUnit.compilationResult.compilationUnit;
this.filePath = CharOperation.charToString(sourceUnit.getFileName());
// get the CU: it has already been built during model building in JDTBasedSpoonCompiler
this.spoonUnit = factory.CompilationUnit().getOrCreate(filePath);
this.spoonUnit = JDTTreeBuilder.getOrCreateCompilationUnit(declarationUnit, factory);
this.imports = new HashSet<>();
}

Expand Down
13 changes: 12 additions & 1 deletion src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
import spoon.reflect.code.CtTypeAccess;
import spoon.reflect.code.CtUnaryOperator;
import spoon.reflect.code.UnaryOperatorKind;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.declaration.CtAnnotationMethod;
import spoon.reflect.declaration.CtAnonymousExecutable;
Expand Down Expand Up @@ -775,10 +776,20 @@ public boolean visit(Javadoc javadoc, ClassScope scope) {
return false;
}

static CompilationUnit getOrCreateCompilationUnit(CompilationUnitDeclaration compilationUnitDeclaration, Factory factory) {
CompilationUnit compilationUnitSpoon = factory.CompilationUnit().getOrCreate(new String(compilationUnitDeclaration.getFileName()));
if (compilationUnitSpoon.getLineSeparatorPositions() == null) {
compilationUnitSpoon.setLineSeparatorPositions(compilationUnitDeclaration.compilationResult.lineSeparatorPositions);
} else if (compilationUnitSpoon.getLineSeparatorPositions() != compilationUnitDeclaration.compilationResult.lineSeparatorPositions) {
throw new SpoonException("Unexpected CompilationUnit lineSeparatorPositions");
}
return compilationUnitSpoon;
}

@Override
public boolean visit(CompilationUnitDeclaration compilationUnitDeclaration, CompilationUnitScope scope) {
context.compilationunitdeclaration = scope.referenceContext;
context.compilationUnitSpoon = getFactory().CompilationUnit().getOrCreate(new String(context.compilationunitdeclaration.getFileName()));
context.compilationUnitSpoon = getOrCreateCompilationUnit(context.compilationunitdeclaration, getFactory());
ModuleBinding enclosingModule = scope.fPackage.enclosingModule;

CtModule module;
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/spoon/support/compiler/jdt/JDTTreeBuilderQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,16 @@ static TypeBinding searchTypeBinding(String qualifiedName, CompilationUnitDeclar
return null;
}
for (CompilationUnitDeclaration unitToProcess : unitsToProcess) {
for (TypeDeclaration type : unitToProcess.types) {
if (qualifiedName.equals(CharOperation.toString(type.binding.compoundName))) {
return type.binding;
}
if (type.memberTypes != null) {
for (TypeDeclaration memberType : type.memberTypes) {
if (qualifiedName.equals(CharOperation.toString(memberType.binding.compoundName))) {
return type.binding;
if (unitToProcess.types != null) {
for (TypeDeclaration type : unitToProcess.types) {
if (qualifiedName.equals(CharOperation.toString(type.binding.compoundName))) {
return type.binding;
}
if (type.memberTypes != null) {
for (TypeDeclaration memberType : type.memberTypes) {
if (qualifiedName.equals(CharOperation.toString(memberType.binding.compoundName))) {
return type.binding;
}
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/spoon/support/compiler/jdt/PositionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,9 @@ SourcePosition buildPositionCtElement(CtElement e, ASTNode node) {
return SourcePosition.NOPOSITION;
}
CoreFactory cf = this.jdtTreeBuilder.getFactory().Core();
CompilationUnit cu = this.jdtTreeBuilder.getFactory().CompilationUnit().getOrCreate(new String(this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.getFileName()));
CompilationUnit cu = this.jdtTreeBuilder.getContextBuilder().compilationUnitSpoon;
CompilationResult cr = this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.compilationResult;
int[] lineSeparatorPositions = cr.lineSeparatorPositions;
cu.setLineSeparatorPositions(lineSeparatorPositions);
char[] contents = cr.compilationUnit.getContents();

int sourceStart = node.sourceStart;
Expand Down Expand Up @@ -511,7 +510,7 @@ private int getNrOfCastExpressionBrackets() {

private void setModifiersPosition(CtModifiable e, int start, int end) {
CoreFactory cf = this.jdtTreeBuilder.getFactory().Core();
CompilationUnit cu = this.jdtTreeBuilder.getFactory().CompilationUnit().getOrCreate(new String(this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.getFileName()));
CompilationUnit cu = this.jdtTreeBuilder.getContextBuilder().compilationUnitSpoon;
CompilationResult cr = this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.compilationResult;
char[] contents = cr.compilationUnit.getContents();

Expand Down
9 changes: 9 additions & 0 deletions src/test/java/spoon/test/position/PositionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,7 @@ public void testNestedLabels() throws Exception {
assertEquals("label2: while(true);", contentAtPosition(classContent, stmt3.getPosition()));
}
}

@Test
public void testPackageDeclaration() throws Exception {
//contract: check position of package declaration
Expand Down Expand Up @@ -1269,4 +1270,12 @@ public void testPackageDeclarationWithCommentPosition() throws Exception {
assertEquals("/* package declaration comments*/\n" +
"package spoon.test.position.testclasses;", contentAtPosition(classContent, packageDecl.getPosition()));
}

@Test
public void testCommentedOutClass() {
//contract: commented out class should not fail model build
final Factory build = build(new File("src/test/java/spoon/test/position/testclasses/TestCommentedOutClass.java"));
CtType<?> type = build.Type().get("spoon.test.position.testclasses.TestCommentedOutClass");
assertNull(type);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package spoon.test.position.testclasses;

//public class TestCommentedOutClass {
//}

0 comments on commit 5da7f8e

Please sign in to comment.