Skip to content

Cheat sheet

A single-page reference for the common CLDK calls. Construct one analysis object with a per-language factory, then query it. For background and walkthroughs, see What is CLDK?, Core concepts, and the Quickstart.

pip install cldk

Reachability queries use networkx. It is a CLDK dependency; install it explicitly if you query graphs directly:

pip install cldk networkx

Use the per-language factory for the language you want: CLDK.java(...), CLDK.python(...), CLDK.typescript(...), or CLDK.c(project_path). The analysis level determines how much is computed.

from cldk import CLDK
from cldk.analysis import AnalysisLevel
# Default level is symbol_table. Use call_graph for callers/callees/reachability.
analysis = CLDK.java(
project_path="commons-cli",
analysis_level=AnalysisLevel.call_graph,
)
print(len(analysis.get_classes()), "classes") # -> 23 classes
ArgumentValueNotes
factoryCLDK.java / CLDK.python / CLDK.typescript / CLDK.cSelects the language facade.
project_pathpath to checkoutThe project to analyze.
analysis_levelAnalysisLevel.symbol_table (default) / AnalysisLevel.call_graphCall graph, callers, callees, and reachability need call_graph.
target_fileslist of paths (optional)Restrict analysis to these files.
eagerbool (default False)Force regeneration of cached analysis.
backendconfig object (optional)Selects the backend by type (see below). Omit for the default codeanalyzer backend.

The backend is chosen by the type of the backend= config object, imported from cldk.analysis.commons.backend_config. Omit backend= for the default in-process codeanalyzer backend. cache_dir, use_codeql, and use_ray now live inside these configs.

from cldk import CLDK
from cldk.analysis.commons.backend_config import (
CodeAnalyzerConfig,
PyCodeAnalyzerConfig,
Neo4jConnectionConfig,
)
# Default codeanalyzer backend, with an explicit cache root (optional).
analysis = CLDK.java(
project_path="commons-cli",
backend=CodeAnalyzerConfig(cache_dir="/tmp/cache"),
)
# Python codeanalyzer backend with call-graph knobs.
analysis = CLDK.python(
project_path="my_pkg",
backend=PyCodeAnalyzerConfig(use_codeql=True, use_ray=False),
)
# Read-only Neo4j backend (Java / Python / TypeScript); the graph is populated out of band.
analysis = CLDK.typescript(
backend=Neo4jConnectionConfig(uri="bolt://localhost:7687", username="neo4j", password="neo4j", application_name="my_project"),
)
Config typeBackendNotes
(omit) / CodeAnalyzerConfigIn-process codeanalyzerDefault for Java/TypeScript. cache_dir only.
PyCodeAnalyzerConfigIn-process codeanalyzer (Python)Default for Python. Adds use_codeql / use_ray.
Neo4jConnectionConfig(uri=...)Read-only Neo4j/CypherJava, Python, and TypeScript. project_path is optional; the graph is loaded out of band — see Analysis at scale.

The layer available at every analysis level: every class, method, and field, typed and queryable.

MethodReturnsDescription
get_symbol_table()Dict[str, JCompilationUnit | PyModule]The entire project, indexed by file.
get_classes()Dict[str, JType | PyClass]Every class or type, keyed by qualified name.
get_methods()DictEvery method (Java).
get_method(qualified_class_name, qualified_method_name)JCallable | PyCallable | NoneA single method; .code holds its source body.
m = analysis.get_method(
"org.apache.commons.cli.OptionBuilder", "create(java.lang.String)"
)
print(m.code) # -> source body of OptionBuilder.create(java.lang.String)

Java additionally provides class-relationship queries: get_sub_classes(cls), get_extended_classes(cls), and get_implemented_interfaces(cls), along with get_all_crud_operations() and get_all_comments().

MethodReturnsNotes
get_call_graph()networkx.DiGraphEdges point caller → callee.
get_call_graph_json()strSame graph, serialized.
get_callers(target_class_name, target_method_declaration)DictMethods that call this method.
get_callees(source_class_name, source_method_declaration)DictMethods that this method calls.
get_class_call_graph(qualified_class_name, method_signature=None)graphCall graph scoped to one class (Java).
cg = analysis.get_call_graph() # -> networkx.DiGraph
callers = analysis.get_callers(
"org.apache.commons.cli.Options", "addOption(org.apache.commons.cli.Option)"
) # -> Dict of call sites

Reachability is a networkx query over the call graph:

import networkx as nx
cg = analysis.get_call_graph()
print(nx.has_path(cg, src, sink)) # reachable? -> True / False
# nx.shortest_path(cg, src, sink) # one path
# list(nx.all_simple_paths(cg, src, sink)) # every path

The typed objects returned by each language’s analysis API.

LanguageModels
JavaJType, JCallable, JCompilationUnit, JField, JComment, JCRUDOperation
PythonPyModule, PyClass, PyCallable, PyImport, PyComment