title: JDK11 to JDK12 Migration java.lang.NoSuchFieldException: modifiers
body: <p>Below function Hacks the &quot;HttpURLConnection#methods&quot; static field (through Java Reflection). I am using reflection to unit test my code functionality. I found out we can not change the static final fields in JDK12. I found one solution to use unsafe but I am not sure how can I get this function working in JDK12 using unsafe.</p>
  <pre><code>protected static void allowMethods(String... methods) {
      try {
          Field methodsField = HttpURLConnection.class.getDeclaredField(&quot;methods&quot;);
  
          Field modifiersField = Unsafe.class.getDeclaredField(&quot;modifiers&quot;);
          modifiersField.setAccessible(true);
          modifiersField.setInt(methodsField, methodsField.getModifiers() &amp; ~Modifier.FINAL);
  
          methodsField.setAccessible(true);
  
          String[] oldMethods = (String[]) methodsField.get(null);
          Set&lt;String&gt; methodsSet = new LinkedHashSet&lt;&gt;(Arrays.asList(oldMethods));
          methodsSet.addAll(Arrays.asList(methods));
          String[] newMethods = methodsSet.toArray(new String[0]);
  
          methodsField.set(null/*static field*/, newMethods);
      } catch (NoSuchFieldException | IllegalAccessException e) {
          throw new IllegalStateException(e);
      }
  }  
  </code></pre>
  <p>This is the stacktrace for the above code:</p>
  <pre><code>Caused by: java.lang.IllegalStateException: java.lang.NoSuchFieldException: modifiers
  at pii.rest.call.RestUtils.allowMethods(RestUtils.java:75)
  at pii.rest.call.JobAbort.&lt;clinit&gt;(JobAbort.java:39)
  Caused by: java.lang.NoSuchFieldException: modifiers
  at java.base/java.lang.Class.getDeclaredField(Class.java:2549)
  at pii.rest.call.RestUtils.allowMethods(RestUtils.java:62)
  </code></pre>
  <p>Can anybody help me convert this function to use Unsafe so therefore it works with JDK12+. I have tried this until now:</p>
  <pre><code>final Field ourField = HttpURLConnection.class.getDeclaredField(&quot;methods&quot;);
  final Field unsafeField = Unsafe.class.getDeclaredField(&quot;theUnsafe&quot;);
  unsafeField.setAccessible(true);
  final Unsafe unsafe = (Unsafe) unsafeField.get(null);
  final Object staticFieldBase = unsafe.staticFieldBase(ourField);
  final long staticFieldOffset = unsafe.staticFieldOffset(ourField);
  unsafe.putObject(staticFieldBase, staticFieldOffset, &quot;it works&quot;);
  </code></pre>