Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can always patch the old class with <a href="http://asm.ow2.org/" rel="nofollow">ASM</a>. Generating a new class from the old bytecode by ignoring the clinit block should be easy.</p> <pre><code>import org.objectweb.asm.*; import org.objectweb.asm.commons.EmptyVisitor; import java.io.*; public class ClinitKiller { public static void main (String[] args) { final InputStream input = ClinitKiller.class.getResourceAsStream(Test.class.getName() + ".class"); try { final byte[] bytes = instrument(input); FileOutputStream out = new FileOutputStream("/tmp/Test.class"); out.write(bytes); out.close(); } catch (IOException e) { e.printStackTrace(); } } public static byte[] instrument(InputStream is) throws IOException { ClassReader reader = new ClassReader(is); ClassWriter writer = new ClassWriter(0); ClinitKillerClassAdapter classAdapter = new ClinitKillerClassAdapter(writer); reader.accept(classAdapter, 0); return writer.toByteArray(); } } class ClinitKillerClassAdapter extends ClassAdapter { public ClinitKillerClassAdapter(final ClassVisitor cv) { super(cv); } public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature, final String[] exceptions) { if (name.equals("&lt;clinit&gt;")) { return new EmptyVisitor(); } return cv.visitMethod(access, name, desc, signature, exceptions); } } </code></pre> <p>Here is the before and after for the following class:</p> <pre><code>public class Test { private static final String value; static { System.out.println("Test static"); value = "test value"; } public static void main(String[] args) { System.out.println(value); } } </code></pre> <p>Before: </p> <pre><code>javap -c Test Compiled from "Test.java" public class Test extends java.lang.Object{ public Test(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."&lt;init&gt;":()V 4: return public static void main(java.lang.String[]); Code: 0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream; 3: getstatic #3; //Field value:Ljava/lang/String; 6: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 9: return static {}; Code: 0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #5; //String Test static 5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 8: ldc #6; //String test value 10: putstatic #3; //Field value:Ljava/lang/String; 13: return } </code></pre> <p>Output:<br/> Test static<br/> test value<br/></p> <p>After:</p> <pre><code>javap -c Test Compiled from "Test.java" public class Test extends java.lang.Object{ public Test(); Code: 0: aload_0 1: invokespecial #11; //Method java/lang/Object."&lt;init&gt;":()V 4: return public static void main(java.lang.String[]); Code: 0: getstatic #21; //Field java/lang/System.out:Ljava/io/PrintStream; 3: getstatic #23; //Field value:Ljava/lang/String; 6: invokevirtual #29; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 9: return } </code></pre> <p>Output:<br/> null</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload