tree: 003701f4a899fbf8299c0ae30850b7a508dc0b81 [search]
  1. src/
  2. tests/
  3. Android.bp
  4. manifest.txt
  5. OWNERS
  6. README.md
tools/changelogging/README.md

Changelogging Tool

The changelogging tool is a code generation utility designed to automatically instrument Java source files to track and log changes to specific fields.

Overview

The tool scans input Java files for classes annotated with @TrackedType and fields annotated with @TrackedField. For these annotated elements, it:

  1. Injects Declarations: Adds necessary helper fields (e.g., dirty bits, change logs) and methods (e.g., writers, loggers) to the classes.
  2. Instruments Mutations: Automatically wraps field assignments and modifications with code that updates the dirty bits and logs the changes.

This automation makes it easy to track and log state changes within complex object graphs, e.g. the WindowManager hierarchy.

CLI Interface

The tool can be executed via command line with the following options:

changelogging [--classpath <paths>] --out-srcjar <output.srcjar> [--packages <pkg1,pkg2,...>] [--emit-companion-classes] [--noop] <input.srcjar>
changelogging [--classpath <paths>] --out-srcjar <output.srcjar> [--packages <pkg1,pkg2,...>] [--emit-companion-classes] [--noop] [<input.java>, ...]

Arguments

  • --classpath <paths>: A list of paths (jars or directories) containing dependencies of the input files, separated by the system path separator (e.g., : on Linux/macOS, ; on Windows). This is crucial for resolving types during analysis.
  • --out-srcjar <output.srcjar>: The path to the output .srcjar file where the transformed source files will be written. This argument is required.
  • --packages <pkg1,pkg2,...>: Optional comma-separated list of packages to instrument. When provided, only .java files whose package ...; declaration matches one of the specified packages (or any of their subpackages) will undergo AST analysis and transformation; all other .java files and non-Java files are copied directly to the output .srcjar without modification. As a safety guardrail, if any skipped .java file contains @TrackedType or @TrackedField annotations, the tool will abort with an error.
  • --emit-companion-classes: Optional flag. When provided, the tool generates companion classes for non-instrumentable types (e.g. android.graphics.Rect) and writes them to the output .srcjar. This flag should be enabled in exactly one module invocation (e.g. in core/java/Android.bp) to avoid duplicate class definitions.
  • --noop: Optional flag. If provided, the tool runs in “no-operation” mode, simply copying the input files to the output .srcjar without applying any transformations.
  • <input.srcjar> | <input.java> ...: The input files to process. This can be a single .srcjar file containing Java sources, or a list of individual .java files.

Architecture

The following diagram illustrates the data flow and the main components of the tool:

Source Files InternalRawTypeFinder ExternalRawTypeFinder Classpath TrackedGraph PolymorphismAnalyzer ImmutabilityAnalyzer DirtyBitAssigner DeclarationsInjector MutationInstrumentor CompanionClassGenerator Code Changes CodeTransformer Transformed Source Files

Component Details

  • InputFilesReader: Reads input .srcjar archives or individual .java files from disk into memory for processing.
  • SrcJarWriter: Writes transformed Java source files, generated companion classes, and passthrough files into the output .srcjar.
  • PackageAllowList: Filters input .java files against an optional package allowlist (--packages), and acts as a guardrail by aborting if any disallowed file contains changelogging annotations.
  • InternalRawTypeFinder: Parses the AST of input files to identify @TrackedType and @TrackedField annotations, producing raw metadata (RawType) about what needs to be tracked.
  • ExternalRawTypeFinder: Searches the project classpath via UAST/IntelliJ APIs to resolve metadata for external @TrackedType classes and parent classes referenced by internal types.
  • TrackedGraph: Represents the unified graph of all tracked types and their fields across internal and external sources, linking class hierarchies, parent-child relationships, and non-instrumentable types.
  • PolymorphismAnalyzer: Analyzes class hierarchies in TrackedGraph to propagate polymorphism flags from parent classes down to child classes and validate polymorphic consistency.
  • ImmutabilityAnalyzer: Analyzes TrackedGraph to identify immutable versus mutable tracked types and fields, propagating mutability across enclosing types, derived types, and field types.
  • DirtyBitAssigner: Assigns unique, sequential dirty bit indices to each mutable tracked field within a type.
  • DeclarationsInjector: Generates code changes to inject tracking infrastructure into classes: a bitset to track dirty fields (one bit per field) and helper methods.
  • MutationInstrumentor: Analyzes code for assignments or modifications to tracked fields and generates code changes to wrap these operations with change detection logic.
  • CompanionClassGenerator: Generates companion classes for non-instrumentable types (e.g. android.graphics.Rect, Point, and Insets) when --emit-companion-classes is enabled.
  • CodeTransformer: Applies the collection of generated CodeChanges to the original source code, ensuring original line numbers are preserved.