Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(position): position of expressions ending with ... List<?> #2151

Merged
merged 4 commits into from
Jul 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/main/java/spoon/support/compiler/jdt/ParentExiter.java
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,18 @@ public <T> void visitCtBinaryOperator(CtBinaryOperator<T> operator) {
operator.setLeftHandOperand((CtExpression<?>) child);
return;
} else if (operator.getRightHandOperand() == null) {
if (child.getPosition().isValidPosition()) {
int childEnd = child.getPosition().getSourceEnd();
SourcePosition oldPos = operator.getPosition();
if (oldPos.isValidPosition() && oldPos.getSourceEnd() < childEnd) {
//fix parent position if right hand expression is `x instanceof List<?>` which has bad sourceEnd ending before `<?>
int[] lineSeparatorPositions = this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.compilationResult.lineSeparatorPositions;
operator.setPosition(operator.getFactory().Core().createSourcePosition(
oldPos.getCompilationUnit(),
oldPos.getSourceStart(), childEnd,
lineSeparatorPositions));
}
}
operator.setRightHandOperand((CtExpression<?>) child);
return;
} else if (jdtTreeBuilder.getContextBuilder().stack.peek().node instanceof StringLiteralConcatenation) {
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/spoon/support/compiler/jdt/PositionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.ast.TypeParameter;
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
import org.eclipse.jdt.internal.compiler.ast.Wildcard;

import spoon.SpoonException;
import spoon.reflect.code.CtCase;
import spoon.reflect.code.CtCatch;
Expand Down Expand Up @@ -510,7 +512,7 @@ private void setModifiersPosition(CtModifiable e, int start, int end) {

private int getSourceEndOfTypeReference(char[] contents, TypeReference node, int sourceEnd) {
//e.g. SomeType<String,T>
TypeReference[][] typeArgs = ((TypeReference) node).getTypeArguments();
TypeReference[][] typeArgs = node.getTypeArguments();
if (typeArgs != null && typeArgs.length > 0) {
TypeReference[] trs = typeArgs[typeArgs.length - 1];
if (trs != null && trs.length > 0) {
Expand All @@ -523,6 +525,12 @@ private int getSourceEndOfTypeReference(char[] contents, TypeReference node, int
}
}
}
if (node instanceof Wildcard) {
Wildcard wildcard = (Wildcard) node;
if (wildcard.bound != null) {
sourceEnd = getSourceEndOfTypeReference(contents, wildcard.bound, sourceEnd);
}
}
return sourceEnd;
}

Expand Down
53 changes: 39 additions & 14 deletions src/test/java/spoon/test/position/PositionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,45 @@ public void testPositionParameterTypeReference() throws Exception {
final CtType<?> foo = buildClass(PositionParameterTypeWithReference.class);
String classContent = getClassContent(foo);

CtTypeReference<?> field2Type = foo.getField("field2").getType();
//this already worked well
assertEquals("List<T>[][]", contentAtPosition(classContent, field2Type.getPosition()));

CtTypeReference<?> field1Type = foo.getField("field1").getType();
//this probably points to an bug in JDT. But we have no workaround in Spoon
assertEquals("List<T>", contentAtPosition(classContent, field1Type.getPosition()));

CtTypeReference<?> field3Type = foo.getField("field3").getType();
//this probably points to an bug in JDT. But we have no workaround in Spoon, which handles spaces and comments too
assertEquals("List<T // */ >\n\t/*// */>", contentAtPosition(classContent, field3Type.getPosition()));

CtTypeReference<?> field4Type = foo.getField("field4").getType();
assertEquals("List<List<?>>", contentAtPosition(classContent, field4Type.getPosition()));
{
assertEquals("T extends List<?>", contentAtPosition(classContent, foo.getFormalCtTypeParameters().get(0).getPosition()));
assertEquals("X", contentAtPosition(classContent, foo.getFormalCtTypeParameters().get(1).getPosition()));
}
{
CtTypeReference<?> field1Type = foo.getField("field1").getType();
//this probably points to an bug in JDT. But we have no workaround in Spoon
assertEquals("List<T>", contentAtPosition(classContent, field1Type.getPosition()));
}
{
CtTypeReference<?> field2Type = foo.getField("field2").getType();
//this already worked well
assertEquals("List<T>[][]", contentAtPosition(classContent, field2Type.getPosition()));
}
{
CtTypeReference<?> field3Type = foo.getField("field3").getType();
//this probably points to an bug in JDT. But we have no workaround in Spoon, which handles spaces and comments too
assertEquals("List<T // */ >\n\t/*// */>", contentAtPosition(classContent, field3Type.getPosition()));
}
{
CtTypeReference<?> field4Type = foo.getField("field4").getType();
assertEquals("List<List<?>>", contentAtPosition(classContent, field4Type.getPosition()));
}
{
CtTypeReference<?> fieldType = foo.getField("field5").getType();
assertEquals("List<? extends List<?>>", contentAtPosition(classContent, fieldType.getPosition()));
}
{
CtReturn<?> retStmt = foo.getMethodsByName("m1").get(0).getBody().getStatement(0);
assertEquals("o instanceof List<?>", contentAtPosition(classContent, retStmt.getReturnedExpression().getPosition()));
}
{
CtReturn<?> retStmt = foo.getMethodsByName("m2").get(0).getBody().getStatement(0);
assertEquals("false || o instanceof List<?>", contentAtPosition(classContent, retStmt.getReturnedExpression().getPosition()));
}
{
CtTypeParameter methodGType = foo.getMethodsByName("m3").get(0).getFormalCtTypeParameters().get(0);
assertEquals("U extends List<?>", contentAtPosition(classContent, methodGType.getPosition()));
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@

import java.util.List;

public class PositionParameterTypeWithReference<T> {
public class PositionParameterTypeWithReference<T extends List<?>, X> {
List<T> field1;
List<T>[][] field2;
List<T // */ >
/*// */>
//>
field3;
List<List<?>> field4;
List<? extends List<?>> field5;
boolean m1(Object o) {
return o instanceof List<?>;
}
boolean m2(Object o) {
return false || o instanceof List<?>;
}

<U extends List<?>> void m3(U u) {}
}