{"record":{"id":"226fb460ef46e0ff","repo":"apache/beam","slug":"failed-to-get-the-enclosing-class-of-lambda","errorCode":null,"errorMessage":"Failed to get the enclosing class of lambda ","messagePattern":"Failed to get the enclosing class of lambda ","errorType":"exception","errorClass":"PopulateDisplayDataException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/display/DisplayData.java","lineNumber":770,"sourceCode":"      visitedPathMap.put(path, subComponent);\n      Class<?> namespace = subComponent.getClass();\n      // Common case: AutoValue classes such as AutoValue_FooIO_Read. It's more useful\n      // to show the user the FooIO.Read class, which is the direct superclass of the AutoValue\n      // generated class.\n      if (ReflectHelpers.getSimpleName(namespace).startsWith(\"AutoValue_\")) {\n        namespace = namespace.getSuperclass();\n      }\n      if (namespace.isSynthetic() && ReflectHelpers.getSimpleName(namespace).contains(\"$$Lambda\")) {\n        try {\n          String className = namespace.getCanonicalName();\n          // A local class, local interface, or anonymous class does not have a canonical name.\n          // https://docs.oracle.com/javase/specs/jls/se17/html/jls-6.html#jls-6.7\n          if (className == null) {\n            className = namespace.getName();\n          }\n          namespace = Class.forName(className.replaceFirst(\"\\\\$\\\\$Lambda.*\", \"\"));\n        } catch (Exception e) {\n          throw new PopulateDisplayDataException(\n              \"Failed to get the enclosing class of lambda \" + subComponent, e);\n        }\n      }\n\n      Path prevPath = latestPath;\n      Class<?> prevNs = latestNs;\n      latestPath = path;\n      latestNs = namespace;\n\n      try {\n        subComponent.populateDisplayData(this);\n      } catch (PopulateDisplayDataException e) {\n        // Don't re-wrap exceptions recursively.\n        throw e;\n      } catch (OutOfMemoryError oom) {\n        throw oom;\n      } catch (Throwable e) {\n        String msg =","sourceCodeStart":752,"sourceCodeEnd":788,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/display/DisplayData.java#L752-L788","documentation":"When populateDisplayData walks into a subcomponent that is a lambda, Beam derives the display-data namespace from the lambda's enclosing class via Class.forName, stripping the $$Lambda suffix. If any step of this reflective lookup fails (unknown class name, ClassNotFound, security/initializer error), Beam wraps the exception in PopulateDisplayDataException with this message. It signals a lambda whose enclosing class cannot be resolved reflectively.","triggerScenarios":"Including a lambda (e.g. a SerializableFunction used as a transform) as a subcomponent in populateDisplayData when its class name ends with $$Lambda and the stripped name cannot be loaded via Class.forName — e.g. exotic classloaders, shaded classes, or dynamically generated lambdas.","commonSituations":"Pipelines run under custom/shaded classloaders (Flink/Spark submission) where lambda synthetic classes resolve differently; lambdas defined in nested classes under relocation/shading; using lambdas where named DoFn classes are expected.","solutions":["Replace the lambda subcomponent with a named static inner class implementing the functional interface","Verify the enclosing class name is loadable by the runtime classloader (check shading/relocation config)","Call getEnclosingClass()/debug Class.forName on the stripped name to identify the unresolvable class","Catch PopulateDisplayDataException around display-data population if it is non-fatal in your tooling"],"exampleFix":"// before\nbuilder.include(\"fn\", (SerializableFunction<String, String>) s -> s.trim());\n// after\nbuilder.include(\"fn\", new TrimFn()); // named static class with populateDisplayData","handlingStrategy":"try-catch","validationCode":"String name = lambda.getClass().getName().replaceFirst(\"\\\\$\\\\$Lambda.*\", \"\");\ntry { Class.forName(name); } catch (ClassNotFoundException e) { /* use a named class instead of the lambda */ }","typeGuard":null,"tryCatchPattern":"try { builder.include(\"fn\", lambda); } catch (PopulateDisplayDataException e) { /* fall back to registering the lambda's functional type only */ }","preventionTips":["Prefer named static inner classes over lambdas for includable subcomponents","Check shading/relocation config keeps enclosing classes loadable","Smoke-test display-data population under the production classloader"],"tags":["beam","display-data","lambda","reflection"],"backgroundTag":"class-not-found","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}