punAhuja commented on PR #3741:
URL: https://github.com/apache/solr/pull/3741#issuecomment-3497223279

   > Java 9 and beyond, I suggest VarHandle API instead:
   > 
   > ```java
   > // A lookup object that can see the private members of this class.
   >     MethodHandles.Lookup lookup = MethodHandles.lookup();
   >     VarHandle fieldHandle = null;
   >     Class<?> currentClass = config.getClass();
   > 
   >     // Loop to find the field in the class hierarchy
   >     while (currentClass != null && fieldHandle == null) {
   >         try {
   >             // Get a special lookup that can access private members of the 
target class.
   >             // This requires `--add-opens` if the target is in another 
module.
   >             MethodHandles.Lookup privateLookup = 
MethodHandles.privateLookupIn(currentClass, lookup);
   >             // Find a handle for the field by name and type.
   >             fieldHandle = privateLookup.findVarHandle(currentClass, 
"perThreadHardLimitMB", int.class);
   >         } catch (IllegalAccessException | NoSuchFieldException e) {
   >             // Field not in this class, try the superclass.
   >             currentClass = currentClass.getSuperclass();
   >         }
   >     }
   > 
   >     try {
   >         if (fieldHandle != null) {
   >             // Set the value using the handle.
   >             fieldHandle.set(config, limitMB);
   >             log.info("Set perThreadHardLimitMB to {} MB via VarHandle", 
limitMB);
   >         } else {
   >             log.error("Could not find VarHandle for perThreadHardLimitMB 
field");
   >         }
   >     } catch (Throwable t) { // VarHandle.set can throw Throwable
   >         log.error("Failed to set per-thread RAM limit via VarHandle", t);
   >     }
   > ```
   
   Thanks, I have updated the pull request to use VarHandle instead of 
reflection.


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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to