Search in sources :

Example 36 with NamedElementFilter

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);
}
Also used : CtElement(spoon.reflect.declaration.CtElement) CtVariable(spoon.reflect.declaration.CtVariable) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) HashMap(java.util.HashMap) Map(java.util.Map) VariableReferencesModelTest(spoon.test.query_function.testclasses.VariableReferencesModelTest) Test(org.junit.Test)

Example 37 with NamedElementFilter

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());
    }
}
Also used : CheckBoundMatcher(spoon.test.template.testclasses.bounds.CheckBoundMatcher) CtClass(spoon.reflect.declaration.CtClass) CheckBound(spoon.test.template.testclasses.bounds.CheckBound) CtElement(spoon.reflect.declaration.CtElement) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) TemplateMatcher(spoon.template.TemplateMatcher) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) CtIf(spoon.reflect.code.CtIf) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 38 with NamedElementFilter

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());
}
Also used : NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 39 with NamedElementFilter

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());
}
Also used : NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtCodeSnippetStatement(spoon.reflect.code.CtCodeSnippetStatement) Test(org.junit.Test)

Example 40 with NamedElementFilter

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());
}
Also used : CtBreak(spoon.reflect.code.CtBreak) CtSwitch(spoon.reflect.code.CtSwitch) CtIf(spoon.reflect.code.CtIf) CtWhile(spoon.reflect.code.CtWhile) CtBlock(spoon.reflect.code.CtBlock) CtStatement(spoon.reflect.code.CtStatement) CtCase(spoon.reflect.code.CtCase) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) CtDo(spoon.reflect.code.CtDo) CtFor(spoon.reflect.code.CtFor) CtMethod(spoon.reflect.declaration.CtMethod) CtContinue(spoon.reflect.code.CtContinue) Test(org.junit.Test)

Aggregations

NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)43 Test (org.junit.Test)42 Launcher (spoon.Launcher)19 Factory (spoon.reflect.factory.Factory)17 CtClass (spoon.reflect.declaration.CtClass)10 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)10 CtMethod (spoon.reflect.declaration.CtMethod)8 MainTest (spoon.test.main.MainTest)8 CtIf (spoon.reflect.code.CtIf)7 CtTypeParameter (spoon.reflect.declaration.CtTypeParameter)7 CtTypeReference (spoon.reflect.reference.CtTypeReference)7 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)7 CtBlock (spoon.reflect.code.CtBlock)6 CtCodeSnippetStatement (spoon.reflect.code.CtCodeSnippetStatement)6 File (java.io.File)4 CtAnnotation (spoon.reflect.declaration.CtAnnotation)4 CtElement (spoon.reflect.declaration.CtElement)4 Annotation (java.lang.annotation.Annotation)3 CtLiteral (spoon.reflect.code.CtLiteral)3 CtParameter (spoon.reflect.declaration.CtParameter)3