Skip to content

Commit

Permalink
fix: position of String...arg and String[]...arg
Browse files Browse the repository at this point in the history
  • Loading branch information
pvojtechovsky committed Jun 30, 2018
1 parent dcb9645 commit dec6437
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
49 changes: 33 additions & 16 deletions src/main/java/spoon/support/compiler/jdt/PositionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import spoon.support.compiler.jdt.ContextBuilder.CastInfo;
import spoon.support.reflect.CtExtendedModifier;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -130,6 +131,9 @@ SourcePosition buildPositionCtElement(CtElement e, ASTNode node) {
int nrOfBrackets = getNrOfFirstCastExpressionBrackets();
while (nrOfBrackets > 0) {
declarationSourceStart = findPrevNonWhitespace(contents, getParentsSourceStart(), declarationSourceStart - 1);
if (declarationSourceStart < 0) {
return handlePositionProblem("Cannot found beginning of cast expression until offset: " + getParentsSourceStart());
}
if (contents[declarationSourceStart] != '(') {
return handlePositionProblem("Unexpected character \'" + contents[declarationSourceStart] + "\' at start of expression on offset: " + declarationSourceStart);
}
Expand Down Expand Up @@ -191,19 +195,21 @@ SourcePosition buildPositionCtElement(CtElement e, ASTNode node) {
declarationSourceStart = bracketStart + 1;
}

if (variableDeclaration.type instanceof ArrayTypeReference) {
if (variableDeclaration instanceof Argument && variableDeclaration.type instanceof ArrayTypeReference) {
//handle type declarations like `String[] arg` `String arg[]` and `String []arg[]`
ArrayTypeReference arrTypeRef = (ArrayTypeReference) variableDeclaration.type;
int dimensions = arrTypeRef.dimensions();
//count number of brackets between type and variable name
int foundDimensions = getNrOfDimensions(contents, declarationSourceStart, declarationSourceEnd);
while (dimensions > foundDimensions) {
//some brackets are after the variable name
declarationSourceEnd = findNextChar(contents, contents.length, declarationSourceEnd + 1, ']');
if (declarationSourceEnd < 0) {
throw new SpoonException("Unexpected array type declaration on offset: " + declarationSourceStart);
if (dimensions > 0) {
//count number of brackets between type and variable name
int foundDimensions = getNrOfDimensions(contents, declarationSourceStart, declarationSourceEnd);
while (dimensions > foundDimensions) {
//some brackets are after the variable name
declarationSourceEnd = findNextChar(contents, contents.length, declarationSourceEnd + 1, ']');
if (declarationSourceEnd < 0) {
return handlePositionProblem("Unexpected array type declaration on offset: " + declarationSourceStart);
}
foundDimensions++;
}
foundDimensions++;
}
}

Expand Down Expand Up @@ -367,20 +373,31 @@ SourcePosition buildPositionCtElement(CtElement e, ASTNode node) {
}

private int getParentsSourceStart() {
ASTPair pair = this.jdtTreeBuilder.getContextBuilder().stack.peek();
if (pair != null) {
SourcePosition pos = pair.element.getPosition();
if (pos.isValidPosition()) {
return pos.getSourceStart();
Iterator<ASTPair> iter = this.jdtTreeBuilder.getContextBuilder().stack.iterator();
if (iter.hasNext()) {
iter.next();
if (iter.hasNext()) {
ASTPair pair = iter.next();
SourcePosition pos = pair.element.getPosition();
if (pos.isValidPosition()) {
return pos.getSourceStart();
}
}
}
return 0;
}

private int getNrOfDimensions(char[] contents, int start, int end) {
int nrDims = 0;
while ((start = findNextChar(contents, end, start, ']')) >= 0) {
nrDims++;
while ((start = findNextNonWhitespace(contents, end, start)) >= 0) {
if (contents[start] == ']') {
nrDims++;
}
if (contents[start] == '.' && start + 2 <= end && contents[start + 1] == '.' && contents[start + 2] == '.') {
//String...arg is same like String[] arg, so it is counted as dimension too
start = start + 2;
nrDims++;
}
start++;
}
return nrDims;
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/spoon/test/position/PositionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,16 @@ public void testArrayArgParameter() throws Exception {
assertEquals("String[]//[]\n" +
" p[]", contentAtPosition(classContent, param.getType().getPosition()));
}
{
CtParameter<?> param = foo.getMethodsByName("m7").get(0).getParameters().get(0);
assertEquals("String...arg", contentAtPosition(classContent, param.getPosition()));
assertEquals("String...", contentAtPosition(classContent, param.getType().getPosition()));
}
{
CtParameter<?> param = foo.getMethodsByName("m8").get(0).getParameters().get(0);
assertEquals("String[]...arg", contentAtPosition(classContent, param.getPosition()));
assertEquals("String[]...", contentAtPosition(classContent, param.getType().getPosition()));
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public interface ArrayArgParameter {
void m5(/*1*/ String /*2*/ arg /*3*/ [ /*4 []*/ ] /* 5 */[][]/**/ []);
void m6(String[]//[]
p[]);
void m7(String...arg);
void m8(String[]...arg);
}

0 comments on commit dec6437

Please sign in to comment.