Class CodeRefiner

All Implemented Interfaces:
ContextProvider, PropertyChangeSource, Rebindable

public class CodeRefiner extends AnahataToolkit
V3.0.0 of the structural Java code refinement toolkit. High-precision structural manipulation with a pragmatic approach to comment preservation.
Author:
anahata
  • Constructor Details

    • CodeRefiner

      public CodeRefiner()
  • Method Details

    • initialize

      public void initialize()
      Description copied from class: AnahataToolkit
      Performs initial setup for a newly created toolkit instance.

      This method is called exactly once by the ToolManager after the toolkit has been instantiated and registered. It provides a clean hook for toolkits to perform one-time initialization logic that requires the tool environment to be fully configured, such as scanning for project instructions or registering default resources.

      Overrides:
      initialize in class AnahataToolkit
    • getSystemInstructions

      public List<String> getSystemInstructions() throws Exception
      Description copied from interface: ContextProvider
      Gets a list of system instruction strings provided by this context provider. These are typically prepended to the conversation as high-level guidance.
      Returns:
      A list of system instruction strings.
      Throws:
      Exception - if an error occurs during instruction generation.
    • addImports

      public String addImports(String filePath, List<String> imports, boolean save) throws Exception
      Adds one or more imports to a file structurally.
      Parameters:
      filePath - the absolute path of the Java file
      imports - list of FQNs to import
      save - whether to save the file
      Returns:
      a success message
      Throws:
      Exception - if import fails
    • optimizeImports

      public String optimizeImports(String filePath, Boolean removeUnused, boolean save) throws Exception
      Optimizes imports (converts FQNs to simple names, removes unused).
      Parameters:
      filePath - the absolute path of the Java file
      removeUnused - whether to remove unused imports
      save - whether to save the file
      Returns:
      a success message
      Throws:
      Exception - if optimization fails
    • reformat

      public String reformat(String filePath, boolean save) throws Exception
      Reformats a file using IDE rules, logging each step and returning the resulting unified diff.
      Parameters:
      filePath - the absolute path of the Java file
      save - whether to save the file
      Returns:
      the generated unified diff string or a no-changes message
      Throws:
      Exception - if reformat fails
    • optimizeImportsInternal

      private void optimizeImportsInternal(org.openide.filesystems.FileObject fo, boolean removeUnused) throws Exception
      Internal implementation of the import optimization logic.
      Parameters:
      fo - the file object
      removeUnused - whether to remove unused imports
      Throws:
      Exception - if optimization fails
    • optimizeImportsInMemory

      public static String optimizeImportsInMemory(org.netbeans.api.java.source.ClasspathInfo cpInfo, String sourceCode, boolean optimize, List<String> explicitImportsToAdd, List<String> explicitImportsToRemove) throws Exception
      Helper to optimize imports entirely in-memory, preserving atomicity during batch edits.
      Parameters:
      cpInfo - The classpath info
      sourceCode - The source code string
      optimize - Whether to automatically resolve missing imports via ClassIndex and remove unused imports
      explicitImportsToAdd - Additional FQNs to add
      explicitImportsToRemove - FQNs to forcibly remove
      Returns:
      The import-optimized source string
      Throws:
      Exception
    • addAnnotation

      public String addAnnotation(String filePath, String memberFqn, String annotation, boolean save) throws Exception
      Adds an annotation to a class, method, or field.
      Parameters:
      filePath - the absolute path of the Java file
      memberFqn - the ABSOLUTE FQN of the member
      annotation - the annotation to add
      save - whether to save
      Returns:
      a success message
      Throws:
      Exception - if it fails