use of spoon.reflect.path.impl.CtRolePathElement in project spoon by INRIA.
the class CtPathStringBuilder method fromString.
/**
* Build path from a string representation.
*
* for example:
* new CtPathBuilder().fromString(".spoon.test.path.Foo.foo#statement[index=0]")
* Match the first statement of method foo from class spoon.test.path.Foo.
*
* Some specials characters
* . : match with the given name
* # : match with a CtPathRole
* / : match with a element type (for example, to match all classes, use /CtClass
*/
public CtPath fromString(String pathStr) throws CtPathException {
Matcher matcher = pathPattern.matcher(pathStr);
CtPathImpl path = new CtPathImpl();
while (matcher.find()) {
String kind = matcher.group(1);
CtPathElement pathElement = null;
if (CtNamedPathElement.STRING.equals(kind)) {
pathElement = new CtNamedPathElement(matcher.group(2));
} else if (CtTypedNameElement.STRING.equals(kind)) {
pathElement = new CtTypedNameElement(load(matcher.group(2)));
} else if (CtRolePathElement.STRING.equals(kind)) {
pathElement = new CtRolePathElement(CtRole.fromName(matcher.group(2)));
}
String args = matcher.group(4);
if (args != null) {
for (String arg : args.split(";")) {
Matcher argmatcher = argumentPattern.matcher(arg);
if (argmatcher.matches()) {
pathElement.addArgument(argmatcher.group(1), argmatcher.group(2));
}
}
}
path.addLast(pathElement);
}
return path;
}
use of spoon.reflect.path.impl.CtRolePathElement in project spoon by INRIA.
the class CtElementPathBuilder method fromElement.
/**
* Build path to a CtElement el, from one of its parent.
*
* @throws CtPathException is thrown when root is not a parent of el.
*
* @param el : the element to which the CtPath leads to
* @param root : Starting point of the CtPath
* @return CtPath from root to el
*/
public CtPath fromElement(CtElement el, CtElement root) throws CtPathException {
CtPathImpl path = new CtPathImpl();
CtElement cur = el;
while (cur != root) {
CtElement parent = cur.getParent();
CtRole role = cur.getRoleInParent();
if (role == null) {
throw new CtPathException();
}
RoleHandler roleHandler = RoleHandlerHelper.getOptionalRoleHandler(parent.getClass(), role);
if (roleHandler == null) {
throw new CtPathException();
}
CtPathElement pathElement = new CtRolePathElement(role);
switch(roleHandler.getContainerKind()) {
case SINGLE:
break;
case LIST:
// Element needs to be differentiated from its brothers
List list = roleHandler.asList(parent);
// Assumes that List's order is deterministic.
// Can't be replaced by list.indexOf(cur)
// Because objects must be the same (and not just equals)
int index = 0;
for (Object o : list) {
if (o == cur) {
break;
}
index++;
}
pathElement.addArgument("index", index + "");
break;
case SET:
String name;
if (cur instanceof CtNamedElement) {
name = ((CtNamedElement) cur).getSimpleName();
} else if (cur instanceof CtReference) {
name = ((CtReference) cur).getSimpleName();
} else {
throw new CtPathException();
}
pathElement.addArgument("name", name);
break;
case MAP:
Map map = roleHandler.asMap(parent);
String key = null;
for (Object o : map.keySet()) {
if (map.get(o) == cur) {
key = (String) o;
break;
}
}
if (key == null) {
throw new CtPathException();
} else {
pathElement.addArgument("key", key);
}
break;
}
cur = parent;
path.addFirst(pathElement);
}
return path;
}
use of spoon.reflect.path.impl.CtRolePathElement in project spoon by INRIA.
the class CtPathBuilder method role.
/**
* Match on elements by their role.
*
* @see CtRole
*/
public CtPathBuilder role(CtRole role, String[]... args) {
CtRolePathElement e = new CtRolePathElement(role);
if (args != null) {
for (String[] arg : args) {
e.addArgument(arg[0], arg[1]);
}
}
elements.add(e);
return this;
}
Aggregations