Search in sources :

Example 1 with CtTypedNameElement

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

the class CtPathBuilder method type.

/**
 * Match on element of a given type.
 */
public <T extends CtElement> CtPathBuilder type(Class<T> type, String[]... args) {
    CtTypedNameElement e = new CtTypedNameElement(type);
    if (args != null) {
        for (String[] arg : args) {
            e.addArgument(arg[0], arg[1]);
        }
    }
    elements.add(e);
    return this;
}
Also used : CtTypedNameElement(spoon.reflect.path.impl.CtTypedNameElement)

Example 2 with CtTypedNameElement

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

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