Class CodeModel
java.lang.Object
uno.anahata.asi.agi.tool.ToolContext
uno.anahata.asi.agi.tool.AnahataToolkit
uno.anahata.asi.intellij.tools.java.CodeModel
- All Implemented Interfaces:
ContextProvider, PropertyChangeSource, Rebindable
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
-
Field Summary
Fields inherited from class AnahataToolkit
childrenProviders, propertyChangeSupportFields inherited from class ToolContext
toolkit -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate voidaddClassToResults(com.intellij.psi.PsiClass cl, List<JavaType> results) Appends a class to the result list as aJavaTypekeychain DTO, de-duplicating by fully-qualified name.private voidBlocks (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 throwingIndexNotReadyExceptionmid-index.private com.intellij.psi.PsiClassfindPsiClass(String fqn) Finds a PSI class by fully-qualified name across all open projects.findTypes(String query, boolean caseSensitive, boolean preferOpenProjects, Integer startIndex, Integer pageSize) Finds any Java types matching a query within the aggregated classpath of all open projects.findTypesInPackage(String packageName, ElementKind kindFilter, boolean recursive, Integer startIndex, Integer pageSize) Finds all types within a given package, with an option for recursive search.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.getMembers(JavaType javaType, String nameQuery, Integer startIndex, Integer pageSize, List<ElementKind> kindFilters) Gets a paginated list of all members 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.private StringgetMethodFqn(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.getModifiersSet(com.intellij.psi.PsiModifierList modifierList) Extracts the Java modifier keywords (public, static, final, …) present on a modifier list.private StringgetPsiMemberJavadoc(com.intellij.psi.PsiClass cl, String memberFqn) Returns the Javadoc comment text of a member (method, field or inner class) identified by its canonical FQN within a class.private List<JavaMember> getPsiMembers(com.intellij.psi.PsiClass cl) Collects the fields, methods, constructors and inner classes of a PSI class asJavaMemberkeychain DTOs.private StringgetPsiMemberSource(com.intellij.psi.PsiClass cl, String memberFqn) Returns the source text of a member (method, field or inner class) identified by its canonical FQN within a class.getSubtypes(JavaType javaType, Integer maxDepth) Recursively searches for all subtypes of a given JavaType.getSubtypesByFqn(String fqn, Integer maxDepth) Recursively searches for all subtypes of a type specified by its fully qualified name.private JavaHierarchyNodegetSubtypesNode(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 viaClassInheritorsSearch.getSupertypes(JavaType javaType, Integer maxDepth) Recursively searches for all supertypes of a given JavaType.getSupertypesByFqn(String fqn, Integer maxDepth) Recursively searches for all supertypes of a type specified by its fully qualified name.private JavaHierarchyNodegetSupertypesNode(com.intellij.psi.PsiClass cl, int maxDepth, int currentDepth) Recursively builds the supertype hierarchy node for a class up to a maximum depth, skippingjava.lang.Object.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.private URLgetUrlOfClass(com.intellij.psi.PsiClass cl) Resolves the source/class-filefile:URL backing a PSI class, used as the keychain for later source/javadoc lookups.loadTypeSources(JavaType javaType) Loads the source file for a given JavaType as a managed text resource.Loads the source file for of a java type as a managed resource by its fully qualified name.private JavaMemberresolveUniqueMember(String memberFqn) Resolves a canonical member FQN to a uniqueJavaMemberkeychain, waiting for indexing to finish first.private JavaTyperesolveUniqueType(String fqn) Resolves a fully-qualified type name to a uniqueJavaTypekeychain, waiting for indexing to finish first.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, populateMessage, setParentProvider, setProviding
-
Constructor Details
-
CodeModel
public CodeModel()Default constructor for the IntelliJ CodeModel toolkit.
-
-
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 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
Appends a class to the result list as aJavaTypekeychain 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
Resolves the source/class-filefile:URL backing a PSI class, used as the keychain for later source/javadoc lookups.- Parameters:
cl- the PSI class.- Returns:
- the file URL, or
nullif it cannot be resolved.
-
loadTypeSources
-
loadTypeSourcesByFqn
-
getTypeJavadocs
-
getTypeJavadocsByFqn
-
getMemberSources
Gets the source code for a specific JavaMember.- Parameters:
member- The target member.- Returns:
- Member source text.
- Throws:
Exception- on execution failure.
-
getMemberSourcesByFqn
-
getMemberJavadocs
Gets the Javadoc for a specific JavaMember.- Parameters:
member- The target member.- Returns:
- Member Javadoc comment text.
- Throws:
Exception- on execution failure.
-
getMemberJavadocsByFqn
-
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
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
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
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
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
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 throwingIndexNotReadyExceptionmid-index.- Throws:
AgiToolException- if any open project is still indexing after the bounded wait.
-
findPsiClass
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
nullif none is found.
-
resolveUniqueType
Resolves a fully-qualified type name to a uniqueJavaTypekeychain, 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
Resolves a canonical member FQN to a uniqueJavaMemberkeychain, 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
Collects the fields, methods, constructors and inner classes of a PSI class asJavaMemberkeychain DTOs. Must be called inside a read action.- Parameters:
cl- the declaring class.- Returns:
- the list of member keychains.
-
getMethodFqn
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
-
getPsiMemberSource
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
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, skippingjava.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 viaClassInheritorsSearch. 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.
-

