Skip to content

Commit

Permalink
fix(position): CtCatch has no modifiers, they are in CatchVariable (#…
Browse files Browse the repository at this point in the history
…2156)

The modifiers were on two places before
1) CtCatch - wrong
2) CtCactchVariable - OK

So this PR removes them from CtCatch

Note: CtCatch would need a bettet source position structure. I actually I used BodyHolderSourcePosition, but it is may be not fitting well here. WDYT? Can we live with that or we make new structure for that?
  • Loading branch information
pvojtechovsky authored and surli committed Jul 3, 2018
1 parent 05662a7 commit 9a45459
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
8 changes: 5 additions & 3 deletions src/main/java/spoon/support/compiler/jdt/PositionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -445,13 +445,15 @@ SourcePosition buildPosition(CtCatch catcher) {
CtTry tryElement = catcher.getParent(CtTry.class);
//offset after last bracket before catch
int declarationStart = getEndOfLastTryBlock(tryElement, 1) + 1;
DeclarationSourcePosition oldCatcherPos = (DeclarationSourcePosition) catcher.getParameter().getPosition();
DeclarationSourcePosition paramPos = (DeclarationSourcePosition) catcher.getParameter().getPosition();
int bodyStart = catcher.getBody().getPosition().getSourceStart();
int bodyEnd = catcher.getBody().getPosition().getSourceEnd();
return catcher.getFactory().Core().createBodyHolderSourcePosition(
tryElement.getPosition().getCompilationUnit(),
oldCatcherPos.getNameStart(), oldCatcherPos.getNameEnd(),
oldCatcherPos.getModifierSourceStart(), oldCatcherPos.getModifierSourceEnd(),
//on the place of name there is catch variable
paramPos.getSourceStart(), paramPos.getSourceEnd(),
//catch has no modifiers, They are in catch variable
declarationStart, declarationStart - 1,
declarationStart, bodyEnd,
bodyStart, bodyEnd,
lineSeparatorPositions);
Expand Down
33 changes: 23 additions & 10 deletions src/test/java/spoon/test/position/PositionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -887,25 +887,37 @@ public void testCatchPosition() throws Exception {
CtTry tryStatement = (CtTry) foo.getMethodsByName("method").get(0).getBody().getStatement(0);
{
CtCatch catcher = tryStatement.getCatchers().get(0);
BodyHolderSourcePosition pos = (BodyHolderSourcePosition) catcher.getPosition();
CtCatchVariable<?> catchVar = catcher.getParameter();

BodyHolderSourcePosition catcherPos = (BodyHolderSourcePosition) catcher.getPosition();
DeclarationSourcePosition catchVarPos = (DeclarationSourcePosition) catchVar.getPosition();

assertEquals(" catch (final IOException e) {\n" +
" throw new RuntimeException(e);\n" +
" }", contentAtPosition(classContent, pos.getSourceStart(), pos.getSourceEnd()));
assertEquals("final", contentAtPosition(classContent, pos.getModifierSourceStart(), pos.getModifierSourceEnd()));
assertEquals(" IOException ", contentAtPosition(classContent, pos.getModifierSourceEnd() + 1, pos.getNameStart() - 1));
assertEquals("e", contentAtPosition(classContent, pos.getNameStart(), pos.getNameEnd()));
" }", contentAtPosition(classContent, catcherPos.getSourceStart(), catcherPos.getSourceEnd()));
assertEquals("final IOException e", contentAtPosition(classContent, catchVarPos.getSourceStart(), catchVarPos.getSourceEnd()));

assertEquals("", contentAtPosition(classContent, catcherPos.getModifierSourceStart(), catcherPos.getModifierSourceEnd()));
assertEquals("final", contentAtPosition(classContent, catchVarPos.getModifierSourceStart(), catchVarPos.getModifierSourceEnd()));

assertEquals(" catch (", contentAtPosition(classContent, catcherPos.getModifierSourceEnd() + 1, catcherPos.getNameStart() - 1));
assertEquals(" IOException ", contentAtPosition(classContent, catchVarPos.getModifierSourceEnd() + 1, catchVarPos.getNameStart() - 1));

assertEquals("final IOException e", contentAtPosition(classContent, catcherPos.getNameStart(), catcherPos.getNameEnd()));
assertEquals("e", contentAtPosition(classContent, catchVarPos.getNameStart(), catchVarPos.getNameEnd()));

assertEquals("{\n" +
" throw new RuntimeException(e);\n" +
" }", contentAtPosition(classContent, pos.getBodyStart(), pos.getBodyEnd()));
" }", contentAtPosition(classContent, catcherPos.getBodyStart(), catcherPos.getBodyEnd()));
}
{
CtCatch catcher = tryStatement.getCatchers().get(1);
BodyHolderSourcePosition pos = (BodyHolderSourcePosition) catcher.getPosition();
assertEquals(" /*1*/ catch/*2*/ ( /*3*/ final @Deprecated /*4*/ ClassCastException /*5*/ e /*6*/) /*7*/ {\n" +
" throw new RuntimeException(e);\n" +
" }", contentAtPosition(classContent, pos.getSourceStart(), pos.getSourceEnd()));
assertEquals("final @Deprecated", contentAtPosition(classContent, pos.getModifierSourceStart(), pos.getModifierSourceEnd()));
assertEquals("e", contentAtPosition(classContent, pos.getNameStart(), pos.getNameEnd()));
assertEquals("", contentAtPosition(classContent, pos.getModifierSourceStart(), pos.getModifierSourceEnd()));
assertEquals(" /*3*/ final @Deprecated /*4*/ ClassCastException /*5*/ e", contentAtPosition(classContent, pos.getNameStart(), pos.getNameEnd()));
assertEquals("{\n" +
" throw new RuntimeException(e);\n" +
" }", contentAtPosition(classContent, pos.getBodyStart(), pos.getBodyEnd()));
Expand All @@ -921,8 +933,9 @@ public void testCatchPosition() throws Exception {
" throw new RuntimeException(e);\n" +
" }", contentAtPosition(classContent, pos.getSourceStart(), pos.getSourceEnd()));
assertEquals("", contentAtPosition(classContent, pos.getModifierSourceStart(), pos.getModifierSourceEnd()));
assertEquals("OutOfMemoryError|RuntimeException ", contentAtPosition(classContent, pos.getModifierSourceEnd() + 1, pos.getNameStart()-1));
assertEquals("e", contentAtPosition(classContent, pos.getNameStart(), pos.getNameEnd()));
assertEquals(" /**catch it ( */\n" +
" //catch (\n" +
" OutOfMemoryError|RuntimeException e", contentAtPosition(classContent, pos.getNameStart(), pos.getNameEnd()));
assertEquals("{\n" +
" throw new RuntimeException(e);\n" +
" }", contentAtPosition(classContent, pos.getBodyStart(), pos.getBodyEnd()));
Expand Down

0 comments on commit 9a45459

Please sign in to comment.