Skip to content

Commit

Permalink
Added test case for PositionBuilder when generic type param has no sp…
Browse files Browse the repository at this point in the history
…ace following modifier
  • Loading branch information
Strum355 committed Aug 28, 2020
1 parent d7b2ae0 commit 7319121
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,12 @@ SourcePosition buildPositionCtElement(CtElement e, ASTNode node) {

TypeParameter[] typeParameters = methodDeclaration.typeParameters();
if (typeParameters != null && typeParameters.length > 0) {
modifiersSourceEnd = typeParameters[0].declarationSourceStart - 3;
// if there is no space between the modifier and the type parameter vs if there is a space
if (contents[typeParameters[0].declarationSourceStart - 2] != ' ') {
modifiersSourceEnd = typeParameters[0].declarationSourceStart - 2;
} else {
modifiersSourceEnd = typeParameters[0].declarationSourceStart - 3;
}
}

if (getModifiers(methodDeclaration.modifiers, false, true).isEmpty()) {
Expand Down
15 changes: 13 additions & 2 deletions src/test/java/spoon/test/position/PositionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,10 @@ public void testPositionGeneric() throws Exception {
BodyHolderSourcePosition position = (BodyHolderSourcePosition) foo.getPosition();

assertEquals(3, position.getLine());
assertEquals(31, position.getEndLine());
assertEquals(35, position.getEndLine());

assertEquals(42, position.getSourceStart());
assertEquals(411, position.getSourceEnd());
assertEquals(468, position.getSourceEnd());

assertEquals("FooGeneric", contentAtPosition(classContent, position.getNameStart(), position.getNameEnd()));
assertEquals("public", contentAtPosition(classContent, position.getModifierSourceStart(), position.getModifierSourceEnd()));
Expand Down Expand Up @@ -411,6 +411,17 @@ public void testPositionGeneric() throws Exception {

// /!\ the annotations can be between two modifiers
assertEquals("public @Deprecated static", contentAtPosition(classContent, position2.getModifierSourceStart(), position2.getModifierSourceEnd()));

CtMethod<?> method2 = foo.getMethodsByName("n").get(0);
BodyHolderSourcePosition position3 = (BodyHolderSourcePosition) method2
.getPosition();

assertEquals("protected static<S> S n(int parm2) {\n"
+ "\t\treturn null;\n"
+ "\t}", contentAtPosition(classContent, position3));
assertEquals("n", contentAtPosition(classContent, position3.getNameStart(), position3.getNameEnd()));

assertEquals("protected static", contentAtPosition(classContent, position3.getModifierSourceStart(), position3.getModifierSourceEnd()));
}

@Test
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/spoon/test/position/testclasses/FooGeneric.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ public class FooGeneric<T extends Object> {
return null;
}

protected static<S> S n(int parm2) {
return null;
}

/**
* Method with javadoc
* @param parm1 the parameter
Expand Down

0 comments on commit 7319121

Please sign in to comment.