Class BatchCodeRefiner

All Implemented Interfaces:
ContextProvider, PropertyChangeSource, Rebindable

public class BatchCodeRefiner extends AnahataToolkit
The authoritative toolkit for structural Java refinement.

This toolkit replaces the legacy path-based CodeRefiner with a batch-oriented, resource-centric approach. It guarantees atomicity and context-integrity through optimistic locking and memory-backed AST simulation.

Author:
anahata
  • Constructor Details

    • BatchCodeRefiner

      public BatchCodeRefiner()
  • 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.
    • refine

      public String refine(CodeRefinementBatch batch) throws Exception
      Applies a batch of member-level modifications to ONE Java file synchronously, saving and rebuilding the AST.
      Parameters:
      batch - The robust refinement batch containing modifications.
      Returns:
      The generated unified diff string representing changes applied to disk.
      Throws:
      Exception - if validation or file I/O fails.
    • findMemberInWorkingCopy

      public static Tree findMemberInWorkingCopy(org.netbeans.api.java.source.CompilationInfo info, String memberFqn)
      Locates a member tree within a working copy compilation context by its canonical FQN.
      Parameters:
      info - The active compilation context.
      memberFqn - The canonical FQN identifying the target member.
      Returns:
      The matching Tree node if found, or null otherwise.
    • findMemberIndex

      public static int findMemberIndex(org.netbeans.api.java.source.CompilationInfo info, List<? extends Tree> members, String memberName)
      Finds the 0-based index of a member within a list of AST class members by matching its signature.
      Parameters:
      info - The active compilation context.
      members - The list of class member trees to search.
      memberName - The canonical name or signature to match against.
      Returns:
      The 0-based index of the matching member, or -1 if no match is found.
    • parseMember

      public static Tree parseMember(org.netbeans.api.java.source.WorkingCopy wc, String declaration, String body, org.netbeans.api.java.source.ClasspathInfo cpInfo) throws Exception
      Parses a raw Java string into a detached AST Tree node, capturing and importing all inline comments.
      Parameters:
      wc - The target working copy context.
      declaration - The member declaration header string.
      body - The optional member body code block.
      cpInfo - The classpath information for resolution context.
      Returns:
      The successfully parsed and detached AST Tree node.
      Throws:
      Exception - if parsing, compile phase resolution, or compilation fails.
    • getInsertIndex

      public static int getInsertIndex(org.netbeans.api.java.source.CompilationInfo wc, List<? extends Tree> members, RelativePosition position, String anchor) throws AgiToolException
      Calculates the insertion index for a new member based on a relative position and an anchor name.
      Parameters:
      wc - The target working copy or compilation context.
      members - The list of existing class members.
      position - The relative position (START, END, BEFORE, AFTER).
      anchor - The anchor member name, required for BEFORE and AFTER positions.
      Returns:
      The calculated 0-based insertion index.
      Throws:
      AgiToolException - if anchor position rules are violated or the anchor is not found.
    • getMemberSignature

      public static String getMemberSignature(String memberFqn)
      Extracts the raw signature from a canonical identification FQN to be used for matching.
      Parameters:
      memberFqn - The absolute canonical member FQN.
      Returns:
      The simple member signature (e.g., 'myMethod(java.lang.String)').
    • throwMemberNotFound

      public static void throwMemberNotFound(org.netbeans.api.java.source.CompilationInfo info, String memberFqn)
      Throws a highly descriptive exception with available suggestions when a member is not found.
      Parameters:
      info - The active compilation context.
      memberFqn - The canonical member FQN that was not resolved.
      Throws:
      AgiToolException - always, containing detailed candidate recommendations.
    • rebuildClassTree

      public static ClassTree rebuildClassTree(org.netbeans.api.java.source.TreeMaker make, ClassTree ct, List<Tree> members)
      Rebuilds a ClassTree node with a new, modified list of class members based on class kind.
      Parameters:
      make - The TreeMaker utility instance.
      ct - The original class tree to reconstruct.
      members - The new list of member trees.
      Returns:
      The newly constructed and reconstructed ClassTree node.