Allow reflective access in UT 48/100448/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 7 Apr 2022 12:09:35 +0000 (14:09 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 7 Apr 2022 12:12:27 +0000 (14:12 +0200)
We have mucking around with internals, make sure we open them up from
java.base. We also need to switch to VarHandles to get at
Field.modifiers.

Change-Id: I111e8bd1e64d37c454e9935adce23743ac43fdaa
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
hwvtepsouthbound/hwvtepsouthbound-impl/pom.xml
hwvtepsouthbound/hwvtepsouthbound-impl/src/test/java/org/opendaylight/ovsdb/hwvtepsouthbound/DataChangeListenerTestBase.java
southbound/southbound-impl/pom.xml

index 48157f52eb71a032982ea8232d80047625d74cdb..199a071e7c38da8b16d5bc3282517bb19fb0df50 100644 (file)
@@ -62,11 +62,11 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
       <artifactId>library</artifactId>
       <version>${project.version}</version>
     </dependency>
-      <dependency>
-          <groupId>org.apache.karaf.shell</groupId>
-          <artifactId>org.apache.karaf.shell.console</artifactId>
-          <scope>provided</scope>
-      </dependency>
+    <dependency>
+      <groupId>org.apache.karaf.shell</groupId>
+      <artifactId>org.apache.karaf.shell.console</artifactId>
+      <scope>provided</scope>
+    </dependency>
     <dependency>
       <groupId>${project.groupId}</groupId>
       <artifactId>schema.hardwarevtep</artifactId>
@@ -123,6 +123,13 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
         <configuration>
           <forkCount>1</forkCount>
           <reuseForks>false</reuseForks>
+
+          <!-- FIXME: remove this once we do not muck with static final fields -->
+          <argLine>
+            @{argLine}
+            --add-opens java.base/java.lang=ALL-UNNAMED
+            --add-opens java.base/java.lang.reflect=ALL-UNNAMED
+          </argLine>
         </configuration>
       </plugin>
     </plugins>
index 59775b9d5c844c72adfd6a45bd55e87a59aa1af3..ee45b3b78e16742e78953d59addd90b7dfb2c1db 100644 (file)
@@ -18,6 +18,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 import com.google.common.util.concurrent.ListenableFuture;
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.VarHandle;
 import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
 import java.net.InetAddress;
@@ -77,6 +79,18 @@ import org.slf4j.LoggerFactory;
 public class DataChangeListenerTestBase extends AbstractDataBrokerTest {
     private static final Logger LOG = LoggerFactory.getLogger(DataChangeListenerTestBase.class);
 
+    // Hack to hack into Field.class for now
+    private static final VarHandle MODIFIERS;
+
+    static {
+        try {
+            var lookup = MethodHandles.privateLookupIn(Field.class, MethodHandles.lookup());
+            MODIFIERS = lookup.findVarHandle(Field.class, "modifiers", int.class);
+        } catch (IllegalAccessException | NoSuchFieldException e) {
+            throw new ExceptionInInitializerError(e);
+        }
+    }
+
     static DataBroker dataBroker;
 
     EntityOwnershipService entityOwnershipService;
@@ -138,9 +152,12 @@ public class DataChangeListenerTestBase extends AbstractDataBrokerTest {
         for (Field field : fields) {
             if (fieldName.equals(field.getName())) {
                 field.setAccessible(true);
-                Field modifiersField = Field.class.getDeclaredField("modifiers");
-                modifiersField.setAccessible(true);
-                modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
+
+                final int mods = field.getModifiers();
+                if (Modifier.isFinal(mods)) {
+                    MODIFIERS.set(field, mods & ~Modifier.FINAL);
+                }
+
                 field.set(null, newValue);
                 break;
             }
@@ -260,7 +277,7 @@ public class DataChangeListenerTestBase extends AbstractDataBrokerTest {
         return mergeNode(logicalDatastoreType, nodeIid, nodeBuilder);
     }
 
-    void deleteData(final LogicalDatastoreType datastoreType, InstanceIdentifier<?>... iids) {
+    void deleteData(final LogicalDatastoreType datastoreType, final InstanceIdentifier<?>... iids) {
         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
         for (InstanceIdentifier<?> id : iids) {
             transaction.delete(datastoreType, id);
index c735b627cc5b1a71df98613ea9a4e1d48e9a2a5c..509f705a149ac1b671a7eee0aac18d6e6d691576 100644 (file)
@@ -173,6 +173,17 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
               <value>org.sonar.java.jacoco.JUnitListener</value>
             </property>
           </properties>
+
+          <!-- FIXME: remove this declaration once powermock is gone -->
+          <argLine>
+            @{argLine}
+            --add-opens java.base/java.lang=ALL-UNNAMED
+            --add-opens java.base/java.lang.reflect=ALL-UNNAMED
+            --add-opens java.base/java.net=ALL-UNNAMED
+            --add-opens java.base/java.util=ALL-UNNAMED
+            --add-opens java.base/java.util.concurrent=ALL-UNNAMED
+            --add-opens java.base/java.util.stream=ALL-UNNAMED
+          </argLine>
         </configuration>
       </plugin>
       <plugin>