8

I am iterating over one class using ASM code without manipulating any byte code. But when I am converting classwriter to bytearray(cw.toByteArray()), I am getting Method code too large! exception.

Can anybody tell me when does this happen ..

My code snippet is as follows ---

InputStream in= new FileInputStream("D:/AshiqWorkspace/RandD/ByteCodeStudy/temp/GameManager.class");
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS|ClassWriter.COMPUTE_FRAMES);
ClassVisitor ca = null;
ca = new CustomClassNode(cw);  // CustomClassNode class extends ClassNode implements Opcodes
cr.accept(ca, 0);   
File outputDir=new File("D:/AshiqWorkspace/RandD/ByteCodeStudy/out");
outputDir.mkdirs();
DataOutputStream dout=new DataOutputStream(new FileOutputStream(new File(outputDir,"GameManager.class")));
dout.write(cw.toByteArray()); // on this line "method code too large exception coming"
vijay
  • 2,646
  • 2
  • 23
  • 37
Ashiq Sayyad
  • 111
  • 2
  • 5

1 Answers1

6

Certain methods in Java are already quite large, and instrumenting them increases their size further causing the exception that you observe. If i am not wrong the JVM imposes a size of 65535 bytes on the size of any method.

One solution/project that tries to overcome this problem looks at splitting the offending methods ... here is the link to its git repository: https://github.com/ThexXTURBOXx/asm-method-size. It is based off ASM itself. Hope this helps.

vijay
  • 2,646
  • 2
  • 23
  • 37
  • I checked the above link. Do we need to make the asm jar file from the above link project to import it the project... – Ashiq Sayyad Sep 19 '13 at 10:10
  • i have never had to use this project per se ... but what you are saying sounds about right. – vijay Sep 19 '13 at 11:43
  • 1
    The linked project hasn't had an update in a while, as an alternative I developed a simpler yet likely less robust solution at https://github.com/cretz/msplit – Chad Retz Aug 14 '18 at 17:41
  • 2
    Happens now in JLink for the JDK, which has no workaround sigh. Error: jdk.internal.org.objectweb.asm.MethodTooLargeException: Method too large: jdk/internal/module/SystemModules$all.moduleDescriptors ()[Ljava/lang/module/ModuleDescriptor; – Marc Magon Mar 03 '20 at 12:40
  • @vijay The bitbucket link is dead. Could you please fix it? Thanks! – Instein May 14 '21 at 08:41
  • 2
    For future reference: I have recovered the repository and converted it from Mercurial to Git. You can find the recovered repo here: https://github.com/ThexXTURBOXx/asm-method-size – ThexXTURBOXx Aug 11 '21 at 14:31
  • 1
    For reference: The jlink bug is https://bugs.openjdk.java.net/browse/JDK-8240567. – koppor Apr 18 '22 at 22:20