Search in sources :

Example 1 with Filter

use of spoon.reflect.visitor.Filter in project spoon by INRIA.

the class FilterTest method testFilterChildrenWithoutFilterQueryStep.

@Test
public void testFilterChildrenWithoutFilterQueryStep() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
    launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
    launcher.run();
    // Contract: the filterChildren(null) without Filter returns same results like filter which returns true for each input.
    List<CtElement> list = launcher.getFactory().Package().getRootPackage().filterChildren(null).list();
    Iterator<CtElement> iter = list.iterator();
    launcher.getFactory().Package().getRootPackage().filterChildren(e -> {
        return true;
    }).forEach(real -> {
        // the elements produced by each query are same
        CtElement expected = iter.next();
        if (real != expected) {
            assertEquals(expected, real);
        }
    });
    assertTrue(list.size() > 0);
    assertTrue(iter.hasNext() == false);
}
Also used : Arrays(java.util.Arrays) Launcher(spoon.Launcher) CtSwitch(spoon.reflect.code.CtSwitch) CtInterface(spoon.reflect.declaration.CtInterface) CtConsumableFunction(spoon.reflect.visitor.chain.CtConsumableFunction) Tostada(spoon.test.filters.testclasses.Tostada) CtNamedElement(spoon.reflect.declaration.CtNamedElement) CtQueryImpl(spoon.reflect.visitor.chain.CtQueryImpl) CtType(spoon.reflect.declaration.CtType) QueryFailurePolicy(spoon.reflect.visitor.chain.QueryFailurePolicy) CtElement(spoon.reflect.declaration.CtElement) ReturnOrThrowFilter(spoon.reflect.visitor.filter.ReturnOrThrowFilter) FieldAccessFilterTacos(spoon.test.filters.testclasses.FieldAccessFilterTacos) OverridingMethodFilter(spoon.reflect.visitor.filter.OverridingMethodFilter) DeepRepresentationComparator(spoon.support.comparator.DeepRepresentationComparator) Assert.fail(org.junit.Assert.fail) CtMethodImpl(spoon.support.reflect.declaration.CtMethodImpl) CtExecutable(spoon.reflect.declaration.CtExecutable) Query(spoon.reflect.visitor.Query) CompositeFilter(spoon.reflect.visitor.filter.CompositeFilter) CtQuery(spoon.reflect.visitor.chain.CtQuery) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtScannerFunction(spoon.reflect.visitor.filter.CtScannerFunction) ModelUtils.build(spoon.testing.utils.ModelUtils.build) CtField(spoon.reflect.declaration.CtField) Predicate(java.util.function.Predicate) Collection(java.util.Collection) QualifiedNameComparator(spoon.support.comparator.QualifiedNameComparator) Antojito(spoon.test.filters.testclasses.Antojito) ModelUtils(spoon.testing.utils.ModelUtils) Collectors(java.util.stream.Collectors) CtModule(spoon.reflect.declaration.CtModule) Constants(spoon.test.imports.testclasses.internal4.Constants) CtFieldAccess(spoon.reflect.code.CtFieldAccess) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) LineFilter(spoon.reflect.visitor.filter.LineFilter) List(java.util.List) AnnotationFilter(spoon.reflect.visitor.filter.AnnotationFilter) Assert.assertFalse(org.junit.Assert.assertFalse) CtMethod(spoon.reflect.declaration.CtMethod) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtIf(spoon.reflect.code.CtIf) ITostada(spoon.test.filters.testclasses.ITostada) SubInheritanceHierarchyResolver(spoon.support.visitor.SubInheritanceHierarchyResolver) CtPackage(spoon.reflect.declaration.CtPackage) TreeSet(java.util.TreeSet) CtStatement(spoon.reflect.code.CtStatement) AbstractTostada(spoon.test.filters.testclasses.AbstractTostada) ArrayList(java.util.ArrayList) Assert.assertSame(org.junit.Assert.assertSame) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) SubTostada(spoon.test.filters.testclasses.SubTostada) CtExpression(spoon.reflect.code.CtExpression) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) CtNewClass(spoon.reflect.code.CtNewClass) Filter(spoon.reflect.visitor.Filter) FieldAccessFilter(spoon.reflect.visitor.filter.FieldAccessFilter) ScanningMode(spoon.reflect.visitor.chain.ScanningMode) InvocationFilter(spoon.reflect.visitor.filter.InvocationFilter) Before(org.junit.Before) CtLoop(spoon.reflect.code.CtLoop) CtScannerListener(spoon.reflect.visitor.chain.CtScannerListener) Iterator(java.util.Iterator) CtInvocation(spoon.reflect.code.CtInvocation) Assert.assertNotNull(org.junit.Assert.assertNotNull) OverriddenMethodFilter(spoon.reflect.visitor.filter.OverriddenMethodFilter) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Factory(spoon.reflect.factory.Factory) CtVariable(spoon.reflect.declaration.CtVariable) OverriddenMethodQuery(spoon.reflect.visitor.filter.OverriddenMethodQuery) Tacos(spoon.test.filters.testclasses.Tacos) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) FilteringOperator(spoon.reflect.visitor.filter.FilteringOperator) CtTypeReference(spoon.reflect.reference.CtTypeReference) Assert.assertNull(org.junit.Assert.assertNull) ParentFunction(spoon.reflect.visitor.filter.ParentFunction) CtCFlowBreak(spoon.reflect.code.CtCFlowBreak) CtConsumer(spoon.reflect.visitor.chain.CtConsumer) CtClass(spoon.reflect.declaration.CtClass) ModifierKind(spoon.reflect.declaration.ModifierKind) CtFieldReference(spoon.reflect.reference.CtFieldReference) RegexFilter(spoon.reflect.visitor.filter.RegexFilter) Comparator(java.util.Comparator) Assert.assertEquals(org.junit.Assert.assertEquals) CtFunction(spoon.reflect.visitor.chain.CtFunction) CtElement(spoon.reflect.declaration.CtElement) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 2 with Filter

use of spoon.reflect.visitor.Filter in project spoon by INRIA.

the class FilterTest method testEarlyTerminatingQuery.

@Test
public void testEarlyTerminatingQuery() throws Exception {
    // contract: a method first evaluates query until first element is found and then terminates the query
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
    launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
    launcher.run();
    class Context {

        boolean wasTerminated = false;

        void failIfTerminated(String place) {
            assertTrue("The " + place + " is called after query was terminated.", wasTerminated == false);
        }
    }
    Context context = new Context();
    CtMethod firstMethod = launcher.getFactory().Package().getRootPackage().filterChildren(e -> {
        context.failIfTerminated("Filter#match of filterChildren");
        return true;
    }).map((CtElement e) -> {
        context.failIfTerminated("Array returning CtFunction#apply of map");
        // send result twice to check that second item is skipped
        return new CtElement[] { e, e };
    }).map((CtElement e) -> {
        context.failIfTerminated("List returning CtFunction#apply of map");
        // send result twice to check that second item is skipped
        return Arrays.asList(new CtElement[] { e, e });
    }).map((CtElement e, CtConsumer<Object> out) -> {
        context.failIfTerminated("CtConsumableFunction#apply of map");
        if (e instanceof CtMethod) {
            // this call should pass and cause termination of the query
            out.accept(e);
            context.wasTerminated = true;
        // let it call out.accept(e); once more to check that next step is not called after query is terminated
        }
        out.accept(e);
    }).map(e -> {
        context.failIfTerminated("CtFunction#apply of map after CtConsumableFunction");
        return e;
    }).first(CtMethod.class);
    assertTrue(firstMethod != null);
    assertTrue(context.wasTerminated);
}
Also used : Arrays(java.util.Arrays) Launcher(spoon.Launcher) CtSwitch(spoon.reflect.code.CtSwitch) CtInterface(spoon.reflect.declaration.CtInterface) CtConsumableFunction(spoon.reflect.visitor.chain.CtConsumableFunction) Tostada(spoon.test.filters.testclasses.Tostada) CtNamedElement(spoon.reflect.declaration.CtNamedElement) CtQueryImpl(spoon.reflect.visitor.chain.CtQueryImpl) CtType(spoon.reflect.declaration.CtType) QueryFailurePolicy(spoon.reflect.visitor.chain.QueryFailurePolicy) CtElement(spoon.reflect.declaration.CtElement) ReturnOrThrowFilter(spoon.reflect.visitor.filter.ReturnOrThrowFilter) FieldAccessFilterTacos(spoon.test.filters.testclasses.FieldAccessFilterTacos) OverridingMethodFilter(spoon.reflect.visitor.filter.OverridingMethodFilter) DeepRepresentationComparator(spoon.support.comparator.DeepRepresentationComparator) Assert.fail(org.junit.Assert.fail) CtMethodImpl(spoon.support.reflect.declaration.CtMethodImpl) CtExecutable(spoon.reflect.declaration.CtExecutable) Query(spoon.reflect.visitor.Query) CompositeFilter(spoon.reflect.visitor.filter.CompositeFilter) CtQuery(spoon.reflect.visitor.chain.CtQuery) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtScannerFunction(spoon.reflect.visitor.filter.CtScannerFunction) ModelUtils.build(spoon.testing.utils.ModelUtils.build) CtField(spoon.reflect.declaration.CtField) Predicate(java.util.function.Predicate) Collection(java.util.Collection) QualifiedNameComparator(spoon.support.comparator.QualifiedNameComparator) Antojito(spoon.test.filters.testclasses.Antojito) ModelUtils(spoon.testing.utils.ModelUtils) Collectors(java.util.stream.Collectors) CtModule(spoon.reflect.declaration.CtModule) Constants(spoon.test.imports.testclasses.internal4.Constants) CtFieldAccess(spoon.reflect.code.CtFieldAccess) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) LineFilter(spoon.reflect.visitor.filter.LineFilter) List(java.util.List) AnnotationFilter(spoon.reflect.visitor.filter.AnnotationFilter) Assert.assertFalse(org.junit.Assert.assertFalse) CtMethod(spoon.reflect.declaration.CtMethod) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtIf(spoon.reflect.code.CtIf) ITostada(spoon.test.filters.testclasses.ITostada) SubInheritanceHierarchyResolver(spoon.support.visitor.SubInheritanceHierarchyResolver) CtPackage(spoon.reflect.declaration.CtPackage) TreeSet(java.util.TreeSet) CtStatement(spoon.reflect.code.CtStatement) AbstractTostada(spoon.test.filters.testclasses.AbstractTostada) ArrayList(java.util.ArrayList) Assert.assertSame(org.junit.Assert.assertSame) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) SubTostada(spoon.test.filters.testclasses.SubTostada) CtExpression(spoon.reflect.code.CtExpression) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) CtNewClass(spoon.reflect.code.CtNewClass) Filter(spoon.reflect.visitor.Filter) FieldAccessFilter(spoon.reflect.visitor.filter.FieldAccessFilter) ScanningMode(spoon.reflect.visitor.chain.ScanningMode) InvocationFilter(spoon.reflect.visitor.filter.InvocationFilter) Before(org.junit.Before) CtLoop(spoon.reflect.code.CtLoop) CtScannerListener(spoon.reflect.visitor.chain.CtScannerListener) Iterator(java.util.Iterator) CtInvocation(spoon.reflect.code.CtInvocation) Assert.assertNotNull(org.junit.Assert.assertNotNull) OverriddenMethodFilter(spoon.reflect.visitor.filter.OverriddenMethodFilter) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Factory(spoon.reflect.factory.Factory) CtVariable(spoon.reflect.declaration.CtVariable) OverriddenMethodQuery(spoon.reflect.visitor.filter.OverriddenMethodQuery) Tacos(spoon.test.filters.testclasses.Tacos) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) FilteringOperator(spoon.reflect.visitor.filter.FilteringOperator) CtTypeReference(spoon.reflect.reference.CtTypeReference) Assert.assertNull(org.junit.Assert.assertNull) ParentFunction(spoon.reflect.visitor.filter.ParentFunction) CtCFlowBreak(spoon.reflect.code.CtCFlowBreak) CtConsumer(spoon.reflect.visitor.chain.CtConsumer) CtClass(spoon.reflect.declaration.CtClass) ModifierKind(spoon.reflect.declaration.ModifierKind) CtFieldReference(spoon.reflect.reference.CtFieldReference) RegexFilter(spoon.reflect.visitor.filter.RegexFilter) Comparator(java.util.Comparator) Assert.assertEquals(org.junit.Assert.assertEquals) CtFunction(spoon.reflect.visitor.chain.CtFunction) CtElement(spoon.reflect.declaration.CtElement) Launcher(spoon.Launcher) CtConsumer(spoon.reflect.visitor.chain.CtConsumer) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 3 with Filter

use of spoon.reflect.visitor.Filter in project spoon by INRIA.

the class CtRenameLocalVariableRefactoring method detectNameConflicts.

@Override
protected void detectNameConflicts() {
    /*
		 * There can be these conflicts
		 * 1) target variable would shadow before declared variable (parameter, localVariable, catchVariable)
		 * --------------------------------------------------------------------------------------------------
		 */
    PotentialVariableDeclarationFunction potentialDeclarationFnc = new PotentialVariableDeclarationFunction(newName);
    CtVariable<?> var = getTarget().map(potentialDeclarationFnc).first();
    if (var != null) {
        if (var instanceof CtField) {
        /*
				 * we have found a field of same name.
				 * It is not problem, because variables can hide field declaration.
				 * Do nothing - OK
				 */
        } else if (potentialDeclarationFnc.isTypeOnTheWay()) {
            /*
				 * There is a local class declaration between future variable reference and variable declaration `var`.
				 * The found variable declaration `var` can be hidden by target variable with newName
				 * as long as there is no reference to `var` in visibility scope of the target variable.
				 * So search for such `var` reference now
				 */
            CtVariableReference<?> shadowedVar = target.map(new SiblingsFunction().includingSelf(true).mode(Mode.NEXT)).map(new VariableReferenceFunction(var)).first();
            if (shadowedVar != null) {
                // found variable reference, which would be shadowed by variable after rename.
                createNameConflictIssue(var, shadowedVar);
            } else {
            /*
					 * there is no local variable reference, which would be shadowed by variable after rename.
					 * OK
					 */
            }
        } else {
            /*
				 * the found variable is in conflict with target variable with newName
				 */
            createNameConflictIssue(var);
        }
    }
    /*
		 * 2) target variable is shadowed by later declared variable
		 * ---------------------------------------------------------
		 */
    final QueryDriver queryDriver = new QueryDriver();
    getTarget().map(new LocalVariableScopeFunction(queryDriver)).select(new Filter<CtElement>() {

        /**
         * return true for all CtVariables, which are in conflict
         */
        @Override
        public boolean matches(CtElement element) {
            if (element instanceof CtType<?>) {
                CtType<?> localClass = (CtType<?>) element;
                // TODO use faster hasField, implemented using map(new AllFieldsFunction()).select(new NameFilter(newName)).first()!=null
                Collection<CtFieldReference<?>> fields = localClass.getAllFields();
                for (CtFieldReference<?> fieldRef : fields) {
                    if (newName.equals(fieldRef.getSimpleName())) {
                        /*
							 * we have found a local class field, which will shadow input local variable if it's reference is in visibility scope of that field.
							 * Search for target variable reference in visibility scope of this field.
							 * If found than we cannot rename target variable to newName, because that reference would be shadowed
							 */
                        queryDriver.ignoreChildrenOf(element);
                        CtLocalVariableReference<?> shadowedVar = element.map(new LocalVariableReferenceFunction(target)).first();
                        if (shadowedVar != null) {
                            createNameConflictIssue(fieldRef.getFieldDeclaration(), shadowedVar);
                            return true;
                        }
                        return false;
                    }
                }
                return false;
            }
            if (element instanceof CtVariable<?>) {
                CtVariable<?> variable = (CtVariable<?>) element;
                if (newName.equals(variable.getSimpleName()) == false) {
                    // the variable with different name. Ignore it
                    return false;
                }
                // we have found a variable with new name
                if (variable instanceof CtField) {
                    throw new SpoonException("This should not happen. The children of local class which contains a field with new name should be skipped!");
                }
                if (variable instanceof CtCatchVariable || variable instanceof CtLocalVariable || variable instanceof CtParameter) {
                    /*
						 * we have found a catch variable or local variable or parameter with new name.
						 */
                    if (queryDriver.isInContextOfLocalClass()) {
                        /*
							 * We are in context of local class.
							 * This variable would shadow input local variable after rename
							 * so we cannot rename if there exist a local variable reference in variable visibility scope.
							 */
                        queryDriver.ignoreChildrenOf(variable.getParent());
                        CtQueryable searchScope;
                        if (variable instanceof CtLocalVariable) {
                            searchScope = variable.map(new SiblingsFunction().includingSelf(true).mode(Mode.NEXT));
                        } else {
                            searchScope = variable.getParent();
                        }
                        CtLocalVariableReference<?> shadowedVar = searchScope.map(new LocalVariableReferenceFunction(target)).first();
                        if (shadowedVar != null) {
                            // found local variable reference, which would be shadowed by variable after rename.
                            createNameConflictIssue(variable, shadowedVar);
                            return true;
                        }
                        // there is no local variable reference, which would be shadowed by variable after rename.
                        return false;
                    } else {
                        /*
							 * We are not in context of local class.
							 * So this variable is in conflict. Return it
							 */
                        createNameConflictIssue(variable);
                        return true;
                    }
                } else {
                    // Any new variable type???
                    throw new SpoonException("Unexpected variable " + variable.getClass().getName());
                }
            }
            return false;
        }
    }).first();
}
Also used : CtVariableReference(spoon.reflect.reference.CtVariableReference) SiblingsFunction(spoon.reflect.visitor.filter.SiblingsFunction) SpoonException(spoon.SpoonException) CtElement(spoon.reflect.declaration.CtElement) VariableReferenceFunction(spoon.reflect.visitor.filter.VariableReferenceFunction) LocalVariableReferenceFunction(spoon.reflect.visitor.filter.LocalVariableReferenceFunction) CtFieldReference(spoon.reflect.reference.CtFieldReference) CtParameter(spoon.reflect.declaration.CtParameter) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtType(spoon.reflect.declaration.CtType) Filter(spoon.reflect.visitor.Filter) CtField(spoon.reflect.declaration.CtField) CtQueryable(spoon.reflect.visitor.chain.CtQueryable) CtVariable(spoon.reflect.declaration.CtVariable) PotentialVariableDeclarationFunction(spoon.reflect.visitor.filter.PotentialVariableDeclarationFunction) CtCatchVariable(spoon.reflect.code.CtCatchVariable) LocalVariableScopeFunction(spoon.reflect.visitor.filter.LocalVariableScopeFunction) LocalVariableReferenceFunction(spoon.reflect.visitor.filter.LocalVariableReferenceFunction)

Example 4 with Filter

use of spoon.reflect.visitor.Filter in project spoon by INRIA.

the class ReplaceParametrizedTest method testContract.

@Test
public void testContract() throws Throwable {
    List<String> problems = new ArrayList<>();
    // contract: all elements are replaceable wherever they are in the model
    // this test puts them at all possible locations
    CtType<?> toTest = typeToTest.getModelInterface();
    CtElement o = factory.Core().create((Class<? extends CtElement>) toTest.getActualClass());
    for (MetamodelProperty mmField : typeToTest.getRoleToProperty().values()) {
        Class<?> argType = mmField.getItemValueType().getActualClass();
        if (!CtElement.class.isAssignableFrom(argType)) {
            continue;
        }
        CtTypeReference<?> itemType = mmField.getItemValueType();
        // special cases...
        if (itemType.getQualifiedName().equals(CtStatement.class.getName())) {
            // the children of CtLoop wraps CtStatement into an implicit CtBlock. So make a block directly to test plain get/set and not wrapping.
            itemType = factory.createCtTypeReference(CtBlock.class);
        }
        if (o.getClass().getSimpleName().equals("CtAnnotationFieldAccessImpl") && mmField.getRole() == CtRole.VARIABLE) {
            itemType = factory.createCtTypeReference(CtFieldReference.class);
        } else if (CtFieldAccess.class.isAssignableFrom(o.getClass()) && mmField.getRole() == CtRole.VARIABLE) {
            itemType = factory.createCtTypeReference(CtFieldReference.class);
        }
        CtElement argument = (CtElement) createCompatibleObject(itemType);
        assertNotNull(argument);
        // we create a fresh object
        CtElement receiver = ((CtElement) o).clone();
        RoleHandler rh = RoleHandlerHelper.getRoleHandler(o.getClass(), mmField.getRole());
        if (mmField.isUnsettable()) {
            try {
                // we invoke the setter
                invokeSetter(rh, receiver, argument);
            } catch (SpoonException e) {
                // ok this unsettable property has no setter at all
                return;
            }
            // this unsettable property has setter, but it should do nothing
            CtRole argumentsRoleInParent = argument.getRoleInParent();
            if (argumentsRoleInParent == null) {
                // OK - unsettable property set no value
                continue;
            }
            if (argumentsRoleInParent == mmField.getRole()) {
                problems.add("UnsettableProperty " + mmField + " sets the value");
            } else {
                if (mmField.isDerived()) {
                // it is OK, that setting of value into derived unsettable field influences other field
                // Example 1: CtCatchVariable.setType(x) influences result of getMultitype()
                // Example 2: CtEnumValue.setAssignment(x) influences result of getDefaultExpression()
                } else {
                    problems.add("UnsettableProperty " + mmField + " sets the value into different role " + argumentsRoleInParent);
                }
            }
            continue;
        }
        // we invoke the setter
        invokeSetter(rh, receiver, argument);
        // contract: a property setter sets properties that are visitable by a scanner
        CtElement finalArgument = argument;
        class Scanner extends CtScanner {

            boolean found = false;

            @Override
            public void scan(CtRole role, CtElement e) {
                super.scan(role, e);
                if (e == finalArgument) {
                    if (rh.getRole() == role || rh.getRole().getSuperRole() == role) {
                        found = true;
                        return;
                    }
                    // if (rh.getRole()==CtRole.TYPE && role==CtRole.MULTI_TYPE) {
                    // //CtCatchVaraible#type sets CtCatchVaraible#multiType - OK
                    // found = true;
                    // return;
                    // }
                    problems.add("Argument was set into " + rh.getRole() + " but was found in " + role);
                }
            }
        }
        ;
        Scanner s = new Scanner();
        receiver.accept(s);
        assertTrue("Settable field " + mmField.toString() + " should set value.\n" + getReport(problems), s.found);
        // contract: a property getter on the same role can be used to get the value back
        assertSame(argument, invokeGetter(rh, receiver));
        final CtElement argument2 = argument.clone();
        assertNotSame(argument, argument2);
        // we do the replace
        argument.replace(argument2);
        // the new element is indeed now in this AST
        assertTrue(receiver.getClass().getSimpleName() + " failed for " + mmField, receiver.getElements(new Filter<CtElement>() {

            @Override
            public boolean matches(CtElement element) {
                return element == argument2;
            }
        }).size() == 1);
    }
    if (problems.size() > 0) {
        fail(getReport(problems));
    }
}
Also used : CtScanner(spoon.reflect.visitor.CtScanner) SpoonException(spoon.SpoonException) CtElement(spoon.reflect.declaration.CtElement) RoleHandler(spoon.reflect.meta.RoleHandler) ArrayList(java.util.ArrayList) CtFieldReference(spoon.reflect.reference.CtFieldReference) CtBlock(spoon.reflect.code.CtBlock) CtStatement(spoon.reflect.code.CtStatement) Filter(spoon.reflect.visitor.Filter) MetamodelProperty(spoon.test.metamodel.MetamodelProperty) CtRole(spoon.reflect.path.CtRole) CtScanner(spoon.reflect.visitor.CtScanner) Test(org.junit.Test)

Example 5 with Filter

use of spoon.reflect.visitor.Filter 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)

Aggregations

CtElement (spoon.reflect.declaration.CtElement)6 CtType (spoon.reflect.declaration.CtType)4 Filter (spoon.reflect.visitor.Filter)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 SpoonException (spoon.SpoonException)3 CtLocalVariable (spoon.reflect.code.CtLocalVariable)3 CtStatement (spoon.reflect.code.CtStatement)3 CtField (spoon.reflect.declaration.CtField)3 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Comparator (java.util.Comparator)2 Iterator (java.util.Iterator)2 List (java.util.List)2 TreeSet (java.util.TreeSet)2 Predicate (java.util.function.Predicate)2 Collectors (java.util.stream.Collectors)2 Assert.assertArrayEquals (org.junit.Assert.assertArrayEquals)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertFalse (org.junit.Assert.assertFalse)2