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(deque): Order to iterate with a Deque and Stack are differents. #710

Merged
merged 1 commit into from
Jun 16, 2016
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
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;
}
}