javaresolve

package
v0.13.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package javaresolve implements the Java language in-process type resolver. It implements the typresolve.Resolver interface, resolving Java call expressions and type references using tree-sitter AST walking and a shared type registry built from extracted definitions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildImportMap

func BuildImportMap(root *sitter.Node, content []byte) map[string]string

BuildImportMap extracts all import declarations from a Java AST root and builds a map from simple class name to fully-qualified package path. Handles regular imports (import com.pkg.Class -> "Class" -> "com.pkg"), static imports (import static com.pkg.Class.method -> "Class" -> "com.pkg"), and skips wildcard imports (import com.pkg.*).

func BuildRegistry

func BuildRegistry(defs []typresolve.ResolverDef) *typresolve.Registry

BuildRegistry builds a typresolve.Registry from ResolverDef entries. Registers functions, methods (with receiver type extracted from QN), classes, interfaces, and enums.

func EvalExprType

func EvalExprType(ctx *ResolveContext, node *sitter.Node) *typresolve.Type

EvalExprType evaluates the type of a Java expression AST node using scope lookup, registry lookup, import resolution, and method dispatch.

func ExtractPackage

func ExtractPackage(root *sitter.Node, content []byte) string

ExtractPackage extracts the package declaration from the Java AST root. Returns the dotted package name (e.g., "com.example.service"). Returns an empty string if no package declaration is found.

func GetStdlibRegistry

func GetStdlibRegistry() *typresolve.Registry

GetStdlibRegistry returns the shared stdlib registry for use as a fallback.

func IsBuiltinFunc

func IsBuiltinFunc(name string) bool

IsBuiltinFunc returns true if the given name is a Java method that should be skipped during resolution (Object methods: toString, hashCode, equals, getClass, notify, notifyAll, wait, clone, finalize; also println for System.out/err).

func IsBuiltinType

func IsBuiltinType(name string) bool

IsBuiltinType returns true if the given name is a Java primitive or common builtin type (int, long, String, Object, Integer, etc.).

func LiteralType

func LiteralType(nodeType string) *typresolve.Type

LiteralType maps Java tree-sitter literal node types to builtin types. Returns nil if the node type is not a recognized literal.

func LookupField

func LookupField(reg *typresolve.Registry, typeQN string, fieldName string) *typresolve.Type

LookupField looks up a field on a named type, following the superclass chain (stored as EmbeddedTypes) up to maxEmbedDepth levels deep. Returns the field's type if found, nil otherwise.

func LookupFieldOrMethod

func LookupFieldOrMethod(reg *typresolve.Registry, typeQN string, memberName string) *typresolve.RegisteredFunc

LookupFieldOrMethod looks up a method on a named type, following superclass and interface chains (stored as EmbeddedTypes) up to maxEmbedDepth levels deep. Returns the registered function if found, nil otherwise.

func ParseTypeNode

func ParseTypeNode(node *sitter.Node, content []byte, pkgQN string, imports map[string]string) *typresolve.Type

ParseTypeNode converts a tree-sitter Java type AST node to a typresolve.Type. It handles type_identifier, generic_type, array_type, scoped_type_identifier, void_type, integral_type, floating_point_type, boolean_type, and wildcard patterns.

func ProcessStatement

func ProcessStatement(ctx *ResolveContext, node *sitter.Node)

ProcessStatement processes a Java statement node to bind variables into the current scope. Handles local_variable_declaration, enhanced_for_statement, for_statement, try_with_resources_statement, catch_clause, and expression_statement (assignments).

func ResolveBuiltinType

func ResolveBuiltinType(name string) *typresolve.Type

ResolveBuiltinType returns a typresolve.Builtin type if the name is a Java builtin type, nil otherwise.

func ResolveCallsInFile

func ResolveCallsInFile(ctx *ResolveContext, root *sitter.Node, fileHash types.Hash, repoURL string, filePath string) []types.Edge

ResolveCallsInFile walks a Java file's AST resolving call expressions and emitting edges. Uses the shared registry, per-file scope chain, and import map.

func ResolveImport

func ResolveImport(imports map[string]string, className string) (string, bool)

ResolveImport looks up a class name in the import map, returning the package path and true if found, or empty string and false otherwise.

func StdlibPackageTypes

func StdlibPackageTypes(pkg string) map[string]bool

StdlibPackageTypes returns the set of simple type names for a given package, or nil if the package is not a known stdlib package.

Types

type ImportInfo

type ImportInfo struct {
	// Regular: className -> packagePath (e.g., "UserService" -> "com.example.service")
	Regular map[string]string
	// WildcardPackages: list of package paths imported with .* (e.g., ["java.util", "com.example.model"])
	WildcardPackages []string
	// StaticMethods: methodName -> classQN (e.g., "assertEquals" -> "org.junit.Assert")
	StaticMethods map[string]string
	// StaticWildcardClasses: list of classQNs imported with static .* (e.g., ["java.lang.Math"])
	StaticWildcardClasses []string
}

ImportInfo holds the full import context for a Java file, including regular imports, wildcard imports, and static imports.

func BuildImportInfo

func BuildImportInfo(root *sitter.Node, content []byte) *ImportInfo

BuildImportInfo extracts all import declarations from a Java AST root and returns full import context including wildcards and static imports.

type JavaResolver

type JavaResolver struct {
	// contains filtered or unexported fields
}

JavaResolver implements typresolve.Resolver for Java. It uses tree-sitter AST walking and a shared type registry to resolve call expressions and type references without requiring an external LSP server.

func NewJavaResolver

func NewJavaResolver() *JavaResolver

NewJavaResolver creates a new JavaResolver.

func (*JavaResolver) InitWorkspace

func (r *JavaResolver) InitWorkspace(ctx context.Context, defs []typresolve.ResolverDef) error

InitWorkspace builds the cross-file type registry from extracted definitions. Called once before any ResolveFile calls. Sets the stdlib registry as fallback so java.lang, java.util, java.io types resolve without explicit definitions.

func (*JavaResolver) Language

func (r *JavaResolver) Language() string

Language returns "java".

func (*JavaResolver) ResolveFile

func (r *JavaResolver) ResolveFile(ctx context.Context, opts typresolve.ResolveFileOpts) ([]types.Edge, error)

ResolveFile resolves type references in a single Java file. Thread-safe after InitWorkspace completes (registry is read-only, all mutable state is stack-local).

type ResolveContext

type ResolveContext struct {
	Registry         *typresolve.Registry
	Scope            *typresolve.Scope
	Imports          map[string]string // className -> packagePath
	ImportInfo       *ImportInfo       // full import context (wildcards, static)
	PkgQN            string            // current package qualified name (e.g., "org.apache.kafka.clients")
	Content          []byte            // source file content
	EnclosingFuncQN  string            // QN of enclosing method
	EnclosingClassQN string            // QN of enclosing class (for this/super resolution)
}

ResolveContext holds per-file state for Java type resolution.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL