title: How can I control the order of constant pool entries using ASM?
body: <p>I'm implementing a transformation that removes unused elements from .class files to reduce their size.  Because some constant pool entries will become unused, I'm having ASM recompute the constant pool, rather than copying it over from the input.  However, the transformed .class files are sometimes larger than the originals because ASM's constant pool ordering requires using <code>ldc_w</code> instructions (with a 2-byte index) where the input .class file used <code>ldc</code> (with a 1-byte index).  I'd like to manually sort the constant pool such that constants referred to by <code>ldc</code> come first.</p>
  
  <p>One might want to sort the constant pool for other reasons too: for example, to make a set of .class files more compressible by putting their constant pools in a canonical order, to test tools that consume .class files, to use the order as a software watermark, or to confuse poorly-implemented decompilers/deobfuscators.</p>
  
  <p>I grepped through the <a href="https://duckduckgo.com/l/?kh=-1&amp;uddg=http%3A%2F%2Fdownload.forge.objectweb.org%2Fasm%2Fasm4-guide.pdf" rel="noreferrer">ASM guide</a> for "constant", but there were no useful hits besides a generic explanation of what the constant pool is and "Hopefully ASM hides all the details related to the constant pool, so you will not have to bother about it.", which is anti-helpful in this case.</p>
  
  <p>How can I control the order in which ASM emits constant pool entries?</p>