FrankYang0529 commented on code in PR #69257:
URL: https://github.com/apache/airflow/pull/69257#discussion_r3536750008


##########
java-sdk/processor/src/main/kotlin/org/apache/airflow/sdk/BuilderProcessor.kt:
##########
@@ -215,25 +213,49 @@ private val NUMBER_ACCESSORS: Map<TypeName, String> =
     }
   }
 
-private fun xcomAccess(xcom: RequiredXCom): CodeBlock {
-  val call = CodeBlock.of($$"client.getXCom($S)", xcom.taskId)
+private fun addXComAccess(
+  executeSpec: MethodSpec.Builder,
+  xcom: RequiredXCom,
+) {
   val type = TypeName.get(xcom.paramType)
   val accessor = NUMBER_ACCESSORS[type]
   val number = ClassName.get(Number::class.java)
   // Wire integers decode to Long and floats to Double, so a direct 
(Integer)/(Float)
   // cast throws ClassCastException; widen via Number instead.
-  return when {
-    accessor == null -> CodeBlock.of($$"($T) $L", if (type.isPrimitive) 
type.box() else type, call)
-    type.isPrimitive -> CodeBlock.of($$"(($T) $L).$L()", number, call, 
accessor)
-    else ->
-      CodeBlock.of(
-        $$"$T.ofNullable(($T) $L).map($T::$L).orElse(null)",
-        ClassName.get(Optional::class.java),
-        number,
-        call,
-        number,
-        accessor,
-      )
+  if (!type.isPrimitive) {
+    val getXCom = CodeBlock.of($$"client.getXCom($S)", xcom.taskId)
+    val value =
+      if (accessor == null) {
+        CodeBlock.of($$"($T) $L", type, getXCom)
+      } else {
+        CodeBlock.of(
+          $$"$T.ofNullable(($T) $L).map($T::$L).orElse(null)",
+          ClassName.get(Optional::class.java),
+          number,
+          getXCom,
+          number,
+          accessor,
+        )
+      }
+    executeSpec.addStatement($$"var $L = $L", xcom.paramName, value)
+    return
+  }
+  // A primitive parameter cannot hold null. Read the raw value first and fail 
with a
+  // clear error instead of an opaque NullPointerException while unboxing when 
absent.
+  val raw = "${xcom.paramName}Raw"

Review Comment:
   I changed to use `Optional.ofNullable(client.getXCom(...)).orElseThrow(() -> 
new MissingXComException(...))`, so we don't need variable assignments.



-- 
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]

Reply via email to