Example usage for org.objectweb.asm MethodVisitor visitJumpInsn

List of usage examples for org.objectweb.asm MethodVisitor visitJumpInsn

Introduction

In this page you can find the example usage for org.objectweb.asm MethodVisitor visitJumpInsn.

Prototype

public void visitJumpInsn(final int opcode, final Label label) 

Source Link

Document

Visits a jump instruction.

Usage

From source file:org.codehaus.groovy.classgen.Verifier.java

License:Apache License

private void addStaticMetaClassField(final ClassNode node, final String classInternalName) {
    String _staticClassInfoFieldName = "$staticClassInfo";
    while (node.getDeclaredField(_staticClassInfoFieldName) != null)
        _staticClassInfoFieldName = _staticClassInfoFieldName + "$";
    final String staticMetaClassFieldName = _staticClassInfoFieldName;

    FieldNode staticMetaClassField = node.addField(staticMetaClassFieldName,
            ACC_PRIVATE | ACC_STATIC | ACC_SYNTHETIC, ClassHelper.make(ClassInfo.class, false), null);
    staticMetaClassField.setSynthetic(true);

    node.addSyntheticMethod("$getStaticMetaClass", ACC_PROTECTED, ClassHelper.make(MetaClass.class),
            Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, new BytecodeSequence(new BytecodeInstruction() {
                @Override/*from   www.ja  v a  2  s.  c  o  m*/
                public void visit(MethodVisitor mv) {
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;",
                            false);
                    if (BytecodeHelper.isClassLiteralPossible(node)
                            || BytecodeHelper.isSameCompilationUnit(classNode, node)) {
                        BytecodeHelper.visitClassLiteral(mv, node);
                    } else {
                        mv.visitMethodInsn(INVOKESTATIC, classInternalName,
                                "$get$$class$" + classInternalName.replaceAll("/", "\\$"),
                                "()Ljava/lang/Class;", false);
                    }
                    Label l1 = new Label();
                    mv.visitJumpInsn(IF_ACMPEQ, l1);

                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitMethodInsn(INVOKESTATIC, "org/codehaus/groovy/runtime/ScriptBytecodeAdapter",
                            "initMetaClass", "(Ljava/lang/Object;)Lgroovy/lang/MetaClass;", false);
                    mv.visitInsn(ARETURN);

                    mv.visitLabel(l1);

                    mv.visitFieldInsn(GETSTATIC, classInternalName, staticMetaClassFieldName,
                            "Lorg/codehaus/groovy/reflection/ClassInfo;");
                    mv.visitVarInsn(ASTORE, 1);
                    mv.visitVarInsn(ALOAD, 1);
                    Label l0 = new Label();
                    mv.visitJumpInsn(IFNONNULL, l0);

                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;",
                            false);
                    mv.visitMethodInsn(INVOKESTATIC, "org/codehaus/groovy/reflection/ClassInfo", "getClassInfo",
                            "(Ljava/lang/Class;)Lorg/codehaus/groovy/reflection/ClassInfo;", false);
                    mv.visitInsn(DUP);
                    mv.visitVarInsn(ASTORE, 1);
                    mv.visitFieldInsn(PUTSTATIC, classInternalName, staticMetaClassFieldName,
                            "Lorg/codehaus/groovy/reflection/ClassInfo;");

                    mv.visitLabel(l0);

                    mv.visitVarInsn(ALOAD, 1);
                    mv.visitMethodInsn(INVOKEVIRTUAL, "org/codehaus/groovy/reflection/ClassInfo",
                            "getMetaClass", "()Lgroovy/lang/MetaClass;", false);
                    mv.visitInsn(ARETURN);
                }
            }));
}

From source file:org.codehaus.groovy.classgen.Verifier.java

License:Apache License

protected void addGroovyObjectInterfaceAndMethods(ClassNode node, final String classInternalName) {
    if (!node.isDerivedFromGroovyObject())
        node.addInterface(ClassHelper.make(GroovyObject.class));
    FieldNode metaClassField = getMetaClassField(node);

    boolean shouldAnnotate = classNode.getModule().getContext() != null;
    AnnotationNode generatedAnnotation = shouldAnnotate
            ? new AnnotationNode(ClassHelper.make(GENERATED_ANNOTATION))
            : null;//from w  w w  . j  a  va  2  s  . co m
    AnnotationNode internalAnnotation = shouldAnnotate
            ? new AnnotationNode(ClassHelper.make(INTERNAL_ANNOTATION))
            : null;

    if (!node.hasMethod("getMetaClass", Parameter.EMPTY_ARRAY)) {
        metaClassField = setMetaClassFieldIfNotExists(node, metaClassField);
        MethodNode methodNode = addMethod(node, !shouldAnnotate, "getMetaClass", ACC_PUBLIC,
                ClassHelper.METACLASS_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY,
                new BytecodeSequence(new BytecodeInstruction() {
                    @Override
                    public void visit(MethodVisitor mv) {
                        Label nullLabel = new Label();
                        /*
                         *  the code is:
                         *  if (this.metaClass==null) {
                         *      this.metaClass = this.$getStaticMetaClass()
                         *      return this.metaClass
                         *  } else {
                         *      return this.metaClass
                         *  }
                         *  with the optimization that the result of the
                         *  first this.metaClass is duped on the operand
                         *  stack and reused for the return in the else part
                         */
                        mv.visitVarInsn(ALOAD, 0);
                        mv.visitFieldInsn(GETFIELD, classInternalName, "metaClass", "Lgroovy/lang/MetaClass;");
                        mv.visitInsn(DUP);
                        mv.visitJumpInsn(IFNULL, nullLabel);
                        mv.visitInsn(ARETURN);

                        mv.visitLabel(nullLabel);
                        mv.visitInsn(POP);
                        mv.visitVarInsn(ALOAD, 0);
                        mv.visitInsn(DUP);
                        mv.visitMethodInsn(INVOKEVIRTUAL, classInternalName, "$getStaticMetaClass",
                                "()Lgroovy/lang/MetaClass;", false);
                        mv.visitFieldInsn(PUTFIELD, classInternalName, "metaClass", "Lgroovy/lang/MetaClass;");
                        mv.visitVarInsn(ALOAD, 0);
                        mv.visitFieldInsn(GETFIELD, classInternalName, "metaClass", "Lgroovy/lang/MetaClass;");
                        mv.visitInsn(ARETURN);
                    }
                }));
        if (shouldAnnotate) {
            methodNode.addAnnotation(generatedAnnotation);
            methodNode.addAnnotation(internalAnnotation);
        }
    }

    Parameter[] parameters = new Parameter[] { new Parameter(ClassHelper.METACLASS_TYPE, "mc") };
    if (!node.hasMethod("setMetaClass", parameters)) {
        metaClassField = setMetaClassFieldIfNotExists(node, metaClassField);
        Statement setMetaClassCode;
        if (isFinal(metaClassField.getModifiers())) {
            ConstantExpression text = new ConstantExpression("cannot set read-only meta class");
            ConstructorCallExpression cce = new ConstructorCallExpression(
                    ClassHelper.make(IllegalArgumentException.class), text);
            setMetaClassCode = new ExpressionStatement(cce);
        } else {
            List list = new ArrayList();
            list.add(new BytecodeInstruction() {
                @Override
                public void visit(MethodVisitor mv) {
                    /*
                     * the code is (meta class is stored in 1):
                     * this.metaClass = <1>
                     */
                    mv.visitVarInsn(ALOAD, 0);
                    mv.visitVarInsn(ALOAD, 1);
                    mv.visitFieldInsn(PUTFIELD, classInternalName, "metaClass", "Lgroovy/lang/MetaClass;");
                    mv.visitInsn(RETURN);
                }
            });
            setMetaClassCode = new BytecodeSequence(list);
        }

        MethodNode methodNode = addMethod(node, !shouldAnnotate, "setMetaClass", ACC_PUBLIC,
                ClassHelper.VOID_TYPE, SET_METACLASS_PARAMS, ClassNode.EMPTY_ARRAY, setMetaClassCode);
        if (shouldAnnotate) {
            methodNode.addAnnotation(generatedAnnotation);
            methodNode.addAnnotation(internalAnnotation);
        }
    }
}

From source file:org.codehaus.groovy.runtime.callsite.CallSiteGenerator.java

License:Apache License

private static MethodVisitor writeMethod(ClassWriter cw, String name, int argumentCount,
        final String superClass, CachedMethod cachedMethod, String receiverType, String parameterDescription,
        boolean useArray) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "call" + name,
            "(L" + receiverType + ";" + parameterDescription + ")Ljava/lang/Object;", null, null);
    mv.visitCode();//from w  w  w  .  j a v  a  2s  . c om

    final Label tryStart = new Label();
    mv.visitLabel(tryStart);

    // call for checking if method is still valid
    for (int i = 0; i < argumentCount; ++i)
        mv.visitVarInsn(Opcodes.ALOAD, i);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, superClass, "checkCall",
            "(Ljava/lang/Object;" + parameterDescription + ")Z", false);
    Label l0 = new Label();
    mv.visitJumpInsn(Opcodes.IFEQ, l0);

    // valid method branch

    Class callClass = cachedMethod.getDeclaringClass().getTheClass();
    boolean useInterface = callClass.isInterface();

    String type = BytecodeHelper.getClassInternalName(callClass.getName());
    String descriptor = BytecodeHelper.getMethodDescriptor(cachedMethod.getReturnType(),
            cachedMethod.getNativeParameterTypes());

    // prepare call
    int invokeMethodCode = Opcodes.INVOKEVIRTUAL;
    if (cachedMethod.isStatic()) {
        invokeMethodCode = Opcodes.INVOKESTATIC;
    } else {
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        BytecodeHelper.doCast(mv, callClass);
        if (useInterface)
            invokeMethodCode = Opcodes.INVOKEINTERFACE;
    }

    Class<?>[] parameters = cachedMethod.getPT();
    int size = parameters.length;
    for (int i = 0; i < size; i++) {
        if (useArray) {
            // unpack argument from Object[]
            mv.visitVarInsn(Opcodes.ALOAD, 2);
            BytecodeHelper.pushConstant(mv, i);
            mv.visitInsn(Opcodes.AALOAD);
        } else {
            mv.visitVarInsn(Opcodes.ALOAD, i + 2);
        }

        // cast argument to parameter class, inclusive unboxing
        // for methods with primitive types
        BytecodeHelper.doCast(mv, parameters[i]);
    }

    // make call
    mv.visitMethodInsn(invokeMethodCode, type, cachedMethod.getName(), descriptor, useInterface);

    // produce result
    BytecodeHelper.box(mv, cachedMethod.getReturnType());
    if (cachedMethod.getReturnType() == void.class) {
        mv.visitInsn(Opcodes.ACONST_NULL);
    }

    // return
    mv.visitInsn(Opcodes.ARETURN);

    // fall back after method change
    mv.visitLabel(l0);
    for (int i = 0; i < argumentCount; ++i)
        mv.visitVarInsn(Opcodes.ALOAD, i);
    if (!useArray) {
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "org/codehaus/groovy/runtime/ArrayUtil", "createArray",
                "(" + parameterDescription + ")[Ljava/lang/Object;", false);
    }
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "org/codehaus/groovy/runtime/callsite/CallSiteArray",
            "defaultCall" + name, "(Lorg/codehaus/groovy/runtime/callsite/CallSite;L" + receiverType
                    + ";[Ljava/lang/Object;)Ljava/lang/Object;",
            false);
    mv.visitInsn(Opcodes.ARETURN);

    // exception unwrapping for stackless exceptions
    final Label tryEnd = new Label();
    mv.visitLabel(tryEnd);
    final Label catchStart = new Label();
    mv.visitLabel(catchStart);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "org/codehaus/groovy/runtime/ScriptBytecodeAdapter", "unwrap",
            "(Lgroovy/lang/GroovyRuntimeException;)Ljava/lang/Throwable;", false);
    mv.visitInsn(Opcodes.ATHROW);
    mv.visitTryCatchBlock(tryStart, tryEnd, catchStart, "groovy/lang/GroovyRuntimeException");

    mv.visitMaxs(0, 0);
    mv.visitEnd();
    return mv;
}

From source file:org.codehaus.groovy.runtime.ProxyGeneratorAdapter.java

License:Apache License

/**
 * When an object doesn't implement the GroovyObject interface, we generate bytecode for the
 * {@link GroovyObject} interface methods. Otherwise, the superclass is expected to implement them.
 *//*from w w w.  j a v  a 2  s.  c o m*/
private void createGroovyObjectSupport() {
    visitField(ACC_PRIVATE + ACC_TRANSIENT, "metaClass", "Lgroovy/lang/MetaClass;", null, null);

    // getMetaClass
    MethodVisitor mv;
    {
        mv = super.visitMethod(ACC_PUBLIC, "getMetaClass", "()Lgroovy/lang/MetaClass;", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, proxyName, "metaClass", "Lgroovy/lang/MetaClass;");
        Label l1 = new Label();
        mv.visitJumpInsn(IFNONNULL, l1);
        Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
        mv.visitMethodInsn(INVOKESTATIC, "org/codehaus/groovy/runtime/InvokerHelper", "getMetaClass",
                "(Ljava/lang/Class;)Lgroovy/lang/MetaClass;", false);
        mv.visitFieldInsn(PUTFIELD, proxyName, "metaClass", "Lgroovy/lang/MetaClass;");
        mv.visitLabel(l1);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, proxyName, "metaClass", "Lgroovy/lang/MetaClass;");
        mv.visitInsn(ARETURN);
        mv.visitMaxs(2, 1);
        mv.visitEnd();
    }

    // setMetaClass
    {
        mv = super.visitMethod(ACC_PUBLIC, "setMetaClass", "(Lgroovy/lang/MetaClass;)V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitFieldInsn(PUTFIELD, proxyName, "metaClass", "Lgroovy/lang/MetaClass;");
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitInsn(RETURN);
        Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
}

From source file:org.codehaus.groovy.runtime.ProxyGeneratorAdapter.java

License:Apache License

protected MethodVisitor makeDelegateToClosureCall(final String name, final String desc, final String signature,
        final String[] exceptions, final int accessFlags) {
    MethodVisitor mv = super.visitMethod(accessFlags, name, desc, signature, exceptions);
    //        TraceMethodVisitor tmv = new TraceMethodVisitor(mv);
    //        mv = tmv;
    mv.visitCode();/*from  ww w .ja va 2 s . c  o  m*/
    int stackSize = 0;
    // method body should be:
    //  this.$delegate$closure$methodName.call(new Object[] { method arguments })
    Type[] args = Type.getArgumentTypes(desc);
    int arrayStore = args.length + 1;
    BytecodeHelper.pushConstant(mv, args.length);
    mv.visitTypeInsn(ANEWARRAY, "java/lang/Object"); // stack size = 1
    stackSize = 1;
    int idx = 1;
    for (int i = 0; i < args.length; i++) {
        Type arg = args[i];
        mv.visitInsn(DUP); // stack size = 2
        BytecodeHelper.pushConstant(mv, i); // array index, stack size = 3
        // primitive types must be boxed
        boxPrimitiveType(mv, idx, arg);
        idx += registerLen(arg);
        stackSize = Math.max(4, 3 + registerLen(arg));
        mv.visitInsn(AASTORE); // store value into array
    }
    mv.visitVarInsn(ASTORE, arrayStore); // store array
    int arrayIndex = arrayStore;
    mv.visitVarInsn(ALOAD, 0); // load this
    mv.visitFieldInsn(GETFIELD, proxyName, CLOSURES_MAP_FIELD, "Ljava/util/Map;"); // load closure map
    mv.visitLdcInsn(name); // load method name
    mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", true);
    arrayStore++;
    mv.visitVarInsn(ASTORE, arrayStore);
    // if null, test if wildcard exists
    Label notNull = new Label();
    mv.visitIntInsn(ALOAD, arrayStore);
    mv.visitJumpInsn(IFNONNULL, notNull);
    mv.visitVarInsn(ALOAD, 0); // load this
    mv.visitFieldInsn(GETFIELD, proxyName, CLOSURES_MAP_FIELD, "Ljava/util/Map;"); // load closure map
    mv.visitLdcInsn("*"); // load wildcard
    mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", true);
    mv.visitVarInsn(ASTORE, arrayStore);
    mv.visitLabel(notNull);
    mv.visitVarInsn(ALOAD, arrayStore);
    mv.visitMethodInsn(INVOKESTATIC, BytecodeHelper.getClassInternalName(this.getClass()), "ensureClosure",
            "(Ljava/lang/Object;)Lgroovy/lang/Closure;", false);
    mv.visitVarInsn(ALOAD, arrayIndex); // load argument array
    stackSize++;
    mv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/Closure", "call", "([Ljava/lang/Object;)Ljava/lang/Object;",
            false); // call closure
    unwrapResult(mv, desc);
    mv.visitMaxs(stackSize, arrayStore + 1);
    mv.visitEnd();
    //        System.out.println("tmv.getText() = " + tmv.getText());
    return null;
}

From source file:org.codehaus.groovy.tools.DgmConverter.java

License:Apache License

private static void createIsValidMethodMethod(CachedMethod method, ClassWriter cw, String className) {
    MethodVisitor mv;
    if (method.getParamsCount() == 2 && method.getParameterTypes()[0].isNumber
            && method.getParameterTypes()[1].isNumber) {
        // 1 param meta method
        mv = cw.visitMethod(ACC_PUBLIC, "isValidMethod", "([Ljava/lang/Class;)Z", null, null);
        mv.visitCode();//from www .jav a2  s . c  om
        mv.visitVarInsn(ALOAD, 1);
        Label l0 = new Label();
        mv.visitJumpInsn(IFNULL, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, className, "getParameterTypes",
                "()[Lorg/codehaus/groovy/reflection/CachedClass;", false);
        mv.visitInsn(ICONST_0);
        mv.visitInsn(AALOAD);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitInsn(ICONST_0);
        mv.visitInsn(AALOAD);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/codehaus/groovy/reflection/CachedClass", "isAssignableFrom",
                "(Ljava/lang/Class;)Z", false);
        Label l1 = new Label();
        mv.visitJumpInsn(IFEQ, l1);
        mv.visitLabel(l0);
        mv.visitInsn(ICONST_1);
        Label l2 = new Label();
        mv.visitJumpInsn(GOTO, l2);
        mv.visitLabel(l1);
        mv.visitInsn(ICONST_0);
        mv.visitLabel(l2);
        mv.visitInsn(IRETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
}

From source file:org.codehaus.groovy.transform.sc.transformers.CompareIdentityExpression.java

License:Apache License

@Override
public void visit(final GroovyCodeVisitor visitor) {
    if (visitor instanceof AsmClassGenerator) {
        AsmClassGenerator acg = (AsmClassGenerator) visitor;
        WriterController controller = acg.getController();
        controller.getTypeChooser().resolveType(leftExpression, controller.getClassNode());
        controller.getTypeChooser().resolveType(rightExpression, controller.getClassNode());
        MethodVisitor mv = controller.getMethodVisitor();
        leftExpression.visit(acg);/*from  w  w w.  j  a  v  a  2 s.  co m*/
        controller.getOperandStack().box();
        rightExpression.visit(acg);
        controller.getOperandStack().box();
        Label l1 = new Label();
        mv.visitJumpInsn(IF_ACMPNE, l1);
        mv.visitInsn(ICONST_1);
        Label l2 = new Label();
        mv.visitJumpInsn(GOTO, l2);
        mv.visitLabel(l1);
        mv.visitInsn(ICONST_0);
        mv.visitLabel(l2);
        controller.getOperandStack().replace(ClassHelper.boolean_TYPE, 2);
    } else {
        super.visit(visitor);
    }
}

From source file:org.codehaus.groovy.transform.sc.transformers.CompareToNullExpression.java

License:Apache License

@Override
public void visit(final GroovyCodeVisitor visitor) {
    if (visitor instanceof AsmClassGenerator) {
        AsmClassGenerator acg = (AsmClassGenerator) visitor;
        WriterController controller = acg.getController();
        MethodVisitor mv = controller.getMethodVisitor();
        objectExpression.visit(acg);/*from w w  w .ja  v  a2  s  .  c  o m*/
        ClassNode top = controller.getOperandStack().getTopOperand();
        if (ClassHelper.isPrimitiveType(top)) {
            controller.getOperandStack().pop();
            mv.visitInsn(equalsNull ? ICONST_0 : ICONST_1);
            controller.getOperandStack().push(ClassHelper.boolean_TYPE);
            return;
        }
        Label zero = new Label();
        mv.visitJumpInsn(equalsNull ? IFNONNULL : IFNULL, zero);
        mv.visitInsn(ICONST_1);
        Label end = new Label();
        mv.visitJumpInsn(GOTO, end);
        mv.visitLabel(zero);
        mv.visitInsn(ICONST_0);
        mv.visitLabel(end);
        controller.getOperandStack().replace(ClassHelper.boolean_TYPE);
    } else {
        super.visit(visitor);
    }
}

From source file:org.eclipse.core.databinding.pojo.bindable.internal.asm.ClassBindable.java

License:Open Source License

/**
 * Add the implementation of the _bindable_getPropertyChangeSupport method
 * to the class. The result is a method that looks as follows:
 * /*from   w  w w .j  a  va2  s. c om*/
 * private PropertyChangeSupport _bindable_getPropertyChangeSupport() { if
 * (_bindable_propertyChangeSupport == null) {
 * this._bindable_propertyChangeSupport = new PropertyChangeSupport( this);
 * } return _bindable_propertyChangeSupport; }
 */
public void addGetPropertyChangeSupport() {

    // private PropertyChangeSupport _bindable_getPropertyChangeSupport()
    MethodVisitor mv = cv.visitMethod(ACC_PRIVATE, PCS_GETTER, "()" + PCS_SIGNATURE, null, null);
    mv.visitCode();

    // if (_bindable_propertyChangeSupport == null)
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, PCS_FIELD, PCS_SIGNATURE);
    Label l0 = new Label();
    mv.visitJumpInsn(IFNONNULL, l0);

    // this._bindable_propertyChangeSupport = new PropertyChangeSupport(
    // this);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitTypeInsn(NEW, PCS_SHORT_SIGNATURE);
    mv.visitInsn(DUP);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, PCS_SHORT_SIGNATURE, "<init>", "(Ljava/lang/Object;)V");
    mv.visitFieldInsn(PUTFIELD, className, PCS_FIELD, PCS_SIGNATURE);
    mv.visitLabel(l0);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);

    // return _bindable_propertyChangeSupport;
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, className, PCS_FIELD, PCS_SIGNATURE);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(4, 1);

}

From source file:org.eclipse.golo.compiler.JavaBytecodeStructGenerator.java

License:Open Source License

private void makeSetMethod(ClassWriter classWriter, Struct struct) {
    String owner = struct.getPackageAndClass().toJVMType();
    MethodVisitor visitor = classWriter.visitMethod(ACC_PUBLIC, "set",
            "(Ljava/lang/String;Ljava/lang/Object;)L" + owner + ";", null, null);
    visitor.visitCode();// w w  w  .  j  av a 2s.co  m
    insertPrivateElementCheck(struct, visitor);
    Label nextCase = new Label();
    for (Member member : struct.getMembers()) {
        visitor.visitLdcInsn(member.getName());
        visitor.visitVarInsn(ALOAD, 1);
        visitor.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
        visitor.visitJumpInsn(IFEQ, nextCase);
        visitor.visitVarInsn(ALOAD, 0);
        visitor.visitVarInsn(ALOAD, 2);
        visitor.visitMethodInsn(INVOKEVIRTUAL, owner, member.getName(), "(Ljava/lang/Object;)L" + owner + ";",
                false);
        visitor.visitInsn(ARETURN);
        visitor.visitLabel(nextCase);
        nextCase = new Label();
    }
    insertUnknowElementCode(struct, visitor);
    visitor.visitMaxs(0, 0);
    visitor.visitEnd();
}