Skip to content

Commit 6fc1b37

Browse files
dfa1claude
andcommitted
refactor: collapse 13 copies of the sneaky-rethrow helper into NativeCall
Every binding class carried its own private rethrow/sneaky to launder the checked Throwable from MethodHandle.invokeExact. Replace them with one shared NativeCall.rethrow and repoint all catch blocks. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 7fa794d commit 6fc1b37

14 files changed

Lines changed: 53 additions & 115 deletions

zstd/src/main/java/io/github/dfa1/zstd/NativeCall.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static long checkReturnValue(ZstdCall c) {
2222
try {
2323
code = c.run();
2424
} catch (Throwable t) {
25-
throw sneaky(t);
25+
throw rethrow(t);
2626
}
2727
if (isError(code)) {
2828
throw new ZstdException(errorName(code), ZstdErrorCode.of(errorCode(code)));
@@ -34,15 +34,15 @@ static boolean isError(long code) {
3434
try {
3535
return ((int) Bindings.IS_ERROR.invokeExact(code)) != 0;
3636
} catch (Throwable t) {
37-
throw sneaky(t);
37+
throw rethrow(t);
3838
}
3939
}
4040

4141
private static int errorCode(long code) {
4242
try {
4343
return (int) Bindings.GET_ERROR_CODE.invokeExact(code);
4444
} catch (Throwable t) {
45-
throw sneaky(t);
45+
throw rethrow(t);
4646
}
4747
}
4848

@@ -52,7 +52,7 @@ private static String errorName(long code) {
5252
MemorySegment p = (MemorySegment) Bindings.GET_ERROR_NAME.invokeExact(code);
5353
return p.reinterpret(Long.MAX_VALUE).getString(0, StandardCharsets.US_ASCII);
5454
} catch (Throwable t) {
55-
throw sneaky(t);
55+
throw rethrow(t);
5656
}
5757
}
5858

@@ -67,8 +67,11 @@ static MemorySegment requireNative(MemorySegment seg, String name) {
6767
return seg;
6868
}
6969

70+
/// Rethrows any `Throwable` as if unchecked, laundering the checked
71+
/// `Throwable` that {@link java.lang.invoke.MethodHandle#invokeExact} declares.
72+
/// The shared sink for every binding class's native-call catch blocks.
7073
@SuppressWarnings("unchecked")
71-
private static <E extends Throwable> RuntimeException sneaky(Throwable t) throws E {
74+
static <E extends Throwable> RuntimeException rethrow(Throwable t) throws E {
7275
throw (E) t;
7376
}
7477

zstd/src/main/java/io/github/dfa1/zstd/Zstd.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public static long decompressedSize(MemorySegment frame) {
9696
try {
9797
size = (long) Bindings.GET_FRAME_CONTENT_SIZE.invokeExact(frame, frame.byteSize());
9898
} catch (Throwable t) {
99-
throw sneaky(t);
99+
throw NativeCall.rethrow(t);
100100
}
101101
if (size == CONTENTSIZE_UNKNOWN) {
102102
throw new ZstdException("decompressed size not stored in frame");
@@ -116,7 +116,7 @@ public static long compressBound(long srcSize) {
116116
try {
117117
return (long) Bindings.COMPRESS_BOUND.invokeExact(srcSize);
118118
} catch (Throwable t) {
119-
throw sneaky(t);
119+
throw NativeCall.rethrow(t);
120120
}
121121
}
122122

@@ -126,7 +126,7 @@ private static long frameContentSize(byte[] compressed) {
126126
MemorySegment in = copyIn(arena, compressed);
127127
return (long) Bindings.GET_FRAME_CONTENT_SIZE.invokeExact(in, (long) compressed.length);
128128
} catch (Throwable t) {
129-
throw sneaky(t);
129+
throw NativeCall.rethrow(t);
130130
}
131131
}
132132

@@ -137,7 +137,7 @@ public static int maxCompressionLevel() {
137137
try {
138138
return (int) Bindings.MAX_C_LEVEL.invokeExact();
139139
} catch (Throwable t) {
140-
throw sneaky(t);
140+
throw NativeCall.rethrow(t);
141141
}
142142
}
143143

@@ -148,7 +148,7 @@ public static int minCompressionLevel() {
148148
try {
149149
return (int) Bindings.MIN_C_LEVEL.invokeExact();
150150
} catch (Throwable t) {
151-
throw sneaky(t);
151+
throw NativeCall.rethrow(t);
152152
}
153153
}
154154

@@ -159,7 +159,7 @@ public static int defaultCompressionLevel() {
159159
try {
160160
return (int) Bindings.DEFAULT_C_LEVEL.invokeExact();
161161
} catch (Throwable t) {
162-
throw sneaky(t);
162+
throw NativeCall.rethrow(t);
163163
}
164164
}
165165

@@ -172,7 +172,7 @@ public static long estimateCompressContextSize(int level) {
172172
try {
173173
return (long) Bindings.ESTIMATE_CCTX_SIZE.invokeExact(level);
174174
} catch (Throwable t) {
175-
throw sneaky(t);
175+
throw NativeCall.rethrow(t);
176176
}
177177
}
178178

@@ -183,7 +183,7 @@ public static long estimateDecompressContextSize() {
183183
try {
184184
return (long) Bindings.ESTIMATE_DCTX_SIZE.invokeExact();
185185
} catch (Throwable t) {
186-
throw sneaky(t);
186+
throw NativeCall.rethrow(t);
187187
}
188188
}
189189

@@ -197,7 +197,7 @@ public static long estimateCompressDictSize(long dictSize, int level) {
197197
try {
198198
return (long) Bindings.ESTIMATE_CDICT_SIZE.invokeExact(dictSize, level);
199199
} catch (Throwable t) {
200-
throw sneaky(t);
200+
throw NativeCall.rethrow(t);
201201
}
202202
}
203203

@@ -210,7 +210,7 @@ public static long estimateDecompressDictSize(long dictSize) {
210210
try {
211211
return (long) Bindings.ESTIMATE_DDICT_SIZE.invokeExact(dictSize, 0); // ZSTD_dlm_byCopy
212212
} catch (Throwable t) {
213-
throw sneaky(t);
213+
throw NativeCall.rethrow(t);
214214
}
215215
}
216216

@@ -223,7 +223,7 @@ public static String version() {
223223
MemorySegment p = (MemorySegment) Bindings.VERSION_STRING.invokeExact();
224224
return p.reinterpret(Long.MAX_VALUE).getString(0, StandardCharsets.US_ASCII);
225225
} catch (Throwable t) {
226-
throw sneaky(t);
226+
throw NativeCall.rethrow(t);
227227
}
228228
}
229229

@@ -242,11 +242,6 @@ static byte[] copyOut(MemorySegment seg, long len) {
242242
return out;
243243
}
244244

245-
@SuppressWarnings("unchecked")
246-
private static <E extends Throwable> RuntimeException sneaky(Throwable t) throws E {
247-
throw (E) t;
248-
}
249-
250245
private Zstd() {
251246
// no instances
252247
}

zstd/src/main/java/io/github/dfa1/zstd/ZstdBounds.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,7 @@ static ZstdBounds query(MethodHandle getBounds, int parameter) {
2626
}
2727
return new ZstdBounds(bounds.get(JAVA_INT, 8), bounds.get(JAVA_INT, 12));
2828
} catch (Throwable t) {
29-
throw rethrow(t);
29+
throw NativeCall.rethrow(t);
3030
}
3131
}
32-
33-
@SuppressWarnings("unchecked")
34-
private static <E extends Throwable> RuntimeException rethrow(Throwable t) throws E {
35-
throw (E) t;
36-
}
3732
}

zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressCtx.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private static MemorySegment create() {
3333
}
3434
return p;
3535
} catch (Throwable t) {
36-
throw rethrow(t);
36+
throw NativeCall.rethrow(t);
3737
}
3838
}
3939

@@ -216,17 +216,12 @@ public long sizeOf() {
216216
try {
217217
return (long) Bindings.SIZEOF_CCTX.invokeExact(ptr());
218218
} catch (Throwable t) {
219-
throw rethrow(t);
219+
throw NativeCall.rethrow(t);
220220
}
221221
}
222222

223223
@Override
224224
protected void tryClose(MemorySegment ptr) throws Throwable {
225225
var _ = (long) Bindings.FREE_CCTX.invokeExact(ptr);
226226
}
227-
228-
@SuppressWarnings("unchecked")
229-
private static <E extends Throwable> RuntimeException rethrow(Throwable t) throws E {
230-
throw (E) t;
231-
}
232227
}

zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressDict.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private static MemorySegment create(ZstdDictionary dict, int level) {
6363
}
6464
return p;
6565
} catch (Throwable t) {
66-
throw rethrow(t);
66+
throw NativeCall.rethrow(t);
6767
}
6868
}
6969

@@ -76,7 +76,7 @@ private static MemorySegment create(MemorySegment dict, int level) {
7676
}
7777
return p;
7878
} catch (Throwable t) {
79-
throw rethrow(t);
79+
throw NativeCall.rethrow(t);
8080
}
8181
}
8282

@@ -94,7 +94,7 @@ public int id() {
9494
try {
9595
return (int) Bindings.GET_DICT_ID_FROM_CDICT.invokeExact(ptr());
9696
} catch (Throwable t) {
97-
throw rethrow(t);
97+
throw NativeCall.rethrow(t);
9898
}
9999
}
100100

@@ -105,17 +105,12 @@ public long sizeOf() {
105105
try {
106106
return (long) Bindings.SIZEOF_CDICT.invokeExact(ptr());
107107
} catch (Throwable t) {
108-
throw rethrow(t);
108+
throw NativeCall.rethrow(t);
109109
}
110110
}
111111

112112
@Override
113113
protected void tryClose(MemorySegment ptr) throws Throwable {
114114
var _ = (long) Bindings.FREE_CDICT.invokeExact(ptr);
115115
}
116-
117-
@SuppressWarnings("unchecked")
118-
private static <E extends Throwable> RuntimeException rethrow(Throwable t) throws E {
119-
throw (E) t;
120-
}
121116
}

zstd/src/main/java/io/github/dfa1/zstd/ZstdCompressStream.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public ZstdCompressStream(int level, ZstdDictionary dictionary) {
6060
}
6161
} catch (Throwable t) {
6262
close();
63-
throw rethrow(t);
63+
throw NativeCall.rethrow(t);
6464
}
6565
}
6666

@@ -72,7 +72,7 @@ private static MemorySegment createCctx() {
7272
}
7373
return cctx;
7474
} catch (Throwable t) {
75-
throw rethrow(t);
75+
throw NativeCall.rethrow(t);
7676
}
7777
}
7878

@@ -122,7 +122,7 @@ public ZstdFrameProgression progress() {
122122
p.get(JAVA_INT, 32), // currentJobID
123123
p.get(JAVA_INT, 36)); // nbActiveWorkers
124124
} catch (Throwable t) {
125-
throw rethrow(t);
125+
throw NativeCall.rethrow(t);
126126
}
127127
}
128128

@@ -133,7 +133,7 @@ public long sizeOf() {
133133
try {
134134
return (long) Bindings.SIZEOF_CCTX.invokeExact(ptr());
135135
} catch (Throwable t) {
136-
throw rethrow(t);
136+
throw NativeCall.rethrow(t);
137137
}
138138
}
139139

@@ -145,9 +145,4 @@ protected void tryClose(MemorySegment ptr) throws Throwable {
145145
arena.close();
146146
}
147147
}
148-
149-
@SuppressWarnings("unchecked")
150-
private static <E extends Throwable> RuntimeException rethrow(Throwable t) throws E {
151-
throw (E) t;
152-
}
153148
}

zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressCtx.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ private static MemorySegment create() {
2222
}
2323
return p;
2424
} catch (Throwable t) {
25-
throw rethrow(t);
25+
throw NativeCall.rethrow(t);
2626
}
2727
}
2828

@@ -176,17 +176,12 @@ public long sizeOf() {
176176
try {
177177
return (long) Bindings.SIZEOF_DCTX.invokeExact(ptr());
178178
} catch (Throwable t) {
179-
throw rethrow(t);
179+
throw NativeCall.rethrow(t);
180180
}
181181
}
182182

183183
@Override
184184
protected void tryClose(MemorySegment ptr) throws Throwable {
185185
var _ = (long) Bindings.FREE_DCTX.invokeExact(ptr);
186186
}
187-
188-
@SuppressWarnings("unchecked")
189-
private static <E extends Throwable> RuntimeException rethrow(Throwable t) throws E {
190-
throw (E) t;
191-
}
192187
}

zstd/src/main/java/io/github/dfa1/zstd/ZstdDecompressDict.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private static MemorySegment create(ZstdDictionary dict) {
4040
}
4141
return p;
4242
} catch (Throwable t) {
43-
throw rethrow(t);
43+
throw NativeCall.rethrow(t);
4444
}
4545
}
4646

@@ -53,7 +53,7 @@ private static MemorySegment create(MemorySegment dict) {
5353
}
5454
return p;
5555
} catch (Throwable t) {
56-
throw rethrow(t);
56+
throw NativeCall.rethrow(t);
5757
}
5858
}
5959

@@ -64,7 +64,7 @@ public int id() {
6464
try {
6565
return (int) Bindings.GET_DICT_ID_FROM_DDICT.invokeExact(ptr());
6666
} catch (Throwable t) {
67-
throw rethrow(t);
67+
throw NativeCall.rethrow(t);
6868
}
6969
}
7070

@@ -75,17 +75,12 @@ public long sizeOf() {
7575
try {
7676
return (long) Bindings.SIZEOF_DDICT.invokeExact(ptr());
7777
} catch (Throwable t) {
78-
throw rethrow(t);
78+
throw NativeCall.rethrow(t);
7979
}
8080
}
8181

8282
@Override
8383
protected void tryClose(MemorySegment ptr) throws Throwable {
8484
var _ = (long) Bindings.FREE_DDICT.invokeExact(ptr);
8585
}
86-
87-
@SuppressWarnings("unchecked")
88-
private static <E extends Throwable> RuntimeException rethrow(Throwable t) throws E {
89-
throw (E) t;
90-
}
9186
}

0 commit comments

Comments
 (0)