defines the path to a
in a model respectively to another element. A
CtPath can also be used
to make a query to get elements and is based on three concepts:
names of elements, types of elements and roles of code elements.
A role is a relation between two AST nodes, encoded as an AST node field.
For instance, a "then" branch in a if/then/else is a role (and not an node).
To build a path, you have two possibilities:
CtPathBuilder defines a fluent api to build your path.
CtPathStringBuilder creates a path object from a string according to a
syntax inspired from XPath and CSS selectors.
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.