Package com.tonic.analysis.cpg
Class CodePropertyGraph
- java.lang.Object
-
- com.tonic.analysis.cpg.CodePropertyGraph
-
public class CodePropertyGraph extends Object
Unified code property graph over a class pool, combining AST, CFG, dependence, and call edges in one node/edge store with an index and a fluent query entry point.
-
-
Constructor Summary
Constructors Constructor Description CodePropertyGraph(ClassPool classPool)Creates an empty graph over the given class pool.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddEdge(CPGEdge edge)Adds an edge and links it into both endpoint nodes; duplicates are ignored.voidaddEdge(CPGNode source, CPGNode target, CPGEdgeType type)Adds an edge of the given type between two nodes.voidaddEdge(CPGNode source, CPGNode target, CPGEdgeType type, Map<String,Object> properties)Adds an edge of the given type carrying the supplied properties.voidaddNode(CPGNode node)Adds a node to the graph and its index.longallocateNodeId()Reserves the next unique node id.Stream<CPGEdge>edges(CPGEdgeType type)Streams all edges of the given edge type.Set<CPGEdge>getAllEdges()Collection<CPGNode>getAllNodes()Stream<CallSiteNode>getCallsTo(String owner, String name)Streams call sites targeting a method name regardless of descriptor.Stream<CallSiteNode>getCallsTo(String owner, String name, String descriptor)Streams call sites targeting an exact method signature.ClassPoolgetClassPool()intgetEdgeCount()intgetEdgeCount(CPGEdgeType type)Counts edges of one type.Set<CPGEdge>getEdges()Map<CPGEdgeType,Integer>getEdgeTypeCounts()Tallies edge counts per edge type.CPGIndexgetIndex()Stream<InstructionNode>getInstructionsOfType(Class<? extends IRInstruction> type)Streams instruction nodes wrapping a specific IR instruction class.Optional<MethodNode>getMethod(String owner, String name, String descriptor)Looks up a method node by its exact signature.intgetMethodCount()longgetNextNodeId()CPGNodegetNode(long id)Looks up a node by id.intgetNodeCount()Map<Long,CPGNode>getNodes()Stream<CPGNode>nodes(CPGNodeType type)Streams all nodes of the given node type via the index.<T extends CPGNode>
Stream<T>nodes(Class<T> nodeType)Streams all nodes that are instances of the given class.CPGQueryquery()Starts a fluent query over this graph.voidremoveEdge(CPGEdge edge)Removes an edge and unlinks it from both endpoint nodes.voidremoveNode(CPGNode node)Removes a node along with every edge touching it.StringtoString()
-
-
-
Constructor Detail
-
CodePropertyGraph
public CodePropertyGraph(ClassPool classPool)
Creates an empty graph over the given class pool.- Parameters:
classPool- the classes this graph describes
-
-
Method Detail
-
getClassPool
public ClassPool getClassPool()
- Returns:
- the class pool
-
getIndex
public CPGIndex getIndex()
- Returns:
- the index
-
getNextNodeId
public long getNextNodeId()
- Returns:
- the next node id
-
allocateNodeId
public long allocateNodeId()
Reserves the next unique node id.- Returns:
- the allocated id
-
addNode
public void addNode(CPGNode node)
Adds a node to the graph and its index.- Parameters:
node- the node to add
-
removeNode
public void removeNode(CPGNode node)
Removes a node along with every edge touching it.- Parameters:
node- the node to remove
-
addEdge
public void addEdge(CPGEdge edge)
Adds an edge and links it into both endpoint nodes; duplicates are ignored.- Parameters:
edge- the edge to add
-
addEdge
public void addEdge(CPGNode source, CPGNode target, CPGEdgeType type)
Adds an edge of the given type between two nodes.- Parameters:
source- the edge sourcetarget- the edge targettype- the edge type
-
addEdge
public void addEdge(CPGNode source, CPGNode target, CPGEdgeType type, Map<String,Object> properties)
Adds an edge of the given type carrying the supplied properties.- Parameters:
source- the edge sourcetarget- the edge targettype- the edge typeproperties- initial edge properties
-
removeEdge
public void removeEdge(CPGEdge edge)
Removes an edge and unlinks it from both endpoint nodes.- Parameters:
edge- the edge to remove
-
getNode
public CPGNode getNode(long id)
Looks up a node by id.- Parameters:
id- the node id- Returns:
- the node, or null if absent
-
nodes
public <T extends CPGNode> Stream<T> nodes(Class<T> nodeType)
Streams all nodes that are instances of the given class.- Type Parameters:
T- the node class- Parameters:
nodeType- the node class to filter by- Returns:
- a stream of matching nodes
-
nodes
public Stream<CPGNode> nodes(CPGNodeType type)
Streams all nodes of the given node type via the index.- Parameters:
type- the node type- Returns:
- a stream of matching nodes
-
edges
public Stream<CPGEdge> edges(CPGEdgeType type)
Streams all edges of the given edge type.- Parameters:
type- the edge type- Returns:
- a stream of matching edges
-
getMethod
public Optional<MethodNode> getMethod(String owner, String name, String descriptor)
Looks up a method node by its exact signature.- Parameters:
owner- the declaring class internal namename- the method namedescriptor- the method descriptor- Returns:
- the method node, if present
-
getCallsTo
public Stream<CallSiteNode> getCallsTo(String owner, String name)
Streams call sites targeting a method name regardless of descriptor.- Parameters:
owner- the target class internal namename- the target method name- Returns:
- a stream of matching call sites
-
getCallsTo
public Stream<CallSiteNode> getCallsTo(String owner, String name, String descriptor)
Streams call sites targeting an exact method signature.- Parameters:
owner- the target class internal namename- the target method namedescriptor- the target method descriptor- Returns:
- a stream of matching call sites
-
getInstructionsOfType
public Stream<InstructionNode> getInstructionsOfType(Class<? extends IRInstruction> type)
Streams instruction nodes wrapping a specific IR instruction class.- Parameters:
type- the IR instruction class- Returns:
- a stream of matching instruction nodes
-
query
public CPGQuery query()
Starts a fluent query over this graph.- Returns:
- a new query
-
getNodeCount
public int getNodeCount()
- Returns:
- the number of nodes
-
getEdgeCount
public int getEdgeCount()
- Returns:
- the number of edges
-
getMethodCount
public int getMethodCount()
- Returns:
- the number of method nodes
-
getEdgeCount
public int getEdgeCount(CPGEdgeType type)
Counts edges of one type.- Parameters:
type- the edge type- Returns:
- the number of edges of that type
-
getEdgeTypeCounts
public Map<CPGEdgeType,Integer> getEdgeTypeCounts()
Tallies edge counts per edge type.- Returns:
- a map from edge type to occurrence count
-
getAllNodes
public Collection<CPGNode> getAllNodes()
- Returns:
- an unmodifiable view of all nodes
-
-