Skip to content

Commit

Permalink
fix bug introduced in #689 (order to iterate with a Deque and Stack a…
Browse files Browse the repository at this point in the history
…re different)
  • Loading branch information
GerardPaligot authored and monperrus committed Jun 16, 2016
1 parent 0f6a4a9 commit 843bbd1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
18 changes: 4 additions & 14 deletions src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1981,12 +1981,7 @@ public List<CtType<?>> getCreatedTypes() {
}

protected <T> CtLocalVariable<T> getLocalVariableDeclaration(final String name) {
List<CtElement> reversedElements = new ArrayList<>(context.stack.size());
for (ASTPair element : context.stack) {
reversedElements.add(0, element.element);
}

for (CtElement element : reversedElements) {
for (ASTPair astPair : context.stack) {
// TODO check if the variable is visible from here

EarlyTerminatingScanner<CtLocalVariable<?>> scanner = new EarlyTerminatingScanner<CtLocalVariable<?>>() {
Expand All @@ -2000,7 +1995,7 @@ public <T> void visitCtLocalVariable(CtLocalVariable<T> localVariable) {
super.visitCtLocalVariable(localVariable);
}
};
element.accept(scanner);
astPair.element.accept(scanner);
CtLocalVariable<T> var = (CtLocalVariable<T>) scanner.getResult();
if (var != null) {
return var;
Expand All @@ -2013,12 +2008,7 @@ public <T> void visitCtLocalVariable(CtLocalVariable<T> localVariable) {
}

protected <T> CtCatchVariable<T> getCatchVariableDeclaration(final String name) {
List<CtElement> reversedElements = new ArrayList<>(context.stack.size());
for (ASTPair element : context.stack) {
reversedElements.add(0, element.element);
}

for (CtElement element : reversedElements) {
for (ASTPair astPair : context.stack) {
EarlyTerminatingScanner<CtCatchVariable<?>> scanner = new EarlyTerminatingScanner<CtCatchVariable<?>>() {
@Override
public <T> void visitCtCatchVariable(CtCatchVariable<T> catchVariable) {
Expand All @@ -2030,7 +2020,7 @@ public <T> void visitCtCatchVariable(CtCatchVariable<T> catchVariable) {
super.visitCtCatchVariable(catchVariable);
}
};
element.accept(scanner);
astPair.element.accept(scanner);

CtCatchVariable<T> var = (CtCatchVariable<T>) scanner.getResult();
if (var != null) {
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/spoon/test/reference/VariableAccessTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package spoon.test.reference;

import org.junit.Test;
import spoon.reflect.code.CtArrayWrite;
import spoon.reflect.code.CtLocalVariable;
import spoon.reflect.code.CtVariableAccess;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtType;
import spoon.reflect.reference.CtParameterReference;
import spoon.reflect.visitor.filter.AbstractReferenceFilter;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.test.reference.testclasses.Pozole;
import spoon.testing.utils.ModelUtils;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
Expand All @@ -29,4 +37,14 @@ public boolean matches(CtParameterReference<?> reference) {
assertNotNull("Declaration of declaring type of the method can't be null", ref.getDeclaringExecutable().getDeclaringType().getDeclaration());
assertNotNull("Declaration of root class can't be null", ref.getDeclaringExecutable().getDeclaringType().getDeclaringType().getDeclaration());
}

@Test
public void name() throws Exception {
final CtType<Pozole> aPozole = ModelUtils.buildClass(Pozole.class);
final CtMethod<Object> m2 = aPozole.getMethod("m2");
final CtArrayWrite<?> ctArrayWrite = m2.getElements(new TypeFilter<CtArrayWrite<?>>(CtArrayWrite.class)).get(0);
final CtLocalVariable expected = m2.getElements(new TypeFilter<CtLocalVariable>(CtLocalVariable.class)).get(0);

assertEquals(expected, ((CtVariableAccess) ctArrayWrite.getTarget()).getVariable().getDeclaration());
}
}
12 changes: 12 additions & 0 deletions src/test/java/spoon/test/reference/testclasses/Pozole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package spoon.test.reference.testclasses;

public class Pozole {
void m() {
double[] repair = new double[45];
}

void m2() {
final double[] repair = new double[1];
repair[0] = 5d;
}
}

0 comments on commit 843bbd1

Please sign in to comment.