use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class VisitorPartialEvaluator method visitCtBlock.
public <R> void visitCtBlock(CtBlock<R> block) {
CtBlock<?> b = block.getFactory().Core().createBlock();
for (CtStatement s : block.getStatements()) {
CtElement res = evaluate(s);
if (res != null) {
if (res instanceof CtStatement) {
b.addStatement((CtStatement) res);
} else {
// the context expectes statement. We cannot simplify in this case
b.addStatement(s);
}
}
// do not copy unreachable statements
if (flowEnded) {
break;
}
}
setResult(b);
}
use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class VisitorPartialEvaluator method evaluate.
@SuppressWarnings("unchecked")
public <R extends CtElement> R evaluate(R element) {
if (element == null) {
return null;
}
element.accept(this);
if (result != null) {
CtElement r = result;
// reset result, to not influence another evaluation.
result = null;
if (element.isParentInitialized()) {
r.setParent(element.getParent());
}
return (R) r;
}
// otherwise nothing has been changed
return (R) element.clone();
}
use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class AbstractTypingContext method adaptType.
@Override
public CtTypeReference<?> adaptType(CtTypeInformation type) {
CtTypeReference<?> result;
boolean isCopy = false;
if (type instanceof CtTypeReference<?>) {
if (type instanceof CtTypeParameterReference) {
return adaptTypeParameterReference((CtTypeParameterReference) type);
}
result = (CtTypeReference<?>) type;
} else {
if (type instanceof CtTypeParameter) {
return adaptTypeParameter((CtTypeParameter) type);
}
CtType<?> t = (CtType<?>) type;
result = t.getFactory().Type().createReference(t, true);
isCopy = true;
}
if (result.getActualTypeArguments().size() > 0) {
// we have to adapt actual type arguments recursive too
if (isCopy == false) {
CtElement parent = result.getParent();
result = result.clone();
result.setParent(parent);
List<CtTypeReference<?>> actTypeArgs = new ArrayList<>(result.getActualTypeArguments());
for (int i = 0; i < actTypeArgs.size(); i++) {
CtTypeReference adaptedTypeArgs = adaptType(actTypeArgs.get(i));
// for some type argument we might return null to avoid recursive calls
if (adaptedTypeArgs != null) {
actTypeArgs.set(i, adaptedTypeArgs.clone());
}
}
result.setActualTypeArguments(actTypeArgs);
}
}
return result;
}
use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class CtTypeParameterReferenceImpl method getDeclaration.
@Override
public CtTypeParameter getDeclaration() {
if (!isParentInitialized()) {
return null;
}
CtElement e = this;
CtElement parent = getParent();
if (parent instanceof CtTypeReference) {
if (parent.isParentInitialized() == false) {
return null;
}
parent = parent.getParent();
}
if (parent instanceof CtExecutableReference) {
CtExecutableReference parentExec = (CtExecutableReference) parent;
if (!parentExec.getDeclaringType().equals(e)) {
CtElement parent2 = parentExec.getExecutableDeclaration();
if (parent2 instanceof CtMethod) {
e = parent2;
} else {
e = e.getParent(CtFormalTypeDeclarer.class);
}
} else {
e = e.getParent(CtFormalTypeDeclarer.class);
}
} else {
e = e.getParent(CtFormalTypeDeclarer.class);
}
// collecting all formal type declarers of the hierarchy
while (e != null) {
CtTypeParameter result = findTypeParamDeclaration((CtFormalTypeDeclarer) e, this.getSimpleName());
if (result != null) {
return result;
}
e = e.getParent(CtFormalTypeDeclarer.class);
}
return null;
}
use of spoon.reflect.declaration.CtElement in project spoon by INRIA.
the class TemplateTest method testTemplateMatcherMatchTwoSnippets.
@Test
public void testTemplateMatcherMatchTwoSnippets() throws Exception {
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/template/testclasses/TwoSnippets.java");
spoon.addTemplateResource(new FileSystemFile("./src/test/java/spoon/test/template/testclasses/SecurityCheckerTemplate.java"));
spoon.buildModel();
Factory factory = spoon.getFactory();
CtClass<?> templateKlass = factory.Class().get(SecurityCheckerTemplate.class);
CtMethod templateMethod = (CtMethod) templateKlass.getElements(new NamedElementFilter<>(CtMethod.class, "matcher1")).get(0);
CtIf templateRoot = (CtIf) templateMethod.getBody().getStatement(0);
TemplateMatcher matcher = new TemplateMatcher(templateRoot);
// match using legacy TemplateMatcher#find method
List<CtElement> matches = matcher.find(factory.getModel().getRootPackage());
assertEquals(2, matches.size());
CtElement match1 = matches.get(0);
CtElement match2 = matches.get(1);
assertTrue(match1.equals(match2));
// match using TemplateMatcher#matches method and query filter
matches = factory.getModel().filterChildren(matcher).list();
assertEquals(2, matches.size());
match1 = matches.get(0);
match2 = matches.get(1);
assertTrue(match1.equals(match2));
}
Aggregations