Issue #23507 Reflection helper methods moved to Utils class
- temporary solution, will be refactored in this PR
- we need them also on another place to reduce duplications
diff --git a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/LocationTest.java b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/LocationTest.java
index 23468b8..21227c9 100644
--- a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/LocationTest.java
+++ b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/LocationTest.java
@@ -26,12 +26,11 @@
import org.glassfish.contextpropagation.internal.ViewImpl;
import org.glassfish.contextpropagation.spi.ContextMapHelper;
import org.glassfish.contextpropagation.wireadapters.glassfish.DefaultWireAdapter;
+import org.glassfish.tests.utils.Utils;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
-import static org.glassfish.contextpropagation.adaptors.BootstrapUtils.getField;
-import static org.glassfish.contextpropagation.adaptors.BootstrapUtils.setField;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class LocationTest {
@@ -47,7 +46,7 @@
Location location = new Location(new ViewImpl("prefix") {
});
assertEquals("guid", location.getOrigin());
- setField(location, "origin", "non-null origin");
+ Utils.setField(location, "origin", "non-null origin");
assertEquals("non-null origin", location.getOrigin());
}
@@ -68,8 +67,8 @@
assertEquals(location, locationToPropagate);
Location propagatedLocation = new Location(new ViewImpl("prefix") {
});
- View view = getField(location, "view");
- setField(propagatedLocation, "view", view);
+ View view = Utils.getField(location, "view");
+ Utils.setField(propagatedLocation, "view", view);
propagatedLocation.contextAdded();
assertEquals("[0, 1]", propagatedLocation.getLocationId());
}
diff --git a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/adaptors/BootstrapUtils.java b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/adaptors/BootstrapUtils.java
index 954a6d2..fa2de04 100644
--- a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/adaptors/BootstrapUtils.java
+++ b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/adaptors/BootstrapUtils.java
@@ -17,7 +17,6 @@
package org.glassfish.contextpropagation.adaptors;
-import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashSet;
@@ -32,8 +31,7 @@
import org.glassfish.contextpropagation.internal.Utils.ContextMapAdditionalAccessors;
import org.glassfish.contextpropagation.spi.ContextMapHelper;
import org.glassfish.contextpropagation.wireadapters.WireAdapter;
-
-import mockit.internal.reflection.FieldReflection;
+import org.glassfish.tests.utils.Utils;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
@@ -105,7 +103,7 @@
public static void reset() {
try {
- setStaticField(ContextBootstrap.class, "isConfigured", false);
+ Utils.setStaticField(ContextBootstrap.class, "isConfigured", false);
ContextMapHelper.getScopeAwareContextMap().get("true");
fail("Should get IllegalStateException");
} catch (IllegalStateException e) {
@@ -114,46 +112,4 @@
fail(e.toString());
}
}
-
-
- // Reason for following methods: API of JMockit changes with minor versions
- public static void setStaticField(Class<?> clazz, String fieldName, Object value) {
- try {
- Field field = clazz.getDeclaredField(fieldName);
- field.setAccessible(true);
- field.set(null, value);
- } catch (Exception e) {
- throw new IllegalStateException("Failed to set static field " + fieldName + " of " + clazz + " to " + value, e);
- }
- }
-
- public static void getStaticField(Class<?> clazz, String fieldName) {
- try {
- Field field = clazz.getDeclaredField(fieldName);
- field.setAccessible(true);
- field.get(null);
- } catch (Exception e) {
- throw new IllegalStateException("Failed to get static field " + fieldName + " of " + clazz, e);
- }
- }
-
- public static void setField(Object instance, String fieldName, Object value) {
- try {
- Field field = instance.getClass().getDeclaredField(fieldName);
- FieldReflection.setFieldValue(field, instance, value);
- } catch (Exception e) {
- throw new IllegalStateException("Failed to set field " + fieldName + " of " + instance + " to " + value, e);
- }
- }
-
-
-
- public static <T> T getField(Object instance, String fieldName) {
- try {
- Field field = instance.getClass().getDeclaredField(fieldName);
- return FieldReflection.getFieldValue(field, instance);
- } catch (Exception e) {
- throw new IllegalStateException("Failed to get a value of field " + fieldName + " of " + instance , e);
- }
- }
}
diff --git a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/ContextMapImplTest.java b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/ContextMapImplTest.java
index fc8bfa8..4200a9b 100644
--- a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/ContextMapImplTest.java
+++ b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/ContextMapImplTest.java
@@ -37,7 +37,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import static org.glassfish.contextpropagation.adaptors.BootstrapUtils.*;
+import static org.glassfish.tests.utils.Utils.setStaticField;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
diff --git a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/UtilsTest.java b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/UtilsTest.java
index 095c12c..72a553d 100644
--- a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/UtilsTest.java
+++ b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/UtilsTest.java
@@ -35,6 +35,7 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
+import static org.glassfish.tests.utils.Utils.setStaticField;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -156,7 +157,7 @@
@Test
public void testValidateFactoryRegistrationAlreadyRegistered() {
RecordingLoggerAdapter logger = new RecordingLoggerAdapter();
- BootstrapUtils.setStaticField(ContextBootstrap.class, "loggerAdapter", logger);
+ setStaticField(ContextBootstrap.class, "loggerAdapter", logger);
Map<String, Object> map = new HashMap<>();
Utils.validateFactoryRegistrationArgs("key", msgID, "context class name", CONTEXT_VIEW_FACTORY, map);
logger.verify(null, null, null, (Object[]) null);
diff --git a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/ViewImplTest.java b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/ViewImplTest.java
index 7ec7604..05a73ba 100644
--- a/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/ViewImplTest.java
+++ b/nucleus/core/context-propagation/src/test/java/org/glassfish/contextpropagation/internal/ViewImplTest.java
@@ -21,6 +21,7 @@
import org.glassfish.contextpropagation.adaptors.BootstrapUtils;
import org.glassfish.contextpropagation.internal.Entry.ContextType;
import org.glassfish.contextpropagation.wireadapters.glassfish.DefaultWireAdapter;
+import org.glassfish.tests.utils.Utils;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
@@ -36,7 +37,7 @@
BootstrapUtils.bootstrap(new DefaultWireAdapter());
view = new ViewImpl("prefix");
sm = new SimpleMap();
- BootstrapUtils.setField(view, "sMap", sm);
+ Utils.setField(view, "sMap", sm);
sm.put("prefix.removeMe",
new Entry("removeMe", PropagationMode.defaultSet(), ContextType.STRING).init(true, true));
sm.put("prefix.getMe", new Entry("getMe", PropagationMode.defaultSet(), ContextType.STRING).init(true, true));
diff --git a/nucleus/test-utils/utils/src/main/java/org/glassfish/tests/utils/Utils.java b/nucleus/test-utils/utils/src/main/java/org/glassfish/tests/utils/Utils.java
index 46dae0b..3794965 100644
--- a/nucleus/test-utils/utils/src/main/java/org/glassfish/tests/utils/Utils.java
+++ b/nucleus/test-utils/utils/src/main/java/org/glassfish/tests/utils/Utils.java
@@ -21,6 +21,7 @@
import com.sun.enterprise.module.bootstrap.StartupContext;
import com.sun.enterprise.module.single.StaticModulesRegistry;
+import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.net.URL;
@@ -127,6 +128,46 @@
}
+ public static void setStaticField(Class<?> clazz, String fieldName, Object value) {
+ try {
+ Field field = clazz.getDeclaredField(fieldName);
+ field.setAccessible(true);
+ field.set(null, value);
+ } catch (Exception e) {
+ throw new IllegalStateException("Failed to set static field " + fieldName + " of " + clazz + " to " + value, e);
+ }
+ }
+
+ public static void getStaticField(Class<?> clazz, String fieldName) {
+ try {
+ Field field = clazz.getDeclaredField(fieldName);
+ field.setAccessible(true);
+ field.get(null);
+ } catch (Exception e) {
+ throw new IllegalStateException("Failed to get static field " + fieldName + " of " + clazz, e);
+ }
+ }
+
+ public static void setField(Object instance, String fieldName, Object value) {
+ try {
+ Field field = instance.getClass().getDeclaredField(fieldName);
+ field.setAccessible(true);
+ field.set(instance, value);
+ } catch (Exception e) {
+ throw new IllegalStateException("Failed to set field " + fieldName + " of " + instance + " to " + value, e);
+ }
+ }
+
+ public static <T> T getField(Object instance, String fieldName) {
+ try {
+ Field field = instance.getClass().getDeclaredField(fieldName);
+ field.setAccessible(true);
+ return (T) field.get(instance);
+ } catch (Exception e) {
+ throw new IllegalStateException("Failed to get a value of field " + fieldName + " of " + instance , e);
+ }
+ }
+
public static Subject createInternalAsadminSubject() {
final Subject subject = new Subject();
subject.getPrincipals().add(new PrincipalImpl("asadmin"));