Search in sources :

Example 1 with CtPathException

use of spoon.reflect.path.CtPathException in project spoon by INRIA.

the class CtRolePathElement method getElements.

@Override
public Collection<CtElement> getElements(Collection<CtElement> roots) {
    Collection<CtElement> matchs = new LinkedList<>();
    for (CtElement root : roots) {
        RoleHandler roleHandler = RoleHandlerHelper.getOptionalRoleHandler(root.getClass(), getRole());
        if (roleHandler != null) {
            switch(roleHandler.getContainerKind()) {
                case SINGLE:
                    if (roleHandler.getValue(root) != null) {
                        matchs.add(roleHandler.getValue(root));
                    }
                    break;
                case LIST:
                    if (getArguments().containsKey("index")) {
                        int index = Integer.parseInt(getArguments().get("index"));
                        if (index < roleHandler.asList(root).size()) {
                            matchs.add((CtElement) roleHandler.asList(root).get(index));
                        }
                    } else {
                        matchs.addAll(roleHandler.asList(root));
                    }
                    break;
                case SET:
                    if (getArguments().containsKey("name")) {
                        String name = getArguments().get("name");
                        try {
                            CtElement match = getFromSet(roleHandler.asSet(root), name);
                            if (match != null) {
                                matchs.add(match);
                            }
                        } catch (CtPathException e) {
                        // System.err.println("[ERROR] Element not found for name: " + name);
                        // No element found for name.
                        }
                    } else {
                        matchs.addAll(roleHandler.asSet(root));
                    }
                    break;
                case MAP:
                    if (getArguments().containsKey("key")) {
                        String name = getArguments().get("key");
                        if (roleHandler.asMap(root).containsKey(name)) {
                            matchs.add((CtElement) roleHandler.asMap(root).get(name));
                        }
                    } else {
                        Map<String, CtElement> map = roleHandler.asMap(root);
                        matchs.addAll(map.values());
                    }
                    break;
            }
        }
    }
    return matchs;
}
Also used : CtElement(spoon.reflect.declaration.CtElement) RoleHandler(spoon.reflect.meta.RoleHandler) LinkedList(java.util.LinkedList) CtPathException(spoon.reflect.path.CtPathException)

Example 2 with CtPathException

use of spoon.reflect.path.CtPathException in project spoon by INRIA.

the class PathTest method exceptionTest.

@Test
public void exceptionTest() {
    try {
        new CtPathStringBuilder().fromString("/CtClassss");
        fail();
    } catch (CtPathException e) {
        assertEquals("Unable to locate element with type CtClassss in Spoon model", e.getMessage());
    }
}
Also used : CtPathStringBuilder(spoon.reflect.path.CtPathStringBuilder) CtPathException(spoon.reflect.path.CtPathException) Test(org.junit.Test)

Example 3 with CtPathException

use of spoon.reflect.path.CtPathException in project spoon by INRIA.

the class PathTest method testGetPathFromNonParent.

@Test
public void testGetPathFromNonParent() throws Exception {
    CtMethod fooMethod = (CtMethod) new CtPathStringBuilder().fromString(".spoon.test.path.Foo.foo").evaluateOn(factory.getModel().getRootPackage()).iterator().next();
    CtMethod barMethod = (CtMethod) new CtPathStringBuilder().fromString(".spoon.test.path.Foo.bar").evaluateOn(factory.getModel().getRootPackage()).iterator().next();
    try {
        new CtElementPathBuilder().fromElement(fooMethod, barMethod);
        fail("No path should be found to .spoon.test.path.Foo.foo from .spoon.test.path.Foo.bar");
    } catch (CtPathException e) {
    }
}
Also used : CtPathStringBuilder(spoon.reflect.path.CtPathStringBuilder) CtMethod(spoon.reflect.declaration.CtMethod) CtElementPathBuilder(spoon.reflect.path.CtElementPathBuilder) CtPathException(spoon.reflect.path.CtPathException) Test(org.junit.Test)

Aggregations

CtPathException (spoon.reflect.path.CtPathException)3 Test (org.junit.Test)2 CtPathStringBuilder (spoon.reflect.path.CtPathStringBuilder)2 LinkedList (java.util.LinkedList)1 CtElement (spoon.reflect.declaration.CtElement)1 CtMethod (spoon.reflect.declaration.CtMethod)1 RoleHandler (spoon.reflect.meta.RoleHandler)1 CtElementPathBuilder (spoon.reflect.path.CtElementPathBuilder)1