Class CodeModel
java.lang.Object
uno.anahata.asi.agi.tool.ToolContext
uno.anahata.asi.agi.tool.AnahataToolkit
uno.anahata.asi.nb.tools.java.CodeModel
- All Implemented Interfaces:
ContextProvider, PropertyChangeSource, Rebindable
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.
-
Field Summary
Fields inherited from class AnahataToolkit
childrenProviders, propertyChangeSupportFields inherited from class ToolContext
toolkit -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfindTypes(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.findTypesInPackage(String packageName, ElementKind kindFilter, boolean recursive, Integer startIndex, Integer pageSize) Finds all types within a given package, with an option for recursive search.static org.netbeans.api.java.source.ClasspathInfoBuilds a global ClasspathInfo of all SOURCE, COMPILE and BOOT classpaths of all open projects.getMemberJavadocs(JavaMember member) Gets the Javadoc for a specific JavaMember.getMemberJavadocsByFqn(String memberFqn) Gets the Javadoc for a member specified by its fully qualified name.private static StringgetMemberNotFoundMessage(org.netbeans.api.java.source.CompilationInfo info, String memberFqn) Generates a descriptive error message with closest candidates when a member is not found.getMembers(JavaType javaType, String nameQuery, Integer startIndex, Integer pageSize, List<ElementKind> kindFilters) Gets a paginated list of all members (fields, constructors, methods) for a given type.getMembersByFqn(String fqn, String nameQuery, Integer startIndex, Integer pageSize, List<ElementKind> kindFilters) Gets a paginated list of all members for a type specified by its fully qualified name.getMemberSources(JavaMember member) Gets the source code for a specific JavaMember.getMemberSourcesByFqn(String memberFqn) Gets the source code for a member specified by its fully qualified name.getSubtypes(JavaType javaType, Integer maxDepth) Recursively searches for all subtypes (implementations and subclasses) of a given JavaType.getSubtypesByFqn(String fqn, Integer maxDepth) Recursively searches for all subtypes of a type specified by its fully qualified name.getSupertypes(JavaType javaType, Integer maxDepth) Recursively searches for all supertypes (base classes and interfaces) of a given JavaType.getSupertypesByFqn(String fqn, Integer maxDepth) Recursively searches for all supertypes of a type specified by its fully qualified name.Gets a list of system instruction strings provided by this context provider.getTypeJavadocs(JavaType javaType) Gets the Javadoc for a given JavaType.Gets the Javadoc for a type specified by its fully qualified name.loadTypeSources(JavaType javaType) Gets the source file for a given JavaType and automatically registers it as a resource.loadTypeSourcesByFqn(List<String> fqns) Gets the source files for types specified by their fully qualified names and registers them as resources.voidpopulateMessage(RagMessage ragMessage) Populates the givenRagMessagewith dynamic, just-in-time context parts.private JavaMemberresolveUniqueMember(String memberFqn) Resolves a member FQN to a unique JavaMember.private JavaTyperesolveUniqueType(String fqn) Resolves a fully qualified name to a unique JavaType.Methods inherited from class AnahataToolkit
getChildrenProviders, getDescription, getId, getName, getParentProvider, getPropertyChangeSupport, initialize, isEffectivelyProviding, postActivate, rebindMethods inherited from class ToolContext
addAttachment, addAttachment, addAttachment, addAttachment, error, error, error, getAgi, getApplicationMap, getAsiContainer, getAsiContainerMap, getCall, getExecutorService, getModelId, getModelMessage, getResourceManager, getResponse, getSessionMap, getThreadSafeLogger, getTool, getToolContext, getToolkit, getToolManager, getTurnMap, isToolExecution, log, peekResponse, runAsyncMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ContextProvider
getAgi, getContextPosition, getFlattenedHierarchy, getFullyQualifiedId, getHeader, getIconId, getInstructionsTokenCount, getRagTokenCount, isProviding, setParentProvider, setProviding
-
Constructor Details
-
CodeModel
public CodeModel()
-
-
Method Details
-
getSystemInstructions
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
Populates the givenRagMessagewith 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
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
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
-
getTypeJavadocsByFqn
-
getMemberSources
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
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
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
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
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
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
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
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
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
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.
-

