mardi 4 décembre 2018

Set private field of package-private Class from outer package Class with reflection

I need to change the value of a private variable in a package-private class from a outer-package class.

  • package: java.util.jar from jdk-9.0.1
  • class: JarVerifier.java
  • variable: parsingBlockOrSF (private boolean)

I tried this:

private void writePrivateJarVerifierField(boolean newValue) throws Exception {
    Class<?> clazz = Class.forName("java.util.jar.JarVerifier");
    Field field = clazz.getDeclaredField("parsingBlockOrSF");
    field.setAccessible(true);
    field.setBoolean(clazz.newInstance(), newValue);
}

It gives me Exception in thread "main" java.lang.InstantiationException: java.util.jar.JarVerifier

I have seen this, this, this and this question already, though I am not able to derive a solution for my problem.

Can someone give me a hint please ?





Aucun commentaire:

Enregistrer un commentaire