Skip to content

Commit c07eb4c

Browse files
committed
fix: initialize generated interface fields
1 parent 8fd7a7b commit c07eb4c

6 files changed

Lines changed: 75 additions & 21 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@
33
<!-- Keep only the latest release notes here. Do not include a version number or
44
append older releases. Replace the entries below when publishing a new release. -->
55

6-
- Organize member-query output around individual `overloads`, with stable
7-
identities, latest signatures, structured members, and independent version
8-
ranges.
9-
- Distinguish a missing parameter signature with `overload-not-found`, and
10-
expose the overloads available together through top-level range IDs.
11-
- Add a non-persistent Web selector for switching between all Java overloads
12-
and one specific signature while keeping the aggregate view as the default.
13-
- Keep hidden-API code generation, cache invalidation, CLI documentation, and
14-
the release-matched Codex Skill aligned with the new result model.
6+
- Initialize generated interface fields with `RemapStub.value()` so the Java
7+
source compiles without embedding or inlining literal constants.
8+
- Omit redundant `static` and `final` modifiers from generated interface
9+
fields while keeping class-field output unchanged.

‎packages/android-api-diff/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "android-api-diff",
3-
"version": "0.3.1",
3+
"version": "0.3.2",
44
"description": "Inspect Android framework API changes and generate hidden-API Java code",
55
"type": "module",
66
"keywords": [

‎packages/api-parser/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@android-cs/api-parser",
3-
"version": "0.3.1",
3+
"version": "0.3.2",
44
"type": "module",
55
"repository": {
66
"type": "git",

‎packages/api-query/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@android-cs/api-query",
3-
"version": "0.3.1",
3+
"version": "0.3.2",
44
"type": "module",
55
"repository": {
66
"type": "git",

‎packages/api-query/scripts/code-render.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,51 @@ const baseResult = (
261261
assert.doesNotMatch(result.code, /transact\(.*RuntimeException/);
262262
}
263263

264+
{
265+
const staticField: AndroidApiMemberResult = {
266+
kind: 'field',
267+
name: 'STATE',
268+
type: 'int',
269+
imports: [],
270+
isStatic: true,
271+
};
272+
const result = renderAndroidApiCodeWithMemberCode(
273+
baseResult(
274+
'IBinder.SHELL_COMMAND_TRANSACTION',
275+
'core/java/android/os/IBinder.java',
276+
['IBinder', 'SHELL_COMMAND_TRANSACTION'],
277+
[
278+
range('8', 'O', 26, 'android-8.0.0_r1', [
279+
constant('int', 'SHELL_COMMAND_TRANSACTION'),
280+
staticField,
281+
]),
282+
],
283+
[{ name: 'IBinder', kind: 'interface', isHidden: false }],
284+
),
285+
);
286+
287+
assert.match(result.code, /import li\.songe\.remap\.RemapStub;/);
288+
assert.match(result.code, /import li\.songe\.remap\.RemapType;/);
289+
assert.match(
290+
result.code,
291+
/@RemapType\(IBinder\.class\)\npublic interface IBinderHidden \{/,
292+
);
293+
assert.match(
294+
result.code,
295+
/\n int SHELL_COMMAND_TRANSACTION = RemapStub\.value\(\);\n/,
296+
);
297+
assert.match(result.code, /\n int STATE = RemapStub\.value\(\);\n/);
298+
assert.doesNotMatch(result.code, /\b(?:static|final)\s+int\b/);
299+
assert.equal(
300+
result.memberCode,
301+
[
302+
' int SHELL_COMMAND_TRANSACTION = RemapStub.value();',
303+
'',
304+
' int STATE = RemapStub.value();',
305+
].join('\n'),
306+
);
307+
}
308+
264309
{
265310
const result = renderAndroidApiCode(
266311
baseResult(

‎packages/api-query/src/code-render.ts‎

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -867,21 +867,25 @@ const formatFieldDeclaration = (
867867
sourceImports: string[],
868868
conflictingNames: Set<string>,
869869
) => {
870-
const prefix =
871-
member.kind === 'constant' || member.isStatic
872-
? inInterface
873-
? 'static '
874-
: 'public static '
875-
: inInterface
876-
? ''
877-
: 'public ';
870+
const prefix = inInterface
871+
? ''
872+
: member.kind === 'constant' || member.isStatic
873+
? 'public static '
874+
: 'public ';
875+
const initializer = inInterface
876+
? ` = ${getGeneratedName(
877+
'RemapStub',
878+
'li.songe.remap.RemapStub',
879+
conflictingNames,
880+
)}.value()`
881+
: '';
878882
return `${indent(level)}${prefix}${formatAnnotatedType(
879883
member.type,
880884
member.fieldNullability,
881885
member,
882886
sourceImports,
883887
conflictingNames,
884-
)} ${member.name};`;
888+
)} ${member.name}${initializer};`;
885889
};
886890

887891
const formatMemberDeclaration = (
@@ -1197,6 +1201,7 @@ const collectImports = (
11971201
packageName: string,
11981202
aidlSource: boolean,
11991203
aidlInterface: boolean,
1204+
inInterface: boolean,
12001205
actualClassPath: string[],
12011206
displayClassPath: string[],
12021207
baselineApiVersion: number | undefined,
@@ -1214,6 +1219,14 @@ const collectImports = (
12141219
if (hasRemappedClass(actualClassPath, displayClassPath)) {
12151220
addImport(imports, packageName, 'li.songe.remap.RemapType');
12161221
}
1222+
if (
1223+
inInterface &&
1224+
declarations.some(
1225+
({ member }) => member.kind === 'field' || member.kind === 'constant',
1226+
)
1227+
) {
1228+
addImport(imports, packageName, 'li.songe.remap.RemapStub');
1229+
}
12171230
if (declarations.some((declaration) => declaration.remapMethodName)) {
12181231
addImport(imports, packageName, 'li.songe.remap.RemapMethod');
12191232
}
@@ -1366,6 +1379,7 @@ const renderCode = (
13661379
packageName,
13671380
!!result.source?.path.endsWith('.aidl'),
13681381
includeAidlStub,
1382+
inInterface,
13691383
actualClassPath,
13701384
displayClassPath,
13711385
baselineApiVersion,

0 commit comments

Comments
 (0)