use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.
the class VariableReferencesTest method testCheckModelConsistency.
@Test
public void testCheckModelConsistency() throws Exception {
// 2) check that each of them is using different identification value
class Context {
Map<Integer, CtElement> unique = new HashMap<>();
int maxKey = 0;
void checkKey(int key, CtElement ele) {
CtElement ambiquous = unique.put(key, ele);
if (ambiquous != null) {
fail("Two variables [" + ambiquous.toString() + " in " + getParentMethodName(ambiquous) + "," + ele.toString() + " in " + getParentMethodName(ele) + "] has same value");
}
maxKey = Math.max(maxKey, key);
}
}
Context context = new Context();
modelClass.filterChildren((CtElement e) -> {
if (e instanceof CtVariable) {
CtVariable<?> var = (CtVariable<?>) e;
if (isTestFieldName(var.getSimpleName()) == false) {
return false;
}
// check only these variables whose name is isTestFieldName(name)==true
Integer val = getLiteralValue(var);
// System.out.println("key = "+val+" - "+var.toString());
context.checkKey(val, var);
}
return false;
}).list();
// System.out.println("Next available key is: "+(context.maxKey+1));
assertTrue(context.unique.size() > 0);
assertEquals("Only these keys were found: " + context.unique.keySet(), context.maxKey, context.unique.size());
assertEquals("AllLocalVars#maxValue must be equal to maximum value number ", (int) getLiteralValue((CtVariable) modelClass.filterChildren(new NamedElementFilter<>(CtVariable.class, "maxValue")).first()), context.maxKey);
}
use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.
the class TemplateTest method testTemplateMatcher.
@Test
public void testTemplateMatcher() throws Exception {
// contract: the given templates should match the expected elements
Launcher spoon = new Launcher();
Factory factory = spoon.getFactory();
spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/java/spoon/test/template/testclasses/bounds/CheckBound.java"), SpoonResourceHelper.resources("./src/test/java/spoon/test/template/testclasses/bounds/CheckBoundMatcher.java")).build();
{
// testing matcher1
CtClass<?> templateKlass = factory.Class().get(CheckBoundMatcher.class);
CtClass<?> klass = factory.Class().get(CheckBound.class);
CtIf templateRoot = (CtIf) ((CtMethod) templateKlass.getElements(new NamedElementFilter<>(CtMethod.class, "matcher1")).get(0)).getBody().getStatement(0);
TemplateMatcher matcher = new TemplateMatcher(templateRoot);
assertEquals(2, matcher.find(klass).size());
assertThat(asList("foo", "fbar"), is(klass.filterChildren(matcher).map((CtElement e) -> e.getParent(CtMethod.class).getSimpleName()).list()));
}
{
// testing matcher2
CtClass<?> templateKlass = factory.Class().get(CheckBoundMatcher.class);
CtClass<?> klass = factory.Class().get(CheckBound.class);
CtIf templateRoot = (CtIf) ((CtMethod) templateKlass.getElements(new NamedElementFilter<>(CtMethod.class, "matcher2")).get(0)).getBody().getStatement(0);
TemplateMatcher matcher = new TemplateMatcher(templateRoot);
assertEquals(1, matcher.find(klass).size());
assertThat(asList("bov"), is(klass.filterChildren(matcher).map((CtElement e) -> e.getParent(CtMethod.class).getSimpleName()).list()));
}
{
// testing matcher3
CtClass<?> templateKlass = factory.Class().get(CheckBoundMatcher.class);
CtClass<?> klass = factory.Class().get(CheckBound.class);
CtIf templateRoot = (CtIf) ((CtMethod) templateKlass.getElements(new NamedElementFilter<>(CtMethod.class, "matcher3")).get(0)).getBody().getStatement(0);
TemplateMatcher matcher = new TemplateMatcher(templateRoot);
assertEquals(2, matcher.find(klass).size());
assertThat(asList("foo", "fbar"), is(klass.filterChildren(matcher).map((CtElement e) -> e.getParent(CtMethod.class).getSimpleName()).list()));
}
{
// testing matcher4
CtClass<?> templateKlass = factory.Class().get(CheckBoundMatcher.class);
CtClass<?> klass = factory.Class().get(CheckBound.class);
CtIf templateRoot = (CtIf) ((CtMethod) templateKlass.getElements(new NamedElementFilter<>(CtMethod.class, "matcher4")).get(0)).getBody().getStatement(0);
TemplateMatcher matcher = new TemplateMatcher(templateRoot);
assertEquals(3, matcher.find(klass).size());
assertThat(asList("foo", "foo2", "fbar"), is(klass.filterChildren(matcher).map((CtElement e) -> e.getParent(CtMethod.class).getSimpleName()).list()));
}
{
// testing matcher5
CtClass<?> templateKlass = factory.Class().get(CheckBoundMatcher.class);
CtClass<?> klass = factory.Class().get(CheckBound.class);
CtIf templateRoot = (CtIf) ((CtMethod) templateKlass.getElements(new NamedElementFilter<>(CtMethod.class, "matcher5")).get(0)).getBody().getStatement(0);
TemplateMatcher matcher = new TemplateMatcher(templateRoot);
assertEquals(6, matcher.find(klass).size());
assertThat(asList("foo", "foo2", "fbar", "baz", "bou", "bov"), is(klass.filterChildren(matcher).map((CtElement e) -> e.getParent(CtMethod.class).getSimpleName()).list()));
}
{
// testing matcher6
CtClass<?> templateKlass = factory.Class().get(CheckBoundMatcher.class);
CtClass<?> klass = factory.Class().get(CheckBound.class);
CtIf templateRoot = (CtIf) ((CtMethod) templateKlass.getElements(new NamedElementFilter<>(CtMethod.class, "matcher6")).get(0)).getBody().getStatement(0);
TemplateMatcher matcher = new TemplateMatcher(templateRoot);
assertEquals(2, matcher.find(klass).size());
assertThat(asList("baz", "bou"), is(klass.filterChildren(matcher).map((CtElement e) -> e.getParent(CtMethod.class).getSimpleName()).list()));
}
// testing with named elements, at the method level
{
CtClass<?> templateKlass = factory.Class().get(CheckBoundMatcher.class);
CtClass<?> klass = factory.Class().get(CheckBound.class);
CtMethod meth = (CtMethod) templateKlass.getElements(new NamedElementFilter<>(CtMethod.class, "matcher3")).get(0);
// exact match
meth.setSimpleName("foo");
TemplateMatcher matcher = new TemplateMatcher(meth);
List<CtMethod> ctElements = matcher.find(klass);
assertEquals(1, ctElements.size());
assertEquals("foo", ctElements.get(0).getSimpleName());
}
{
// contract: the name to be matched does not have to be an exact match
CtClass<?> templateKlass = factory.Class().get(CheckBoundMatcher.class);
CtClass<?> klass = factory.Class().get(CheckBound.class);
CtMethod meth = (CtMethod) templateKlass.getElements(new NamedElementFilter<>(CtMethod.class, "matcher5")).get(0);
// together with the appropriate @Parameter _w_, this means
// we match all methods with name f*, because parameter _w_ acts as a wildcard
meth.setSimpleName("f_w_");
TemplateMatcher matcher = new TemplateMatcher(meth);
List<CtMethod> ctElements = matcher.find(klass);
assertEquals(3, ctElements.size());
assertEquals("foo", ctElements.get(0).getSimpleName());
assertEquals("foo2", ctElements.get(1).getSimpleName());
assertEquals("fbar", ctElements.get(2).getSimpleName());
}
}
use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.
the class TargetedExpressionTest method testCtSuperAccess.
@Test
public void testCtSuperAccess() throws Exception {
final Factory factory = build(InternalSuperCall.class);
final CtClass<?> ctClass = factory.Class().get(InternalSuperCall.class);
CtMethod<?> method = ctClass.getElements(new NamedElementFilter<>(CtMethod.class, "methode")).get(0);
assertEquals("spoon.test.targeted.testclasses.InternalSuperCall.super.toString()", method.getBody().getStatements().get(0).toString());
assertNotNull(method.getElements(new TypeFilter<>(CtSuperAccess.class)).get(0).getTarget());
CtMethod<?> toStringMethod = ctClass.getElements(new NamedElementFilter<>(CtMethod.class, "toString")).get(0);
assertEquals("return super.toString()", toStringMethod.getBody().getStatements().get(0).toString());
assertNull(toStringMethod.getElements(new TypeFilter<>(CtSuperAccess.class)).get(0).getTarget());
}
use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.
the class InsertMethodsTest method testInsertAfterSwitchCase.
@Test
public void testInsertAfterSwitchCase() throws Exception {
CtMethod<?> sm = insertExampleClass.getElements(new NamedElementFilter<>(CtMethod.class, "switchMethod")).get(0);
// Adds a new snippet in a case.
CtSwitch<?> sw = sm.getElements(new TypeFilter<CtSwitch<?>>(CtSwitch.class)).get(0);
CtCase<?> ctCase1 = sw.getCases().get(2);
CtCase<?> ctCase2 = sw.getCases().get(3);
CtCodeSnippetStatement snippet = factory.Code().createCodeSnippetStatement("System.out.println(\"foo\")");
ctCase1.getStatements().get(0).insertAfter(snippet);
assertEquals(snippet, ctCase1.getStatements().get(1));
assertEquals(ctCase1, snippet.getParent());
CtCodeSnippetStatement snippet2 = snippet.clone();
ctCase2.getStatements().get(1).insertAfter(snippet2);
assertEquals(snippet2, ctCase2.getStatements().get(2));
assertEquals(ctCase2, snippet2.getParent());
// Creates a new case.
CtCase<Object> caseElem = factory.Core().createCase();
CtLiteral<Object> literal = factory.Core().createLiteral();
literal.setValue(1);
caseElem.setCaseExpression(literal);
// here we may call either insertAfter(CtStatement) or insertAfter(CtStatementList)
// ctCase.insertAfter(caseElem);
// so force the correct insert
CtCase<?> ctCase = sw.getCases().get(1);
ctCase.insertAfter((CtStatement) caseElem);
assertEquals(5, sw.getCases().size());
assertEquals(caseElem, sw.getCases().get(2));
assertEquals(ctCase, sw.getCases().get(1));
assertEquals(sw, caseElem.getParent());
}
use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.
the class TestLabels method testLabelsAreDetected.
@Test
public void testLabelsAreDetected() {
Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/labels/testclasses/ManyLabels.java");
launcher.buildModel();
CtMethod mainMethod = launcher.getFactory().getModel().getElements(new NamedElementFilter<>(CtMethod.class, "main")).get(0);
CtBlock body = mainMethod.getBody();
assertEquals(2, body.getStatements().size());
CtBlock block = (CtBlock) body.getStatement(0);
CtSwitch ctSwitch = (CtSwitch) body.getStatement(1);
assertEquals("labelBlock", block.getLabel());
assertEquals("sw", ctSwitch.getLabel());
assertTrue(block.getStatement(1) instanceof CtIf);
CtIf firstIf = (CtIf) block.getStatement(1);
CtBlock then = firstIf.getThenStatement();
CtBreak firstBreak = (CtBreak) then.getStatement(1);
assertEquals("labelBlock", firstBreak.getTargetLabel());
assertSame(block, firstBreak.getLabelledStatement());
CtIf secondIf = (CtIf) block.getStatement(2);
assertEquals("labelIf", secondIf.getLabel());
CtBlock thenBlock = secondIf.getThenStatement();
CtIf innerIf = (CtIf) thenBlock.getStatement(0);
CtBlock innerThenBlock = innerIf.getThenStatement();
CtBreak breakInnerIf = (CtBreak) innerThenBlock.getStatement(0);
assertSame(secondIf, breakInnerIf.getLabelledStatement());
CtCase firstCase = (CtCase) ctSwitch.getCases().get(0);
List<CtStatement> statementList = firstCase.getStatements();
assertEquals(2, statementList.size());
CtDo ctDo = (CtDo) statementList.get(0);
assertEquals("label", ctDo.getLabel());
CtBreak finalBreak = (CtBreak) statementList.get(1);
assertNull(finalBreak.getTargetLabel());
assertNull(finalBreak.getLabelledStatement());
CtBlock doBlock = (CtBlock) ctDo.getBody();
CtWhile ctWhile = (CtWhile) doBlock.getStatement(1);
assertEquals("lWhile", ctWhile.getLabel());
CtBlock whileBlock = (CtBlock) ctWhile.getBody();
CtFor forLoop = (CtFor) whileBlock.getStatement(0);
CtBreak breakSwitch = (CtBreak) whileBlock.getStatement(1);
assertEquals("sw", breakSwitch.getTargetLabel());
assertSame(ctSwitch, breakSwitch.getLabelledStatement());
assertEquals("forloop", forLoop.getLabel());
CtBlock forBlock = (CtBlock) forLoop.getBody();
assertEquals(7, forBlock.getStatements().size());
CtIf firstForIf = (CtIf) forBlock.getStatement(1);
CtIf secondForIf = (CtIf) forBlock.getStatement(2);
CtIf thirdForIf = (CtIf) forBlock.getStatement(3);
CtIf fourthForIf = (CtIf) forBlock.getStatement(4);
CtBreak breakItself = (CtBreak) forBlock.getStatement(6);
CtContinue continueFor = (CtContinue) ((CtBlock) firstForIf.getThenStatement()).getStatement(0);
assertSame(forLoop, continueFor.getLabelledStatement());
CtContinue continueWhile = (CtContinue) ((CtBlock) secondForIf.getThenStatement()).getStatement(0);
assertSame(ctWhile, continueWhile.getLabelledStatement());
CtContinue continueDo = (CtContinue) ((CtBlock) thirdForIf.getThenStatement()).getStatement(0);
assertSame(ctDo, continueDo.getLabelledStatement());
CtBreak breakDo = (CtBreak) ((CtBlock) fourthForIf.getThenStatement()).getStatement(0);
assertSame(ctDo, breakDo.getLabelledStatement());
assertEquals("labelbreak", breakItself.getLabel());
assertEquals("labelbreak", breakItself.getTargetLabel());
assertSame(breakItself, breakItself.getLabelledStatement());
}
Aggregations