Class 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 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
      • getNodes

        public Map<Long,​CPGNode> getNodes()
        Returns:
        the nodes
      • getEdges

        public Set<CPGEdge> getEdges()
        Returns:
        the edges
      • 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 source
        target - the edge target
        type - 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 source
        target - the edge target
        type - the edge type
        properties - 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 CPGNodeStream<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 name
        name - the method name
        descriptor - 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 name
        name - 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 name
        name - the target method name
        descriptor - 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
      • getAllEdges

        public Set<CPGEdge> getAllEdges()
        Returns:
        an unmodifiable view of all edges