Skip to content

Commit

Permalink
fix and simplify the solution
Browse files Browse the repository at this point in the history
  • Loading branch information
pvojtechovsky committed Jun 30, 2018
1 parent e292a1a commit f900186
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
11 changes: 10 additions & 1 deletion src/main/java/spoon/support/compiler/jdt/ContextBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ void exit(ASTNode node) {
}
}

<T extends CtElement> T getParentContextOfType(Class<T> clazz) {
<T extends CtElement> T getParentElementOfType(Class<T> clazz) {
for (ASTPair pair : stack) {
if (clazz.isInstance(pair.element)) {
return (T) pair.element;
Expand All @@ -147,6 +147,15 @@ <T extends CtElement> T getParentContextOfType(Class<T> clazz) {
return null;
}

ASTPair getParentContextOfType(Class<? extends CtElement> clazz) {
for (ASTPair pair : stack) {
if (clazz.isInstance(pair.element)) {
return pair;
}
}
return null;
}

@SuppressWarnings("unchecked")
<T> CtLocalVariable<T> getLocalVariableDeclaration(final String name) {
final Class<CtLocalVariable<T>> clazz = (Class<CtLocalVariable<T>>)
Expand Down
21 changes: 15 additions & 6 deletions src/main/java/spoon/support/compiler/jdt/PositionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.eclipse.jdt.internal.compiler.ast.AllocationExpression;
import org.eclipse.jdt.internal.compiler.ast.Annotation;
import org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration;
import org.eclipse.jdt.internal.compiler.ast.Argument;
import org.eclipse.jdt.internal.compiler.ast.Expression;
import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
import org.eclipse.jdt.internal.compiler.ast.Initializer;
Expand Down Expand Up @@ -71,6 +72,11 @@ SourcePosition buildPosition(int sourceStart, int sourceEnd) {
}

SourcePosition buildPositionCtElement(CtElement e, ASTNode node) {
if (e instanceof CtCatch) {
//we cannot compute position of CtCatch, because we do not know position of it's body yet
//it is computed later by #buildPosition(CtCatch)
return SourcePosition.NOPOSITION;
}
CoreFactory cf = this.jdtTreeBuilder.getFactory().Core();
CompilationUnit cu = this.jdtTreeBuilder.getFactory().CompilationUnit().getOrCreate(new String(this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.getFileName()));
CompilationResult cr = this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration.compilationResult;
Expand Down Expand Up @@ -159,14 +165,14 @@ SourcePosition buildPositionCtElement(CtElement e, ASTNode node) {
if (declarationSourceStart == 0 && declarationSourceEnd == 0) {
return SourcePosition.NOPOSITION;
}
if (e instanceof CtCatch) {
if (e instanceof CtCatchVariable) {
/* compiler delivers wrong declarationSourceStart in case like: */
//... catch/*2*/ ( /*3*/ final @Deprecated /*4*/ ClassCastException /*5*/ e /*6*/) /*7*/ {
/*
* the declarationSourceStart should be after the '(', but sometime it is before
* So we have to compute correct offset here
*/
CtTry tryStatement = this.jdtTreeBuilder.getContextBuilder().getParentContextOfType(CtTry.class);
CtTry tryStatement = this.jdtTreeBuilder.getContextBuilder().getParentElementOfType(CtTry.class);
int endOfTry = tryStatement.getPosition().getSourceEnd();
//offset of the bracket before catch
int lastBracket = getEndOfLastTryBlock(tryStatement, 0);
Expand Down Expand Up @@ -323,9 +329,12 @@ SourcePosition buildPositionCtElement(CtElement e, ASTNode node) {
lineSeparatorPositions);
}
} else if (e instanceof CtCatchVariable) {
CtCatch catcher = this.jdtTreeBuilder.getContextBuilder().getParentContextOfType(CtCatch.class);
//the CtCatch has the position of CTCatchVariable now, so take it
return (DeclarationSourcePosition) catcher.getPosition();
ASTPair pair = this.jdtTreeBuilder.getContextBuilder().getParentContextOfType(CtCatch.class);
if (pair == null) {
throw new SpoonException("There is no CtCatch parent for CtCatchVariable");
}
//build position with appropriate context
return buildPositionCtElement(e, (Argument) pair.node);
} else if (node instanceof TypeReference) {
sourceEnd = getSourceEndOfTypeReference(contents, (TypeReference) node, sourceEnd);
} else if (node instanceof AllocationExpression) {
Expand Down Expand Up @@ -355,7 +364,7 @@ void 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.getPosition();
DeclarationSourcePosition oldCatcherPos = (DeclarationSourcePosition) catcher.getParameter().getPosition();
int bodyStart = catcher.getBody().getPosition().getSourceStart();
int bodyEnd = catcher.getBody().getPosition().getSourceEnd();
catcher.setPosition(catcher.getFactory().Core().createBodyHolderSourcePosition(
Expand Down

0 comments on commit f900186

Please sign in to comment.