Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is possible to dump the raw byte code with <code>javap -v</code> but to get all the information in the class file in a format you can edit I use ASM. The reason you wouldn't use this for editing is;</p> <ul> <li>it doesn't allow you to do much that you can't do in Java.</li> <li>it much, much harder to use.</li> <li>you don't get much feed back as to what is wrong if you break it.</li> </ul> <p>Its worth nothing that the byte code is just a compiled for of the Java code which is why they do much the same thing. The JVM is free to optimise it in any number of ways which makes understand the byte code not very useful if you want to understand how the JVM works or the code runs.</p> <hr> <p>Thank you to @Antimony for suggested uses for manually written byte code. While some of them are valid they are either fairly advanced or obscure or have an alternative way of achieving the same thing.</p> <p>Some ideas for </p> <blockquote> <p>throw checked exceptions without declaring them, </p> </blockquote> <pre><code>Thread.currentThread().stop(checkedException); </code></pre> <blockquote> <p>catch checked exceptions without declaring them, </p> </blockquote> <pre><code> if (false) throw new CheckedException(); } catch(CheckedException ce) { </code></pre> <blockquote> <p>use identifiers invalid in Java</p> </blockquote> <p>There are so many valid, but not so useful characters to use I would use one of those</p> <pre><code>if( ⁀ ‿ ⁀ == ⁀ ⁔ ⁀ || ¢ + ¢== ₡) </code></pre> <p>and</p> <pre><code>for (char c‮h = 0; c‮h &lt; Character.MAX_VALUE; c‮h++) if (Character.isJavaIdentifierPart(c‮h) &amp;&amp; !Character.isJavaIdentifierStart(c‮h)) System.out.printf("%04x &lt;%s&gt;%n", (int) c‮h, "" + c‮h); </code></pre> <p>is valid code due to an invisible character in the identifier which causes the text to print backwards.</p> <p><a href="http://vanillajava.blogspot.co.uk/2012/09/hidden-code.html">http://vanillajava.blogspot.co.uk/2012/09/hidden-code.html</a></p> <p><a href="http://vanillajava.blogspot.co.uk/2012/08/uses-for-special-characters-in-java-code.html">http://vanillajava.blogspot.co.uk/2012/08/uses-for-special-characters-in-java-code.html</a></p> <blockquote> <p>access fields before constructor call</p> </blockquote> <p>I am pretty sure this causes a VerifyError (or it did when I tried it). Instead you can use</p> <pre><code>Object o = Unsafe.allocateInstance(clazz); // create without calling a constructor. </code></pre> <blockquote> <p>call different constructors conditionally</p> </blockquote> <p>Again I have been able to place code between the object creation and the instance call, but there might be a way to get it to work. In Java you would write</p> <pre><code>MyClass mc = condition ? new MyClass(a) : new MyClass(a, b); </code></pre> <blockquote> <p>do exception handling on constructors.</p> </blockquote> <p>Not sure what you mean here as you can catch or throw exceptions in constructors.</p> <blockquote> <p>assign final fields more than once or not at all</p> </blockquote> <p>You could use reflections instead and give final fields their default value. i.e. it not possible for a field to have no value.</p> <blockquote> <p>have a strictfp constructor with nonstrictfp methods</p> </blockquote> <p>That is an interesting one, but not one I have ever needed.</p> <blockquote> <p>use laxer type checking</p> </blockquote> <p>The byte code does allow different rules for type checking but they are easy to fall foul of with not so helpful VerifyErrors. You could do this but its very hard to get right IMHO.</p> <blockquote> <p>throw exceptions from static initializer</p> </blockquote> <p>You can throw unchecked exceptions already, and to throw checked ones you can use the trick above, however I usually wrap with in an AssertionError or the like.</p> <blockquote> <p>use invokedynamic</p> </blockquote> <p>You can use MethodHandles but they are rather clunky in Java 7. I hope that in Java 8 they will be more natural to use.</p> <blockquote> <p>use subroutines</p> </blockquote> <p>Interesting, I haven't tried this, but I am not sure what advantage they would give over method calls.</p> <blockquote> <p>use unusual control flow</p> </blockquote> <p>True, but this is more likely to confuse the JIT optimiser so the code can be slower as a result so may not give you the advantage you might hope for.</p> <blockquote> <p>use old super call semantics</p> </blockquote> <p>You could but I am not sure how useful that is.</p> <blockquote> <p>use thread monitors</p> </blockquote> <p>You can use</p> <pre><code>Unsafe.enterMonitor(); Unsafe.exitMonitor(); Unsafe.tryMonitorEnter(); </code></pre> <blockquote> <p>initial values for fields.</p> </blockquote> <p>I agree the compiler doesn't use these as much as I would expect but then again I am not sure they what difference it would make if it did.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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