Commit d8d12c7
authored
[runners-spark] Use robust constructor resolution in EncoderFactory (#38271)
* [runners-spark] Use robust constructor resolution in EncoderFactory
Replace `(Constructor<X>) X.class.getConstructors()[0]` for
StaticInvoke, Invoke, and NewInstance with a `primaryConstructor()`
helper that picks the constructor with the most parameters.
Class.getConstructors() returns constructors in JVM-defined order
that is not guaranteed stable, so resolving the widest constructor
explicitly makes the lookup robust to future Spark releases that
add overloaded constructors.
Today this is a no-op: StaticInvoke / Invoke / NewInstance only
have one public constructor each in Spark 3.1.x through 3.5.x, so
getConstructors()[0] and the widest constructor resolve to the
same one. The change is purely defensive.
Same fix has already landed in the Spark 4 override in PR #38255
(commit 9c071c5, flagged by Gemini Code Assist round 1). Porting
it to the shared base keeps both code paths consistent.
* Address Gemini review: guard primaryConstructor against empty ctors
If Class.getConstructors() returns empty (e.g. all public constructors
removed in a future Spark version), throw an IllegalStateException with
the class name instead of letting ctors[0] surface as a cryptic
ArrayIndexOutOfBoundsException during static initialization.1 parent 0884c46 commit d8d12c7
1 file changed
Lines changed: 22 additions & 5 deletions
File tree
- runners/spark/src/main/java/org/apache/beam/runners/spark/structuredstreaming/translation/helpers
Lines changed: 22 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
34 | | - | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
35 | 38 | | |
36 | | - | |
| 39 | + | |
37 | 40 | | |
38 | | - | |
39 | | - | |
| 41 | + | |
40 | 42 | | |
41 | 43 | | |
42 | | - | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
43 | 60 | | |
44 | 61 | | |
45 | 62 | | |
| |||
0 commit comments