Class DependencyTracker

  • All Implemented Interfaces:
    SymbolDependencyContainer

    public final class DependencyTracker
    extends java.lang.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 Detail

      • DependencyTracker

        public DependencyTracker()
    • Method Detail

      • getDependencies

        public java.util.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​(java.lang.String name)
        Gets the first found dependency by name.
        Parameters:
        name - Package name of the dependency to get.
        Returns:
        Returns the dependency.
        Throws:
        java.lang.IllegalArgumentException - if the dependency cannot be found.
      • getByName

        public SymbolDependency getByName​(java.lang.String name,
                                          java.lang.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:
        java.lang.IllegalArgumentException - if the dependency cannot be found.
      • getByType

        public java.util.List<SymbolDependency> getByType​(java.lang.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 java.util.List<SymbolDependency> getByProperty​(java.lang.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 java.util.List<SymbolDependency> getByProperty​(java.lang.String property,
                                                              java.lang.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​(java.lang.String packageName,
                                  java.lang.String version,
                                  java.lang.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.
      • addDependenciesFromJson

        public void addDependenciesFromJson​(java.net.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.