use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class CtParameterReferenceImpl method lookupDynamically.
private CtParameter<T> lookupDynamically() {
CtElement element = this;
CtParameter optional = null;
String name = getSimpleName();
try {
do {
CtExecutable executable = element.getParent(CtExecutable.class);
if (executable == null) {
return null;
}
for (CtParameter parameter : (List<CtParameter>) executable.getParameters()) {
if (name.equals(parameter.getSimpleName())) {
optional = parameter;
}
}
element = executable;
} while (optional == null);
} catch (ParentNotInitializedException e) {
return null;
}
return optional;
}
use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class SubstitutionVisitor method substitute.
/**
* Substitutes all template parameters of element and returns substituted element.
*
* @param element to be substituted model
* @return substituted model
*/
public <E extends CtElement> List<E> substitute(E element) {
final Map<CtElement, String> elementToGeneratedByComment = addGeneratedBy ? new IdentityHashMap<CtElement, String>() : null;
if (addGeneratedBy) {
/*
* collect 'generated by' comments for each type member of the substituted element, before the substitution is done,
* so we know the origin names of the members.
*/
final CtInheritanceScanner internalScanner = new CtInheritanceScanner() {
public void scanCtTypeMember(CtTypeMember typeMeber) {
elementToGeneratedByComment.put(typeMeber, getGeneratedByComment(typeMeber));
}
};
new CtScanner() {
@Override
public void scan(CtElement p_element) {
internalScanner.scan(p_element);
super.scan(p_element);
}
}.scan(element);
}
List<E> result = createContext().substitute(element);
if (addGeneratedBy) {
// add generated by comments after substitution, otherwise they would be substituted in comments too.
applyGeneratedByComments(elementToGeneratedByComment);
}
return result;
}
use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class ReplacementVisitor method replaceInSetIfExist.
private <T extends CtElement> void replaceInSetIfExist(Set<T> setProtected, ReplaceSetListener listener) {
Set<T> set = new HashSet<>(setProtected);
T shouldBeDeleted = null;
for (T element : set) {
if (element == original) {
shouldBeDeleted = element;
break;
}
}
if (shouldBeDeleted != null) {
set.remove(shouldBeDeleted);
for (CtElement ele : replace) {
if (ele != null) {
set.add((T) ele);
ele.setParent(shouldBeDeleted.getParent());
}
}
listener.set(set);
}
}
use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class CloneTest method testCloneListener.
@Test
public void testCloneListener() throws Exception {
// contract: it is possible to extend the cloning behavior
// in this example extension, a listener of cloning process gets access to origin node and cloned node
// we check the contract with some complicated class as target of cloning
Factory factory = ModelUtils.build(new File("./src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java"));
CtType<?> cloneSource = factory.Type().get(DefaultJavaPrettyPrinter.class);
class CloneListener extends CloneHelper {
Map<CtElement, CtElement> sourceToTarget = new IdentityHashMap<>();
@Override
public <T extends CtElement> T clone(T source) {
if (source == null) {
return null;
}
T target = super.clone(source);
onCloned(source, target);
return target;
}
private void onCloned(CtElement source, CtElement target) {
CtElement previousTarget = sourceToTarget.put(source, target);
assertNull(previousTarget);
}
}
CloneListener cl = new CloneListener();
CtType<?> cloneTarget = cl.clone(cloneSource);
cloneSource.filterChildren(null).forEach(sourceElement -> {
// contract: there exists cloned target for each visitable element
CtElement targetElement = cl.sourceToTarget.remove(sourceElement);
assertNotNull("Missing target for sourceElement\n" + sourceElement, targetElement);
assertEquals("Source and Target are not equal", sourceElement, targetElement);
});
// contract: each visitable elements was cloned exactly once. No more no less.
assertTrue(cl.sourceToTarget.isEmpty());
}
use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class CloneTest method testCopyType.
@Test
public void testCopyType() throws Exception {
// contract: the copied type is well formed, it never points to the initial type
Factory factory = ModelUtils.build(new File("./src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java"));
CtType<?> intialElement = factory.Type().get(DefaultJavaPrettyPrinter.class);
CtType<?> cloneTarget = intialElement.copyType();
assertEquals("spoon.reflect.visitor.DefaultJavaPrettyPrinterCopy", cloneTarget.getQualifiedName());
// we go over all references
for (CtReference reference : cloneTarget.getElements(new TypeFilter<>(CtReference.class))) {
CtElement declaration = reference.getDeclaration();
if (declaration == null) {
continue;
}
// the core assertion: not a single reference points to the initial element
if (declaration.hasParent(intialElement)) {
fail();
}
}
}
Aggregations