Example usage for org.objectweb.asm Opcodes IFNONNULL

List of usage examples for org.objectweb.asm Opcodes IFNONNULL

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes IFNONNULL.

Prototype

int IFNONNULL

To view the source code for org.objectweb.asm Opcodes IFNONNULL.

Click Source Link

Usage

From source file:com.google.test.metric.asm.MethodVisitorBuilder.java

License:Apache License

public void visitJumpInsn(final int opcode, final Label label) {
    if (opcode == Opcodes.GOTO) {
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Transform(lineNumber, "GOTO", null, null, null));
                block.unconditionalGoto(label);
            }// w  w w  .  j  ava 2  s  .c o m
        });
    } else if (opcode == Opcodes.JSR) {
        recorder.add(new Runnable() {
            public void run() {
                block.jumpSubroutine(label, lineNumber);
            }
        });
    } else {
        recorder.add(new Runnable() {
            public void run() {
                cyclomaticComplexity.add(lineNumber);
                switch (opcode) {
                case Opcodes.IFEQ:
                    if1("IFEQ");
                    break;
                case Opcodes.IFNE:
                    if1("IFNE");
                    break;
                case Opcodes.IFLT:
                    if1("IFLT");
                    break;
                case Opcodes.IFGE:
                    if1("IFGE");
                    break;
                case Opcodes.IFGT:
                    if1("IFGT");
                    break;
                case Opcodes.IFLE:
                    if1("IFLE");
                    break;
                case Opcodes.IFNONNULL:
                    if1("IFNONNULL");
                    break;
                case Opcodes.IFNULL:
                    if1("IFNULL");
                    break;
                case Opcodes.IF_ACMPEQ:
                    if2("IF_ACMPEQ");
                    break;
                case Opcodes.IF_ACMPNE:
                    if2("IF_ACMPNE");
                    break;
                case Opcodes.IF_ICMPEQ:
                    if2("IF_ICMPEQ");
                    break;
                case Opcodes.IF_ICMPGE:
                    if2("IF_ICMPGE");
                    break;
                case Opcodes.IF_ICMPGT:
                    if2("IF_ICMPGT");
                    break;
                case Opcodes.IF_ICMPLE:
                    if2("IF_ICMPLE");
                    break;
                case Opcodes.IF_ICMPLT:
                    if2("IF_ICMPLT");
                    break;
                case Opcodes.IF_ICMPNE:
                    if2("IF_ICMPNE");
                    break;
                default:
                    throw new UnsupportedOperationException("" + opcode);
                }
                block.conditionalGoto(label);
            }

            private void if1(String name) {
                block.addOp(new Transform(lineNumber, name, JavaType.INT, null, null));
            }

            private void if2(String name) {
                block.addOp(new Transform(lineNumber, name, JavaType.INT, JavaType.INT, null));
            }
        });
    }
}

From source file:com.googlecode.ddom.weaver.ext.ModelExtensionFactoryImplementation.java

License:Apache License

public void accept(ClassVisitor classVisitor) {
    // Note: the name chosen here must match what is expected in ExtensionFactoryLocator
    String name = Util.classNameToInternalName(implementationInfo.getFactoryInterface().getName() + "$$Impl");
    String factoryInterfaceName = Util
            .classNameToInternalName(implementationInfo.getFactoryInterface().getName());
    classVisitor.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, name, null, "java/lang/Object",
            new String[] { factoryInterfaceName });
    {//from   w  w  w. ja v  a 2 s . c  o m
        FieldVisitor fw = classVisitor.visitField(Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_STATIC,
                "INSTANCE", "L" + factoryInterfaceName + ";", null, null);
        if (fw != null) {
            fw.visitEnd();
        }
    }
    {
        FieldVisitor fw = classVisitor.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "delegates",
                "Ljava/util/Map;", null, null);
        if (fw != null) {
            fw.visitEnd();
        }
    }
    {
        MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_PRIVATE, "<init>", "()V", null, null);
        if (mv != null) {
            mv.visitCode();
            Label l0 = new Label();
            mv.visitLabel(l0);
            // Call constructor from superclass
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
            // Create delegates map
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitTypeInsn(Opcodes.NEW, "java/util/HashMap");
            mv.visitInsn(Opcodes.DUP);
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/HashMap", "<init>", "()V");
            mv.visitFieldInsn(Opcodes.PUTFIELD, name, "delegates", "Ljava/util/Map;");
            // Populate delegates map
            for (ModelExtensionInfo modelExtensionInfo : implementationInfo.getModelExtensions()) {
                for (ModelExtensionInterfaceInfo extensionInterface : modelExtensionInfo
                        .getExtensionInterfaces()) {
                    if (!extensionInterface.isAbstract()) {
                        // TODO: this is stupid; we should not recreate the info object here
                        ModelExtensionClassInfo modelExtensionClassInfo = new ModelExtensionClassInfo(
                                implementationInfo.getImplementation(), modelExtensionInfo.getRootInterface(),
                                extensionInterface);
                        String factoryDelegateImplName = Util.classNameToInternalName(
                                modelExtensionClassInfo.getFactoryDelegateImplementationClassName());
                        mv.visitVarInsn(Opcodes.ALOAD, 0);
                        mv.visitFieldInsn(Opcodes.GETFIELD, name, "delegates", "Ljava/util/Map;");
                        mv.visitLdcInsn(Type.getObjectType(Util.classNameToInternalName(
                                modelExtensionClassInfo.getExtensionInterface().getClassInfo().getName())));
                        mv.visitTypeInsn(Opcodes.NEW, factoryDelegateImplName);
                        mv.visitInsn(Opcodes.DUP);
                        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, factoryDelegateImplName, "<init>", "()V");
                        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put",
                                "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
                        mv.visitInsn(Opcodes.POP);
                    }
                }
            }
            mv.visitInsn(Opcodes.RETURN);
            Label l1 = new Label();
            mv.visitLabel(l1);
            mv.visitLocalVariable("this", "L" + name + ";", null, l0, l1, 0);
            mv.visitMaxs(4, 1);
            mv.visitEnd();
        }
    }
    String factoryDelegateInterfaceName = Util
            .classNameToInternalName(implementationInfo.getFactoryDelegateInterfaceName());
    String getDelegateDesc = "(Ljava/lang/Class;)L" + factoryDelegateInterfaceName + ";";
    {
        MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_PRIVATE, "getDelegate", getDelegateDesc, null,
                null);
        if (mv != null) {
            mv.visitCode();
            Label l0 = new Label();
            mv.visitLabel(l0);
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitFieldInsn(Opcodes.GETFIELD, name, "delegates", "Ljava/util/Map;");
            mv.visitVarInsn(Opcodes.ALOAD, 1);
            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "get",
                    "(Ljava/lang/Object;)Ljava/lang/Object;");
            mv.visitTypeInsn(Opcodes.CHECKCAST, factoryDelegateInterfaceName);
            mv.visitInsn(Opcodes.ARETURN);
            Label l1 = new Label();
            mv.visitLabel(l1);
            mv.visitLocalVariable("this", "L" + name + ";", null, l0, l1, 0);
            mv.visitLocalVariable("extensionInterface", "Ljava/lang/Class;", null, l0, l1, 1);
            mv.visitMaxs(2, 2);
            mv.visitEnd();
        }
    }
    String implementationName = Util.classNameToInternalName(implementationInfo.getImplementation().getName());
    for (ConstructorInfo constructor : implementationInfo.getConstructors()) {
        Type[] constructorArgumentTypes = constructor.getArgumentTypes();
        Type[] argumentTypes = new Type[constructorArgumentTypes.length + 1];
        argumentTypes[0] = Type.getObjectType("java/lang/Class");
        System.arraycopy(constructorArgumentTypes, 0, argumentTypes, 1, constructorArgumentTypes.length);
        MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_PUBLIC, "create",
                Type.getMethodDescriptor(Type.getObjectType(implementationName), argumentTypes), null, null);
        if (mv != null) {
            mv.visitCode();
            Label l0 = new Label();
            mv.visitLabel(l0);
            mv.visitVarInsn(Opcodes.ALOAD, 1);
            Label l1 = new Label();
            mv.visitJumpInsn(Opcodes.IFNONNULL, l1);
            mv.visitTypeInsn(Opcodes.NEW, implementationName);
            mv.visitInsn(Opcodes.DUP);
            for (int i = 0; i < constructorArgumentTypes.length; i++) {
                mv.visitVarInsn(constructorArgumentTypes[i].getOpcode(Opcodes.ILOAD), i + 2);
            }
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, implementationName, "<init>",
                    constructor.getDescriptor());
            mv.visitInsn(Opcodes.ARETURN);
            mv.visitLabel(l1);
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitVarInsn(Opcodes.ALOAD, 1);
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, name, "getDelegate", getDelegateDesc);
            for (int i = 0; i < constructorArgumentTypes.length; i++) {
                mv.visitVarInsn(constructorArgumentTypes[i].getOpcode(Opcodes.ILOAD), i + 2);
            }
            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, factoryDelegateInterfaceName, "create",
                    constructor.getFactoryDelegateMethodDescriptor());
            mv.visitInsn(Opcodes.ARETURN);
            Label l3 = new Label();
            mv.visitLabel(l3);
            mv.visitLocalVariable("this", "L" + name + ";", null, l0, l3, 0);
            mv.visitLocalVariable("extensionInterface", "Ljava/lang/Class;", null, l0, l3, 1);
            for (int i = 0; i < constructorArgumentTypes.length; i++) {
                mv.visitLocalVariable("arg" + i, constructorArgumentTypes[i].getDescriptor(), null, l0, l3,
                        i + 2);
            }
            mv.visitMaxs(argumentTypes.length + 1, argumentTypes.length + 1);
            mv.visitEnd();
        }
    }
    {
        MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
        if (mv != null) {
            mv.visitCode();
            mv.visitTypeInsn(Opcodes.NEW, name);
            mv.visitInsn(Opcodes.DUP);
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, name, "<init>", "()V");
            mv.visitFieldInsn(Opcodes.PUTSTATIC, name, "INSTANCE", "L" + factoryInterfaceName + ";");
            mv.visitInsn(Opcodes.RETURN);
            mv.visitMaxs(2, 0);
            mv.visitEnd();
        }
    }
    classVisitor.visitEnd();
}

From source file:com.intellij.compiler.notNullVerification.ThrowOnNullMethodVisitor.java

License:Apache License

/**
 * Visits a zero operand instruction (ie return).
 * <p>/*from  www . j ava  2 s  .c om*/
 * {@inheritDoc}
 */
public void visitInsn(final int opcode) {
    if (shouldInclude() && opcode == Opcodes.ARETURN) {
        if (isReturnNotNull) {
            mv.visitInsn(Opcodes.DUP);
            final Label skipLabel = new Label();
            mv.visitJumpInsn(Opcodes.IFNONNULL, skipLabel);
            generateThrow(ISE_CLASS_NAME,
                    "NotNull method " + classInfo.getName() + "." + methodName + " must not return null",
                    skipLabel);
        }
    }
    mv.visitInsn(opcode);
}

From source file:com.intellij.compiler.notNullVerification.ThrowOnNullMethodVisitor.java

License:Apache License

/**
 * Starts the visit of the method's code, if any (ie non abstract method).
 *///from w  w w .ja v a 2  s .  com
@Override
public void visitCode() {
    if (shouldInclude()) {
        if (!notNullParams.isEmpty()) {
            startGeneratedCodeLabel = new Label();
            mv.visitLabel(startGeneratedCodeLabel);
        }
        for (final Integer notNullParam : notNullParams) {
            int var = ((methodAccess & Opcodes.ACC_STATIC) == 0) ? 1 : 0;
            for (int i = 0; i < notNullParam + syntheticCount; ++i) {
                var += argumentTypes[i].getSize();
            }
            mv.visitVarInsn(Opcodes.ALOAD, var);

            final Label end = new Label();
            mv.visitJumpInsn(Opcodes.IFNONNULL, end);

            generateThrow(IAE_CLASS_NAME, getThrowMessage(notNullParam), end);
        }
    }
    mv.visitCode();
}

From source file:com.mebigfatguy.junitflood.jvm.OperandStack.java

License:Apache License

public void performJumpInsn(int opcode, Label label) {
    switch (opcode) {
    case Opcodes.IFEQ:
    case Opcodes.IFNE:
    case Opcodes.IFLT:
    case Opcodes.IFGE:
    case Opcodes.IFGT:
    case Opcodes.IFLE:
    case Opcodes.IFNULL:
    case Opcodes.IFNONNULL:
        pop();//from www  .  j a  v a2  s  .c  om
        break;

    case Opcodes.IF_ICMPEQ:
    case Opcodes.IF_ICMPNE:
    case Opcodes.IF_ICMPLT:
    case Opcodes.IF_ICMPGE:
    case Opcodes.IF_ICMPGT:
    case Opcodes.IF_ICMPLE:
    case Opcodes.IF_ACMPEQ:
    case Opcodes.IF_ACMPNE:
        pop2();
        break;

    case Opcodes.GOTO:
        //nop
        break;

    case Opcodes.JSR:
        //nop -- a fudge
        break;
    }
}

From source file:com.nginious.http.serialize.JsonSerializerCreator.java

License:Apache License

/**
 * Creates bytecode which implements the {@link JsonSerializer#serializeProperties(org.json.JSONObject, Object)}
 * method for the serializer class being created.
 * //from ww  w .j  a  v  a2  s.c  o  m
 * @param writer class byte code writer
 * @param intBeanClazzName binary name of serializer class being generated
 * @return a method visitor for writing bytecode inside the generated method
 */
private MethodVisitor createSerializeMethod(ClassWriter writer, String intBeanClazzName) {
    String[] exceptions = { "com/nginious/serialize/SerializerException" };
    MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PUBLIC, "serializeProperties",
            "(Lorg/json/JSONObject;Ljava/lang/Object;)V", null, exceptions);
    visitor.visitCode();

    Label label = new Label();
    visitor.visitVarInsn(Opcodes.ALOAD, 2);
    visitor.visitJumpInsn(Opcodes.IFNONNULL, label);
    visitor.visitInsn(Opcodes.RETURN);
    visitor.visitLabel(label);
    visitor.visitVarInsn(Opcodes.ALOAD, 2);
    visitor.visitTypeInsn(Opcodes.CHECKCAST, intBeanClazzName);
    visitor.visitIntInsn(Opcodes.ASTORE, 3);

    return visitor;
}

From source file:com.nginious.http.serialize.XmlSerializerCreator.java

License:Apache License

/**
 * Creates bytecode which implements the {@link XmlSerializer#serializeProperties(javax.xml.transform.sax.TransformerHandler, Object)}
 * method for the serializer class being created.
 * /*from   ww  w.  j  av a  2 s.c  o  m*/
 * @param writer class byte code writer
 * @param intBeanClazzName binary name of serializer class being generated
 * @return a method visitor for writing bytecode inside the generated method
 */
private MethodVisitor createSerializeMethod(ClassWriter writer, String intBeanClazzName) {
    String[] exceptions = { "com/nginious/serialize/SerializerException" };
    MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PUBLIC, "serializeProperties",
            "(Ljavax/xml/transform/sax/TransformerHandler;Ljava/lang/Object;)V", null, exceptions);
    visitor.visitCode();

    Label label = new Label();
    visitor.visitVarInsn(Opcodes.ALOAD, 2);
    visitor.visitJumpInsn(Opcodes.IFNONNULL, label);
    visitor.visitInsn(Opcodes.RETURN);
    visitor.visitLabel(label);
    visitor.visitVarInsn(Opcodes.ALOAD, 0);
    visitor.visitVarInsn(Opcodes.ALOAD, 2);
    visitor.visitTypeInsn(Opcodes.CHECKCAST, intBeanClazzName);
    visitor.visitIntInsn(Opcodes.ASTORE, 3);
    return visitor;
}

From source file:com.retroduction.carma.transformer.asm.ror.IFNONNULL_2_IFNULL_Transition.java

License:Open Source License

public IFNONNULL_2_IFNULL_Transition() {
    super();
    this.sourceInstruction = Opcodes.IFNONNULL;
    this.targetInstruction = Opcodes.IFNULL;
}

From source file:com.retroduction.carma.transformer.asm.ror.IFNULL_2_IFNONNULL_Transition.java

License:Open Source License

public IFNULL_2_IFNONNULL_Transition() {
    super();
    this.sourceInstruction = Opcodes.IFNULL;
    this.targetInstruction = Opcodes.IFNONNULL;
}

From source file:com.tencent.tinker.build.auxiliaryclass.AuxiliaryClassInjectAdapter.java

License:Open Source License

@Override
public void visitEnd() {
    // If method <clinit> and <init> are not found, we should generate a <clinit>.
    if (!this.isClInitExists && !this.isInitExists) {
        MethodVisitor mv = super.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
        mv.visitCode();//from w  w w . j  a v a  2s.  c  o m
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "lineSeparator", "()Ljava/lang/String;",
                false);
        Label lblSkipInvalidInsn = new Label();
        mv.visitJumpInsn(Opcodes.IFNONNULL, lblSkipInvalidInsn);
        mv.visitLdcInsn(Type.getType(this.auxiliaryClassDesc));
        mv.visitVarInsn(Opcodes.ASTORE, 0);
        mv.visitLabel(lblSkipInvalidInsn);
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    super.visitEnd();
}