javagen

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2026 License: CC0-1.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildCallbackDispatch

func BuildCallbackDispatch(cb *MergedCallback) string

BuildCallbackDispatch generates the Go code for a callback's switch/case dispatch inside a jni.NewProxy handler.

func BuildCallbackType

func BuildCallbackType(cb *MergedCallback) string

BuildCallbackType generates the Go struct type definition with function fields for a callback interface.

func Generate

func Generate(specPath, overlayPath, templatesDir, outputDir, goModule string) error

Generate is the main entry point. It loads spec + overlay, merges them, renders templates, and writes output files.

The goModule parameter is the Go module path (e.g. "github.com/AndroidGoLab/jni"). Output directories and Go package names are derived from the spec's go_import field by stripping the module prefix.

func GoImportToRelDir

func GoImportToRelDir(goImport, goModule string) string

GoImportToRelDir strips the Go module prefix from a go_import path to produce the relative output directory. e.g. GoImportToRelDir("github.com/foo/bar/hw/cam", "github.com/foo/bar") → "hw/cam"

func IsManager

func IsManager(cls *MergedClass) bool

IsManager returns true if the class is obtained via system service.

func JNISignature

func JNISignature(params []Param, returnType string) string

JNISignature computes the JNI method signature from parameter types and return type.

func JNITypeSignature

func JNITypeSignature(javaType string) string

JNITypeSignature converts a single Java type to its JNI type signature. Generic type parameters are stripped because JNI uses erased types.

func JavaClassToSlash

func JavaClassToSlash(className string) string

JavaClassToSlash converts a dotted Java class name to slash-separated JNI format. Handles inner classes: "android.app.AlarmManager.AlarmClockInfo" becomes "android/app/AlarmManager$AlarmClockInfo".

func ParamConversionCode

func ParamConversionCode(p MergedParam) string

ParamConversionCode generates Go code to convert a Go parameter to its JNI representation before a JNI call.

func ReturnConversionCode

func ReturnConversionCode(javaType, goType string) string

ReturnConversionCode generates Go code to convert a JNI return value to Go.

Types

type Callback

type Callback struct {
	JavaInterface string           `yaml:"java_interface"`
	GoType        string           `yaml:"go_type"`
	Methods       []CallbackMethod `yaml:"methods"`
}

Callback describes a Java callback interface to bridge to Go.

type CallbackMethod

type CallbackMethod struct {
	JavaMethod string   `yaml:"java_method"`
	Params     []string `yaml:"params"`
	GoField    string   `yaml:"go_field"`
}

CallbackMethod describes a single method in a callback interface.

type Class

type Class struct {
	JavaClass     string        `yaml:"java_class"`
	GoType        string        `yaml:"go_type"`
	Obtain        string        `yaml:"obtain"`
	ServiceName   string        `yaml:"service_name"`
	Kind          string        `yaml:"kind"`
	Close         bool          `yaml:"close"`
	Methods       []Method      `yaml:"methods"`
	StaticMethods []Method      `yaml:"static_methods"`
	Fields        []Field       `yaml:"fields"`
	StaticFields  []StaticField `yaml:"static_fields"`
	IntentExtras  []IntentExtra `yaml:"intent_extras"`
}

Class describes a Java class to wrap.

func (Class) AllMethods

func (c Class) AllMethods() []Method

AllMethods returns both instance and static methods. Static methods have their Static field set to true.

type Constant

type Constant struct {
	GoName       string `yaml:"go_name"`
	Value        string `yaml:"value"`
	GoType       string `yaml:"go_type"`
	GoUnderlying string `yaml:"go_underlying"`
}

Constant describes a named constant value.

type ConversionOverride

type ConversionOverride struct {
	ToGo   string `yaml:"to_go"`
	ToJava string `yaml:"to_java"`
}

ConversionOverride specifies custom type conversion expressions.

type Field

type Field struct {
	JavaMethod string `yaml:"java_method"`
	JavaField  string `yaml:"java_field"`
	Returns    string `yaml:"returns"`
	GoName     string `yaml:"go_name"`
	GoType     string `yaml:"go_type"`
}

Field describes a getter method on a data class. Supports both java_method (getter) and java_field (direct field access).

func (Field) GetterName

func (f Field) GetterName() string

GetterName returns the Java getter method name for this field. If java_method is set, it is used directly. If java_field is set, it is converted to a getter name (e.g., "packageName" → "getPackageName").

type IntentExtra

type IntentExtra struct {
	JavaExtra string `yaml:"java_extra"`
	JavaType  string `yaml:"java_type"`
	GoName    string `yaml:"go_name"`
	GoType    string `yaml:"go_type"`
}

IntentExtra describes a value extracted from a sticky broadcast intent.

type ManagerData

type ManagerData struct {
	GoType      string
	ServiceName string
	HasClose    bool
}

ManagerData holds template data for generating a system service manager constructor.

func ManagerConstructorData

func ManagerConstructorData(cls *MergedClass) ManagerData

ManagerConstructorData returns template data for the NewManager constructor.

type MergedCallback

type MergedCallback struct {
	JavaInterface string
	GoType        string
	Methods       []MergedCallbackMethod
}

MergedCallback is a resolved callback interface.

type MergedCallbackMethod

type MergedCallbackMethod struct {
	JavaMethod string
	GoField    string
	Params     []MergedParam
	GoParams   string
}

MergedCallbackMethod is a resolved callback method.

type MergedClass

type MergedClass struct {
	JavaClass      string
	JavaClassSlash string
	GoType         string
	Obtain         string
	ServiceName    string
	Kind           string
	Close          bool
	Methods        []MergedMethod
}

MergedClass is a resolved Java class wrapper.

type MergedConstant

type MergedConstant struct {
	GoName string
	Value  string
}

MergedConstant is a resolved constant value.

type MergedConstantGroup

type MergedConstantGroup struct {
	GoType   string
	BaseType string
	Values   []MergedConstant
}

MergedConstantGroup groups constants by their Go type.

type MergedDataClass

type MergedDataClass struct {
	JavaClass      string
	JavaClassSlash string
	GoType         string
	Fields         []MergedField
}

MergedDataClass is a resolved data class (getter extraction).

type MergedField

type MergedField struct {
	JavaMethod string // getter method name (for java_method fields)
	JavaName   string // raw field name (for java_field direct access)
	GoName     string
	GoType     string
	JNISig     string
	CallSuffix string
	IsField    bool // true = use GetFieldID/Get*Field, false = use GetMethodID/Call*Method
}

MergedField is a resolved field getter on a data class.

type MergedMethod

type MergedMethod struct {
	JavaMethod string
	GoName     string
	Static     bool
	Params     []MergedParam
	Returns    string
	GoReturn   string
	Error      bool
	JNISig     string
	CallSuffix string
	ReturnKind ReturnKind

	GoParamList    string
	GoReturnList   string
	GoReturnVars   string
	GoReturnValues string
	JNIArgs        string
	HasError       bool
}

MergedMethod is a resolved method with all computed helpers.

type MergedParam

type MergedParam struct {
	JavaType       string
	GoName         string
	GoType         string
	ConversionCode string
	IsString       bool
	IsBool         bool
	IsObject       bool
}

MergedParam is a resolved parameter.

type MergedSpec

type MergedSpec struct {
	Package         string
	GoImport        string
	JavaPackageDesc string

	Classes        []MergedClass
	DataClasses    []MergedDataClass
	Callbacks      []MergedCallback
	ConstantGroups []MergedConstantGroup
}

MergedSpec is the fully resolved specification ready for template rendering.

func Merge

func Merge(spec *Spec, overlay *Overlay) (*MergedSpec, error)

Merge combines a Spec and Overlay into a fully resolved MergedSpec.

type Method

type Method struct {
	JavaMethod string  `yaml:"java_method"`
	GoName     string  `yaml:"go_name"`
	Static     bool    `yaml:"static"`
	Params     []Param `yaml:"params"`
	Returns    string  `yaml:"returns"`
	Error      bool    `yaml:"error"`
}

Method describes a Java method to generate a Go wrapper for.

type Overlay

type Overlay struct {
	GoNameOverrides map[string]string             `yaml:"go_name_overrides"`
	MethodGrouping  map[string][]string           `yaml:"method_grouping"`
	ExtraMethods    []Method                      `yaml:"extra_methods"`
	TypeOverrides   map[string]string             `yaml:"type_overrides"`
	ConversionFuncs map[string]ConversionOverride `yaml:"conversion_funcs"`
}

Overlay provides per-package customization applied on top of a Spec.

func LoadOverlay

func LoadOverlay(path string) (*Overlay, error)

LoadOverlay reads an overlay YAML file. If the file does not exist, an empty Overlay is returned without error.

type Param

type Param struct {
	JavaType string `yaml:"java_type"`
	GoName   string `yaml:"go_name"`
	GoType   string `yaml:"go_type"`
}

Param describes a method parameter.

type ReturnKind

type ReturnKind string

ReturnKind classifies how a method's return value should be handled in the generated code.

const (
	ReturnVoid      ReturnKind = "void"
	ReturnString    ReturnKind = "string"
	ReturnBool      ReturnKind = "bool"
	ReturnObject    ReturnKind = "object"
	ReturnPrimitive ReturnKind = "primitive"
)

type Spec

type Spec struct {
	Package      string        `yaml:"package"`
	GoImport     string        `yaml:"go_import"`
	Classes      []Class       `yaml:"classes"`
	Callbacks    []Callback    `yaml:"callbacks"`
	Constants    []Constant    `yaml:"constants"`
	IntentExtras []IntentExtra `yaml:"intent_extras"`
}

Spec represents a per-package Java API specification loaded from YAML.

func LoadSpec

func LoadSpec(path string) (*Spec, error)

LoadSpec reads and validates a Java API spec from a YAML file.

type StaticField

type StaticField struct {
	JavaField string `yaml:"java_field"`
	Returns   string `yaml:"returns"`
	GoName    string `yaml:"go_name"`
	GoType    string `yaml:"go_type"`
}

StaticField describes a Java static field to read.

type TypeConv

type TypeConv struct {
	GoType     string
	JNISig     string
	CallSuffix string
	IsObject   bool
}

TypeConv holds the Go and JNI type information for a Java type.

func ResolveType

func ResolveType(javaType string) TypeConv

ResolveType maps a Java type string to its TypeConv. Generic type parameters are stripped because JNI uses erased types.

Jump to

Keyboard shortcuts

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