Class CodeModel

All Implemented Interfaces:
ContextProvider, PropertyChangeSource, Rebindable

public class CodeModel extends AnahataToolkit
Provides tools for interacting with the Java code model in IntelliJ IDEA. This includes finding types, listing members, retrieving source code fragments, loading Javadocs, and navigating type hierarchies.
Author:
anahata
  • Constructor Details

    • CodeModel

      public CodeModel()
      Default constructor for the IntelliJ CodeModel toolkit.
  • Method Details

    • getSystemInstructions

      public List<String> getSystemInstructions() throws Exception
      Gets a list of system instruction strings provided by this context provider. These are typically prepended to the conversation as high-level guidance.

      Provides context-aware system instructions for the CodeModel toolkit, detailing the usage of FQN-based methods versus discovery-based searches.

      Returns:
      A list of system instruction strings.
      Throws:
      Exception - if an error occurs during instruction generation.
    • findTypes

      public Page<JavaType> findTypes(String query, boolean caseSensitive, boolean preferOpenProjects, Integer startIndex, Integer pageSize) throws AgiToolException
      Finds any Java types matching a query within the aggregated classpath of all open projects.
      Parameters:
      query - The search query.
      caseSensitive - Whether the search should be case-sensitive.
      preferOpenProjects - Prioritize open projects.
      startIndex - Start index for pagination.
      pageSize - Max results per page.
      Returns:
      Paginated result of JavaType.
      Throws:
      AgiToolException
    • addClassToResults

      private void addClassToResults(com.intellij.psi.PsiClass cl, List<JavaType> results)
      Appends a class to the result list as a JavaType keychain DTO, de-duplicating by fully-qualified name. Classes without an FQN (anonymous/local) are skipped.
      Parameters:
      cl - the PSI class to add.
      results - the accumulating result list.
    • getUrlOfClass

      private URL getUrlOfClass(com.intellij.psi.PsiClass cl)
      Resolves the source/class-file file: URL backing a PSI class, used as the keychain for later source/javadoc lookups.
      Parameters:
      cl - the PSI class.
      Returns:
      the file URL, or null if it cannot be resolved.
    • loadTypeSources

      public String loadTypeSources(JavaType javaType) throws Exception
      Loads the source file for a given JavaType as a managed text resource.
      Parameters:
      javaType - The target JavaType.
      Returns:
      Confirmation message.
      Throws:
      Exception - on execution failure.
    • loadTypeSourcesByFqn

      public String loadTypeSourcesByFqn(String fqn) throws Exception
      Loads the source file for of a java type as a managed resource by its fully qualified name.
      Parameters:
      fqn - The target FQN.
      Returns:
      Confirmation message.
      Throws:
      Exception - on execution failure.
    • getTypeJavadocs

      public String getTypeJavadocs(JavaType javaType) throws Exception
      Gets the Javadoc for a given JavaType.
      Parameters:
      javaType - The target JavaType.
      Returns:
      The Javadoc content.
      Throws:
      Exception - on execution failure.
    • getTypeJavadocsByFqn

      public String getTypeJavadocsByFqn(String fqn) throws Exception
      Gets the Javadoc for a type specified by its fully qualified name.
      Parameters:
      fqn - The target FQN.
      Returns:
      The Javadoc content.
      Throws:
      Exception - on execution failure.
    • getMemberSources

      public String getMemberSources(JavaMember member) throws Exception
      Gets the source code for a specific JavaMember.
      Parameters:
      member - The target member.
      Returns:
      Member source text.
      Throws:
      Exception - on execution failure.
    • getMemberSourcesByFqn

      public String getMemberSourcesByFqn(String memberFqn) throws Exception
      Gets the source code for a member specified by its fully qualified name.
      Parameters:
      memberFqn - The target member FQN.
      Returns:
      Member source text.
      Throws:
      Exception - on execution failure.
    • getMemberJavadocs

      public String getMemberJavadocs(JavaMember member) throws Exception
      Gets the Javadoc for a specific JavaMember.
      Parameters:
      member - The target member.
      Returns:
      Member Javadoc comment text.
      Throws:
      Exception - on execution failure.
    • getMemberJavadocsByFqn

      public String getMemberJavadocsByFqn(String memberFqn) throws Exception
      Gets the Javadoc for a member specified by its fully qualified name.
      Parameters:
      memberFqn - The target member FQN.
      Returns:
      Javadoc comment text.
      Throws:
      Exception - on execution failure.
    • getMembers

      public JavaMemberPage getMembers(JavaType javaType, String nameQuery, Integer startIndex, Integer pageSize, List<ElementKind> kindFilters) throws Exception
      Gets a paginated list of all members for a given type.
      Parameters:
      javaType - The target JavaType.
      nameQuery - Name filter query.
      startIndex - Start index for pagination.
      pageSize - Page size.
      kindFilters - Type kinds filter list.
      Returns:
      Member page.
      Throws:
      Exception - on execution failure.
    • getMembersByFqn

      public JavaMemberPage getMembersByFqn(String fqn, String nameQuery, Integer startIndex, Integer pageSize, List<ElementKind> kindFilters) throws Exception
      Gets a paginated list of all members for a type specified by its fully qualified name.
      Parameters:
      fqn - The target FQN.
      nameQuery - Name filter query.
      startIndex - Start index.
      pageSize - Page size.
      kindFilters - Kinds filter.
      Returns:
      Member page.
      Throws:
      Exception - on execution failure.
    • findTypesInPackage

      public Page<JavaType> findTypesInPackage(String packageName, ElementKind kindFilter, boolean recursive, Integer startIndex, Integer pageSize) throws AgiToolException
      Finds all types within a given package, with an option for recursive search.
      Parameters:
      packageName - Package FQN.
      kindFilter - Target kind filter.
      recursive - Recursive search.
      startIndex - Start index.
      pageSize - Page size.
      Returns:
      Paginated Page.
      Throws:
      AgiToolException
    • getSubtypes

      public JavaHierarchyNode getSubtypes(JavaType javaType, Integer maxDepth) throws Exception
      Recursively searches for all subtypes of a given JavaType.
      Parameters:
      javaType - Starting Type.
      maxDepth - Max recursion depth.
      Returns:
      Hierarchy node.
      Throws:
      Exception - on execution failure.
    • getSubtypesByFqn

      public JavaHierarchyNode getSubtypesByFqn(String fqn, Integer maxDepth) throws Exception
      Recursively searches for all subtypes of a type specified by its fully qualified name.
      Parameters:
      fqn - FQN of starting type.
      maxDepth - Max depth.
      Returns:
      Hierarchy node.
      Throws:
      Exception - on execution failure.
    • getSupertypes

      public JavaHierarchyNode getSupertypes(JavaType javaType, Integer maxDepth) throws Exception
      Recursively searches for all supertypes of a given JavaType.
      Parameters:
      javaType - Starting type.
      maxDepth - Max depth.
      Returns:
      Hierarchy node.
      Throws:
      Exception - on execution failure.
    • getSupertypesByFqn

      public JavaHierarchyNode getSupertypesByFqn(String fqn, Integer maxDepth) throws Exception
      Recursively searches for all supertypes of a type specified by its fully qualified name.
      Parameters:
      fqn - FQN of starting type.
      maxDepth - Max depth.
      Returns:
      Hierarchy node.
      Throws:
      Exception - on execution failure.
    • awaitSmart

      private void awaitSmart() throws AgiToolException
      Blocks (bounded) until every open project has finished indexing, so the PSI short-name caches, stub indexes and inheritor searches this toolkit relies on return complete results instead of throwing IndexNotReadyException mid-index.
      Throws:
      AgiToolException - if any open project is still indexing after the bounded wait.
    • findPsiClass

      private com.intellij.psi.PsiClass findPsiClass(String fqn)
      Finds a PSI class by fully-qualified name across all open projects. Must be called inside a read action.
      Parameters:
      fqn - the fully-qualified type name.
      Returns:
      the first matching class, or null if none is found.
    • resolveUniqueType

      private JavaType resolveUniqueType(String fqn) throws AgiToolException
      Resolves a fully-qualified type name to a unique JavaType keychain, waiting for indexing to finish first.
      Parameters:
      fqn - the fully-qualified type name.
      Returns:
      the resolved type keychain.
      Throws:
      AgiToolException - if the type is not found.
    • resolveUniqueMember

      private JavaMember resolveUniqueMember(String memberFqn) throws Exception
      Resolves a canonical member FQN to a unique JavaMember keychain, waiting for indexing to finish first.
      Parameters:
      memberFqn - the canonical member FQN.
      Returns:
      the resolved member keychain.
      Throws:
      Exception - if the member is not found or is ambiguous.
    • getPsiMembers

      private List<JavaMember> getPsiMembers(com.intellij.psi.PsiClass cl)
      Collects the fields, methods, constructors and inner classes of a PSI class as JavaMember keychain DTOs. Must be called inside a read action.
      Parameters:
      cl - the declaring class.
      Returns:
      the list of member keychains.
    • getMethodFqn

      private String getMethodFqn(com.intellij.psi.PsiClass cl, com.intellij.psi.PsiMethod method)
      Builds the canonical method FQN (pkg.Type.name(erasedArg,...); constructors use <init>) so member FQNs round-trip with the other Java toolkits.
      Parameters:
      cl - the declaring class.
      method - the method.
      Returns:
      the canonical method FQN.
    • getModifiersSet

      private Set<String> getModifiersSet(com.intellij.psi.PsiModifierList modifierList)
      Extracts the Java modifier keywords (public, static, final, …) present on a modifier list.
      Parameters:
      modifierList - the modifier list, or null.
      Returns:
      the set of modifier keywords (empty if none or null).
    • getPsiMemberSource

      private String getPsiMemberSource(com.intellij.psi.PsiClass cl, String memberFqn) throws Exception
      Returns the source text of a member (method, field or inner class) identified by its canonical FQN within a class. Must be called inside a read action.
      Parameters:
      cl - the declaring class.
      memberFqn - the canonical member FQN.
      Returns:
      the member's source text.
      Throws:
      Exception - if no member matches the FQN.
    • getPsiMemberJavadoc

      private String getPsiMemberJavadoc(com.intellij.psi.PsiClass cl, String memberFqn) throws Exception
      Returns the Javadoc comment text of a member (method, field or inner class) identified by its canonical FQN within a class. Must be called inside a read action.
      Parameters:
      cl - the declaring class.
      memberFqn - the canonical member FQN.
      Returns:
      the member's Javadoc text, or an empty string if it has none.
      Throws:
      Exception - if resolution fails.
    • getSupertypesNode

      private JavaHierarchyNode getSupertypesNode(com.intellij.psi.PsiClass cl, int maxDepth, int currentDepth)
      Recursively builds the supertype hierarchy node for a class up to a maximum depth, skipping java.lang.Object. Must be called inside a read action.
      Parameters:
      cl - the starting class.
      maxDepth - the maximum recursion depth.
      currentDepth - the current recursion depth.
      Returns:
      the hierarchy node rooted at the class.
    • getSubtypesNode

      private JavaHierarchyNode getSubtypesNode(com.intellij.psi.PsiClass cl, int maxDepth, int currentDepth)
      Recursively builds the subtype (implementors/subclasses) hierarchy node for a class up to a maximum depth via ClassInheritorsSearch. Must be called inside a read action.
      Parameters:
      cl - the starting class.
      maxDepth - the maximum recursion depth.
      currentDepth - the current recursion depth.
      Returns:
      the hierarchy node rooted at the class.