In general, we write Java source code, with the Java compiler to compile the. class file, you will not encounter a failure to verify the situation, because the normal Java compiler will be careful to the generated code. So, one of the easiest ways to see the failure of validation is to generate an illegal byte code yourself.
Here I use the ObjectWeb asm to generate the byte code. You can download Asm-3.1.jar from the website and make sure it is on classpath when compiling and running the following program.
(I would have liked to have tried the Bitescript library written by Charles O ' Nutter, but laziness came up and bothered to download ...) Next time, next time.
Java code
Import Java.io.FileOutputStream;
Import Org.objectweb.asm.ClassWriter;
Import Org.objectweb.asm.MethodVisitor;
Import Org.objectweb.asm.Opcodes; public class Testasm implements opcodes {public static void main (string[] args) throws Exception {Classwrite
R cw = new Classwriter (0);
Cw.visit (v1_5,//class format version acc_public,//class modifiers
' Testverification ',//class name fully qualified name NULL,//generic signature
"Java/lang/object",//Super class fully qualified name new string[] {}//implemented interfaces
);
Methodvisitor mv = Cw.visitmethod (acc_public,//access modifiers "foo",//Method name "() V",//Method description NULL,//generic signature null//Exceptio
NS);
Mv.visitcode (); MV.VISITINSN (FCONST_0);
MV.VISITVARINSN (Fstore, 1);
MV.VISITVARINSN (iload, 1);
MV.VISITVARINSN (istore, 1);
MV.VISITINSN (return);
MV.VISITMAXS (1, 2); Mv.visitend (); End Method Cw.visitend ();
End Class byte[] Clz = Cw.tobytearray ();
FileOutputStream out = new FileOutputStream ("Testverification.class");
Out.write (CLZ);
Out.close (); }
}