Skip to content

Commit

Permalink
feat(ast): Clean reset of collections in intercession methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
GerardPaligot authored and monperrus committed Jun 16, 2016
1 parent 843bbd1 commit f17381a
Show file tree
Hide file tree
Showing 26 changed files with 280 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public <C extends CtTypedElement> C setType(CtTypeReference<T> type) {

@Override
public <C extends CtExpression<T>> C setTypeCasts(List<CtTypeReference<?>> casts) {
if (casts == null || casts.isEmpty()) {
this.typeCasts = CtElementImpl.emptyList();
return (C) this;
}
if (this.typeCasts == CtElementImpl.<CtTypeReference<?>>emptyList()) {
this.typeCasts = new ArrayList<>(CASTS_CONTAINER_DEFAULT_CAPACITY);
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/spoon/support/reflect/code/CtBlockImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ public <T extends CtBlock<R>> T insertBefore(

@Override
public <T extends CtStatementList> T setStatements(List<CtStatement> statements) {
if (statements == null || statements.isEmpty()) {
this.statements = CtElementImpl.emptyList();
return (T) this;
}
this.statements.clear();
for (CtStatement s : statements) {
addStatement(s);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/spoon/support/reflect/code/CtCaseImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public <T extends CtCase<E>> T setCaseExpression(CtExpression<E> caseExpression)

@Override
public <T extends CtStatementList> T setStatements(List<CtStatement> statements) {
if (statements == null || statements.isEmpty()) {
this.statements = CtElementImpl.emptyList();
return (T) this;
}
this.statements.clear();
for (CtStatement stmt : statements) {
addStatement(stmt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public void replace(CtElement element) {

@Override
public <C extends CtAbstractInvocation<T>> C setArguments(List<CtExpression<?>> arguments) {
if (arguments == null || arguments.isEmpty()) {
this.arguments = CtElementImpl.emptyList();
return (C) this;
}
if (this.arguments == CtElementImpl.<CtExpression<?>>emptyList()) {
this.arguments = new ArrayList<>(PARAMETERS_CONTAINER_DEFAULT_CAPACITY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public <C extends CtTypedElement> C setType(CtTypeReference<T> type) {

@Override
public <C extends CtExpression<T>> C setTypeCasts(List<CtTypeReference<?>> casts) {
if (casts == null || casts.isEmpty()) {
this.typeCasts = CtElementImpl.emptyList();
return (C) this;
}
if (this.typeCasts == CtElementImpl.<CtTypeReference<?>>emptyList()) {
this.typeCasts = new ArrayList<>(CASTS_CONTAINER_DEFAULT_CAPACITY);
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/spoon/support/reflect/code/CtForImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public <T extends CtFor> T addForInit(CtStatement statement) {

@Override
public <T extends CtFor> T setForInit(List<CtStatement> statements) {
if (statements == null || statements.isEmpty()) {
this.forInit = CtElementImpl.emptyList();
return (T) this;
}
this.forInit.clear();
for (CtStatement stmt : statements) {
addForInit(stmt);
Expand Down Expand Up @@ -108,6 +112,10 @@ public <T extends CtFor> T addForUpdate(CtStatement statement) {

@Override
public <T extends CtFor> T setForUpdate(List<CtStatement> statements) {
if (statements == null || statements.isEmpty()) {
this.forUpdate = CtElementImpl.emptyList();
return (T) this;
}
this.forUpdate.clear();
for (CtStatement stmt : statements) {
addForUpdate(stmt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ public <C extends CtStatement> C insertBefore(CtStatementList statements) {

@Override
public <C extends CtAbstractInvocation<T>> C setArguments(List<CtExpression<?>> arguments) {
if (arguments == null || arguments.isEmpty()) {
this.arguments = CtElementImpl.emptyList();
return (C) this;
}
if (this.arguments == CtElementImpl.<CtExpression<?>>emptyList()) {
this.arguments = new ArrayList<>(PARAMETERS_CONTAINER_DEFAULT_CAPACITY);
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/spoon/support/reflect/code/CtLambdaImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public List<CtParameter<?>> getParameters() {

@Override
public <C extends CtExecutable<T>> C setParameters(List<CtParameter<?>> params) {
if (params == null || params.isEmpty()) {
this.parameters = CtElementImpl.emptyList();
return (C) this;
}
if (this.parameters == CtElementImpl.<CtParameter<?>>emptyList()) {
this.parameters = new ArrayList<>(PARAMETERS_CONTAINER_DEFAULT_CAPACITY);
}
Expand Down Expand Up @@ -119,6 +123,10 @@ public Set<CtTypeReference<? extends Throwable>> getThrownTypes() {

@Override
public <C extends CtExecutable<T>> C setThrownTypes(Set<CtTypeReference<? extends Throwable>> thrownTypes) {
if (thrownTypes == null || thrownTypes.isEmpty()) {
this.thrownTypes = CtElementImpl.emptySet();
return (C) this;
}
if (this.thrownTypes == CtElementImpl.<CtTypeReference<? extends Throwable>>emptySet()) {
this.thrownTypes = new TreeSet<>();
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/spoon/support/reflect/code/CtNewArrayImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public List<CtExpression<?>> getElements() {

@Override
public <C extends CtNewArray<T>> C setDimensionExpressions(List<CtExpression<Integer>> dimensionExpressions) {
if (dimensionExpressions == null || dimensionExpressions.isEmpty()) {
this.dimensionExpressions = CtElementImpl.emptyList();
return (C) this;
}
this.dimensionExpressions.clear();
for (CtExpression<Integer> expr : dimensionExpressions) {
addDimensionExpression(expr);
Expand Down Expand Up @@ -79,6 +83,10 @@ public boolean removeDimensionExpression(CtExpression<Integer> dimension) {

@Override
public <C extends CtNewArray<T>> C setElements(List<CtExpression<?>> expressions) {
if (expressions == null || expressions.isEmpty()) {
this.expressions = CtElementImpl.emptyList();
return (C) this;
}
this.expressions.clear();
for (CtExpression<?> expr : expressions) {
addElement(expr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public List<CtStatement> getStatements() {

@Override
public <T extends CtStatementList> T setStatements(List<CtStatement> stmts) {
if (stmts == null || stmts.isEmpty()) {
this.statements = CtElementImpl.emptyList();
return (T) this;
}
this.statements.clear();
for (CtStatement stmt : stmts) {
addStatement(stmt);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/spoon/support/reflect/code/CtSwitchImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public CtExpression<S> getSelector() {

@Override
public <T extends CtSwitch<S>> T setCases(List<CtCase<? super S>> cases) {
if (cases == null || cases.isEmpty()) {
this.cases = CtElementImpl.emptyList();
return (T) this;
}
this.cases.clear();
for (CtCase<? super S> aCase : cases) {
addCase(aCase);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/spoon/support/reflect/code/CtTryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public List<CtCatch> getCatchers() {

@Override
public <T extends CtTry> T setCatchers(List<CtCatch> catchers) {
if (catchers == null || catchers.isEmpty()) {
this.catchers = CtElementImpl.emptyList();
return (T) this;
}
this.catchers.clear();
for (CtCatch c : catchers) {
addCatcher(c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public List<CtLocalVariable<?>> getResources() {

@Override
public <T extends CtTryWithResource> T setResources(List<CtLocalVariable<?>> resources) {
if (resources == null || resources.isEmpty()) {
this.resources = CtElementImpl.emptyList();
return (T) this;
}
this.resources.clear();
for (CtLocalVariable<?> l : resources) {
addResource(l);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ public CtTypeReference<?> getSuperclass() {

@Override
public <C extends CtClass<T>> C setAnonymousExecutables(List<CtAnonymousExecutable> anonymousExecutables) {
if (anonymousExecutables == null || anonymousExecutables.isEmpty()) {
this.anonymousExecutables = CtElementImpl.emptyList();
return (C) this;
}
if (this.anonymousExecutables == CtElementImpl.<CtAnonymousExecutable>emptyList()) {
this.anonymousExecutables = new ArrayList<>(ANONYMOUS_EXECUTABLES_CONTAINER_DEFAULT_CAPACITY);
}
Expand All @@ -122,6 +126,10 @@ public <C extends CtClass<T>> C setAnonymousExecutables(List<CtAnonymousExecutab

@Override
public <C extends CtClass<T>> C setConstructors(Set<CtConstructor<T>> constructors) {
if (constructors == null || constructors.isEmpty()) {
this.constructors = CtElementImpl.emptySet();
return (C) this;
}
if (this.constructors == CtElementImpl.<CtConstructor<T>>emptySet()) {
this.constructors = new TreeSet<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ public <T extends CtFormalTypeDeclarer> T addFormalTypeParameter(CtTypeParameter

@Override
public <T extends CtFormalTypeDeclarer> T setFormalTypeParameters(List<CtTypeParameterReference> formalTypeParameters) {
if (formalTypeParameters == null || formalTypeParameters.isEmpty()) {
this.formalTypeParameters = CtElementImpl.emptyList();
return (T) this;
}
if (this.formalTypeParameters == CtElementImpl.<CtTypeParameterReference>emptyList()) {
this.formalTypeParameters = new ArrayList<>(CONSTRUCTOR_TYPE_PARAMETERS_CONTAINER_DEFAULT_CAPACITY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ public int hashCode() {
}

public <E extends CtElement> E setAnnotations(List<CtAnnotation<? extends Annotation>> annotations) {
if (annotations == null || annotations.isEmpty()) {
this.annotations = CtElementImpl.emptyList();
return (E) this;
}
this.annotations.clear();
for (CtAnnotation<? extends Annotation> annot : annotations) {
addAnnotation(annot);
Expand Down Expand Up @@ -437,6 +441,10 @@ public <E extends CtElement> E removeComment(CtComment comment) {

@Override
public <E extends CtElement> E setComments(List<CtComment> comments) {
if (comments == null || comments.isEmpty()) {
this.comments = CtElementImpl.emptyList();
return (E) this;
}
this.comments.clear();
for (CtComment comment : comments) {
addComment(comment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public List<CtParameter<?>> getParameters() {

@Override
public <T extends CtExecutable<R>> T setParameters(List<CtParameter<?>> parameters) {
if (parameters == null || parameters.isEmpty()) {
this.parameters = CtElementImpl.emptyList();
return (T) this;
}
if (this.parameters == CtElementImpl.<CtParameter<?>>emptyList()) {
this.parameters = new ArrayList<>(PARAMETERS_CONTAINER_DEFAULT_CAPACITY);
}
Expand Down Expand Up @@ -104,6 +108,10 @@ public Set<CtTypeReference<? extends Throwable>> getThrownTypes() {

@Override
public <T extends CtExecutable<R>> T setThrownTypes(Set<CtTypeReference<? extends Throwable>> thrownTypes) {
if (thrownTypes == null || thrownTypes.isEmpty()) {
this.thrownTypes = CtElementImpl.emptySet();
return (T) this;
}
if (this.thrownTypes == CtElementImpl.<CtTypeReference<? extends Throwable>>emptySet()) {
this.thrownTypes = new TreeSet<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ public <T extends CtFormalTypeDeclarer> T addFormalTypeParameter(CtTypeParameter

@Override
public <T extends CtFormalTypeDeclarer> T setFormalTypeParameters(List<CtTypeParameterReference> formalTypeParameters) {
if (formalTypeParameters == null || formalTypeParameters.isEmpty()) {
this.formalTypeParameters = CtElementImpl.emptyList();
return (T) this;
}
if (this.formalTypeParameters == CtElementImpl.<CtTypeParameterReference>emptyList()) {
this.formalTypeParameters = new ArrayList<>(METHOD_TYPE_PARAMETERS_CONTAINER_DEFAULT_CAPACITY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ public Set<CtType<?>> getTypes() {

@Override
public <T extends CtPackage> T setPackages(Set<CtPackage> packs) {
if (packs == null || packs.isEmpty()) {
this.packs = CtElementImpl.emptySet();
return (T) this;
}
this.packs.clear();
for (CtPackage p : packs) {
addPackage(p);
Expand All @@ -159,6 +163,10 @@ public <T extends CtPackage> T setPackages(Set<CtPackage> packs) {

@Override
public <T extends CtPackage> T setTypes(Set<CtType<?>> types) {
if (types == null || types.isEmpty()) {
this.types = CtElementImpl.emptySet();
return (T) this;
}
this.types.clear();
for (CtType<?> t : types) {
addType(t);
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/spoon/support/reflect/declaration/CtTypeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ public <F, C extends CtType<T>> C addField(int index, CtField<F> field) {

@Override
public <C extends CtType<T>> C setFields(List<CtField<?>> fields) {
if (fields == null || fields.isEmpty()) {
this.fields = CtElementImpl.emptyList();
return (C) this;
}
this.fields.clear();
for (CtField<?> field : fields) {
addField(field);
Expand Down Expand Up @@ -182,6 +186,10 @@ public <N> boolean removeNestedType(CtType<N> nestedType) {

@Override
public <C extends CtType<T>> C setNestedTypes(Set<CtType<?>> nestedTypes) {
if (nestedTypes == null || nestedTypes.isEmpty()) {
this.nestedTypes = CtElementImpl.emptySet();
return (C) this;
}
if (this.nestedTypes == CtElementImpl.<CtType<?>>emptySet()) {
this.nestedTypes = new TreeSet<>();
}
Expand Down Expand Up @@ -673,6 +681,10 @@ public Set<CtTypeReference<?>> getSuperInterfaces() {

@Override
public <C extends CtFormalTypeDeclarer> C setFormalTypeParameters(List<CtTypeParameterReference> formalTypeParameters) {
if (formalTypeParameters == null || formalTypeParameters.isEmpty()) {
this.formalTypeParameters = CtElementImpl.emptyList();
return (C) this;
}
if (this.formalTypeParameters == CtElementImpl.<CtTypeParameterReference>emptyList()) {
this.formalTypeParameters = new ArrayList<>(TYPE_TYPE_PARAMETERS_CONTAINER_DEFAULT_CAPACITY);
}
Expand All @@ -685,6 +697,10 @@ public <C extends CtFormalTypeDeclarer> C setFormalTypeParameters(List<CtTypePar

@Override
public <C extends CtType<T>> C setMethods(Set<CtMethod<?>> methods) {
if (methods == null || methods.isEmpty()) {
this.methods = CtElementImpl.emptySet();
return (C) this;
}
this.methods.clear();
for (CtMethod<?> meth : methods) {
addMethod(meth);
Expand All @@ -694,6 +710,10 @@ public <C extends CtType<T>> C setMethods(Set<CtMethod<?>> methods) {

@Override
public <C extends CtType<T>> C setSuperInterfaces(Set<CtTypeReference<?>> interfaces) {
if (interfaces == null || interfaces.isEmpty()) {
this.interfaces = CtElementImpl.emptySet();
return (C) this;
}
if (this.interfaces == CtElementImpl.<CtTypeReference<?>>emptySet()) {
this.interfaces = new TreeSet<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public List<CtTypeReference<?>> getParameters() {

@Override
public <C extends CtExecutableReference<T>> C setParameters(List<CtTypeReference<?>> parameters) {
if (parameters.isEmpty()) {
if (parameters == null || parameters.isEmpty()) {
this.parameters = CtElementImpl.emptyList();
return (C) this;
}
Expand Down Expand Up @@ -242,8 +242,11 @@ public boolean isOverriding(CtExecutableReference<?> executable) {
}

@Override
public <C extends CtActualTypeContainer> C setActualTypeArguments(
List<CtTypeReference<?>> actualTypeArguments) {
public <C extends CtActualTypeContainer> C setActualTypeArguments(List<CtTypeReference<?>> actualTypeArguments) {
if (actualTypeArguments == null || actualTypeArguments.isEmpty()) {
this.actualTypeArguments = CtElementImpl.emptyList();
return (C) this;
}
if (this.actualTypeArguments == CtElementImpl.<CtTypeReference<?>>emptyList()) {
this.actualTypeArguments = new ArrayList<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public Set<CtTypeReference<?>> getBounds() {

@Override
public <C extends CtIntersectionTypeReference> C setBounds(Set<CtTypeReference<?>> bounds) {
if (bounds == null || bounds.isEmpty()) {
this.bounds = CtElementImpl.emptySet();
return (C) this;
}
if (this.bounds == CtElementImpl.<CtTypeReference<?>>emptySet()) {
this.bounds = new TreeSet<>(new SourcePositionComparator());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ public boolean isSubtypeOf(CtTypeReference<?> type) {

@Override
public <C extends CtActualTypeContainer> C setActualTypeArguments(List<CtTypeReference<?>> actualTypeArguments) {
if (actualTypeArguments == null || actualTypeArguments.isEmpty()) {
this.actualTypeArguments = CtElementImpl.emptyList();
return (C) this;
}
if (this.actualTypeArguments == CtElementImpl.<CtTypeReference<?>>emptyList()) {
this.actualTypeArguments = new ArrayList<>(TYPE_TYPE_PARAMETERS_CONTAINER_DEFAULT_CAPACITY);
}
Expand Down
Loading

0 comments on commit f17381a

Please sign in to comment.