defines the path to a
in a model. For example,
.spoon.test.path.testclasses.Foo.foo#body#statement[index=0] represents the first statement of the body of method foo.
CtPathis based on: names of elements (eg
foo), and roles of elements with respect to their parent (eg
A role is a relation between two AST nodes.
For instance, a "then" branch in a if/then/else is a role (and not an node). All roles can be found in
CtRole. In addition, each getter or setter in the metamodel is annotated with its role.
To build a path, there are several possibilities:
it creates a path object from a string according to a
syntax inspired from XPath and CSS selectors.
- the low-level
CtPathBuilder (javadoc), it defines a fluent api to build your path.
To evaluate a path, ie getting the elements represented by it, use
path = new CtPathStringBuilder().fromString(".spoon.test.path.testclasses.Foo.foo#body#statement[index=0]");
List<CtElement> l = path.evaluateOn(root)
CtPathStringBuilder exposes only one method to build a path from a string.
builds a path from a string representation.
For instance, if we want the first statement in the body of method
in the class
CtPathBuilder exposes the following methods:
name(String, String) (javadoc)
adds a name matcher to the current path.
type(Class, String) (javadoc)
matches on element of a given type.
role(CtPathRole, String) (javadoc)
matches on elements by their role (where
CtPathRole gives all constants supported).
matches only on elements child of current one.
matches on any child and sub-children.
For instance, if we want all elements named by "toto" and with a default value in
a project. Use
CtPathBuilder like the example below.
The corresponding string syntax would be:
The order in instructions is important and have a meaning. These two pieces of code below have
a different meaning. The first one takes all toto elements in the project. The second takes
the first element named by "toto" at the root of your project and after, makes a search recursively
in your project according to the rest of your path request.