Skip to content

Commit 9431d5e

Browse files
authored
ARROW-17113: [Java] Fail loudly in static initializer blocks (apache#13678)
- MemoryUtil will print a stack trace to the console on failure (since Arrow won't really work without it) - Flight utility classes will print a stack trace but won't prevent class initialization (it will just fall back to the "slow" path) Authored-by: David Li <li.davidm96@gmail.com> Signed-off-by: David Li <li.davidm96@gmail.com>
1 parent 7448322 commit 9431d5e

4 files changed

Lines changed: 7 additions & 12 deletions

File tree

‎java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/AddWritableBuffer.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public class AddWritableBuffer {
7272
tmpBufChainOut = tmpBufChainOut2;
7373

7474
} catch (Exception ex) {
75-
ex.printStackTrace();
75+
new RuntimeException("Failed to initialize AddWritableBuffer, falling back to slow path", ex).printStackTrace();
7676
}
7777

7878
bufConstruct = tmpConstruct;

‎java/flight/flight-core/src/main/java/org/apache/arrow/flight/grpc/GetReadableBuffer.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class GetReadableBuffer {
5151
tmpField = f;
5252
tmpClazz = clazz;
5353
} catch (Exception e) {
54-
e.printStackTrace();
54+
new RuntimeException("Failed to initialize GetReadableBuffer, falling back to slow path", e).printStackTrace();
5555
}
5656
READABLE_BUFFER = tmpField;
5757
BUFFER_INPUT_STREAM = tmpClazz;

‎java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/MemoryUtil.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,11 @@ public Object run() {
133133
}
134134
DIRECT_BUFFER_CONSTRUCTOR = directBufferConstructor;
135135
} catch (Throwable e) {
136-
throw new RuntimeException("Failed to initialize MemoryUtil.", e);
136+
// This exception will get swallowed, but it's necessary for the static analysis that ensures
137+
// the static fields above get initialized
138+
final RuntimeException failure = new RuntimeException("Failed to initialize MemoryUtil", e);
139+
failure.printStackTrace();
140+
throw failure;
137141
}
138142
}
139143

‎java/memory/memory-netty/src/main/java/io/netty/buffer/UnsafeDirectLittleEndian.java‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,7 @@
3131
* Netty classes and underlying Netty memory management.
3232
*/
3333
public class UnsafeDirectLittleEndian extends WrappedByteBuf {
34-
35-
public static final boolean ASSERT_ENABLED;
3634
private static final AtomicLong ID_GENERATOR = new AtomicLong(0);
37-
38-
static {
39-
boolean isAssertEnabled = false;
40-
assert isAssertEnabled = true;
41-
ASSERT_ENABLED = isAssertEnabled;
42-
}
43-
4435
public final long id = ID_GENERATOR.incrementAndGet();
4536
private final AbstractByteBuf wrapped;
4637
private final long memoryAddress;

0 commit comments

Comments
 (0)