Skip to content

Commit 37cac06

Browse files
authored
Fix DataflowV1 test failure by fixing getSimpleName access (#39330)
1 parent 477a47f commit 37cac06

4 files changed

Lines changed: 27 additions & 7 deletions

File tree

.github/trigger_files/beam_PostCommit_Java_DataflowV1.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"https://github.com/apache/beam/pull/36138": "Cleanly separating v1 worker and v2 sdk harness container image handling",
2+
"https://github.com/apache/beam/pull/39330": "Fix DataflowV1 test failure by fixing getSimpleName access",
33
"https://github.com/apache/beam/pull/34902": "Introducing OutputBuilder",
44
"https://github.com/apache/beam/pull/35177": "Introducing WindowedValueReceiver to runners",
55
"comment": "Modify this file in a trivial way to cause this test suite to run",

sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/display/DisplayData.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.Set;
3434
import org.apache.beam.sdk.options.ValueProvider;
3535
import org.apache.beam.sdk.transforms.PTransform;
36+
import org.apache.beam.sdk.util.common.ReflectHelpers;
3637
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Joiner;
3738
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableList;
3839
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableMap;
@@ -617,7 +618,7 @@ FormattedItemValue format(Object value) {
617618
@Override
618619
FormattedItemValue format(Object value) {
619620
Class<?> clazz = checkType(value, Class.class, JAVA_CLASS);
620-
return new FormattedItemValue(clazz.getName(), clazz.getSimpleName());
621+
return new FormattedItemValue(clazz.getName(), ReflectHelpers.getSimpleName(clazz));
621622
}
622623
};
623624

@@ -753,10 +754,10 @@ private Builder include(Path path, HasDisplayData subComponent) {
753754
// Common case: AutoValue classes such as AutoValue_FooIO_Read. It's more useful
754755
// to show the user the FooIO.Read class, which is the direct superclass of the AutoValue
755756
// generated class.
756-
if (namespace.getSimpleName().startsWith("AutoValue_")) {
757+
if (ReflectHelpers.getSimpleName(namespace).startsWith("AutoValue_")) {
757758
namespace = namespace.getSuperclass();
758759
}
759-
if (namespace.isSynthetic() && namespace.getSimpleName().contains("$$Lambda")) {
760+
if (namespace.isSynthetic() && ReflectHelpers.getSimpleName(namespace).contains("$$Lambda")) {
760761
try {
761762
String className = namespace.getCanonicalName();
762763
// A local class, local interface, or anonymous class does not have a canonical name.

sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/reflect/DoFnSignatures.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2421,7 +2421,8 @@ private static String format(TypeDescriptor<?> t) {
24212421
}
24222422

24232423
private static String format(Class<?> kls) {
2424-
return kls.getSimpleName().isEmpty() ? kls.getName() : kls.getSimpleName();
2424+
String simpleName = ReflectHelpers.getSimpleName(kls);
2425+
return simpleName.isEmpty() ? kls.getName() : simpleName;
24252426
}
24262427

24272428
static class ErrorReporter {

sdks/java/core/src/main/java/org/apache/beam/sdk/util/common/ReflectHelpers.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,29 @@ public class ReflectHelpers {
5252

5353
private static final Joiner COMMA_SEPARATOR = Joiner.on(", ");
5454

55+
/**
56+
* Returns the simple name of the given class, or a fallback simple name derived from the class
57+
* name if {@link Class#getSimpleName()} throws an exception (such as an {@link
58+
* IllegalAccessError} on Java 17+ for non-public inner classes).
59+
*/
60+
public static String getSimpleName(Class<?> clazz) {
61+
try {
62+
return clazz.getSimpleName();
63+
} catch (Throwable t) {
64+
if (clazz.isArray()) {
65+
return getSimpleName(clazz.getComponentType()) + "[]";
66+
}
67+
String name = clazz.getName();
68+
int idx = Math.max(name.lastIndexOf('.'), name.lastIndexOf('$'));
69+
return idx != -1 ? name.substring(idx + 1) : name;
70+
}
71+
}
72+
5573
/** Returns a string representation of the signature of a {@link Method}. */
5674
public static String formatMethod(Method input) {
5775
String parameterTypes =
5876
FluentIterable.from(asList(input.getParameterTypes()))
59-
.transform(Class::getSimpleName)
77+
.transform(ReflectHelpers::getSimpleName)
6078
.join(COMMA_SEPARATOR);
6179
return String.format("%s(%s)", input.getName(), parameterTypes);
6280
}
@@ -100,7 +118,7 @@ private static void format(StringBuilder builder, Type t) {
100118
}
101119

102120
private static void formatClass(StringBuilder builder, Class<?> clazz) {
103-
builder.append(clazz.getSimpleName());
121+
builder.append(getSimpleName(clazz));
104122
}
105123

106124
private static void formatTypeVariable(StringBuilder builder, TypeVariable<?> t) {

0 commit comments

Comments
 (0)