Skip to content

Commit

Permalink
refactor SubstitutionVisitor
Browse files Browse the repository at this point in the history
  • Loading branch information
pvojtechovsky committed Jun 8, 2017
1 parent 43dec86 commit ebfbba9
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,7 @@ private <T extends CtElement> void replaceInListIfExist(List<T> listProtected, R

private void replaceElementIfExist(CtElement candidate, ReplaceListener listener) {
if (candidate == original) {
if (replace.size() > 1) {
throw new SpoonException("Cannot replace single value by multiple values in " + listener.getClass().getSimpleName());
}
CtElement val = null;
if (replace.size() == 1) {
val = replace.iterator().next();
}
CtElement val = candidate.getFactory().Code().convertToSingle(replace);
listener.set(val);
if (val != null) {
val.setParent(candidate.getParent());
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/spoon/reflect/factory/CodeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package spoon.reflect.factory;

import spoon.SpoonException;
import spoon.reflect.code.BinaryOperatorKind;
import spoon.reflect.code.CtAssignment;
import spoon.reflect.code.CtBinaryOperator;
Expand Down Expand Up @@ -43,6 +44,7 @@
import spoon.reflect.code.CtVariableAccess;
import spoon.reflect.declaration.CtAnnotation;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtNamedElement;
import spoon.reflect.declaration.CtVariable;
Expand All @@ -60,6 +62,7 @@
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
Expand Down Expand Up @@ -714,4 +717,44 @@ public CtJavaDocTag createJavaDocTag(String content, CtJavaDocTag.TagType type)
return docTag.setContent(content.trim()).setType(type);
}

/**
* Converts collection of elements to single element.
* It actually supports conversion of {@link CtStatement}s to {@link CtBlock}
*
* @param elements a to be converted collection of elements
* @return single element which represents input `elements`
* @throws SpoonException if conversion is not possible
*/
@SuppressWarnings("unchecked")
public <T extends CtElement, E extends CtElement> T convertToSingle(Collection<E> elements) {
if (elements.isEmpty()) {
return null;
}
if (elements.size() == 1) {
return (T) elements.iterator().next();
}
if (isAllSubtypeOf(elements, CtStatement.class)) {
CtBlock block = factory.Core().createBlock();
block.setStatements(toList((Collection<CtStatement>) elements));
return (T) block;
}
throw new SpoonException("Cannot convert collection to single");
}

private <E> List<E> toList(Collection<E> col) {
if (col instanceof List) {
return (List<E>) col;
}
return new ArrayList<>(col);
}

private boolean isAllSubtypeOf(Collection<?> elements, Class<CtStatement> clazz) {
for (Object ele : elements) {
if (clazz.isInstance(ele) == false) {
return false;
}
}
return true;
}

}
Loading

0 comments on commit ebfbba9

Please sign in to comment.