Class DependencyTracker
- All Implemented Interfaces:
SymbolDependencyContainer
A DependencyTracker can include predefined dependencies loaded from a file (for example to track versions of runtime dependencies used in the generator), or dependencies that are accumulated dynamically as code is generated.
Notes:
- Multiple packages of the same name and type can be added to tracker. There's no de-duplication.
- Note that this class is mutable and not synchronized.
Loading from JSON
Dependencies can be loaded from a JSON file to more easily track dependencies used at runtime by generated code. This feature can also be used to generate the dependencies tracked by the generated from from other dependency graph formats like lockfiles.
The JSON file has the following format:
{
"version": "1.0",
"dependencies": [
{
"packageName": "string",
"version": "string",
"dependencyType": "string",
"properties": {
"x": true,
"y": [10],
"z": "string"
}
}
]
}
- "version" (string, required): Must be set to "1.0".
- "dependencies" is a list of dependency objects that contain the following
properties:
- "packageName" (string, required): The required name of the package.
- "version" (string, required): The required dependency version.
- "dependencyType" (string): The optional type of dependency. This value is dependent on the package manager of the target environment.
- "properties" (map of string to any value): Properties to assign to
the symbol. These properties can be any JSON value type other than null.
List values are converted to a
List
, map values are converted to aMap
, boolean values to Java's boolean, numeric values to an appropriateNumber
type, and string values toString
.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addDependencies
(SymbolDependencyContainer container) Adds dependencies from aSymbolDependencyContainer
.void
addDependenciesFromJson
(URL jsonFile) Loads predefined dependencies from a JSON file (for example, to track known dependencies used by generated code at runtime).void
addDependency
(String packageName, String version, String dependencyType) Adds a dependency.void
addDependency
(SymbolDependency dependency) Adds a dependency.Gets the first found dependency by name.Gets the first found dependency by name and dependency type.getByProperty
(String property) Gets a list of matching dependencies that contain a property namedproperty
.getByProperty
(String property, Object value) Gets a list of matching dependencies that contain a property namedproperty
with a value ofvalue
.Gets a list of matching dependencies that have a dependency type matchingdependencyType
.Gets the list of dependencies that this object introduces.
-
Constructor Details
-
DependencyTracker
public DependencyTracker()
-
-
Method Details
-
getDependencies
Description copied from interface:SymbolDependencyContainer
Gets the list of dependencies that this object introduces.A dependency is a dependency on another package that a Symbol or type requires. It is quite different from a reference since a reference only refers to a symbol; a reference provides no context as to whether or not a dependency is required or the dependency's coordinates.
- Specified by:
getDependencies
in interfaceSymbolDependencyContainer
- Returns:
- Returns the dependencies.
-
getByName
Gets the first found dependency by name.- Parameters:
name
- Package name of the dependency to get.- Returns:
- Returns the dependency.
- Throws:
IllegalArgumentException
- if the dependency cannot be found.
-
getByName
Gets the first found dependency by name and dependency type.- Parameters:
name
- Package name of the dependency to get.dependencyType
- The dependency type of package to find.- Returns:
- Returns the dependency.
- Throws:
IllegalArgumentException
- if the dependency cannot be found.
-
getByType
Gets a list of matching dependencies that have a dependency type matchingdependencyType
.- Parameters:
dependencyType
- Dependency type to find.- Returns:
- Returns the matching dependencies.
-
getByProperty
Gets a list of matching dependencies that contain a property namedproperty
.- Parameters:
property
- Property to find.- Returns:
- Returns the matching dependencies.
-
getByProperty
Gets a list of matching dependencies that contain a property namedproperty
with a value ofvalue
.- Parameters:
property
- Property to find.value
- Value to match.- Returns:
- Returns the matching dependencies.
-
addDependency
Adds a dependency.- Parameters:
dependency
- Dependency to add.
-
addDependency
Adds a dependency.- Parameters:
packageName
- Name of the dependency.version
- Version of the dependency.dependencyType
- Type of dependency (e.g., "dev", "test", "runtime", etc). This value wholly depends on the type of dependency graph being generated.
-
addDependencies
Adds dependencies from aSymbolDependencyContainer
.- Parameters:
container
- Container to copy depdencies from.
-
addDependenciesFromJson
Loads predefined dependencies from a JSON file (for example, to track known dependencies used by generated code at runtime).DependencyTracker tracker = new DependencyTracker(); tracker.addDependenciesFromJson(getClass().getResource("some-file.json"));
- Parameters:
jsonFile
- URL location of the JSON file.
-