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

review: alternative to #1589 #1667

Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public interface CtTypeInformation {
* getSuperClass().getDeclaration()/getTypeDeclaration() returns the corresponding CtType (if in the source folder of Spoon).
*
* @return the class type directly extended by this class, or null if there
* is none
* is none or if the super class is not in the classpath (in noclasspath mode)
*/
@PropertyGetter(role = SUPER_TYPE)
CtTypeReference<?> getSuperclass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,24 +497,15 @@ public Set<ModifierKind> getModifiers() {

@Override
public CtTypeReference<?> getSuperclass() {
CtType<T> t = getDeclaration();
if (t != null) {
return t.getSuperclass();
} else {
try {
Class<T> c = getActualClass();
Class<?> sc = c.getSuperclass();
if (sc == null) {
return null;
}
return getFactory().Type().createReference(sc);
} catch (final SpoonClassNotFoundException e) {
if (getFactory().getEnvironment().getNoClasspath()) {
return null;
}
throw e;
try {
CtType<T> t = getTypeDeclaration();
if (t != null) {
return t.getSuperclass();
}
} catch (SpoonClassNotFoundException e) {
return null;
}
return null;
}

@Override
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/spoon/support/visitor/equals/CloneHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
*/
package spoon.support.visitor.equals;

import spoon.SpoonException;
import spoon.reflect.declaration.CtElement;
import spoon.support.util.EmptyClearableList;
import spoon.support.util.EmptyClearableSet;
import spoon.support.visitor.clone.CloneVisitor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand All @@ -24,12 +30,6 @@
import java.util.Set;
import java.util.TreeSet;

import spoon.SpoonException;
import spoon.reflect.declaration.CtElement;
import spoon.support.util.EmptyClearableList;
import spoon.support.util.EmptyClearableSet;
import spoon.support.visitor.clone.CloneVisitor;

/**
* {@link CloneHelper} is responsible for creating clones of {@link CtElement} AST nodes including the whole subtree.
*
Expand Down
62 changes: 62 additions & 0 deletions src/test/java/spoon/test/ctClass/CtClassTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;

import static org.junit.Assert.assertTrue;
import static spoon.testing.utils.ModelUtils.build;
import static spoon.testing.utils.ModelUtils.buildClass;
import static spoon.testing.utils.ModelUtils.canBeBuilt;
Expand All @@ -19,8 +20,11 @@
import org.junit.Test;

import spoon.Launcher;
import spoon.reflect.CtModel;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtConstructorCall;
import spoon.reflect.code.CtNewClass;
import spoon.reflect.declaration.CtAnonymousExecutable;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.declaration.CtField;
Expand All @@ -29,6 +33,8 @@
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtArrayTypeReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.test.ctClass.testclasses.AnonymousClass;
import spoon.test.ctClass.testclasses.Foo;
import spoon.test.ctClass.testclasses.Pozole;

Expand Down Expand Up @@ -179,4 +185,60 @@ public void testDefaultConstructorAreOk() throws Exception {

canBeBuilt("./target/issue1306", 8, true);
}

@Test
public void testCloneAnonymousClassInvocation() {
// contract: after cloning an anonymous class invocation, we still should be able to print it, when not using autoimport

final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/ctClass/testclasses/AnonymousClass.java");
launcher.getEnvironment().setAutoImports(false);
launcher.buildModel();

CtModel model = launcher.getModel();
CtNewClass newClassInvocation = launcher.getModel().getElements(new TypeFilter<CtNewClass>(CtNewClass.class)).get(0);
CtNewClass newClassInvocationCloned = newClassInvocation.clone();

CtClass anonymousClass = newClassInvocation.getAnonymousClass();
CtClass anonymousClassCloned = newClassInvocationCloned.getAnonymousClass();

// The test stops failing if we set the parent below
//newClassInvocationCloned.setParent(launcher.getFactory().Class().get(AnonymousClass.class));

assertEquals(0, anonymousClass.getAllFields().size());
assertEquals(0, anonymousClassCloned.getAllFields().size());

assertTrue(newClassInvocation.toString().length() > 0);
assertTrue(newClassInvocationCloned.toString().length() > 0);

assertEquals(newClassInvocation.toString(), newClassInvocationCloned.toString());
}

@Test
public void testCloneAnonymousClassInvocationWithAutoimports() {
// contract: after cloning an anonymous class invocation, we still should be able to print it, when using autoimport

final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/ctClass/testclasses/AnonymousClass.java");
launcher.getEnvironment().setAutoImports(true);
launcher.buildModel();

CtModel model = launcher.getModel();
CtNewClass newClassInvocation = launcher.getModel().getElements(new TypeFilter<CtNewClass>(CtNewClass.class)).get(0);
CtNewClass newClassInvocationCloned = newClassInvocation.clone();

CtClass anonymousClass = newClassInvocation.getAnonymousClass();
CtClass anonymousClassCloned = newClassInvocationCloned.getAnonymousClass();

// The test stops failing if we set the parent below
//newClassInvocationCloned.setParent(launcher.getFactory().Class().get(AnonymousClass.class));

assertEquals(0, anonymousClass.getAllFields().size());
assertEquals(0, anonymousClassCloned.getAllFields().size());

assertTrue(newClassInvocation.toString().length() > 0);
assertTrue(newClassInvocationCloned.toString().length() > 0);

assertEquals(newClassInvocation.toString(), newClassInvocationCloned.toString());
}
}
17 changes: 17 additions & 0 deletions src/test/java/spoon/test/ctClass/testclasses/AnonymousClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package spoon.test.ctClass.testclasses;

import java.util.Comparator;

/**
* Created by urli on 11/10/2017.
*/
public class AnonymousClass {

final int machin = new Comparator<Integer>() {

@Override
public int compare(Integer o1, Integer o2) {
return 0;
}
}.compare(1,2);
}