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(clone): Scan declaring executable of CtParameterRef. #730

Merged
merged 1 commit into from
Jun 29, 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
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ public <T> void visitCtParameterReference(final spoon.reflect.reference.CtParame
enter(reference);
biScan(reference.getType(), other.getType());
biScan(reference.getAnnotations(), other.getAnnotations());
biScan(reference.getDeclaringExecutable(), other.getDeclaringExecutable());
exit(reference);
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/spoon/reflect/visitor/CtScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,7 @@ public <T> void visitCtParameterReference(final CtParameterReference<T> referenc
enter(reference);
scan(reference.getType());
scan(reference.getAnnotations());
scan(reference.getDeclaringExecutable());
exit(reference);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ public <T> void visitCtParameterReference(final spoon.reflect.reference.CtParame
spoon.reflect.reference.CtParameterReference<T> aCtParameterReference = spoon.support.visitor.clone.CloneBuilder.build(this.builder, reference, reference.getFactory().Core().createParameterReference());
aCtParameterReference.setType(spoon.support.visitor.equals.CloneHelper.clone(reference.getType()));
aCtParameterReference.setAnnotations(spoon.support.visitor.equals.CloneHelper.clone(reference.getAnnotations()));
aCtParameterReference.setDeclaringExecutable(spoon.support.visitor.equals.CloneHelper.clone(reference.getDeclaringExecutable()));
this.other = aCtParameterReference;
}

Expand Down
19 changes: 10 additions & 9 deletions src/main/java/spoon/support/visitor/equals/EqualsVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public static boolean equals(spoon.reflect.declaration.CtElement element, spoon.

private final spoon.support.visitor.equals.EqualsChecker checker = new spoon.support.visitor.equals.EqualsChecker();

public <A extends java.lang.annotation.Annotation> void visitCtAnnotation(final spoon.reflect.declaration.CtAnnotation<A> annotation) {
spoon.reflect.declaration.CtAnnotation other = ((spoon.reflect.declaration.CtAnnotation) (stack.peek()));
enter(annotation);
biScan(annotation.getAnnotationType(), other.getAnnotationType());
biScan(annotation.getAnnotations(), other.getAnnotations());
biScan(annotation.getValues().values(), other.getValues().values());
exit(annotation);
}

@java.lang.Override
protected void enter(spoon.reflect.declaration.CtElement e) {
super.enter(e);
Expand All @@ -41,15 +50,6 @@ protected void enter(spoon.reflect.declaration.CtElement e) {
}
}

public <A extends java.lang.annotation.Annotation> void visitCtAnnotation(final spoon.reflect.declaration.CtAnnotation<A> annotation) {
spoon.reflect.declaration.CtAnnotation other = ((spoon.reflect.declaration.CtAnnotation) (stack.peek()));
enter(annotation);
biScan(annotation.getAnnotationType(), other.getAnnotationType());
biScan(annotation.getAnnotations(), other.getAnnotations());
biScan(annotation.getValues().values(), other.getValues().values());
exit(annotation);
}

public <A extends java.lang.annotation.Annotation> void visitCtAnnotationType(final spoon.reflect.declaration.CtAnnotationType<A> annotationType) {
spoon.reflect.declaration.CtAnnotationType other = ((spoon.reflect.declaration.CtAnnotationType) (stack.peek()));
enter(annotationType);
Expand Down Expand Up @@ -494,6 +494,7 @@ public <T> void visitCtParameterReference(final spoon.reflect.reference.CtParame
enter(reference);
biScan(reference.getType(), other.getType());
biScan(reference.getAnnotations(), other.getAnnotations());
biScan(reference.getDeclaringExecutable(), other.getDeclaringExecutable());
exit(reference);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,19 @@ public void set(java.util.Set replace) {
}
}

class CtParameterReferenceDeclaringExecutableReplaceListener implements spoon.generating.replace.ReplaceListener<spoon.reflect.reference.CtExecutableReference> {
private spoon.reflect.reference.CtParameterReference element;

CtParameterReferenceDeclaringExecutableReplaceListener(spoon.reflect.reference.CtParameterReference element) {
this.element = element;
}

@java.lang.Override
public void set(spoon.reflect.reference.CtExecutableReference replace) {
this.element.setDeclaringExecutable(replace);
}
}

class CtRHSReceiverAssignmentReplaceListener implements spoon.generating.replace.ReplaceListener<spoon.reflect.code.CtExpression> {
private spoon.reflect.code.CtRHSReceiver element;

Expand Down Expand Up @@ -1161,7 +1174,7 @@ private <T extends spoon.reflect.declaration.CtElement> void replaceInListIfExis
}
if (shouldBeDeleted != null) {
if ((replace) != null) {
list.set(index, (T) replace);
list.set(index, ((T) (replace)));
replace.setParent(shouldBeDeleted.getParent());
} else {
list.remove(index);
Expand Down Expand Up @@ -1536,6 +1549,7 @@ public <T> void visitCtParameter(final spoon.reflect.declaration.CtParameter<T>
public <T> void visitCtParameterReference(final spoon.reflect.reference.CtParameterReference<T> reference) {
replaceElementIfExist(reference.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtVariableReferenceTypeReplaceListener(reference));
replaceInListIfExist(reference.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(reference));
replaceElementIfExist(reference.getDeclaringExecutable(), new spoon.support.visitor.replace.ReplacementVisitor.CtParameterReferenceDeclaringExecutableReplaceListener(reference));
}

public <R> void visitCtReturn(final spoon.reflect.code.CtReturn<R> returnStatement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import spoon.Launcher;
import spoon.compiler.SpoonCompiler;
import spoon.compiler.SpoonResourceHelper;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.declaration.CtMethod;
Expand All @@ -16,6 +17,7 @@
import spoon.reflect.visitor.filter.NameFilter;
import spoon.reflect.visitor.filter.ReferenceTypeFilter;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -70,7 +72,7 @@ public void testReferencesBetweenConstructors() throws Exception {
public void testReferencesBetweenConstructorsInOtherClass() throws Exception {
final List<CtConstructor<?>> constructors = getConstructorsByClass("MyClass2");
final CtConstructor<?> ctConstructor = constructors.get(0);
final List<CtExecutableReference<?>> refConstructors = getCtConstructorsByCtConstructor(ctConstructor);
final List<CtExecutableReference<?>> refConstructors = getCtConstructorsReferencedInCtConstructor(ctConstructor);

final CtClass<?> clazz1 = getCtClassByName("MyClass");
final CtConstructor<?> emptyConstructorClass1 = getConstructorsByClass(clazz1.getSimpleName()).get(0);
Expand Down Expand Up @@ -229,7 +231,7 @@ public boolean matches(CtExecutableReference<?> reference) {
}
});

assertEquals(10, refsExecutableClass1.size());
assertEquals(11, refsExecutableClass1.size());
for (CtExecutableReference<?> ref : refsExecutableClass1) {
assertNotNull(ref);
if (!ref.toString().equals("java.lang.Object#Object()")) {
Expand All @@ -255,8 +257,27 @@ public boolean matches(CtConstructor<?> element) {
});
}

private List<CtExecutableReference<?>> getCtConstructorsByCtConstructor(CtConstructor<?> emptyConstructor) {
return emptyConstructor.getReferences(new AbstractReferenceFilter<CtExecutableReference<?>>(CtExecutableReference.class) {
private List<CtExecutableReference<?>> getCtConstructorsByCtConstructor(CtConstructor<?> aConstructor) {
if (aConstructor.getBody().getStatements().size() == 0) {
return new ArrayList<>();
}
if (!(aConstructor.getBody().getStatement(0) instanceof CtInvocation)) {
return new ArrayList<>();
}
final CtInvocation inv = aConstructor.getBody().getStatement(0);
if (!inv.getExecutable().getSimpleName().equals(CtExecutableReference.CONSTRUCTOR_NAME)) {
return new ArrayList<>();
}
return inv.getExecutable().getReferences(new AbstractReferenceFilter<CtExecutableReference<?>>(CtExecutableReference.class) {
@Override
public boolean matches(CtExecutableReference<?> reference) {
return reference.isConstructor();
}
});
}

private List<CtExecutableReference<?>> getCtConstructorsReferencedInCtConstructor(CtConstructor<?> aConstructor) {
return aConstructor.getReferences(new AbstractReferenceFilter<CtExecutableReference<?>>(CtExecutableReference.class) {
@Override
public boolean matches(CtExecutableReference<?> reference) {
return reference.isConstructor();
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/spoon/test/reference/VariableAccessTest.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package spoon.test.reference;

import org.junit.Test;
import spoon.Launcher;
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.reference.CtVariableReference;
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;
import static org.junit.Assert.fail;
import static spoon.testing.utils.ModelUtils.build;

public class VariableAccessTest {
Expand Down Expand Up @@ -47,4 +50,24 @@ public void name() throws Exception {

assertEquals(expected, ((CtVariableAccess) ctArrayWrite.getTarget()).getVariable().getDeclaration());
}

@Test
public void testDeclarationOfVariableReference() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/noclasspath/Foo2.java");
launcher.getEnvironment().setNoClasspath(true);
launcher.buildModel();

launcher.getModel().getElements(new TypeFilter<CtVariableReference>(CtVariableReference.class) {
@Override
public boolean matches(CtVariableReference element) {
try {
element.clone().getDeclaration();
} catch (NullPointerException e) {
fail("Fail with " + element.getSimpleName() + " declared in " + element.getParent().getSignature());
}
return super.matches(element);
}
});
}
}
12 changes: 12 additions & 0 deletions src/test/resources/noclasspath/Foo2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Foo2 {
public void foo() {
JButton b = new JButton();
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
}
}
});
}
}