Skip to content

Commit 0238362

Browse files
xaionaro@dx.centerxaionaro@dx.center
authored andcommitted
refactor: fix go-coding-style violations in manually written code
- Rename cstringLiteral→newCString (semantic integrity: not a literal) - Extract findClassWithFallback to flatten 5-level nesting and remove duplicated ClassLoader fallback blocks in proxy.go - Rename ExtractalarmClockInfo→ExtractAlarmClockInfo (broken PascalCase) - Rename files with prohibited "helpers" suffix: bundle_helpers.go→extract_bundle.go, intent_helpers.go→intent_extra.go, os/build/helpers.go→split into build_info.go, version_info.go, get_build_info.go - Move types to own files (AlarmClockInfo, BuildInfo, VersionInfo) - Fix silently discarded errors in os/build (use errors.Join) and app/context.ContentResolver (now returns error) - Deduplicate readString closure into readStaticStringField - Simplify jnierr cached strings: sync.Once+4 vars → 3 sync.OnceValue - Remove dead unsafe imports and sentinels - Format multi-param functions per style (one param per line) - Fix mismatched doc comment (Location→ExtractedLocation) - Remove what-not-why comments in bluetooth/new_adapter.go - Fix "helper" in testjvm package comment
1 parent a6ed963 commit 0238362

18 files changed

Lines changed: 261 additions & 235 deletions

File tree

app/alarm/alarm_clock_info.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package alarm
2+
3+
// AlarmClockInfo holds extracted fields from AlarmManager.AlarmClockInfo.
4+
type AlarmClockInfo struct {
5+
TriggerTime int64
6+
}

app/alarm/extract.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@ package alarm
22

33
import (
44
"fmt"
5-
"unsafe"
65

76
"github.com/AndroidGoLab/jni"
87
)
98

10-
// AlarmClockInfo holds extracted fields from AlarmManager.AlarmClockInfo.
11-
type AlarmClockInfo struct {
12-
TriggerTime int64
13-
}
14-
15-
// ExtractalarmClockInfo extracts fields from an AlarmManager.AlarmClockInfo JNI object.
16-
func ExtractalarmClockInfo(env *jni.Env, obj *jni.Object) (*AlarmClockInfo, error) {
9+
// ExtractAlarmClockInfo extracts fields from an AlarmManager.AlarmClockInfo JNI object.
10+
func ExtractAlarmClockInfo(
11+
env *jni.Env,
12+
obj *jni.Object,
13+
) (*AlarmClockInfo, error) {
1714
if err := ensureInit(env); err != nil {
1815
return nil, err
1916
}
@@ -33,5 +30,3 @@ func ExtractalarmClockInfo(env *jni.Env, obj *jni.Object) (*AlarmClockInfo, erro
3330

3431
return &AlarmClockInfo{TriggerTime: triggerTime}, nil
3532
}
36-
37-
var _ = unsafe.Pointer(nil)

app/context.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,12 @@ func (m *Context) PackageManager() (*jni.Object, error) {
8585
}
8686

8787
// ContentResolver calls android.content.Context.getContentResolver().
88-
func (m *Context) ContentResolver() *jni.Object {
88+
func (m *Context) ContentResolver() (*jni.Object, error) {
8989
var result *jni.Object
90+
var callErr error
9091
m.VM.Do(func(env *jni.Env) error {
9192
if err := ensureContextInit(env); err != nil {
93+
callErr = err
9294
return err
9395
}
9496
mid, err := env.GetMethodID(
@@ -97,12 +99,13 @@ func (m *Context) ContentResolver() *jni.Object {
9799
"()Landroid/content/ContentResolver;",
98100
)
99101
if err != nil {
100-
return err
102+
callErr = fmt.Errorf("get getContentResolver: %w", err)
103+
return callErr
101104
}
102-
result, _ = env.CallObjectMethod(m.Obj, mid)
103-
return nil
105+
result, callErr = env.CallObjectMethod(m.Obj, mid)
106+
return callErr
104107
})
105-
return result
108+
return result, callErr
106109
}
107110

108111
// GetSystemService calls android.content.Context.getSystemService.
Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package app
22

33
import (
44
"fmt"
5-
"unsafe"
65

76
"github.com/AndroidGoLab/jni"
87
)
@@ -38,7 +37,10 @@ func NewIntent(vm *jni.VM) (*Intent, error) {
3837
}
3938

4039
// PutStringExtra calls Intent.putExtra(key, value) for string values.
41-
func (i *Intent) PutStringExtra(key, value string) {
40+
func (i *Intent) PutStringExtra(
41+
key string,
42+
value string,
43+
) {
4244
i.VM.Do(func(env *jni.Env) error {
4345
cls := env.GetObjectClass(i.Obj)
4446
mid, err := env.GetMethodID(cls, "putExtra",
@@ -61,7 +63,10 @@ func (i *Intent) PutStringExtra(key, value string) {
6163
}
6264

6365
// PutIntExtra calls Intent.putExtra(key, value) for int values.
64-
func (i *Intent) PutIntExtra(key string, value int32) {
66+
func (i *Intent) PutIntExtra(
67+
key string,
68+
value int32,
69+
) {
6570
i.VM.Do(func(env *jni.Env) error {
6671
cls := env.GetObjectClass(i.Obj)
6772
mid, err := env.GetMethodID(cls, "putExtra",
@@ -80,7 +85,10 @@ func (i *Intent) PutIntExtra(key string, value int32) {
8085
}
8186

8287
// PutBoolExtra calls Intent.putExtra(key, value) for boolean values.
83-
func (i *Intent) PutBoolExtra(key string, value bool) {
88+
func (i *Intent) PutBoolExtra(
89+
key string,
90+
value bool,
91+
) {
8492
i.VM.Do(func(env *jni.Env) error {
8593
cls := env.GetObjectClass(i.Obj)
8694
mid, err := env.GetMethodID(cls, "putExtra",
@@ -103,9 +111,10 @@ func (i *Intent) PutBoolExtra(key string, value bool) {
103111
}
104112

105113
// GetBoolExtra calls Intent.getBooleanExtra(key, defaultValue).
106-
func (i *Intent) GetBoolExtra(key string, defaultValue bool) bool {
114+
func (i *Intent) GetBoolExtra(
115+
key string,
116+
defaultValue bool,
117+
) bool {
107118
result, _ := i.GetBooleanExtra(key, defaultValue)
108119
return result
109120
}
110-
111-
var _ = unsafe.Pointer(nil)

bluetooth/new_adapter.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ func NewAdapter(ctx *app.Context) (*Adapter, error) {
2121
return err
2222
}
2323

24-
// Get BluetoothManager via getSystemService("bluetooth").
2524
svc, err := ctx.GetSystemService("bluetooth")
2625
if err != nil {
2726
return fmt.Errorf("get bluetooth service: %w", err)
@@ -30,7 +29,6 @@ func NewAdapter(ctx *app.Context) (*Adapter, error) {
3029
return fmt.Errorf("bluetooth service not available")
3130
}
3231

33-
// Call BluetoothManager.getAdapter() to get the BluetoothAdapter.
3432
bmClass, err := env.FindClass("android/bluetooth/BluetoothManager")
3533
if err != nil {
3634
return fmt.Errorf("find BluetoothManager: %w", err)
@@ -68,5 +66,4 @@ func (m *Adapter) Close() {
6866
}
6967
}
7068

71-
// Ensure imports are used.
7269
var _ = unsafe.Pointer(nil)

examples/app_framework/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,12 @@ func run(vm *jni.VM) error {
106106
}
107107

108108
// Content resolver and package manager.
109-
resolver := ctx.ContentResolver()
110-
fmt.Fprintf(&output, "content resolver: %v\n", resolver)
109+
resolver, err := ctx.ContentResolver()
110+
if err != nil {
111+
fmt.Fprintf(&output, " ContentResolver: %v\n", err)
112+
} else {
113+
fmt.Fprintf(&output, "content resolver: %v\n", resolver)
114+
}
111115

112116
pkgMgr, err := ctx.PackageManager()
113117
if err != nil {

internal/jnierr/exception.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -146,25 +146,12 @@ func extractGoString(env *capi.Env, jstr capi.String) string {
146146
}
147147

148148
var (
149-
cachedStringsOnce sync.Once
150-
bGetName *byte
151-
bGetMessage *byte
152-
bVoidToString *byte
149+
cstrGetName = sync.OnceValue(func() *byte { return newCString("getName") })
150+
cstrGetMessage = sync.OnceValue(func() *byte { return newCString("getMessage") })
151+
cstrVoidToString = sync.OnceValue(func() *byte { return newCString("()Ljava/lang/String;") })
153152
)
154153

155-
func initCachedStrings() {
156-
cachedStringsOnce.Do(func() {
157-
bGetName = cstringLiteral("getName")
158-
bGetMessage = cstringLiteral("getMessage")
159-
bVoidToString = cstringLiteral("()Ljava/lang/String;")
160-
})
161-
}
162-
163-
func cstrGetName() *byte { initCachedStrings(); return bGetName }
164-
func cstrGetMessage() *byte { initCachedStrings(); return bGetMessage }
165-
func cstrVoidToString() *byte { initCachedStrings(); return bVoidToString }
166-
167-
func cstringLiteral(s string) *byte {
154+
func newCString(s string) *byte {
168155
b := make([]byte, len(s)+1)
169156
copy(b, s)
170157
return &b[0]

internal/testjvm/testjvm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Package testjvm provides a helper to create a JVM for testing.
1+
// Package testjvm creates and manages a JVM instance for use in tests.
22
package testjvm
33

44
// #cgo CFLAGS: -I/usr/lib/jvm/java-25-openjdk-amd64/include -I/usr/lib/jvm/java-25-openjdk-amd64/include/linux

jni_proxy_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,19 @@ func TestLookupProxyNotFound(t *testing.T) {
5050
}
5151
}
5252

53-
// --- cstringLiteral ---
53+
// --- newCString ---
5454

55-
func TestCstringLiteral(t *testing.T) {
56-
ptr := cstringLiteral("hello")
55+
func TestNewCString(t *testing.T) {
56+
ptr := newCString("hello")
5757
if ptr == nil {
58-
t.Fatal("cstringLiteral returned nil")
58+
t.Fatal("newCString returned nil")
5959
}
6060
}
6161

62-
func TestCstringLiteralEmpty(t *testing.T) {
63-
ptr := cstringLiteral("")
62+
func TestNewCStringEmpty(t *testing.T) {
63+
ptr := newCString("")
6464
if ptr == nil {
65-
t.Fatal("cstringLiteral returned nil for empty string")
65+
t.Fatal("newCString returned nil for empty string")
6666
}
6767
}
6868

0 commit comments

Comments
 (0)