emberko

Untitled

Feb 2nd, 2021
129
183 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class LocalizationHack {
  2.  
  3.     @SuppressWarnings("FieldCanBeLocal")
  4.     private static Object hackyMemoryLeak;
  5.  
  6.     public static void inject(Locale currentLocale) {
  7.         try {
  8.             String baseName = "yourpackage/controls";
  9.             String fakeBaseName = "com/sun/javafx/scene/control/skin/resources/controls";
  10.             ResourceBundle.Control control = ResourceBundle.Control.getControl(ResourceBundle.Control.FORMAT_DEFAULT);
  11.             String resourceName = control.toResourceName(control.toBundleName(baseName, currentLocale), "properties");
  12.             // System.out.println(resourceName);
  13.             ResourceBundle bundle;
  14.             try (Reader reader = new InputStreamReader(LocalizationHack.class.getResourceAsStream("/" + resourceName), StandardCharsets.UTF_8)) {
  15.                 bundle = new PropertyResourceBundle(reader);
  16.             }
  17.             Class<?> cacheKeyClass = Class.forName("java.util.ResourceBundle$CacheKey");
  18.             Object cacheKey;
  19.             if (SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_9)) {
  20.                 //noinspection JavaReflectionMemberAccess
  21.                 Method mGetModule = Class.class.getDeclaredMethod("getModule");
  22.                 Class<?> cModule = Class.forName("java.lang.Module");
  23.                 Constructor<?> cacheKeyClassConstructor = cacheKeyClass.getDeclaredConstructor(String.class, Locale.class, cModule, cModule);
  24.                 cacheKeyClassConstructor.setAccessible(true);
  25.                 Object thisModule = mGetModule.invoke(LocalizationHack.class);
  26.                 Object javaFxModule = mGetModule.invoke(Control.class);
  27.                 //noinspection JavaReflectionInvocation
  28.                 cacheKey = cacheKeyClassConstructor.newInstance(fakeBaseName, currentLocale, javaFxModule, javaFxModule);
  29.             } else {
  30.                 Constructor<?> cacheKeyClassConstructor = cacheKeyClass.getDeclaredConstructor(String.class, Locale.class, ClassLoader.class);
  31.                 cacheKeyClassConstructor.setAccessible(true);
  32.                 cacheKey = cacheKeyClassConstructor.newInstance(fakeBaseName, currentLocale, ControlResources.class.getClassLoader());
  33.             }
  34.             Method putBundleInCache = ResourceBundle.class.getDeclaredMethod("putBundleInCache", cacheKeyClass, ResourceBundle.class, ResourceBundle.Control.class);
  35.             putBundleInCache.setAccessible(true);
  36.             putBundleInCache.invoke(null, cacheKey, bundle, control);
  37.             hackyMemoryLeak = bundle;
  38.         } catch (Exception e) {
  39.             // fuck your language
  40.         }
  41.     }
  42. }
RAW Paste Data