Search in sources :

Example 1 with CtNamedPathElement

use of spoon.reflect.path.impl.CtNamedPathElement in project spoon by INRIA.

the class CtPathBuilder method name.

/**
 * Add a name matcher to this path.
 *
 * @param name
 * @param args
 * @return
 */
public CtPathBuilder name(String name, String[]... args) {
    CtNamedPathElement e = new CtNamedPathElement(name);
    if (args != null) {
        for (String[] arg : args) {
            e.addArgument(arg[0], arg[1]);
        }
    }
    elements.add(e);
    return this;
}
Also used : CtNamedPathElement(spoon.reflect.path.impl.CtNamedPathElement)

Example 2 with CtNamedPathElement

use of spoon.reflect.path.impl.CtNamedPathElement 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)

Aggregations

CtNamedPathElement (spoon.reflect.path.impl.CtNamedPathElement)2 Matcher (java.util.regex.Matcher)1 CtPathElement (spoon.reflect.path.impl.CtPathElement)1 CtPathImpl (spoon.reflect.path.impl.CtPathImpl)1 CtRolePathElement (spoon.reflect.path.impl.CtRolePathElement)1 CtTypedNameElement (spoon.reflect.path.impl.CtTypedNameElement)1