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): CtCatch has no modifiers, they are in CatchVariable #2156

Merged
merged 1 commit into from
Jul 3, 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
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