gemini-code-assist[bot] commented on code in PR #39330:
URL: https://github.com/apache/beam/pull/39330#discussion_r3583576177
##########
sdks/java/core/src/main/java/org/apache/beam/sdk/util/common/ReflectHelpers.java:
##########
@@ -52,11 +52,26 @@ public class ReflectHelpers {
private static final Joiner COMMA_SEPARATOR = Joiner.on(", ");
+ /**
+ * Returns the simple name of the given class, or a fallback simple name
derived from the class
+ * name if {@link Class#getSimpleName()} throws an exception (such as an
{@link
+ * IllegalAccessError} on Java 17+ for non-public inner classes).
+ */
+ public static String getSimpleName(Class<?> clazz) {
+ try {
+ return clazz.getSimpleName();
+ } catch (Throwable t) {
+ String name = clazz.getName();
+ int idx = Math.max(name.lastIndexOf('.'), name.lastIndexOf('$'));
+ return idx != -1 ? name.substring(idx + 1) : name;
+ }
+ }
Review Comment:

If `clazz` is an array of a non-public inner class (e.g.,
`SomeNonPublicInnerClass[]`), calling `clazz.getSimpleName()` will also fail
because it internally calls `getSimpleName()` on the component type. In this
case, the fallback logic will be triggered, and `clazz.getName()` will return
something like `[Lpackage.Outer$Inner;`. The current fallback logic would then
return `"Inner;"` instead of `"Inner[]"`.
To handle array types correctly in the fallback path, we should check if the
class is an array and recursively call `getSimpleName` on its component type.
```java
public static String getSimpleName(Class<?> clazz) {
try {
return clazz.getSimpleName();
} catch (Throwable t) {
if (clazz.isArray()) {
return getSimpleName(clazz.getComponentType()) + "[]";
}
String name = clazz.getName();
int idx = Math.max(name.lastIndexOf('.'), name.lastIndexOf('$'));
return idx != -1 ? name.substring(idx + 1) : name;
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]