Class DependencyTracker

java.lang.Object
software.amazon.smithy.codegen.core.DependencyTracker
All Implemented Interfaces:
SymbolDependencyContainer

public final class DependencyTracker extends Object implements SymbolDependencyContainer
A container for all known dependencies of a generator.

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 a Map, boolean values to Java's boolean, numeric values to an appropriate Number type, and string values to String.
  • Constructor Details

    • DependencyTracker

      public DependencyTracker()
  • Method Details

    • getDependencies

      public List<SymbolDependency> 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 interface SymbolDependencyContainer
      Returns:
      Returns the dependencies.
    • getByName

      public SymbolDependency getByName(String name)
      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

      public SymbolDependency getByName(String name, String dependencyType)
      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

      public List<SymbolDependency> getByType(String dependencyType)
      Gets a list of matching dependencies that have a dependency type matching dependencyType.
      Parameters:
      dependencyType - Dependency type to find.
      Returns:
      Returns the matching dependencies.
    • getByProperty

      public List<SymbolDependency> getByProperty(String property)
      Gets a list of matching dependencies that contain a property named property.
      Parameters:
      property - Property to find.
      Returns:
      Returns the matching dependencies.
    • getByProperty

      public List<SymbolDependency> getByProperty(String property, Object value)
      Gets a list of matching dependencies that contain a property named property with a value of value.
      Parameters:
      property - Property to find.
      value - Value to match.
      Returns:
      Returns the matching dependencies.
    • addDependency

      public void addDependency(SymbolDependency dependency)
      Adds a dependency.
      Parameters:
      dependency - Dependency to add.
    • addDependency

      public void addDependency(String packageName, String version, String dependencyType)
      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

      public void addDependencies(SymbolDependencyContainer container)
      Adds dependencies from a SymbolDependencyContainer.
      Parameters:
      container - Container to copy depdencies from.
    • addDependenciesFromJson

      public void addDependenciesFromJson(URL jsonFile)
      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.