Skip to content

Commit

Permalink
Replace CtClass.constructors model from Set to List
Browse files Browse the repository at this point in the history
  • Loading branch information
leventov committed May 4, 2016
1 parent d44aebe commit 3832e2b
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public final class ModelElementContainerDefaultCapacities {
* than ArrayList's default of 10.
*/

// JDK 7 average is 1.467
public static final int CONSTRUCTORS_DEFAULT_CAPACITY = 2;

// JDK 7 average is 1.063 (methods), 1.207 (constructors)
public static final int PARAMETERS_CONTAINER_DEFAULT_CAPACITY = 2;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/spoon/reflect/declaration/CtClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public interface CtClass<T extends Object> extends CtType<T>, CtStatement {
* Returns the constructors of this class. This includes the default
* constructor if this class has no constructors explicitly declared.
*/
Set<CtConstructor<T>> getConstructors();
List<CtConstructor<T>> getConstructors();

/**
* Sets the anonymous blocks of this class.
Expand All @@ -74,7 +74,7 @@ public interface CtClass<T extends Object> extends CtType<T>, CtStatement {
/**
* Sets the constructors for this class.
*/
<C extends CtClass<T>> C setConstructors(Set<CtConstructor<T>> constructors);
<C extends CtClass<T>> C setConstructors(List<CtConstructor<T>> constructors);

/**
* Adds a constructor to this class.
Expand Down
22 changes: 9 additions & 13 deletions src/main/java/spoon/support/reflect/declaration/CtClassImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import static spoon.reflect.ModelElementContainerDefaultCapacities.ANONYMOUS_EXECUTABLES_CONTAINER_DEFAULT_CAPACITY;
import static spoon.reflect.ModelElementContainerDefaultCapacities.CONSTRUCTORS_DEFAULT_CAPACITY;

/**
* The implementation for {@link spoon.reflect.declaration.CtClass}.
Expand All @@ -49,7 +48,7 @@ public class CtClassImpl<T extends Object> extends CtTypeImpl<T> implements CtCl

List<CtAnonymousExecutable> anonymousExecutables = emptyList();

Set<CtConstructor<T>> constructors = emptySet();
List<CtConstructor<T>> constructors = emptyList();

CtTypeReference<?> superClass;

Expand Down Expand Up @@ -80,7 +79,7 @@ public CtConstructor<T> getConstructor(CtTypeReference<?>... parameterTypes) {
}

@Override
public Set<CtConstructor<T>> getConstructors() {
public List<CtConstructor<T>> getConstructors() {
return constructors;
}

Expand Down Expand Up @@ -120,9 +119,9 @@ public <C extends CtClass<T>> C setAnonymousExecutables(List<CtAnonymousExecutab
}

@Override
public <C extends CtClass<T>> C setConstructors(Set<CtConstructor<T>> constructors) {
if (this.constructors == CtElementImpl.<CtConstructor<T>>emptySet()) {
this.constructors = new TreeSet<CtConstructor<T>>();
public <C extends CtClass<T>> C setConstructors(List<CtConstructor<T>> constructors) {
if (this.constructors == CtElementImpl.<CtConstructor<T>>emptyList()) {
this.constructors = new ArrayList<CtConstructor<T>>(constructors.size());
}
this.constructors.clear();
for (CtConstructor<T> constructor : constructors) {
Expand All @@ -133,12 +132,9 @@ public <C extends CtClass<T>> C setConstructors(Set<CtConstructor<T>> constructo

@Override
public <C extends CtClass<T>> C addConstructor(CtConstructor<T> constructor) {
if (constructors == CtElementImpl.<CtConstructor<T>>emptySet()) {
constructors = new TreeSet<CtConstructor<T>>();
if (constructors == CtElementImpl.<CtConstructor<T>>emptyList()) {
constructors = new ArrayList<CtConstructor<T>>(CONSTRUCTORS_DEFAULT_CAPACITY);
}
// this needs to be done because of the set that needs the constructor's
// signature : we should use lists!!!
// TODO: CHANGE SETS TO LIST TO AVOID HAVING TO DO THIS
constructor.setParent(this);
constructors.add(constructor);
return (C) this;
Expand All @@ -149,7 +145,7 @@ public void removeConstructor(CtConstructor<T> constructor) {
if (!constructors.isEmpty()) {
if (constructors.size() == 1) {
if (constructors.contains(constructor)) {
constructors = CtElementImpl.<CtConstructor<T>>emptySet();
constructors = CtElementImpl.<CtConstructor<T>>emptyList();
}
} else {
constructors.remove(constructor);
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/spoon/support/template/SubstitutionVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Iterator;
import java.util.TreeSet;

class SkipException extends SpoonException {
Expand Down Expand Up @@ -186,9 +187,10 @@ public <T> void visitCtClass(CtClass<T> ctClass) {
ctClass.removeMethod(m);
}
}
for (CtConstructor<?> c : new TreeSet<CtConstructor<?>>(ctClass.getConstructors())) {
for (Iterator<CtConstructor<T>> it = ctClass.getConstructors().iterator(); it.hasNext();) {
CtConstructor<?> c = it.next();
if (c.getAnnotation(Local.class) != null) {
ctClass.getConstructors().remove(c);
it.remove();
}
}
for (CtField<?> field : new TreeSet<CtField<?>>(ctClass.getFields())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,15 @@ public void set(java.util.List replace) {
}
}

class CtClassConstructorsReplaceListener implements spoon.generating.replace.ReplaceSetListener<java.util.Set> {
class CtClassConstructorsReplaceListener implements spoon.generating.replace.ReplaceListListener<java.util.List> {
private spoon.reflect.declaration.CtClass element;

CtClassConstructorsReplaceListener(spoon.reflect.declaration.CtClass element) {
this.element = element;
}

@java.lang.Override
public void set(java.util.Set replace) {
public void set(java.util.List replace) {
this.element.setConstructors(replace);
}
}
Expand Down Expand Up @@ -1295,7 +1295,7 @@ public <T> void visitCtClass(final spoon.reflect.declaration.CtClass<T> ctClass)
replaceInListIfExist(ctClass.getAnonymousExecutables(), new spoon.support.visitor.replace.ReplacementVisitor.CtClassAnonymousExecutablesReplaceListener(ctClass));
replaceInSetIfExist(ctClass.getNestedTypes(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeNestedTypesReplaceListener(ctClass));
replaceInListIfExist(ctClass.getFields(), new spoon.support.visitor.replace.ReplacementVisitor.CtClassFieldsReplaceListener(ctClass));
replaceInSetIfExist(ctClass.getConstructors(), new spoon.support.visitor.replace.ReplacementVisitor.CtClassConstructorsReplaceListener(ctClass));
replaceInListIfExist(ctClass.getConstructors(), new spoon.support.visitor.replace.ReplacementVisitor.CtClassConstructorsReplaceListener(ctClass));
replaceInSetIfExist(ctClass.getMethods(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeMethodsReplaceListener(ctClass));
replaceInListIfExist(ctClass.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(ctClass));
}
Expand Down Expand Up @@ -1334,7 +1334,7 @@ public <T extends java.lang.Enum<?>> void visitCtEnum(final spoon.reflect.declar
replaceInListIfExist(ctEnum.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(ctEnum));
replaceInSetIfExist(ctEnum.getSuperInterfaces(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeInformationSuperInterfacesReplaceListener(ctEnum));
replaceInListIfExist(ctEnum.getFields(), new spoon.support.visitor.replace.ReplacementVisitor.CtClassFieldsReplaceListener(ctEnum));
replaceInSetIfExist(ctEnum.getConstructors(), new spoon.support.visitor.replace.ReplacementVisitor.CtClassConstructorsReplaceListener(ctEnum));
replaceInListIfExist(ctEnum.getConstructors(), new spoon.support.visitor.replace.ReplacementVisitor.CtClassConstructorsReplaceListener(ctEnum));
replaceInSetIfExist(ctEnum.getMethods(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeMethodsReplaceListener(ctEnum));
replaceInSetIfExist(ctEnum.getNestedTypes(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeNestedTypesReplaceListener(ctEnum));
replaceInListIfExist(ctEnum.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(ctEnum));
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/spoon/test/annotation/AnnotationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public void testAnnotatedElementTypes() throws Exception {
assertEquals(CtAnnotatedElementType.PARAMETER, annotations.get(0).getAnnotatedElementType());

// load constructor of the clazz and check annotated element type of the constructor annotation
Set<? extends CtConstructor<?>> constructors = clazz.getConstructors();
List<? extends CtConstructor<?>> constructors = clazz.getConstructors();
assertEquals(1, constructors.size());

CtConstructor<?> constructor = constructors.iterator().next();
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/spoon/test/ctClass/CtClassTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import spoon.test.ctClass.testclasses.Foo;
import spoon.test.ctClass.testclasses.Pozole;

import java.util.List;
import java.util.Set;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -98,7 +99,7 @@ public void testAllTypeReferencesToALocalTypeShouldNotStartWithNumber() throws E

assertEquals("1Cook", cook.getSimpleName());
assertEquals("spoon.test.ctClass.testclasses.Pozole$1Cook", cook.getQualifiedName());
final Set<? extends CtConstructor<?>> constructors = cook.getConstructors();
final List<? extends CtConstructor<?>> constructors = cook.getConstructors();
final String expectedConstructor = "public Cook() {" + System.lineSeparator() + "}";
assertEquals(expectedConstructor, constructors.toArray(new CtConstructor[constructors.size()])[0].toString());
assertEquals("final java.lang.Class<Cook> cookClass = Cook.class", cook.getMethod("m").getBody().getStatement(0).toString());
Expand Down

0 comments on commit 3832e2b

Please sign in to comment.