Package com.tonic.analysis.cpg.query
Class CPGQuery
- java.lang.Object
-
- com.tonic.analysis.cpg.query.CPGQuery
-
public class CPGQuery extends Object
Fluent, stream-backed traversal over a CPG.
-
-
Constructor Summary
Constructors Constructor Description CPGQuery(CodePropertyGraph cpg)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description CPGQueryall()Selects every node in the graph.CPGQueryastChildren()Steps to direct AST children.CPGQueryastDescendants()Expands to every transitive AST descendant, excluding the starting nodes.CPGQueryastParent()Steps to AST parents.CPGQueryblocks()Selects all basic block nodes.CPGQueryboth(CPGEdgeType... edgeTypes)Steps along edges in both directions.CPGQuerycallees()Steps to direct callees along call edges.CPGQuerycalleesTransitive()Expands to every transitive callee, excluding the starting nodes.CPGQuerycallers()Steps to direct callers along call edges.CPGQuerycallersTransitive()Expands to every transitive caller, excluding the starting nodes.CPGQuerycallSites()Selects all call site nodes.CPGQuerycallsTo(String owner, String method)Selects call sites targeting a method name regardless of descriptor.CPGQuerycallsTo(String owner, String method, String descriptor)Selects call sites targeting an exact method signature.CPGQuerycfgNext()Steps to direct control-flow successors.CPGQuerycfgPrev()Steps to direct control-flow predecessors.CPGQuerycfgReachable()Expands to every node transitively reachable along control-flow edges, including the starting nodes.CPGQuerycontrolDependents()Steps to nodes control-dependent on the current ones.longcount()Terminates the query by counting its nodes.CPGQuerydataFlowIn()Steps backwards along data-flow edges to definition sources.CPGQuerydataFlowOut()Steps forwards along data-flow edges to dependent uses.CPGQuerydedup()Removes duplicate nodes from the result.booleanexists()Terminates the query by testing for any result.CPGQueryfilter(Predicate<CPGNode> predicate)Keeps only nodes matching a predicate.CPGQueryfilterType(CPGNodeType... types)Keeps only nodes of the given node types.Optional<CPGNode>first()Terminates the query with its first node.CPGQueryflatMap(Function<CPGNode,Stream<CPGNode>> mapper)Replaces each node with the nodes produced by the mapper.voidforEach(Consumer<CPGNode> action)Terminates the query by applying an action to each node.CodePropertyGraphgetCpg()Stream<CPGNode>getCurrentNodes()CPGQueryhasProperty(String key)Keeps only nodes carrying a property.CPGQueryhasProperty(String key, Object value)Keeps only nodes carrying a property with the given value.CPGQueryin(CPGEdgeType... edgeTypes)Steps along incoming edges to their sources.CPGQueryinstructions()Selects all instruction nodes.CPGQueryinstructions(Class<? extends IRInstruction> type)Selects instruction nodes wrapping a specific IR instruction class.CPGQueryisAllocation()Keeps only object or array allocation instructions.CPGQueryisBranch()Keeps only conditional branch instructions.CPGQueryisFieldAccess()Keeps only field access instructions.CPGQueryisMethodCall()Keeps only call site nodes.CPGQueryisReturn()Keeps only return instructions.CPGQuerylimit(int n)Truncates the result to at most n nodes.<T> Stream<T>map(Function<CPGNode,T> mapper)Terminates the query by mapping each node to a value.CPGQuerymethod(String owner, String name, String descriptor)Selects a single method by exact signature.CPGQuerymethods()Selects all method nodes.CPGQuerymethods(String namePattern)Selects method nodes whose name matches a regex.CPGQuerynameMatches(String regex)Keeps only nodes whose name property matches a regex.CPGQueryout(CPGEdgeType... edgeTypes)Steps along outgoing edges to their targets.CPGQueryownerMatches(String regex)Keeps only nodes whose owner or target owner property matches a regex.CPGQueryskip(int n)Drops the first n nodes.List<CPGNode>toList()Terminates the query into a list.Set<CPGNode>toSet()Terminates the query into an insertion-ordered set.Stream<CPGNode>toStream()CPGQuerywhere(CPGQuery subQuery)Keeps only the nodes the sub-query also yields.CPGQuerywhereNot(CPGQuery subQuery)Keeps only the nodes the sub-query does not yield.
-
-
-
Constructor Detail
-
CPGQuery
public CPGQuery(CodePropertyGraph cpg)
- Parameters:
cpg- the graph to query
-
-
Method Detail
-
getCpg
public CodePropertyGraph getCpg()
- Returns:
- the cpg
-
methods
public CPGQuery methods()
Selects all method nodes.- Returns:
- a query positioned on every method
-
methods
public CPGQuery methods(String namePattern)
Selects method nodes whose name matches a regex.- Parameters:
namePattern- the regex the method name must match in full- Returns:
- a query positioned on the matching methods
-
method
public CPGQuery method(String owner, String name, String descriptor)
Selects a single method by exact signature.- Parameters:
owner- the declaring class internal namename- the method namedescriptor- the method descriptor- Returns:
- a query positioned on the method, or an empty query
-
instructions
public CPGQuery instructions()
Selects all instruction nodes.- Returns:
- a query positioned on every instruction
-
instructions
public CPGQuery instructions(Class<? extends IRInstruction> type)
Selects instruction nodes wrapping a specific IR instruction class.- Parameters:
type- the IR instruction class- Returns:
- a query positioned on the matching instructions
-
callSites
public CPGQuery callSites()
Selects all call site nodes.- Returns:
- a query positioned on every call site
-
callsTo
public CPGQuery callsTo(String owner, String method)
Selects call sites targeting a method name regardless of descriptor.- Parameters:
owner- the target class internal namemethod- the target method name- Returns:
- a query positioned on the matching call sites
-
callsTo
public CPGQuery callsTo(String owner, String method, String descriptor)
Selects call sites targeting an exact method signature.- Parameters:
owner- the target class internal namemethod- the target method namedescriptor- the target method descriptor- Returns:
- a query positioned on the matching call sites
-
blocks
public CPGQuery blocks()
Selects all basic block nodes.- Returns:
- a query positioned on every block
-
all
public CPGQuery all()
Selects every node in the graph.- Returns:
- a query positioned on all nodes
-
out
public CPGQuery out(CPGEdgeType... edgeTypes)
Steps along outgoing edges to their targets.- Parameters:
edgeTypes- the edge types to follow; empty follows every edge- Returns:
- a query positioned on the reached nodes
-
in
public CPGQuery in(CPGEdgeType... edgeTypes)
Steps along incoming edges to their sources.- Parameters:
edgeTypes- the edge types to follow; empty follows every edge- Returns:
- a query positioned on the reached nodes
-
both
public CPGQuery both(CPGEdgeType... edgeTypes)
Steps along edges in both directions.- Parameters:
edgeTypes- the edge types to follow; empty follows every edge- Returns:
- a query positioned on the reached nodes
-
cfgNext
public CPGQuery cfgNext()
Steps to direct control-flow successors.- Returns:
- a query positioned on the successor nodes
-
cfgPrev
public CPGQuery cfgPrev()
Steps to direct control-flow predecessors.- Returns:
- a query positioned on the predecessor nodes
-
cfgReachable
public CPGQuery cfgReachable()
Expands to every node transitively reachable along control-flow edges, including the starting nodes.- Returns:
- a query positioned on the reachable nodes
-
astParent
public CPGQuery astParent()
Steps to AST parents.- Returns:
- a query positioned on the parent nodes
-
astChildren
public CPGQuery astChildren()
Steps to direct AST children.- Returns:
- a query positioned on the child nodes
-
astDescendants
public CPGQuery astDescendants()
Expands to every transitive AST descendant, excluding the starting nodes.- Returns:
- a query positioned on the descendant nodes
-
dataFlowIn
public CPGQuery dataFlowIn()
Steps backwards along data-flow edges to definition sources.- Returns:
- a query positioned on the source nodes
-
dataFlowOut
public CPGQuery dataFlowOut()
Steps forwards along data-flow edges to dependent uses.- Returns:
- a query positioned on the dependent nodes
-
controlDependents
public CPGQuery controlDependents()
Steps to nodes control-dependent on the current ones.- Returns:
- a query positioned on the dependent nodes
-
callers
public CPGQuery callers()
Steps to direct callers along call edges.- Returns:
- a query positioned on the calling nodes
-
callees
public CPGQuery callees()
Steps to direct callees along call edges.- Returns:
- a query positioned on the called nodes
-
callersTransitive
public CPGQuery callersTransitive()
Expands to every transitive caller, excluding the starting nodes.- Returns:
- a query positioned on the calling nodes
-
calleesTransitive
public CPGQuery calleesTransitive()
Expands to every transitive callee, excluding the starting nodes.- Returns:
- a query positioned on the called nodes
-
filter
public CPGQuery filter(Predicate<CPGNode> predicate)
Keeps only nodes matching a predicate.- Parameters:
predicate- the node test- Returns:
- a query positioned on the matching nodes
-
filterType
public CPGQuery filterType(CPGNodeType... types)
Keeps only nodes of the given node types.- Parameters:
types- the accepted node types- Returns:
- a query positioned on the matching nodes
-
hasProperty
public CPGQuery hasProperty(String key)
Keeps only nodes carrying a property.- Parameters:
key- the property key- Returns:
- a query positioned on the matching nodes
-
hasProperty
public CPGQuery hasProperty(String key, Object value)
Keeps only nodes carrying a property with the given value.- Parameters:
key- the property keyvalue- the required value- Returns:
- a query positioned on the matching nodes
-
where
public CPGQuery where(CPGQuery subQuery)
Keeps only the nodes the sub-query also yields.- Parameters:
subQuery- the query whose nodes are kept- Returns:
- a query positioned on the matching nodes
-
whereNot
public CPGQuery whereNot(CPGQuery subQuery)
Keeps only the nodes the sub-query does not yield.- Parameters:
subQuery- the query whose nodes are dropped- Returns:
- a query positioned on the matching nodes
-
isMethodCall
public CPGQuery isMethodCall()
Keeps only call site nodes.- Returns:
- a query positioned on the matching nodes
-
isFieldAccess
public CPGQuery isFieldAccess()
Keeps only field access instructions.- Returns:
- a query positioned on the matching nodes
-
isAllocation
public CPGQuery isAllocation()
Keeps only object or array allocation instructions.- Returns:
- a query positioned on the matching nodes
-
isReturn
public CPGQuery isReturn()
Keeps only return instructions.- Returns:
- a query positioned on the matching nodes
-
isBranch
public CPGQuery isBranch()
Keeps only conditional branch instructions.- Returns:
- a query positioned on the matching nodes
-
nameMatches
public CPGQuery nameMatches(String regex)
Keeps only nodes whose name property matches a regex.- Parameters:
regex- the pattern the name must match in full- Returns:
- a query positioned on the matching nodes
-
ownerMatches
public CPGQuery ownerMatches(String regex)
Keeps only nodes whose owner or target owner property matches a regex.- Parameters:
regex- the pattern the owner must match in full- Returns:
- a query positioned on the matching nodes
-
limit
public CPGQuery limit(int n)
Truncates the result to at most n nodes.- Parameters:
n- the maximum number of nodes to keep- Returns:
- a query positioned on the truncated set
-
skip
public CPGQuery skip(int n)
Drops the first n nodes.- Parameters:
n- the number of nodes to skip- Returns:
- a query positioned on the remaining nodes
-
dedup
public CPGQuery dedup()
Removes duplicate nodes from the result.- Returns:
- a query positioned on the distinct nodes
-
map
public <T> Stream<T> map(Function<CPGNode,T> mapper)
Terminates the query by mapping each node to a value.- Type Parameters:
T- the result element type- Parameters:
mapper- the node transform- Returns:
- a stream of mapped values
-
flatMap
public CPGQuery flatMap(Function<CPGNode,Stream<CPGNode>> mapper)
Replaces each node with the nodes produced by the mapper.- Parameters:
mapper- the node expansion- Returns:
- a query positioned on the produced nodes
-
toList
public List<CPGNode> toList()
Terminates the query into a list.- Returns:
- the result nodes in encounter order
-
toSet
public Set<CPGNode> toSet()
Terminates the query into an insertion-ordered set.- Returns:
- the distinct result nodes
-
first
public Optional<CPGNode> first()
Terminates the query with its first node.- Returns:
- the first result node, if any
-
count
public long count()
Terminates the query by counting its nodes.- Returns:
- the number of result nodes
-
exists
public boolean exists()
Terminates the query by testing for any result.- Returns:
- whether the query matched at least one node
-
-