specgen

package
v0.0.7 Latest Latest
Warning

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

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

Documentation

Overview

Package specgen generates Java API YAML specs from .class files using javap.

Index

Constants

This section is empty.

Variables

View Source
var AndroidServiceName map[string]string

AndroidServiceName maps known manager class names to their Android Context.getSystemService() constant names. Populated at runtime by LoadServiceNames, which reflects on android.jar via the svcgen Java tool.

Functions

func GenerateFromRefDir

func GenerateFromRefDir(
	refDir string,
	extraClassPath string,
	outputDir string,
	goModule string,
) error

GenerateFromRefDir scans ref/ for .class files and generates one YAML spec per top-level class (inner classes are grouped with their parent). extraClassPath is appended to the javap -cp argument.

func LoadServiceNames added in v0.0.4

func LoadServiceNames(classPath string) (map[string]string, error)

LoadServiceNames runs the Java svcgen program against the given android.jar classpath and returns a map of fully-qualified Java class name to service name string (e.g. "android.app.AlarmManager" -> "alarm").

Types

type JavapClass

type JavapClass struct {
	FullName     string // e.g. "android.app.KeyguardManager"
	IsInterface  bool
	IsAbstract   bool
	IsFinal      bool
	Constants    []JavapConstant
	Methods      []JavapMethod
	Constructors []JavapConstructor
	Implements   []string
}

JavapClass holds the parsed output of javap for one class.

func RunJavap

func RunJavap(classPath string, className string) (*JavapClass, error)

RunJavap executes javap and parses the output for a single class. classPath can contain multiple entries separated by ":". Uses -verbose to extract constant values from ConstantValue attributes.

type JavapConstant

type JavapConstant struct {
	Name     string // e.g. "ERROR_BAD_VALUE"
	JavaType string // e.g. "int", "java.lang.String"
	Value    string // e.g. "1", "gps" -- extracted from ConstantValue attribute
}

JavapConstant is a public static final field.

type JavapConstructor added in v0.0.7

type JavapConstructor struct {
	Params []JavapParam
}

JavapConstructor is a public constructor parsed from javap output.

type JavapMethod

type JavapMethod struct {
	Name       string
	ReturnType string // "void", "int", "boolean", "java.lang.String", etc.
	Params     []JavapParam
	IsStatic   bool
	Throws     bool
}

JavapMethod is a public method parsed from javap output.

type JavapParam

type JavapParam struct {
	JavaType string
}

JavapParam is a method parameter.

type PackageMapping

type PackageMapping struct {
	JavaPrefix string // e.g. "android.app.admin"
	Package    string // e.g. "admin"
	GoImport   string // e.g. "github.com/AndroidGoLab/jni/app/admin"
}

PackageMapping defines how a Java package maps to a Go package.

type SpecCallback

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

SpecCallback is a callback interface in the YAML spec.

type SpecCallbackMethod

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

SpecCallbackMethod is a callback method in the YAML spec.

type SpecClass

type SpecClass struct {
	JavaClass         string       `yaml:"java_class"`
	GoType            string       `yaml:"go_type"`
	Obtain            string       `yaml:"obtain,omitempty"`
	ServiceName       string       `yaml:"service_name,omitempty"`
	Kind              string       `yaml:"kind,omitempty"`
	Close             bool         `yaml:"close,omitempty"`
	ConstructorParams []SpecParam  `yaml:"constructor_params,omitempty"`
	Methods           []SpecMethod `yaml:"methods,omitempty"`
	StaticMethods     []SpecMethod `yaml:"static_methods,omitempty"`
	Fields            []SpecField  `yaml:"fields,omitempty"`
}

SpecClass is a class in the YAML spec.

type SpecConstant

type SpecConstant struct {
	GoName string `yaml:"go_name"`
	Value  string `yaml:"value"`
	GoType string `yaml:"go_type"`
}

SpecConstant is a constant in the YAML spec.

type SpecField

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

SpecField is a data class field in the YAML spec.

type SpecFile

type SpecFile struct {
	Package   string         `yaml:"package"`
	GoImport  string         `yaml:"go_import"`
	Classes   []SpecClass    `yaml:"classes"`
	Callbacks []SpecCallback `yaml:"callbacks,omitempty"`
	Constants []SpecConstant `yaml:"constants,omitempty"`
}

SpecFile is the YAML output structure.

func GenerateSpec

func GenerateSpec(
	classPath string,
	className string,
	pkgMapping PackageMapping,
	goModule string,
) (*SpecFile, error)

GenerateSpec generates a YAML spec from .class files in a directory by running javap on each class.

type SpecMethod

type SpecMethod struct {
	JavaMethod string      `yaml:"java_method"`
	GoName     string      `yaml:"go_name"`
	Params     []SpecParam `yaml:"params,omitempty"`
	Returns    string      `yaml:"returns"`
	Error      bool        `yaml:"error"`
}

SpecMethod is a method in the YAML spec.

type SpecParam

type SpecParam struct {
	JavaType string `yaml:"java_type"`
	GoName   string `yaml:"go_name"`
}

SpecParam is a method parameter in the YAML spec.

Jump to

Keyboard shortcuts

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