Search in sources :

Example 56 with CtElement

use of spoon.reflect.declaration.CtElement in project spoon by INRIA.

the class VariableReferencesTest method testVariableScopeFunction.

@Test
public void testVariableScopeFunction() throws Exception {
    // visits all the CtVariable elements whose name is "field" and search for all elements in their scopes
    // Comparing with the result found by basic functions
    List list = modelClass.filterChildren((CtVariable<?> var) -> {
        if (var.getSimpleName().equals("field")) {
            if (var instanceof CtField) {
                // field scope is not supported
                return false;
            }
            CtElement[] real = var.map(new VariableScopeFunction()).list().toArray(new CtElement[0]);
            if (var instanceof CtLocalVariable) {
                assertArrayEquals(var.map(new LocalVariableScopeFunction()).list().toArray(new CtElement[0]), real);
            } else if (var instanceof CtField) {
            // assertArrayEquals(var.map(new FieldScopeFunction()).list().toArray(new CtElement[0]), real);
            } else if (var instanceof CtParameter) {
                assertArrayEquals(var.map(new ParameterScopeFunction()).list().toArray(new CtElement[0]), real);
            } else if (var instanceof CtCatchVariable) {
                assertArrayEquals(var.map(new CatchVariableScopeFunction()).list().toArray(new CtElement[0]), real);
            } else {
                fail("Unexpected variable of type " + var.getClass().getName());
            }
            return true;
        }
        return false;
    }).list();
    assertTrue(list.size() > 0);
}
Also used : CatchVariableScopeFunction(spoon.reflect.visitor.filter.CatchVariableScopeFunction) LocalVariableScopeFunction(spoon.reflect.visitor.filter.LocalVariableScopeFunction) VariableScopeFunction(spoon.reflect.visitor.filter.VariableScopeFunction) ParameterScopeFunction(spoon.reflect.visitor.filter.ParameterScopeFunction) CatchVariableScopeFunction(spoon.reflect.visitor.filter.CatchVariableScopeFunction) CtField(spoon.reflect.declaration.CtField) CtElement(spoon.reflect.declaration.CtElement) List(java.util.List) CtParameter(spoon.reflect.declaration.CtParameter) CtCatchVariable(spoon.reflect.code.CtCatchVariable) CtLocalVariable(spoon.reflect.code.CtLocalVariable) LocalVariableScopeFunction(spoon.reflect.visitor.filter.LocalVariableScopeFunction) VariableReferencesModelTest(spoon.test.query_function.testclasses.VariableReferencesModelTest) Test(org.junit.Test)

Example 57 with CtElement

use of spoon.reflect.declaration.CtElement in project spoon by INRIA.

the class SnippetTest method testCompileStatementWithReturn.

@Test
public void testCompileStatementWithReturn() throws Exception {
    // contract: a snippet with return can be compiled.
    CtElement el = SnippetCompilationHelper.compileStatement(factory.Code().createCodeSnippetStatement("return 3"), factory.Type().INTEGER);
    assertTrue(CtReturn.class.isAssignableFrom(el.getClass()));
    assertEquals("return 3", el.toString());
}
Also used : CtReturn(spoon.reflect.code.CtReturn) CtElement(spoon.reflect.declaration.CtElement) Test(org.junit.Test)

Example 58 with CtElement

use of spoon.reflect.declaration.CtElement in project spoon by INRIA.

the class SerializationModelStreamer method load.

public Factory load(InputStream in) throws IOException {
    try {
        ObjectInputStream ois = new ObjectInputStream(in);
        final Factory f = (Factory) ois.readObject();
        // create query using factory directly
        // because any try to call CtElement#map or CtElement#filterChildren will fail on uninitialized factory
        f.createQuery(f.Module().getAllModules().toArray()).filterChildren(new Filter<CtElement>() {

            @Override
            public boolean matches(CtElement e) {
                e.setFactory(f);
                return false;
            }
        }).list();
        ois.close();
        return f;
    } catch (ClassNotFoundException e) {
        Launcher.LOGGER.error(e.getMessage(), e);
        throw new IOException(e.getMessage());
    }
}
Also used : Filter(spoon.reflect.visitor.Filter) CtElement(spoon.reflect.declaration.CtElement) Factory(spoon.reflect.factory.Factory) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 59 with CtElement

use of spoon.reflect.declaration.CtElement in project spoon by INRIA.

the class ContextBuilder method exit.

void exit(ASTNode node) {
    ASTPair pair = stack.pop();
    if (pair.node != node) {
        throw new RuntimeException("Inconsistent Stack " + node + "\n" + pair.node);
    }
    CtElement current = pair.element;
    if (!stack.isEmpty()) {
        this.jdtTreeBuilder.getExiter().setChild(current);
        this.jdtTreeBuilder.getExiter().setChild(pair.node);
        this.jdtTreeBuilder.getExiter().scan(stack.peek().element);
    }
}
Also used : CtElement(spoon.reflect.declaration.CtElement)

Example 60 with CtElement

use of spoon.reflect.declaration.CtElement in project spoon by INRIA.

the class FieldReferenceFunction method apply.

@Override
public void apply(CtElement fieldOrScope, CtConsumer<Object> outputConsumer) {
    CtElement scope;
    CtField<?> field = this.field;
    if (field == null) {
        if (fieldOrScope instanceof CtField) {
            field = (CtField<?>) fieldOrScope;
        } else {
            throw new SpoonException("The input of FieldReferenceFunction must be a CtField but is " + fieldOrScope.getClass().getSimpleName());
        }
        scope = field.getFactory().getModel().getUnnamedModule();
    } else {
        scope = fieldOrScope;
    }
    scope.filterChildren(new DirectReferenceFilter<CtFieldReference<?>>(field.getReference())).forEach(outputConsumer);
}
Also used : SpoonException(spoon.SpoonException) CtElement(spoon.reflect.declaration.CtElement) CtField(spoon.reflect.declaration.CtField)

Aggregations

CtElement (spoon.reflect.declaration.CtElement)86 Test (org.junit.Test)39 Launcher (spoon.Launcher)23 Factory (spoon.reflect.factory.Factory)18 ArrayList (java.util.ArrayList)17 CtMethod (spoon.reflect.declaration.CtMethod)17 CtType (spoon.reflect.declaration.CtType)15 CtStatement (spoon.reflect.code.CtStatement)14 CtTypeReference (spoon.reflect.reference.CtTypeReference)14 List (java.util.List)12 CtClass (spoon.reflect.declaration.CtClass)12 CtField (spoon.reflect.declaration.CtField)12 CtIf (spoon.reflect.code.CtIf)11 CtInvocation (spoon.reflect.code.CtInvocation)11 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)11 CtLocalVariable (spoon.reflect.code.CtLocalVariable)10 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)9 CtScanner (spoon.reflect.visitor.CtScanner)9 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)9 Collection (java.util.Collection)8