gnodet-bot commented on code in PR #14028:
URL: https://github.com/apache/maven/pull/14028#discussion_r4588125421


##########
impl/maven-core/src/main/java/org/apache/maven/configuration/internal/EnhancedCompositeBeanHelper.java:
##########
@@ -52,6 +52,9 @@ public final class EnhancedCompositeBeanHelper {
     // Cache for field lookups: Class -> FieldName -> Field
     private static final ConcurrentMap<Class<?>, Map<String, Field>> 
FIELD_CACHE = new ConcurrentHashMap<>();
 
+    // Cache for accessible fields to avoid repeated setAccessible calls
+    private static final ConcurrentMap<Field, Boolean> ACCESSIBLE_FIELD_CACHE 
= new ConcurrentHashMap<>();

Review Comment:
   🚨 **Regression — this cache was deliberately removed from `maven-4.0.x` in 
commit 2bfdc6c (fix #11425).**
   
   The `ACCESSIBLE_FIELD_CACHE` was previously introduced on master, then 
cherry-picked to `maven-4.0.x`, and then **removed** because it causes a field 
accessibility leak: `setAccessible(true)` is never reversed, leaving the 
`Field` object permanently accessible. This broke compiler-plugin unit tests 
that relied on the original accessibility state being preserved.
   
   This PR re-introduces the exact caching that was reverted. The two tests 
`testFieldAccessibilityIsProperlyRestored` and 
`testMultipleFieldAccessesDoNotLeakAccessibility` added in the fix commit will 
fail.
   
   The optimization is not safe to backport to `maven-4.0.x` without also 
incorporating the accessibility-restore logic from the fix.



##########
impl/maven-core/src/main/java/org/apache/maven/configuration/internal/EnhancedCompositeBeanHelper.java:
##########
@@ -301,9 +304,19 @@ private Object convertProperty(
      * Set field value with cached accessibility.
      */
     private void setFieldValue(Object bean, Field field, Object value) throws 
IllegalAccessException {
-        if (!field.canAccess(bean)) {
+        Boolean isAccessible = ACCESSIBLE_FIELD_CACHE.get(field);
+        if (isAccessible == null) {
+            isAccessible = field.canAccess(bean);
+            if (!isAccessible) {
+                field.setAccessible(true);
+                isAccessible = true;
+            }
+            ACCESSIBLE_FIELD_CACHE.put(field, isAccessible);
+        } else if (!isAccessible) {
             field.setAccessible(true);
+            ACCESSIBLE_FIELD_CACHE.put(field, true);
         }
+
         field.set(bean, value);

Review Comment:
   🔍 **Dead code — this branch is unreachable (pre-existing in master PR #2526, 
not introduced here).**
   
   The cache `ACCESSIBLE_FIELD_CACHE` only ever stores `true`: on a cache miss, 
if `canAccess` is `false` we call `setAccessible(true)` and store `true`; if 
`canAccess` is `true` we store `true`. Neither path stores `false`. On a cache 
hit, `isAccessible` is always `true`, so `!isAccessible` is always `false` and 
this `else if` never executes.
   
   Suggested simplified form (once the accessibility-restore issue is resolved):
   ```suggestion
           } else {
               // isAccessible is always true here (we never cache false)
               // no action needed
           }
   ```
   Or simply remove the `else if` branch entirely.



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