* @author Eric Bruneton
*/
public class NullDereferenceAnalyzerTest extends AbstractTestCase {
public void test() throws Exception {
MethodNode mn = new MethodNode(ACC_PUBLIC, "m", "(ZZ)V", null, null);
mn.visitCode();
Label l0 = new Label();
Label l1 = new Label();
Label l2 = new Label();
Label l3 = new Label();
Label l4 = new Label();
Label l5 = new Label();
Label l6 = new Label();
mn.visitTryCatchBlock(l0, l1, l2, null);
mn.visitTryCatchBlock(l2, l3, l2, null);
mn.visitInsn(ACONST_NULL); // insn 0
mn.visitVarInsn(ASTORE, 3);
mn.visitInsn(ACONST_NULL); // insn 2
mn.visitVarInsn(ASTORE, 4);
mn.visitTypeInsn(NEW, "C");
mn.visitInsn(DUP);
mn.visitMethodInsn(INVOKESPECIAL, "C", "<init>", "()V");
mn.visitVarInsn(ASTORE, 5);
mn.visitVarInsn(ILOAD, 1);
mn.visitJumpInsn(IFEQ, l4);
mn.visitTypeInsn(NEW, "java/lang/Integer"); // insn
// 10
mn.visitInsn(DUP);
mn.visitInsn(ICONST_1);
mn.visitMethodInsn(INVOKESPECIAL, "java/lang/Integer", "<init>",
"(I)V");
mn.visitVarInsn(ASTORE, 3);
mn.visitTypeInsn(NEW, "C"); // insn 15
mn.visitInsn(DUP);
mn.visitMethodInsn(INVOKESPECIAL, "C", "<init>", "()V");
mn.visitVarInsn(ASTORE, 4);
mn.visitLabel(l4);
mn.visitVarInsn(ILOAD, 2);
mn.visitJumpInsn(IFEQ, l5);
mn.visitTypeInsn(NEW, "java/lang/Integer"); // insn
// 22
mn.visitInsn(DUP);
mn.visitInsn(ICONST_1);
mn.visitMethodInsn(INVOKESPECIAL, "java/lang/Integer", "<init>",
"(I)V");
mn.visitVarInsn(ASTORE, 3);
mn.visitTypeInsn(NEW, "C"); // insn 27
mn.visitInsn(DUP);
mn.visitMethodInsn(INVOKESPECIAL, "C", "<init>", "()V");
mn.visitVarInsn(ASTORE, 4);
mn.visitLabel(l5);
mn.visitVarInsn(ALOAD, 4);
mn.visitInsn(DUP);
mn.visitVarInsn(ASTORE, 6);
mn.visitInsn(MONITORENTER); // insn 35
mn.visitLabel(l0);
mn.visitVarInsn(ALOAD, 4);
mn.visitVarInsn(ALOAD, 4);
mn.visitFieldInsn(GETFIELD, "C", "i", "I"); // insn
// 39
mn.visitVarInsn(ALOAD, 5);
mn.visitFieldInsn(GETFIELD, "C", "i", "I");
mn.visitInsn(IADD);
mn.visitVarInsn(ALOAD, 3);
mn.visitMethodInsn(INVOKEVIRTUAL, // insn 44
"java/lang/Integer", "intValue", "()I");
mn.visitInsn(IADD);
mn.visitFieldInsn(PUTFIELD, "C", "i", "I"); // insn
// 46
mn.visitVarInsn(ALOAD, 6);
mn.visitInsn(MONITOREXIT); // insn 48
mn.visitLabel(l1);
mn.visitJumpInsn(GOTO, l6);
mn.visitLabel(l2);
mn.visitVarInsn(ALOAD, 6);
mn.visitInsn(MONITOREXIT); // insn 53
mn.visitLabel(l3);
mn.visitInsn(ATHROW);
mn.visitLabel(l6);
mn.visitInsn(RETURN);
mn.visitMaxs(3, 7);
mn.visitEnd();
List<AbstractInsnNode> occurences;
occurences = new NullDereferenceAnalyzer().findNullDereferences(
"D", mn);
assertEquals(6, occurences.size());