Created
October 10, 2018 16:42
-
-
Save exabrial/090ad6b1f6e60d0488a55dcb19e259ad to your computer and use it in GitHub Desktop.
JDK 1.8 Doesn't Support PATCH. Here's how to "patch" it at Runtime as long as your security manager is disabled.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PatchJdk18 { | |
public static void fixJdk18() { | |
try { | |
Field field = HttpURLConnection.class.getDeclaredField("methods"); | |
setFinalStatic(field, new String[] { "GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", "TRACE", "PATCH", "KITTENS" }); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
private static void setFinalStatic(Field field, Object newValue) throws Exception { | |
field.setAccessible(true); | |
Field modifiersField = Field.class.getDeclaredField("modifiers"); | |
modifiersField.setAccessible(true); | |
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); | |
field.set(null, newValue); | |
modifiersField.setInt(field, field.getModifiers() | Modifier.FINAL); | |
modifiersField.setAccessible(false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment