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 getReference and getDeclaration for local type #888

Merged
merged 4 commits into from
Oct 10, 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
1 change: 1 addition & 0 deletions src/main/java/spoon/reflect/factory/CodeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ public <T> CtLocalVariableReference<T> createLocalVariableReference(CtLocalVaria
CtLocalVariableReference<T> ref = factory.Core().createLocalVariableReference();
ref.setType(localVariable.getType() == null ? null : localVariable.getType().clone());
ref.setSimpleName(localVariable.getSimpleName());
ref.setParent(localVariable);
return ref;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public CtLocalVariable<T> getDeclaration() {
// validating that the parent of a finding is parent of this
// reference as well
for (final CtLocalVariable<T> lv : localVariables) {
if (hasParent(lv.getParent())) {
if (getParent().equals(lv) || hasParent(lv.getParent())) {
return lv;
}
}
Expand Down
31 changes: 27 additions & 4 deletions src/test/java/spoon/test/reference/VariableAccessTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static spoon.testing.utils.ModelUtils.build;
Expand Down Expand Up @@ -53,7 +54,7 @@ public boolean matches(CtParameterReference<?> reference) {
}

@Test
public void name() throws Exception {
public void testDeclarationArray() 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);
Expand Down Expand Up @@ -136,16 +137,36 @@ public void testGetDeclarationAfterClone() throws Exception {
final CtLocalVariableReference localVarRefCloned = getLocalVariableRefF1(methodA2Cloned);

assertEquals(localVarRef.getDeclaration(), declaration);
assertTrue(localVarRef.getDeclaration() == declaration);
assertSame(localVarRef.getDeclaration(), declaration);
assertEquals(localVarRefCloned.getDeclaration(), declarationCloned);
assertTrue(localVarRefCloned.getDeclaration() == declarationCloned);
assertSame(localVarRefCloned.getDeclaration(), declarationCloned);
}

@Test
public void testReferences() throws Exception {

/* test getReference on local variable
* getReference().getDeclaration() must be circular
*/

final CtType<Tortillas> aTortillas = buildClass(Tortillas.class);
final CtMethod<Object> make = aTortillas.getMethod("make", aTortillas.getFactory().Type().stringType());
System.out.println(make);

final CtLocalVariable localVar = make.getBody().getStatement(0);
final CtLocalVariable localVarCloned = localVar.clone();

final CtLocalVariableReference localVarRef = localVar.getReference();
final CtLocalVariableReference localVarRefCloned = localVarCloned.getReference();

assertEquals(localVarRef.getDeclaration(), localVar);
assertSame(localVarRef.getDeclaration(), localVar);
assertEquals(localVar.getReference().getDeclaration(), localVar);
assertSame(localVar.getReference().getDeclaration(), localVar);

assertEquals(localVarRefCloned.getDeclaration(), localVarCloned);
assertSame(localVarRefCloned.getDeclaration(), localVarCloned);
assertEquals(localVarCloned.getReference().getDeclaration(), localVarCloned);
assertSame(localVarCloned.getReference().getDeclaration(), localVarCloned);
}

@Test
Expand All @@ -167,6 +188,7 @@ public <T> void visitCtLocalVariableReference(
launcher.getEnvironment().setNoClasspath(true);
launcher.addInputResource("src/test/resources/reference-test/ChangeScanner.java");
launcher.buildModel();

new CtLocalVariableReferenceScanner().scan(launcher.getModel().getRootPackage());
}

Expand All @@ -190,6 +212,7 @@ public <T> void visitCtLocalVariableReference(
launcher.getEnvironment().setNoClasspath(true);
launcher.addInputResource("src/test/resources/reference-test/MultipleDeclarationsOfLocalVariable.java");
launcher.buildModel();

new CtLocalVariableReferenceScanner().scan(launcher.getModel().getRootPackage());
}

Expand Down