Class VeryPrettyClassPathPrinter

java.lang.Object
uno.anahata.asi.toolkit.java.classpath.VeryPrettyClassPathPrinter

public class VeryPrettyClassPathPrinter extends Object
A token-efficient classpath printer that encapsulates both raw and pretty versions.

It manages its own list of JarHandlers to recover metadata from within the JARs, allowing for a rich, environment-agnostic representation of the classpath.

This class follows a reactive pattern: calling setRaw(String) automatically triggers a re-build of the pretty-printed representation via the update() method.

Author:
anahata
  • Field Details

    • raw

      private String raw
      The original, unformatted classpath string as provided by the system.
    • pretty

      private String pretty
      The formatted, token-optimized version of the classpath. This is the version intended for AI context consumption.
    • handlers

      private final List<JarHandler> handlers
      The list of specialized handlers used to extract metadata (IDs, versions, vendors) from within the JARs.
  • Constructor Details

    • VeryPrettyClassPathPrinter

      public VeryPrettyClassPathPrinter()
      Constructs a new printer with default handlers (Maven, Selenium, Default).
  • Method Details

    • setRaw

      public void setRaw(String raw)
      Sets the raw classpath string. If the new value differs from the current one, it automatically triggers an update().
      Parameters:
      raw - The new raw classpath string.
    • addHandler

      public void addHandler(JarHandler handler)
      Adds a new JAR handler to the printer. The new handler is added at the beginning of the list to give it priority over existing handlers.

      Note: You should call update() after adding a handler if you want the changes to be reflected immediately in the pretty string.

      Parameters:
      handler - The handler to add.
    • update

      public String update()
      Re-builds the pretty-printed string using the current set of handlers and the current raw classpath.

      This method builds a collapsed directory tree and then groups JARs by lexical prefix within each node to minimize token usage.

      Returns:
      The updated pretty string.
    • collapse

      private void collapse(VeryPrettyClassPathPrinter.DirectoryNode node)
    • printNode

      private void printNode(VeryPrettyClassPathPrinter.DirectoryNode node, String indent, StringBuilder sb)
    • findCommonRoot

      private String findCommonRoot(Set<String> paths)
    • inspect

      private JarMetadata inspect(File jarFile)
      Inspects a single JAR file using all registered handlers.

      It uses a "Best Effort" strategy, collecting metadata from all handlers and picking the one that provides the most information (version, vendor, etc.).

      Parameters:
      jarFile - The file to inspect.
      Returns:
      The extracted metadata, or null if no handler could process it.
    • formatLexicalGroups

      private String formatLexicalGroups(List<JarMetadata> jars, String indent)
      Formats a list of JAR metadata into lexical groups for a specific directory. It factors out common versions and prefixes to minimize token usage.

      This implementation uses a "Majority Version" rule: it promotes the most frequent version to the group level to save tokens.

      Parameters:
      jars - The list of JARs in the directory.
      indent - The current indentation string.
      Returns:
      A formatted string listing the JARs by prefix and version.
    • getFirstSegment

      private String getFirstSegment(String id)
    • getCommonPrefix

      private String getCommonPrefix(String id1, String id2)
    • toString

      public String toString()

      Returns the pretty-printed version of the classpath.

      Overrides:
      toString in class Object