Example usage for org.objectweb.asm Opcodes NEW

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

Introduction

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

Prototype

int NEW

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

Click Source Link

Usage

From source file:org.batoo.jpa.core.impl.instance.Enhancer.java

License:Open Source License

private static void createMethodCheck(final String enhancedClassName, final String descEnhancer,
        final ClassWriter cw) {
    final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PRIVATE, Enhancer.METHOD_ENHANCED_CHECK,
            Enhancer.makeDescription(Void.TYPE), null, null);
    mv.visitCode();//from w w w .j  a va  2  s.  co m

    final Label lCheckInternal = new Label();
    final Label lCheckInitialized = new Label();
    final Label lReturn = new Label();
    final Label lFind = new Label();
    final Label lInitialized = new Label();
    final Label lChanged = new Label();
    final Label lOut = new Label();

    // if (!this.__enhanced__$$__internal) { return }
    mv.visitLabel(lCheckInternal);
    mv.visitFrame(Opcodes.F_NEW, 1, new Object[] { enhancedClassName }, 0, new Object[] {});
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_INTERNAL,
            Enhancer.DESCRIPTOR_BOOLEAN);
    mv.visitJumpInsn(Opcodes.IFEQ, lCheckInitialized);
    mv.visitInsn(Opcodes.RETURN);

    // if (!this.__enhanced__$$__initialized) {
    mv.visitLabel(lCheckInitialized);
    mv.visitFrame(Opcodes.F_NEW, 1, new Object[] { enhancedClassName }, 0, new Object[] {});
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_INITIALIZED,
            Enhancer.DESCRIPTOR_BOOLEAN);
    mv.visitJumpInsn(Opcodes.IFNE, lChanged);

    //     if (this.__enhanced_$$__session == null)
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_SESSION,
            Enhancer.DESCRIPTOR_SESSION);
    mv.visitJumpInsn(Opcodes.IFNONNULL, lFind);

    //         throw new PersistenceException("No session to initialize the instance");
    mv.visitTypeInsn(Opcodes.NEW, Enhancer.INTERNAL_PERSISTENCE_EXCEPTION);
    mv.visitInsn(Opcodes.DUP);
    mv.visitLdcInsn("No session to initialize the instance");
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Enhancer.INTERNAL_PERSISTENCE_EXCEPTION,
            Enhancer.CONSTRUCTOR_INIT, Enhancer.makeDescription(Void.TYPE, String.class));
    mv.visitInsn(Opcodes.ATHROW);

    //       this.__enhanced_$$__session.getEntityManager().find(this.__enhanced_$$__type, this.__enhanced__$$__id);
    mv.visitLabel(lFind);
    mv.visitFrame(Opcodes.F_NEW, 1, new Object[] { enhancedClassName }, 0, new Object[] {});
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_SESSION,
            Enhancer.DESCRIPTOR_SESSION);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Enhancer.INTERNAL_SESSION, Enhancer.METHOD_GET_ENTITY_MANAGER,
            Enhancer.makeDescription(EntityManagerImpl.class));
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_TYPE,
            Enhancer.DESCRIPTOR_CLASS);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_ID,
            Enhancer.DESCRIPTOR_OBJECT);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Enhancer.INTERNAL_ENTITY_MANAGER, Enhancer.METHOD_FIND,
            Enhancer.makeDescription(Object.class, Class.class, Object.class));
    mv.visitInsn(Opcodes.POP);

    //   this.__enhanced__$$__initialized = true;
    mv.visitLabel(lInitialized);
    mv.visitFrame(Opcodes.F_NEW, 1, new Object[] { enhancedClassName }, 0, new Object[] {});
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ICONST_1);
    mv.visitFieldInsn(Opcodes.PUTFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_INITIALIZED,
            Enhancer.DESCRIPTOR_BOOLEAN);

    // if (this.__enhanced_$$__session != null)
    mv.visitLabel(lChanged);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_SESSION,
            Enhancer.DESCRIPTOR_SESSION);
    mv.visitJumpInsn(Opcodes.IFNULL, lReturn);

    //     this.__enhanced__$$__managedInstance.changed();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_MANAGED_INSTANCE,
            Enhancer.DESCRIPTOR_MANAGED_INSTANCE);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Enhancer.INTERNAL_MANAGED_INSTANCE, Enhancer.METHOD_CHANGED,
            Enhancer.makeDescription(Void.TYPE));

    // return;
    mv.visitLabel(lReturn);
    mv.visitFrame(Opcodes.F_NEW, 1, new Object[] { enhancedClassName }, 0, new Object[] {});
    mv.visitInsn(Opcodes.RETURN);

    mv.visitLabel(lOut);
    mv.visitLocalVariable(Enhancer.THIS, descEnhancer, null, lCheckInternal, lOut, 0);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.boretti.drools.integration.drools5.DroolsAbstractMethodVisitor.java

License:Open Source License

private void addCondition(boolean preCondition, String type, String resourceName, Type error) {
    droolsGoalExecutionLog.getLogs()/*from   www.j a v a  2  s.c  o  m*/
            .add(new DroolsGoalExecutionLog(droolsGoalExecutionLog.getFileName(),
                    droolsGoalExecutionLog.getAction(), "Method instrumentalization for "
                            + ((preCondition) ? "pre" : "post") + "-condition " + name + "/" + desc));
    visitor.setMethodChange(true);
    Type types[] = Type.getArgumentTypes(super.methodDesc);
    //runPreCondition
    this.visitVarInsn(Opcodes.ALOAD, 0);
    this.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getObjectType(parent).getInternalName(), "getClass",
            "()Ljava/lang/Class;");
    if (type == null)
        type = "COMPILED";
    this.visitFieldInsn(Opcodes.GETSTATIC, Type.getInternalName(DroolsServiceType.class), type,
            Type.getDescriptor(DroolsServiceType.class));
    this.visitLdcInsn(resourceName);
    this.visitLdcInsn(error);
    this.visitVarInsn(Opcodes.ALOAD, 0);
    this.visitIntInsn(Opcodes.BIPUSH, types.length);
    this.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
    int position = 1;
    for (int i = 0; i < types.length; i++) {
        this.visitInsn(Opcodes.DUP);
        this.visitIntInsn(Opcodes.BIPUSH, i);
        if (types[i].equals(Type.BOOLEAN_TYPE)) {
            this.visitVarInsn(Opcodes.ILOAD, position);
            this.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Boolean.class), "valueOf",
                    "(Z)Ljava/lang/Boolean;");
            position += 1;
        } else if (types[i].equals(Type.BYTE_TYPE)) {
            this.visitTypeInsn(Opcodes.NEW, "java/lang/Byte");
            this.visitInsn(Opcodes.DUP);
            this.visitVarInsn(Opcodes.ILOAD, position);
            this.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Byte.class), "<init>", "(B)V");
            position += 1;
        } else if (types[i].equals(Type.CHAR_TYPE)) {
            this.visitTypeInsn(Opcodes.NEW, "java/lang/Character");
            this.visitInsn(Opcodes.DUP);
            this.visitVarInsn(Opcodes.ILOAD, position);
            this.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Character.class), "<init>",
                    "(C)V");
            position += 1;
        } else if (types[i].equals(Type.DOUBLE_TYPE)) {
            this.visitTypeInsn(Opcodes.NEW, "java/lang/Double");
            this.visitInsn(Opcodes.DUP);
            this.visitVarInsn(Opcodes.DLOAD, position);
            this.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Double.class), "<init>", "(D)V");
            position += 2;
        } else if (types[i].equals(Type.FLOAT_TYPE)) {
            this.visitTypeInsn(Opcodes.NEW, "java/lang/Float");
            this.visitInsn(Opcodes.DUP);
            this.visitVarInsn(Opcodes.FLOAD, position);
            this.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Float.class), "<init>", "(F)V");
            position += 1;
        } else if (types[i].equals(Type.INT_TYPE)) {
            this.visitTypeInsn(Opcodes.NEW, "java/lang/Integer");
            this.visitInsn(Opcodes.DUP);
            this.visitVarInsn(Opcodes.ILOAD, position);
            this.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Integer.class), "<init>", "(I)V");
            position += 1;
        } else if (types[i].equals(Type.LONG_TYPE)) {
            this.visitTypeInsn(Opcodes.NEW, "java/lang/Long");
            this.visitInsn(Opcodes.DUP);
            this.visitVarInsn(Opcodes.LLOAD, position);
            this.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Long.class), "<init>", "(J)V");
            position += 2;
        } else if (types[i].equals(Type.SHORT_TYPE)) {
            this.visitTypeInsn(Opcodes.NEW, "java/lang/Short");
            this.visitInsn(Opcodes.DUP);
            this.visitVarInsn(Opcodes.ILOAD, position);
            this.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Short.class), "<init>", "(S)V");
            position += 1;
        } else {
            this.visitVarInsn(Opcodes.ALOAD, position);
            position += 1;
        }
        this.visitInsn(Opcodes.AASTORE);
    }
    if (preCondition)
        this.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(DroolsProvider.class),
                "runPreCondition",
                "(Ljava/lang/Class;Lorg/boretti/drools/integration/drools5/DroolsServiceType;Ljava/lang/String;Ljava/lang/Class;Ljava/lang/Object;[Ljava/lang/Object;)V");
    else
        this.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(DroolsProvider.class),
                "runPostCondition",
                "(Ljava/lang/Class;Lorg/boretti/drools/integration/drools5/DroolsServiceType;Ljava/lang/String;Ljava/lang/Class;Ljava/lang/Object;[Ljava/lang/Object;)V");
}

From source file:org.boretti.drools.integration.drools5.DroolsAddConstructorMethodVisitor.java

License:Open Source License

@Override
protected void onMethodEnter() {
    this.visitVarInsn(Opcodes.ALOAD, 0);
    this.visitInsn(Opcodes.DUP);
    this.visitInsn(Opcodes.DUP);
    this.visitInsn(Opcodes.ICONST_0);
    this.visitFieldInsn(Opcodes.PUTFIELD, Type.getObjectType(parent).getInternalName(), droolsName,
            Type.BOOLEAN_TYPE.getDescriptor());
    this.visitTypeInsn(Opcodes.NEW, Type.getType(DroolsProvider.class).getInternalName());
    this.visitInsn(Opcodes.DUP);
    this.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getType(DroolsProvider.class).getInternalName(), "<init>",
            "()V");
    this.visitInsn(Opcodes.SWAP);
    this.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getObjectType(parent).getInternalName(), "getClass",
            "()Ljava/lang/Class;");
    if (overrideNameJVMIndex >= 0) {
        this.visitVarInsn(Opcodes.ALOAD, overrideNameJVMIndex);
    }/* w  w w  .ja  va2 s .c o  m*/
    if (overrideTypeJVMIndex >= 0) {
        this.visitVarInsn(Opcodes.ALOAD, overrideTypeJVMIndex);
    }
    if (overrideNameJVMIndex < 0 && overrideTypeJVMIndex < 0) {
        this.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getType(DroolsProvider.class).getInternalName(),
                "getRuleBase", "(Ljava/lang/Class;)Lorg/drools/RuleBase;");
    } else if (overrideNameJVMIndex < 0 && overrideTypeJVMIndex >= 0) {
        this.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getType(DroolsProvider.class).getInternalName(),
                "getRuleBaseOverride",
                "(Ljava/lang/Class;Lorg/boretti/drools/integration/drools5/DroolsServiceType;)Lorg/drools/RuleBase;");
    } else if (overrideNameJVMIndex >= 0 && overrideTypeJVMIndex < 0) {
        this.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getType(DroolsProvider.class).getInternalName(),
                "getRuleBaseOverride", "(Ljava/lang/Class;Ljava/lang/String;)Lorg/drools/RuleBase;");
    } else {
        this.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getType(DroolsProvider.class).getInternalName(),
                "getRuleBaseOverride",
                "(Ljava/lang/Class;Ljava/lang/String;Lorg/boretti/drools/integration/drools5/DroolsServiceType;)Lorg/drools/RuleBase;");
    }
    this.visitFieldInsn(Opcodes.PUTFIELD, Type.getObjectType(parent).getInternalName(), droolsRule,
            Type.getType(RuleBase.class).getDescriptor());
}

From source file:org.compass.core.util.reflection.asm.AsmReflectionConstructorGenerator.java

License:Apache License

private static void createNewInstanceMethod(ClassWriter cw, Class clz) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "newInstance", "()Ljava/lang/Object;", null, null);
    mv.visitCode();/*from  w  ww  .  j  a v a 2s. co m*/
    mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(clz));
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(clz), "<init>", "()V");
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(2, 1);
    mv.visitEnd();
}

From source file:org.decojer.cavaj.readers.asm.ReadMethodVisitor.java

License:Open Source License

@Override
public void visitTypeInsn(final int opcode, final String type) {
    assert type != null;
    final T t = getDu().getT(type);

    switch (opcode) {
    /********//from   w ww  .  jav a 2  s. c o m
     * CAST *
     ********/
    case Opcodes.CHECKCAST:
        add(new CAST(this.ops.size(), opcode, this.line, T.REF, t));
        break;
    /**************
     * INSTANCEOF *
     **************/
    case Opcodes.INSTANCEOF:
        add(new INSTANCEOF(this.ops.size(), opcode, this.line, t));
        break;
    /*******
     * NEW *
     *******/
    case Opcodes.NEW:
        add(new NEW(this.ops.size(), opcode, this.line, t));
        break;
    /************
     * NEWARRAY *
     ************/
    case Opcodes.ANEWARRAY:
        add(new NEWARRAY(this.ops.size(), opcode, this.line, getDu().getArrayT(t), 1));
        break;
    default:
        log.warn(getM() + ": Unknown var insn opcode '" + opcode + "'!");
    }
}

From source file:org.diorite.inject.controller.Transformer.java

License:Open Source License

@SuppressWarnings("unchecked")
private MethodInsnNode findSuperNode(MethodNode init) {
    // we need to find super(...) invoke, it should be first super.<init> invoke, but it might be invoke to some new created object of super
    // type, so we need to track created objects
    Collection<String> objects = new LinkedList<>();
    ListIterator<AbstractInsnNode> iterator = init.instructions.iterator();
    while (iterator.hasNext()) {
        AbstractInsnNode next_ = iterator.next();
        if (next_.getOpcode() == Opcodes.NEW) {
            TypeInsnNode next = (TypeInsnNode) next_;
            objects.add(next.desc);//from  www  .j av a2 s .c  o  m
        } else if (next_.getOpcode() == Opcodes.INVOKESPECIAL) {
            MethodInsnNode next = (MethodInsnNode) next_;
            if (!next.name.equals(InjectionController.CONSTRUCTOR_NAME)) {
                continue;
            }
            if (!objects.remove(next.owner) && next.owner.equals(this.classNode.superName)) {
                return next;
            }
        }
    }
    throw new TransformerError("Can't find super() invoke for constructor!");
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.AddGlobalTeamActivationAdapter.java

License:Open Source License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    synchronized (AddGlobalTeamActivationAdapter.class) {
        if (!done && isMainMethod(name, desc, access)) {
            done = true;/*  w  w  w. j  a v a2s .co m*/
            final MethodVisitor methodVisitor = cv.visitMethod(access, name, desc, null, null);
            return new AdviceAdapter(this.api, methodVisitor, access, name, desc) {
                @Override
                protected void onMethodEnter() {
                    List<String> teams = getTeamsFromConfigFile();
                    for (String aTeam : teams) {
                        Label start, end, typeHandler, ctorHandler, after;

                        String aTeamSlash = aTeam.replace('.', '/');

                        // new SomeTeam():
                        methodVisitor.visitLabel(start = new Label());
                        methodVisitor.visitTypeInsn(Opcodes.NEW, aTeamSlash);
                        //       .activate(Team.ALL_THREADS):
                        methodVisitor.visitInsn(Opcodes.DUP);
                        methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, aTeamSlash, "<init>", "()V",
                                false);
                        methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, ClassNames.TEAM_SLASH, "ALL_THREADS",
                                "Ljava/lang/Thread;");
                        methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, aTeamSlash, "activate",
                                "(Ljava/lang/Thread;)V", false);

                        methodVisitor.visitLabel(end = new Label());
                        methodVisitor.visitJumpInsn(Opcodes.GOTO, after = new Label());

                        // catch (ClassNotFoundException, NoClassDefFoundError):
                        //   System.err.println(...)
                        methodVisitor.visitLabel(typeHandler = new Label());
                        methodVisitor.visitInsn(Opcodes.POP); // discard the exception
                        methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "err",
                                "Ljava/io/PrintStream;");
                        methodVisitor.visitLdcInsn("Config error: Team class '" + aTeam + "' in config file '"
                                + TEAM_CONFIG_FILE + "' can not be found!");
                        methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println",
                                "(Ljava/lang/String;)V", false);
                        methodVisitor.visitJumpInsn(Opcodes.GOTO, after);
                        methodVisitor.visitTryCatchBlock(start, end, typeHandler,
                                "java/lang/ClassNotFoundException");
                        // dup to avoid stackmap errors (ASM bug at 1.8)
                        methodVisitor.visitLabel(typeHandler = new Label());
                        methodVisitor.visitInsn(Opcodes.POP); // discard the exception
                        methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "err",
                                "Ljava/io/PrintStream;");
                        methodVisitor.visitLdcInsn("Config error: Team class '" + aTeam + "' in config file '"
                                + TEAM_CONFIG_FILE + "' can not be found!");
                        methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println",
                                "(Ljava/lang/String;)V", false);
                        methodVisitor.visitJumpInsn(Opcodes.GOTO, after);
                        //
                        methodVisitor.visitTryCatchBlock(start, end, typeHandler,
                                "java/lang/NoClassDefFoundError");

                        // catch (NoSuchMethodError):
                        //   System.err.println(...)
                        methodVisitor.visitLabel(ctorHandler = new Label());
                        methodVisitor.visitInsn(Opcodes.POP); // discard the exception
                        methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "err",
                                "Ljava/io/PrintStream;");
                        methodVisitor.visitLdcInsn(
                                "Activation failed: Team class '" + aTeam + "' has no default constuctor!");
                        methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println",
                                "(Ljava/lang/String;)V", false);
                        methodVisitor.visitTryCatchBlock(start, end, ctorHandler,
                                "java/lang/NoSuchMethodError");

                        methodVisitor.visitLabel(after);
                    }
                }

                @Override
                public void visitMaxs(int maxStack, int maxLocals) {
                    super.visitMaxs(Math.max(maxStack, 3), maxLocals);
                }
            };
        }
        return null;
    }
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateAddRemoveRoleMethod.java

License:Open Source License

void genGetInitializedRoleSet(InsnList instructions, int targetLocal) {
    // x = this._OT$roleSet 
    instructions.add(new IntInsnNode(Opcodes.ALOAD, 0));
    instructions.add(new FieldInsnNode(Opcodes.GETFIELD, name, ConstantMembers.OT_ROLE_SET,
            ConstantMembers.HASH_SET_FIELD_TYPE));

    instructions.add(new IntInsnNode(Opcodes.ASTORE, targetLocal));
    instructions.add(new IntInsnNode(Opcodes.ALOAD, targetLocal));

    // if (x == null) {
    LabelNode skipInstantiation = new LabelNode();
    instructions.add(new JumpInsnNode(Opcodes.IFNONNULL, skipInstantiation));

    // this._OT$roleSet = new HashSet();
    instructions.add(new IntInsnNode(Opcodes.ALOAD, 0));
    instructions.add(new TypeInsnNode(Opcodes.NEW, ClassNames.HASH_SET_SLASH));
    instructions.add(new InsnNode(Opcodes.DUP));
    instructions/*  w  w  w. j a v a 2 s  .co m*/
            .add(new MethodInsnNode(Opcodes.INVOKESPECIAL, ClassNames.HASH_SET_SLASH, "<init>", "()V", false));

    instructions.add(new IntInsnNode(Opcodes.ASTORE, targetLocal));
    instructions.add(new IntInsnNode(Opcodes.ALOAD, targetLocal));
    instructions.add(new FieldInsnNode(Opcodes.PUTFIELD, name, ConstantMembers.OT_ROLE_SET,
            ConstantMembers.HASH_SET_FIELD_TYPE));

    instructions.add(skipInstantiation);
    // }
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateMethodAccessAdapter.java

License:Open Source License

@Override
public boolean transform() {
    MethodNode methodNode = getMethod(method);
    InsnList instructions = new InsnList();

    if (isConstructor) {
        // create empty object for constructor invocation:
        instructions.add(new TypeInsnNode(Opcodes.NEW, name));
        instructions.add(new InsnNode(Opcodes.DUP));
    } else if (!method.isStatic()) {
        //put "this" on the stack for a non-static method
        instructions.add(new IntInsnNode(Opcodes.ALOAD, 0));
    }//from  w w  w. jav a 2 s  .co  m

    //Unbox arguments
    Type[] args = Type.getArgumentTypes(methodNode.desc);

    if (args.length > 0) {

        for (int i = 0; i < args.length; i++) {
            instructions.add(new IntInsnNode(Opcodes.ALOAD, firstArgIndex + 2));
            instructions.add(createLoadIntConstant(i));
            instructions.add(new InsnNode(Opcodes.AALOAD));
            Type arg = args[i];
            if (arg.getSort() != Type.ARRAY && arg.getSort() != Type.OBJECT) {
                String objectType = AsmTypeHelper.getObjectType(arg);
                instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, objectType));
                instructions.add(AsmTypeHelper.getUnboxingInstructionForType(arg, objectType));
            } else {
                instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, arg.getInternalName()));
            }
        }
    }

    //call original method
    int opcode = Opcodes.INVOKEVIRTUAL;
    if (method.isStatic()) {
        opcode = Opcodes.INVOKESTATIC;
    } else if (isConstructor) {
        opcode = Opcodes.INVOKESPECIAL;
    }
    instructions.add(new MethodInsnNode(opcode, name, method.getName(), method.getSignature()));

    //box return value
    Type returnType = Type.getReturnType(methodNode.desc);

    if (returnType.getSort() != Type.OBJECT && returnType.getSort() != Type.ARRAY
            && returnType.getSort() != Type.VOID) {

        instructions.add(AsmTypeHelper.getBoxingInstructionForType(returnType));
        instructions.add(new InsnNode(Opcodes.ARETURN));
    } else if (returnType.getSort() == Type.VOID && !isConstructor) {
        instructions.add(new InsnNode(Opcodes.ACONST_NULL));
        instructions.add(new InsnNode(Opcodes.ARETURN));
    } else {
        instructions.add(new InsnNode(Opcodes.ARETURN));
    }

    //add the instructions to a new label in the existing switch
    MethodNode access = getMethod(this.access);
    addNewLabelToSwitch(access.instructions, instructions, accessId);

    return true;
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateSwitchForAccessAdapter.java

License:Open Source License

@Override
protected void addInstructionForDefaultLabel(MethodNode method) {
    if (superClassName.equals("java/lang/Object")) {
        method.instructions.add(new TypeInsnNode(Opcodes.NEW, "org/objectteams/NoSuchMethodError"));
        method.instructions.add(new InsnNode(Opcodes.DUP));
        method.instructions.add(new IntInsnNode(Opcodes.ILOAD, getFirstArgIndex())); // accessId
        method.instructions.add(new LdcInsnNode(clazz.getName())); // current class
        method.instructions.add(new LdcInsnNode("decapsulating access")); // access reason
        method.instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, "org/objectteams/NoSuchMethodError",
                "<init>", "(ILjava/lang/String;Ljava/lang/String;)V", false));
        method.instructions.add(new InsnNode(Opcodes.ATHROW));
    } else {/*from w w  w .j a  v  a2 s .  c  o m*/
        Type[] args = Type.getArgumentTypes(method.desc);
        addInstructionsForLoadArguments(method.instructions, args, getMethod().isStatic());

        int opcode = Opcodes.INVOKESPECIAL;
        if (getMethod().isStatic()) {
            opcode = Opcodes.INVOKESTATIC;
        }
        method.instructions.add(
                new MethodInsnNode(opcode, superClassName, getMethod().getName(), getMethod().getSignature()));
        method.instructions.add(new InsnNode(Opcodes.ARETURN));
    }
}