Skip to content

Common tasks

This page is a task index. Each section states a codebase question, then shows the corresponding CLDK call and the shape of its result: the call appears first, and the output follows as a trailing comment. Java examples use Apache Commons CLI (project_path="commons-cli"); Python examples use a generic my_pkg.

Enumerate every method declared on a class.

from cldk import CLDK
analysis = CLDK.python(project_path="my_pkg")
pyclass = analysis.get_classes()["my_pkg.options.Options"]
for method in pyclass.methods.values():
print(method.signature)
# add_option(self, option)
# get_option(self, name)
# has_option(self, name)
# ...

Retrieve the exact source of a single method by its qualified class name and signature, without reading files or tracking line ranges.

method = analysis.get_method(
"my_pkg.options.Options",
"add_option",
)
print(method.code)
# def add_option(self, option):
# self._options[option.name] = option
# return self

The call graph is a networkx.DiGraph whose edges point from caller to callee. It underpins the callers and callees queries and therefore requires the call_graph analysis level.

from cldk import CLDK
from cldk.analysis import AnalysisLevel
analysis = CLDK.python(
project_path="my_pkg",
analysis_level=AnalysisLevel.call_graph, # required for call edges
)
cg = analysis.get_call_graph()
print(cg.number_of_nodes(), "methods,", cg.number_of_edges(), "edges")
# 87 methods, 113 edges

Impact analysis. get_callers returns the set of methods that invoke the target.

callers = analysis.get_callers(
"my_pkg.options.Options",
"add_option",
)
print(list(callers))
# ['my_pkg.cli.build_options', 'my_pkg.options.Options.add_required', ...]

The dependency view. get_callees is the inverse of get_callers, returning the methods that the target invokes.

callees = analysis.get_callees(
"my_pkg.parser.DefaultParser",
"parse",
)
print(list(callees))
# ['my_pkg.options.Options.get_option', 'my_pkg.cli.CommandLine.add_option', ...]

In Java, walk class relationships with get_sub_classes, get_extended_classes, and get_implemented_interfaces.

# Python analysis does not expose get_class_hierarchy / get_sub_classes.
# Recover base classes from the model: each PyClass records its bases.
pyclass = analysis.get_classes()["my_pkg.parser.DefaultParser"]
print(pyclass.base_classes)
# ['my_pkg.parser.Parser']

Locate where the code reads or writes persistent data. This is useful for security triage and data-flow analysis, and is a Java-only capability.

# Not available. CRUD-operation extraction (get_all_crud_operations)
# is part of the Java analysis API only.