Search in sources :

Example 1 with CtPathImpl

use of spoon.reflect.path.impl.CtPathImpl 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;
}
Also used : CtRolePathElement(spoon.reflect.path.impl.CtRolePathElement) Matcher(java.util.regex.Matcher) CtPathImpl(spoon.reflect.path.impl.CtPathImpl) CtTypedNameElement(spoon.reflect.path.impl.CtTypedNameElement) CtPathElement(spoon.reflect.path.impl.CtPathElement) CtNamedPathElement(spoon.reflect.path.impl.CtNamedPathElement)

Example 2 with CtPathImpl

use of spoon.reflect.path.impl.CtPathImpl 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;
}
Also used : CtElement(spoon.reflect.declaration.CtElement) RoleHandler(spoon.reflect.meta.RoleHandler) CtPathElement(spoon.reflect.path.impl.CtPathElement) CtRolePathElement(spoon.reflect.path.impl.CtRolePathElement) CtPathImpl(spoon.reflect.path.impl.CtPathImpl) CtReference(spoon.reflect.reference.CtReference) List(java.util.List) CtNamedElement(spoon.reflect.declaration.CtNamedElement) Map(java.util.Map)

Aggregations

CtPathElement (spoon.reflect.path.impl.CtPathElement)2 CtPathImpl (spoon.reflect.path.impl.CtPathImpl)2 CtRolePathElement (spoon.reflect.path.impl.CtRolePathElement)2 List (java.util.List)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 CtElement (spoon.reflect.declaration.CtElement)1 CtNamedElement (spoon.reflect.declaration.CtNamedElement)1 RoleHandler (spoon.reflect.meta.RoleHandler)1 CtNamedPathElement (spoon.reflect.path.impl.CtNamedPathElement)1 CtTypedNameElement (spoon.reflect.path.impl.CtTypedNameElement)1 CtReference (spoon.reflect.reference.CtReference)1