Class CodeModel

All Implemented Interfaces:
ContextProvider, PropertyChangeSource, Rebindable

public class CodeModel extends AnahataToolkit
Provides tools for interacting with the Java code model in NetBeans. This includes finding types, getting members, and retrieving source code.

Scope: CodeModel operates exclusively against NetBeans GlobalClasspathInfo, which indexes only currently OPEN projects, their dependencies, and the platform JDK.

  • Constructor Details

    • CodeModel

      public CodeModel()
  • 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 instructions for the CodeModel toolkit, explaining the GlobalClasspathInfo scope and canonical identification standard without repeating individual tool definitions.

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

      public void populateMessage(RagMessage ragMessage) throws Exception
      Populates the given RagMessage with dynamic, just-in-time context parts. These parts are appended to the end of the user's prompt (RAG).

      Populates the RAG message with a summary of the unique classpath roots (sources, compile outputs, dependency JARs, and boot roots) across all open projects.

      Parameters:
      ragMessage - The outgoing RAG message for the turn.
      Throws:
      Exception - if an error occurs populating the message.
    • findTypes

      public Page<JavaType> findTypes(String query, boolean caseSensitive, boolean preferOpenProjects, Integer startIndex, Integer pageSize)
      Finds multiple Java types matching a query and returns a paginated result of minimalist, machine-readable keys.
      Parameters:
      query - The search query for the types (e.g., simple name, FQN, wildcards).
      caseSensitive - Whether the search should be case-sensitive.
      preferOpenProjects - Whether to prioritize results from open projects.
      startIndex - The starting index (0-based) for pagination.
      pageSize - The maximum number of results to return per page.
      Returns:
      a paginated result of JavaType objects.
    • loadTypeSources

      public String loadTypeSources(JavaType javaType) throws Exception
      Gets the source file for a given JavaType and automatically registers it as a resource.
      Parameters:
      javaType - The minimalist keychain DTO from a findTypes call.
      Returns:
      a confirmation message.
      Throws:
      Exception - if the source cannot be retrieved.
    • loadTypeSourcesByFqn

      public String loadTypeSourcesByFqn(List<String> fqns) throws Exception
      Gets the source files for types specified by their fully qualified names and registers them as resources.
      Parameters:
      fqns - The list of fully qualified names of the types.
      Returns:
      a confirmation message summarizing loaded sources.
      Throws:
      Exception - if an unexpected error occurs.
    • getTypeJavadocs

      public String getTypeJavadocs(JavaType javaType) throws Exception
      Gets the Javadoc for a given JavaType.
      Parameters:
      javaType - The keychain DTO for the type to inspect.
      Returns:
      the Javadoc comment.
      Throws:
      Exception - if the Javadoc cannot be retrieved.
    • getTypeJavadocsByFqn

      public String getTypeJavadocsByFqn(String fqn) throws Exception
      Gets the Javadoc for a type specified by its fully qualified name.
      Parameters:
      fqn - The fully qualified name of the type.
      Returns:
      the Javadoc comment.
      Throws:
      Exception - if the Javadoc cannot be found or ambiguous.
    • getMemberSources

      public String getMemberSources(JavaMember member) throws Exception
      Gets the source code for a specific JavaMember.
      Parameters:
      member - The keychain DTO for the member to inspect.
      Returns:
      the source code of the member.
      Throws:
      Exception - if the source cannot be retrieved.
    • getMemberSourcesByFqn

      public String getMemberSourcesByFqn(String memberFqn) throws Exception
      Gets the source code for a member specified by its fully qualified name.
      Parameters:
      memberFqn - The FQN of the member (e.g., 'com.foo.Class.method').
      Returns:
      the source code of the member.
      Throws:
      Exception - if the member is not found or ambiguous.
    • getMemberJavadocs

      public String getMemberJavadocs(JavaMember member) throws Exception
      Gets the Javadoc for a specific JavaMember.
      Parameters:
      member - The keychain DTO for the member to inspect.
      Returns:
      the Javadoc comment.
      Throws:
      Exception - if the Javadoc cannot be retrieved.
    • getMemberJavadocsByFqn

      public String getMemberJavadocsByFqn(String memberFqn) throws Exception
      Gets the Javadoc for a member specified by its fully qualified name.
      Parameters:
      memberFqn - The FQN of the member (e.g., 'com.foo.Class.method').
      Returns:
      the Javadoc comment.
      Throws:
      Exception - if the Javadoc cannot be found or ambiguous.
    • getMembers

      public JavaMemberPage getMembers(JavaType javaType, String nameQuery, Integer startIndex, Integer pageSize, List<ElementKind> kindFilters) throws Exception
      Gets a paginated list of all members (fields, constructors, methods) for a given type.
      Parameters:
      javaType - The keychain DTO for the type to inspect.
      nameQuery - Optional query string to filter members by name.
      startIndex - The starting index (0-based) for pagination.
      pageSize - The maximum number of results to return per page.
      kindFilters - Optional list of member kinds to filter by (e.g., ['METHOD', 'FIELD']).
      Returns:
      a paginated result of JavaMember objects.
      Throws:
      Exception - if the members cannot be retrieved.
    • 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 fully qualified name of the type.
      nameQuery - Optional query string to filter members by name.
      startIndex - The starting index (0-based) for pagination.
      pageSize - The maximum number of results to return per page.
      kindFilters - Optional list of member kinds to filter by.
      Returns:
      a paginated result of JavaMember objects.
      Throws:
      Exception - if the type is not found or ambiguous.
    • findTypesInPackage

      public Page<JavaType> findTypesInPackage(String packageName, ElementKind kindFilter, boolean recursive, Integer startIndex, Integer pageSize)
      Finds all types within a given package, with an option for recursive search.
      Parameters:
      packageName - The fully qualified name of the package to search (e.g., 'java.util').
      kindFilter - Optional kind of type to search for (CLASS, INTERFACE, etc.).
      recursive - If true, the search will include all subpackages.
      startIndex - The starting index (0-based) for pagination.
      pageSize - The maximum number of results to return per page.
      Returns:
      a paginated result of JavaType objects.
    • getSubtypes

      public JavaHierarchyNode getSubtypes(JavaType javaType, Integer maxDepth) throws Exception
      Recursively searches for all subtypes (implementations and subclasses) of a given JavaType.
      Parameters:
      javaType - The starting type.
      maxDepth - The maximum depth to recurse. Defaults to 3 if null.
      Returns:
      A recursive JavaHierarchyNode structure.
      Throws:
      Exception - if the search fails.
    • 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 - The fully qualified name of the type.
      maxDepth - The maximum depth to recurse. Defaults to 3 if null.
      Returns:
      A recursive JavaHierarchyNode structure.
      Throws:
      Exception - if the type is not found or ambiguous.
    • getSupertypes

      public JavaHierarchyNode getSupertypes(JavaType javaType, Integer maxDepth) throws Exception
      Recursively searches for all supertypes (base classes and interfaces) of a given JavaType.
      Parameters:
      javaType - The starting type.
      maxDepth - The maximum depth to recurse up. Defaults to 3 if null.
      Returns:
      A recursive JavaHierarchyNode structure.
      Throws:
      Exception - if the search fails.
    • 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 - The fully qualified name of the type.
      maxDepth - The maximum depth to recurse up. Defaults to 3 if null.
      Returns:
      A recursive JavaHierarchyNode structure.
      Throws:
      Exception - if the type is not found or ambiguous.
    • getGlobalClasspathInfo

      public static org.netbeans.api.java.source.ClasspathInfo getGlobalClasspathInfo()
      Builds a global ClasspathInfo of all SOURCE, COMPILE and BOOT classpaths of all open projects.
      Returns:
      All classpaths of all open projects
    • resolveUniqueType

      private JavaType resolveUniqueType(String fqn) throws AgiToolException
      Resolves a fully qualified name to a unique JavaType.
      Parameters:
      fqn - The fully qualified name.
      Returns:
      the unique JavaType.
      Throws:
      AgiToolException - if the type is not found or ambiguous.
    • resolveUniqueMember

      private JavaMember resolveUniqueMember(String memberFqn) throws Exception
      Resolves a member FQN to a unique JavaMember.
      Parameters:
      memberFqn - The member FQN (e.g., 'com.foo.Class.method').
      Returns:
      the unique JavaMember.
      Throws:
      Exception - if the member is not found or ambiguous.
    • getMemberNotFoundMessage

      private static String getMemberNotFoundMessage(org.netbeans.api.java.source.CompilationInfo info, String memberFqn)
      Generates a descriptive error message with closest candidates when a member is not found.
      Parameters:
      info - The compilation context info.
      memberFqn - The FQN of the missing member.
      Returns:
      A detailed error message listing candidate names.