diff --git a/src/main/java/spoon/support/compiler/jdt/ContextBuilder.java b/src/main/java/spoon/support/compiler/jdt/ContextBuilder.java index 8805373ba4d..8dce34eef5b 100644 --- a/src/main/java/spoon/support/compiler/jdt/ContextBuilder.java +++ b/src/main/java/spoon/support/compiler/jdt/ContextBuilder.java @@ -75,7 +75,7 @@ public static class CastInfo { /** * Stack of all parents elements */ - Deque stack = new ArrayDeque<>(); + private final Deque stack = new ArrayDeque<>(); private final JDTTreeBuilder jdtTreeBuilder; @@ -130,6 +130,37 @@ void exit(ASTNode node) { } } + /** + * @return all {@link ASTPair}s currently on the stack + */ + Iterable getAllContexts() { + return stack; + } + + /** + * @return {@code true} if there are any elements on the stack + */ + boolean hasCurrentContext() { + return !stack.isEmpty(); + } + + /** + * + * @return the {@link CtElement} on the top of the stack + * @throws NullPointerException if the stack is empty + */ + CtElement getCurrentElement() { + return stack.peek().element; + } + + /** + * @return the {@link ASTNode} on the top of the stack + * @throws NullPointerException if the stack is empty + */ + ASTNode getCurrentNode() { + return stack.peek().node; + } + CtElement getContextElementOnLevel(int level) { for (ASTPair pair : stack) { if (level == 0) { @@ -168,7 +199,7 @@ CtLocalVariable getLocalVariableDeclaration(final String name) { // note: this happens when using the new try(vardelc) structure this.jdtTreeBuilder.getLogger().error( format("Could not find declaration for local variable %s at %s", - name, stack.peek().element.getPosition())); + name, getCurrentElement().getPosition())); } return localVariable; } @@ -183,7 +214,7 @@ CtCatchVariable getCatchVariableDeclaration(final String name) { // note: this happens when using the new try(vardelc) structure this.jdtTreeBuilder.getLogger().error( format("Could not find declaration for catch variable %s at %s", - name, stack.peek().element.getPosition())); + name, getCurrentElement().getPosition())); } return catchVariable; } @@ -195,7 +226,7 @@ CtVariable getVariableDeclaration(final String name) { // note: this can happen when identifier is not a variable name but e.g. a Type name. this.jdtTreeBuilder.getLogger().debug( format("Could not find declaration for variable %s at %s.", - name, stack.peek().element.getPosition())); + name, getCurrentElement().getPosition())); } return variable; } diff --git a/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java b/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java index 296a2b22212..e5462f2643c 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java @@ -449,8 +449,7 @@ public void endVisit(IntLiteral intLiteral, BlockScope scope) { @Override public void endVisit(LabeledStatement labeledStatement, BlockScope scope) { - ASTPair pair = context.stack.peek(); - CtBlock block = (CtBlock) pair.element; + CtBlock block = (CtBlock) context.getCurrentElement(); if (block.getStatements().size() == 1) { CtStatement childStmt = block.getStatement(0); if (childStmt.getLabel() == null) { @@ -610,7 +609,7 @@ public void endVisit(QualifiedAllocationExpression qualifiedAllocationExpression @Override public void endVisit(QualifiedNameReference qualifiedNameReference, BlockScope scope) { - if (context.stack.peek().node == qualifiedNameReference) { + if (context.getCurrentNode() == qualifiedNameReference) { context.exit(qualifiedNameReference); } } @@ -650,7 +649,7 @@ public void endVisit(SingleMemberAnnotation annotation, BlockScope scope) { @Override public void endVisit(SingleNameReference singleNameReference, BlockScope scope) { - if (context.stack.peek().node == singleNameReference) { + if (context.getCurrentNode() == singleNameReference) { context.exit(singleNameReference); } } @@ -706,16 +705,16 @@ public void endVisit(ThisReference thisReference, BlockScope scope) { @Override public void endVisit(SwitchStatement switchStatement, BlockScope scope) { - if (context.stack.peek().node instanceof CaseStatement) { - context.exit(context.stack.peek().node); + if (context.getCurrentNode() instanceof CaseStatement) { + context.exit(context.getCurrentNode()); } context.exit(switchStatement); } @Override public void endVisit(SwitchExpression switchExpression, BlockScope scope) { - if (context.stack.peek().node instanceof CaseStatement) { - context.exit(context.stack.peek().node); + if (context.getCurrentNode() instanceof CaseStatement) { + context.exit(context.getCurrentNode()); } context.exit(switchExpression); } @@ -757,14 +756,14 @@ public void endVisit(TypeDeclaration localTypeDeclaration, BlockScope scope) { @Override public void endVisit(TypeDeclaration memberTypeDeclaration, ClassScope scope) { - while (!context.stack.isEmpty() && context.stack.peek().node == memberTypeDeclaration) { + while (context.hasCurrentContext() && context.getCurrentNode() == memberTypeDeclaration) { context.exit(memberTypeDeclaration); } } @Override public void endVisit(TypeDeclaration typeDeclaration, CompilationUnitScope scope) { - while (!context.stack.isEmpty() && context.stack.peek().node == typeDeclaration) { + while (context.hasCurrentContext() && context.getCurrentNode() == typeDeclaration) { context.exit(typeDeclaration); } } @@ -879,11 +878,10 @@ public void endVisit(LambdaExpression lambdaExpression, BlockScope blockScope) { public boolean visit(AllocationExpression allocationExpression, BlockScope scope) { CtConstructorCall constructorCall = factory.Core().createConstructorCall(); constructorCall.setExecutable(references.getExecutableReference(allocationExpression)); - ASTPair first = this.context.stack.getFirst(); // in case of enum values the constructor call is often implicit - if (first.element instanceof CtEnumValue) { - if (allocationExpression.sourceEnd == first.node.sourceEnd) { + if (context.getCurrentElement() instanceof CtEnumValue) { + if (allocationExpression.sourceEnd == context.getCurrentNode().sourceEnd) { constructorCall.setImplicit(true); } } @@ -937,7 +935,7 @@ public boolean visit(AnnotationMethodDeclaration annotationMethodDeclaration, Cl @Override public boolean visit(Argument argument, BlockScope scope) { - if (this.getContextBuilder().stack.peekFirst().element instanceof CtTry) { + if (context.getCurrentElement() instanceof CtTry) { context.enter(factory.Core().createCatch(), argument); return true; } @@ -1547,14 +1545,14 @@ public boolean visit(QualifiedTypeReference qualifiedTypeReference, BlockScope s if (skipTypeInAnnotation) { return true; } - if (context.stack.peekFirst().node instanceof UnionTypeReference) { + if (context.getCurrentNode() instanceof UnionTypeReference) { CtTypeReference reference = references.getTypeReference(qualifiedTypeReference.resolvedType); if (reference == null) { reference = getFactory().createReference(qualifiedTypeReference.toString()); } context.enter(reference, qualifiedTypeReference); return true; - } else if (context.stack.peekFirst().element instanceof CtCatch) { + } else if (context.getCurrentElement() instanceof CtCatch) { context.enter(helper.createCatchVariable(qualifiedTypeReference, scope), qualifiedTypeReference); return true; } @@ -1586,7 +1584,7 @@ public boolean visit(SingleNameReference singleNameReference, BlockScope scope) } else if (singleNameReference.binding instanceof TypeBinding) { context.enter(factory.Code().createTypeAccessWithoutCloningReference(references.getTypeReference((TypeBinding) singleNameReference.binding).setSimplyQualified(true)), singleNameReference); } else if (singleNameReference.binding instanceof ProblemBinding) { - if (context.stack.peek().element instanceof CtInvocation && Character.isUpperCase(CharOperation.charToString(singleNameReference.token).charAt(0))) { + if (context.getCurrentElement() instanceof CtInvocation && Character.isUpperCase(CharOperation.charToString(singleNameReference.token).charAt(0))) { context.enter(helper.createTypeAccessNoClasspath(singleNameReference), singleNameReference); } else { context.enter(helper.createFieldAccessNoClasspath(singleNameReference), singleNameReference); @@ -1640,7 +1638,7 @@ public void endVisit(UnionTypeReference unionTypeReference, ClassScope scope) { @Override public boolean visit(UnionTypeReference unionTypeReference, BlockScope scope) { - if (!(context.stack.peekFirst().node instanceof Argument)) { + if (!(context.getCurrentNode() instanceof Argument)) { throw new SpoonException("UnionType is only supported for CtCatch."); } context.enter(helper.createCatchVariable(unionTypeReference, scope), unionTypeReference); @@ -1657,7 +1655,7 @@ public boolean visit(SingleTypeReference singleTypeReference, BlockScope scope) if (skipTypeInAnnotation) { return true; } - if (context.stack.peekFirst().node instanceof UnionTypeReference) { + if (context.getCurrentNode() instanceof UnionTypeReference) { CtTypeReference typeReference; if (singleTypeReference.resolvedType == null) { @@ -1671,10 +1669,10 @@ public boolean visit(SingleTypeReference singleTypeReference, BlockScope scope) context.enter(typeReference, singleTypeReference); typeReference.setSimplyQualified(true); return true; - } else if (context.stack.peekFirst().element instanceof CtCatch) { + } else if (context.getCurrentElement() instanceof CtCatch) { context.enter(helper.createCatchVariable(singleTypeReference, scope), singleTypeReference); return true; - } else if (context.stack.getFirst().element instanceof CtExecutableReferenceExpression) { + } else if (context.getCurrentElement() instanceof CtExecutableReferenceExpression) { context.enter(references.getTypeParameterReference(singleTypeReference.resolvedType, singleTypeReference), singleTypeReference); return true; } @@ -1710,8 +1708,8 @@ public boolean visit(StringLiteralConcatenation literal, BlockScope scope) { @Override public boolean visit(CaseStatement caseStatement, BlockScope scope) { - if (context.stack.peek().node instanceof CaseStatement) { - context.exit(context.stack.peek().node); + if (context.getCurrentNode() instanceof CaseStatement) { + context.exit(context.getCurrentNode()); } context.enter(factory.Core().createCase(), caseStatement); diff --git a/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilderHelper.java b/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilderHelper.java index 44b7a49d335..53606466163 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilderHelper.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilderHelper.java @@ -126,7 +126,7 @@ static String createQualifiedTypeName(char[][] typeName) { * @return a catch variable. */ CtCatchVariable createCatchVariable(TypeReference typeReference, Scope scope) { - final Argument jdtCatch = (Argument) jdtTreeBuilder.getContextBuilder().stack.peekFirst().node; + final Argument jdtCatch = (Argument) jdtTreeBuilder.getContextBuilder().getCurrentNode(); final Set modifiers = getModifiers(jdtCatch.modifiers, false, ModifierTarget.LOCAL_VARIABLE); CtCatchVariable result = jdtTreeBuilder.getFactory().Core().createCatchVariable(); @@ -215,7 +215,7 @@ CtVariableAccess createVariableAccessNoClasspath(SingleNameReference sing // find executable's corresponding jdt element AbstractMethodDeclaration executableJDT = null; - for (final ASTPair astPair : contextBuilder.stack) { + for (final ASTPair astPair : contextBuilder.getAllContexts()) { if (astPair.element == executable) { executableJDT = (AbstractMethodDeclaration) astPair.node; } diff --git a/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilderQuery.java b/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilderQuery.java index acd85f95a84..97c3de7a902 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilderQuery.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilderQuery.java @@ -239,7 +239,7 @@ static boolean isResolvedField(QualifiedNameReference qualifiedNameReference) { * @return true if the lhs is equals to the given expression. */ static boolean isLhsAssignment(ContextBuilder context, Expression lhs) { - return context.stack.peek().node instanceof Assignment && ((Assignment) context.stack.peek().node).lhs.equals(lhs); + return context.getCurrentNode() instanceof Assignment && ((Assignment) context.getCurrentNode()).lhs.equals(lhs); } /** diff --git a/src/main/java/spoon/support/compiler/jdt/ParentExiter.java b/src/main/java/spoon/support/compiler/jdt/ParentExiter.java index fabd0cf6524..af7a080b70d 100644 --- a/src/main/java/spoon/support/compiler/jdt/ParentExiter.java +++ b/src/main/java/spoon/support/compiler/jdt/ParentExiter.java @@ -274,14 +274,14 @@ public void scanCtVariable(CtVariable v) { } private boolean hasChildEqualsToDefaultValue(CtVariable ctVariable) { - if (jdtTreeBuilder.getContextBuilder().stack.peek().node instanceof AnnotationMethodDeclaration) { - final AnnotationMethodDeclaration parent = (AnnotationMethodDeclaration) jdtTreeBuilder.getContextBuilder().stack.peek().node; + if (jdtTreeBuilder.getContextBuilder().getCurrentNode() instanceof AnnotationMethodDeclaration) { + final AnnotationMethodDeclaration parent = (AnnotationMethodDeclaration) jdtTreeBuilder.getContextBuilder().getCurrentNode(); // Default value is equals to the jdt child. return parent.defaultValue != null && getFinalExpressionFromCast(parent.defaultValue).equals(childJDT) // Return type not yet initialized. && !child.equals(ctVariable.getDefaultExpression()); } - final AbstractVariableDeclaration parent = (AbstractVariableDeclaration) jdtTreeBuilder.getContextBuilder().stack.peek().node; + final AbstractVariableDeclaration parent = (AbstractVariableDeclaration) jdtTreeBuilder.getContextBuilder().getCurrentNode(); // Default value is equals to the jdt child. return parent.initialization != null && getFinalExpressionFromCast(parent.initialization).equals(childJDT) // Return type not yet initialized. @@ -325,7 +325,7 @@ public void visitCtMethod(CtMethod e) { } private boolean hasChildEqualsToType(CtMethod ctMethod) { - final MethodDeclaration parent = (MethodDeclaration) jdtTreeBuilder.getContextBuilder().stack.peek().node; + final MethodDeclaration parent = (MethodDeclaration) jdtTreeBuilder.getContextBuilder().getCurrentNode(); // Return type is equals to the jdt child. return parent.returnType != null && parent.returnType.equals(childJDT) // Return type not yet initialized. @@ -342,7 +342,7 @@ public void visitCtAnnotationMethod(CtAnnotationMethod annotationMethod) } private boolean hasChildEqualsToDefaultValue(CtAnnotationMethod ctAnnotationMethod) { - final AnnotationMethodDeclaration parent = (AnnotationMethodDeclaration) jdtTreeBuilder.getContextBuilder().stack.peek().node; + final AnnotationMethodDeclaration parent = (AnnotationMethodDeclaration) jdtTreeBuilder.getContextBuilder().getCurrentNode(); // Default value is equals to the jdt child. return parent.defaultValue != null && parent.defaultValue.equals(childJDT) // Default value not yet initialized. @@ -444,7 +444,7 @@ public void visitCtBinaryOperator(CtBinaryOperator operator) { } operator.setRightHandOperand((CtExpression) child); return; - } else if (jdtTreeBuilder.getContextBuilder().stack.peek().node instanceof StringLiteralConcatenation) { + } else if (jdtTreeBuilder.getContextBuilder().getCurrentNode() instanceof StringLiteralConcatenation) { CtBinaryOperator op = operator.getFactory().Core().createBinaryOperator(); op.setKind(BinaryOperatorKind.PLUS); op.setLeftHandOperand(operator.getLeftHandOperand()); @@ -478,7 +478,7 @@ public void visitCtBreak(CtBreak b) { @Override public void visitCtCase(CtCase caseStatement) { - final ASTNode node = jdtTreeBuilder.getContextBuilder().stack.peek().node; + final ASTNode node = jdtTreeBuilder.getContextBuilder().getCurrentNode(); if (node instanceof CaseStatement) { caseStatement.setCaseKind(((CaseStatement) node).isExpr ? CaseKind.ARROW : CaseKind.COLON); } @@ -511,7 +511,7 @@ public void visitCtCatch(CtCatch catchBlock) { @Override public void visitCtCatchVariable(CtCatchVariable e) { - if (jdtTreeBuilder.getContextBuilder().stack.peekFirst().node instanceof UnionTypeReference) { + if (jdtTreeBuilder.getContextBuilder().getCurrentNode() instanceof UnionTypeReference) { e.addMultiType((CtTypeReference) child); return; } @@ -613,10 +613,10 @@ public void visitCtFor(CtFor forLoop) { } private boolean isContainedInForInit() { - if (!(jdtTreeBuilder.getContextBuilder().stack.peek().node instanceof ForStatement)) { + if (!(jdtTreeBuilder.getContextBuilder().getCurrentNode() instanceof ForStatement)) { return false; } - final ForStatement parent = (ForStatement) jdtTreeBuilder.getContextBuilder().stack.peek().node; + final ForStatement parent = (ForStatement) jdtTreeBuilder.getContextBuilder().getCurrentNode(); if (parent.initializations == null) { return false; } @@ -629,10 +629,10 @@ private boolean isContainedInForInit() { } private boolean isContainedInForUpdate() { - if (!(jdtTreeBuilder.getContextBuilder().stack.peek().node instanceof ForStatement)) { + if (!(jdtTreeBuilder.getContextBuilder().getCurrentNode() instanceof ForStatement)) { return false; } - final ForStatement parent = (ForStatement) jdtTreeBuilder.getContextBuilder().stack.peek().node; + final ForStatement parent = (ForStatement) jdtTreeBuilder.getContextBuilder().getCurrentNode(); if (parent.increments == null) { return false; } @@ -645,10 +645,10 @@ private boolean isContainedInForUpdate() { } private boolean isContainedInForCondition() { - if (!(jdtTreeBuilder.getContextBuilder().stack.peek().node instanceof ForStatement)) { + if (!(jdtTreeBuilder.getContextBuilder().getCurrentNode() instanceof ForStatement)) { return false; } - final ForStatement parent = (ForStatement) jdtTreeBuilder.getContextBuilder().stack.peek().node; + final ForStatement parent = (ForStatement) jdtTreeBuilder.getContextBuilder().getCurrentNode(); return parent.condition != null && parent.condition.equals(childJDT); } @@ -785,10 +785,10 @@ private boolean setTargetFromUnqualifiedAccess(CtInvocation invocation) { } private boolean hasChildEqualsToQualification(CtInvocation ctInvocation) { - if (!(jdtTreeBuilder.getContextBuilder().stack.peek().node instanceof ExplicitConstructorCall)) { + if (!(jdtTreeBuilder.getContextBuilder().getCurrentNode() instanceof ExplicitConstructorCall)) { return false; } - final ExplicitConstructorCall parent = (ExplicitConstructorCall) jdtTreeBuilder.getContextBuilder().stack.peek().node; + final ExplicitConstructorCall parent = (ExplicitConstructorCall) jdtTreeBuilder.getContextBuilder().getCurrentNode(); // qualification is equals to the jdt child. return parent.qualification != null && getFinalExpressionFromCast(parent.qualification).equals(childJDT) // qualification not yet initialized. @@ -796,10 +796,10 @@ private boolean hasChildEqualsToQualification(CtInvocation ctInvocation) } private boolean hasChildEqualsToReceiver(CtInvocation ctInvocation) { - if (!(jdtTreeBuilder.getContextBuilder().stack.peek().node instanceof MessageSend)) { + if (!(jdtTreeBuilder.getContextBuilder().getCurrentNode() instanceof MessageSend)) { return false; } - final MessageSend parent = (MessageSend) jdtTreeBuilder.getContextBuilder().stack.peek().node; + final MessageSend parent = (MessageSend) jdtTreeBuilder.getContextBuilder().getCurrentNode(); // Receiver is equals to the jdt child. return parent.receiver != null && getFinalExpressionFromCast(parent.receiver).equals(childJDT) // Receiver not yet initialized. @@ -816,12 +816,12 @@ private Expression getFinalExpressionFromCast(Expression potentialCase) { @Override public void visitCtNewArray(CtNewArray newArray) { if (childJDT instanceof TypeReference && child instanceof CtTypeAccess) { - final ArrayAllocationExpression arrayAlloc = (ArrayAllocationExpression) jdtTreeBuilder.getContextBuilder().stack.peek().node; + final ArrayAllocationExpression arrayAlloc = (ArrayAllocationExpression) jdtTreeBuilder.getContextBuilder().getCurrentNode(); newArray.setType((CtArrayTypeReference) jdtTreeBuilder.getFactory().Type().createArrayReference(((CtTypeAccess) child).getAccessedType(), arrayAlloc.dimensions.length)); } else if (child instanceof CtExpression) { if (isContainedInDimensionExpression()) { newArray.addDimensionExpression((CtExpression) child); - } else if (child instanceof CtNewArray && childJDT instanceof ArrayInitializer && jdtTreeBuilder.getContextBuilder().stack.peek().node instanceof ArrayAllocationExpression) { + } else if (child instanceof CtNewArray && childJDT instanceof ArrayInitializer && jdtTreeBuilder.getContextBuilder().getCurrentNode() instanceof ArrayAllocationExpression) { newArray.setElements(((CtNewArray) child).getElements()); } else { newArray.addElement((CtExpression) child); @@ -830,10 +830,10 @@ public void visitCtNewArray(CtNewArray newArray) { } private boolean isContainedInDimensionExpression() { - if (!(jdtTreeBuilder.getContextBuilder().stack.peek().node instanceof ArrayAllocationExpression)) { + if (!(jdtTreeBuilder.getContextBuilder().getCurrentNode() instanceof ArrayAllocationExpression)) { return false; } - final ArrayAllocationExpression parent = (ArrayAllocationExpression) jdtTreeBuilder.getContextBuilder().stack.peek().node; + final ArrayAllocationExpression parent = (ArrayAllocationExpression) jdtTreeBuilder.getContextBuilder().getCurrentNode(); if (parent.dimensions == null) { return false; } @@ -866,10 +866,10 @@ public void visitCtConstructorCall(CtConstructorCall ctConstructorCall) { } private boolean hasChildEqualsToEnclosingInstance(CtConstructorCall ctConstructorCall) { - if (!(jdtTreeBuilder.getContextBuilder().stack.peek().node instanceof QualifiedAllocationExpression)) { + if (!(jdtTreeBuilder.getContextBuilder().getCurrentNode() instanceof QualifiedAllocationExpression)) { return false; } - final QualifiedAllocationExpression parent = (QualifiedAllocationExpression) jdtTreeBuilder.getContextBuilder().stack.peek().node; + final QualifiedAllocationExpression parent = (QualifiedAllocationExpression) jdtTreeBuilder.getContextBuilder().getCurrentNode(); // Enclosing instance is equals to the jdt child. return parent.enclosingInstance != null && getFinalExpressionFromCast(parent.enclosingInstance).equals(childJDT) // Enclosing instance not yet initialized. @@ -877,7 +877,7 @@ private boolean hasChildEqualsToEnclosingInstance(CtConstructorCall ctCon } private boolean hasChildEqualsToType(CtConstructorCall ctConstructorCall) { - final AllocationExpression parent = (AllocationExpression) jdtTreeBuilder.getContextBuilder().stack.peek().node; + final AllocationExpression parent = (AllocationExpression) jdtTreeBuilder.getContextBuilder().getCurrentNode(); // Type is equals to the jdt child. return parent.type != null && parent.type.equals(childJDT); } @@ -887,7 +887,7 @@ private boolean hasChildEqualsToType(CtConstructorCall ctConstructorCall) public void visitCtNewClass(CtNewClass newClass) { if (child instanceof CtClass) { newClass.setAnonymousClass((CtClass) child); - final QualifiedAllocationExpression node = (QualifiedAllocationExpression) jdtTreeBuilder.getContextBuilder().stack.peek().node; + final QualifiedAllocationExpression node = (QualifiedAllocationExpression) jdtTreeBuilder.getContextBuilder().getCurrentNode(); final ReferenceBinding[] referenceBindings = node.resolvedType == null ? null : node.resolvedType.superInterfaces(); if (referenceBindings != null && referenceBindings.length > 0) { //the interface of anonymous class is not printed so it must have no position @@ -1064,7 +1064,7 @@ public void visitCtTryWithResource(CtTryWithResource tryWithResource) { tryWithResource.addResource((CtResource) variableRef.getDeclaration().clone().setImplicit(true)); } else { // we have to find it manually - for (ASTPair pair: this.jdtTreeBuilder.getContextBuilder().stack) { + for (ASTPair pair: this.jdtTreeBuilder.getContextBuilder().getAllContexts()) { final List variables = pair.element.getElements(new TypeFilter<>(CtLocalVariable.class)); for (CtLocalVariable v: variables) { if (v.getSimpleName().equals(variableRef.getSimpleName())) { diff --git a/src/main/java/spoon/support/compiler/jdt/PositionBuilder.java b/src/main/java/spoon/support/compiler/jdt/PositionBuilder.java index 5872a4a5b5c..9f353111e61 100644 --- a/src/main/java/spoon/support/compiler/jdt/PositionBuilder.java +++ b/src/main/java/spoon/support/compiler/jdt/PositionBuilder.java @@ -465,7 +465,7 @@ SourcePosition buildPositionCtElement(CtElement e, ASTNode node) { } private int getParentsSourceStart() { - Iterator iter = this.jdtTreeBuilder.getContextBuilder().stack.iterator(); + Iterator iter = this.jdtTreeBuilder.getContextBuilder().getAllContexts().iterator(); if (iter.hasNext()) { iter.next(); if (iter.hasNext()) { diff --git a/src/main/java/spoon/support/compiler/jdt/ReferenceBuilder.java b/src/main/java/spoon/support/compiler/jdt/ReferenceBuilder.java index f33328de4ba..d8db2f91b48 100644 --- a/src/main/java/spoon/support/compiler/jdt/ReferenceBuilder.java +++ b/src/main/java/spoon/support/compiler/jdt/ReferenceBuilder.java @@ -90,7 +90,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.Deque; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -673,14 +672,14 @@ private void insertGenericTypesInNoClasspathFromJDTInSpoon(TypeReference ori * See #3360 for details. */ private void tryRecoverTypeArguments(CtTypeReference type) { - final Deque stack = jdtTreeBuilder.getContextBuilder().stack; - if (stack.peek() == null || !(stack.peek().node instanceof AllocationExpression)) { + ContextBuilder contextBuilder = jdtTreeBuilder.getContextBuilder(); + if (!contextBuilder.hasCurrentContext() || !(contextBuilder.getCurrentNode() instanceof AllocationExpression)) { // have thus far only ended up here with a generic array type, // don't know if we want or need to deal with those return; } - AllocationExpression alloc = (AllocationExpression) stack.peek().node; + AllocationExpression alloc = (AllocationExpression) contextBuilder.getCurrentNode(); if (alloc.expectedType() instanceof ParameterizedTypeBinding) { ParameterizedTypeBinding expectedType = (ParameterizedTypeBinding) alloc.expectedType(); if (expectedType.typeArguments() != null) { @@ -1313,7 +1312,7 @@ private static boolean containsStarImport(ImportReference[] imports) { */ public CtExecutableReference getLambdaExecutableReference(SingleNameReference singleNameReference) { ASTPair potentialLambda = null; - for (ASTPair astPair : jdtTreeBuilder.getContextBuilder().stack) { + for (ASTPair astPair : jdtTreeBuilder.getContextBuilder().getAllContexts()) { if (astPair.node instanceof LambdaExpression) { potentialLambda = astPair; // stop at innermost lambda, fixes #1100