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;
}
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());
}
}
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) {
}
}
Aggregations