Class CodeRefinementBatch

java.lang.Object
uno.anahata.asi.toolkit.resources.text.AbstractTextResourceWrite
uno.anahata.asi.intellij.tools.java.coderefiner.CodeRefinementBatch

public class CodeRefinementBatch extends AbstractTextResourceWrite
An atomic batch of member-level CodeRefinementIntents applied to a single Java file by BatchCodeRefiner#refine.

Extends AbstractTextResourceWrite to participate in the interactive diff UI lifecycle, optimistic locking, and manual override capture.

Author:
anahata
  • Field Details

    • filePath

      private String filePath
      The absolute path of the Java file to modify (optional if resourceUuid is provided).
    • intents

      private List<CodeRefinementIntent> intents
      The ordered list of structural modifications to apply.
    • optimizeImports

      private boolean optimizeImports
      Whether to optimize imports (shorten FQNs, remove unused) after applying the intents.
    • save

      private boolean save
      Whether to persist the file to disk after applying the intents.
    • calculatedComments

      private List<LineComment> calculatedComments
      Optional calculated line comments for UI commentary rendering.
  • Constructor Details

    • CodeRefinementBatch

      public CodeRefinementBatch(String uuid, long lastModified)
      Minimal constructor for standard tool invocation.
      Parameters:
      uuid - the resource UUID.
      lastModified - optimistic locking timestamp.
  • Method Details

    • doCalculateResultingContent

      protected String doCalculateResultingContent(Agi agi) throws Exception
      Performs the actual, tool-specific calculation of the resulting content.

      Replays the refinement intents in-memory on a dummy PsiJavaFile created via PsiFileFactory, shortens class references, optionally optimizes imports, and reformats code without mutating workspace files until committed.

      Specified by:
      doCalculateResultingContent in class AbstractTextResourceWrite
      Parameters:
      agi - The parent agi session.
      Returns:
      The calculated resulting content as a String.
      Throws:
      Exception - if the calculation logic fails.
    • validate

      public void validate(Agi agi) throws Exception
      Performs pre-flight validation of the update operation. Note: This method ensures state is captured.
      Overrides:
      validate in class AbstractTextResourceWrite
      Parameters:
      agi - The parent agi session.
      Throws:
      Exception - if validation fails.
    • resolveProject

      private com.intellij.openapi.project.Project resolveProject(Agi agi)
      Resolves the hosting IntelliJ project for AST parsing and styling.
      Parameters:
      agi - the parent agi session.
      Returns:
      the hosting project or default project.
    • applyIntentToPsi

      public static void applyIntentToPsi(com.intellij.openapi.project.Project project, com.intellij.psi.PsiElementFactory factory, com.intellij.psi.PsiJavaFile file, CodeRefinementIntent intent) throws AgiToolException
      Applies a structural intent to the target PSI file in-memory.
      Parameters:
      project - the project.
      factory - the element factory.
      file - the Java PSI file.
      intent - the structural intent.
      Throws:
      AgiToolException - if an intent target cannot be resolved.
    • findClassInFile

      private static com.intellij.psi.PsiClass findClassInFile(com.intellij.psi.PsiJavaFile file, String classFqn)
      Locates a class by FQN or simple name within the PSI file.
      Parameters:
      file - the Java PSI file.
      classFqn - the class FQN or simple name.
      Returns:
      the resolved class, or first class if unspecified.
    • requireMemberInFile

      private static com.intellij.psi.PsiElement requireMemberInFile(com.intellij.psi.PsiJavaFile file, String memberFqn) throws AgiToolException
      Locates a member by canonical FQN within the PSI file.
      Parameters:
      file - the Java PSI file.
      memberFqn - the member FQN.
      Returns:
      the matching member.
      Throws:
      AgiToolException - if member is absent.
    • searchClassForMember

      private static com.intellij.psi.PsiElement searchClassForMember(com.intellij.psi.PsiClass cls, String memberFqn, String simpleName, boolean isMethod)
    • parseMember

      private static com.intellij.psi.PsiMember parseMember(com.intellij.psi.PsiElementFactory factory, String declaration, com.intellij.psi.PsiElement context) throws AgiToolException
      Throws:
      AgiToolException
    • insertMember

      private static void insertMember(com.intellij.psi.PsiClass target, com.intellij.psi.PsiMember member, RelativePosition position, String anchorName) throws AgiToolException
      Throws:
      AgiToolException
    • requireAnchor

      private static com.intellij.psi.PsiMember requireAnchor(com.intellij.psi.PsiClass target, String anchorName) throws AgiToolException
      Throws:
      AgiToolException