gemini-code-assist[bot] commented on code in PR #38906:
URL: https://github.com/apache/beam/pull/38906#discussion_r3395618215


##########
sdks/java/extensions/protobuf/src/main/java/org/apache/beam/sdk/extensions/protobuf/ProtoByteBuddyUtils.java:
##########
@@ -1190,4 +1209,12 @@ public ByteCodeAppender appender(final Target 
implementationTarget) {
       };
     }
   }
+
+  private static Class<?> getLoadingTarget(Class<?> protoClass) {
+    ClassLoader loader = 
ReflectHelpers.findClassLoader(ProtoByteBuddyUtils.class, protoClass);
+    if (loader == ProtoByteBuddyUtils.class.getClassLoader()) {
+      return ProtoByteBuddyUtils.class;
+    }
+    return protoClass;
+  }

Review Comment:
   ![high](https://www.gstatic.com/codereviewagent/high-priority.svg)
   
   The current implementation of `getLoadingTarget` has a bug due to the order 
of arguments passed to 
`ReflectHelpers.findClassLoader(ProtoByteBuddyUtils.class, protoClass)`.
   
   `ReflectHelpers.findClassLoader` evaluates the classes in the order they are 
provided and returns the class loader of the first class that has a non-null 
class loader. Since `ProtoByteBuddyUtils.class` is passed first, its class 
loader (which is almost always non-null) will always be returned. As a result, 
`loader == ProtoByteBuddyUtils.class.getClassLoader()` will always evaluate to 
`true`, and the method will always return `ProtoByteBuddyUtils.class`.
   
   If `protoClass` is loaded by a different class loader (e.g., in a plugin or 
a runner's user class loader), the generated class will be incorrectly injected 
into `ProtoByteBuddyUtils`'s class loader, which cannot access `protoClass`. 
This will lead to a `NoClassDefFoundError` or `ClassNotFoundException` at 
runtime.
   
   We can fix this and simplify the logic by directly comparing the class 
loaders using reference equality.
   
   ```java
     private static Class<?> getLoadingTarget(Class<?> protoClass) {
       if (protoClass.getClassLoader() == 
ProtoByteBuddyUtils.class.getClassLoader()) {
         return ProtoByteBuddyUtils.class;
       }
       return protoClass;
     }
   ```



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