Example usage for org.objectweb.asm Opcodes INVOKESPECIAL

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

Introduction

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

Prototype

int INVOKESPECIAL

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

Click Source Link

Usage

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

License:Apache License

private static void createConstructor(ClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();/* w w w.  j  a v  a  2  s  .c o  m*/
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}

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 w w.ja v  a2  s . c  o  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.compass.core.util.reflection.asm.AsmReflectionFieldGenerator.java

License:Apache License

private static void createConstructor(ClassVisitor cw) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/lang/reflect/Field;)V", null, null);
    mv.visitCode();//from w  ww .  j  a v a 2 s .c o m
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(PlainReflectionField.class), "<init>",
            "(Ljava/lang/reflect/Field;)V");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
}

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

License:Apache License

/**
 * Creates the class constructor which delegates the input to the super.
 *///from   ww w .  j  a va2 s.  c o  m
private static void createCtor(ClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/lang/reflect/Method;)V", null,
            null);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(PlainReflectionMethod.class), "<init>",
            "(Ljava/lang/reflect/Method;)V");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
}

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

License:Open Source License

@Override
public void visitMethodInsn(final int opcode, final String owner, final String name, final String desc,
        final boolean itf) {
    if (owner == null || name == null || desc == null) {
        log.warn(getM() + ": Cannot read invoke operation with method name '" + owner + "." + name
                + "' and descriptor '" + desc + "'!");
        return;/*from w  w  w  .  ja  va2 s  . c om*/
    }
    switch (opcode) {
    /**********
     * INVOKE *
     **********/
    case Opcodes.INVOKEINTERFACE:
    case Opcodes.INVOKESPECIAL:
        // Constructor or supermethod (any super) or private method callout.
    case Opcodes.INVOKESTATIC:
    case Opcodes.INVOKEVIRTUAL: {
        final T ownerT = getDu().getT(owner);
        if (opcode == Opcodes.INVOKEINTERFACE) {
            ownerT.setInterface(true); // static also possible in interface since JVM 8
        }
        assert opcode != Opcodes.INVOKEINTERFACE || itf;

        final M refM = ownerT.getM(name, desc);
        refM.setStatic(opcode == Opcodes.INVOKESTATIC);
        add(new INVOKE(this.ops.size(), opcode, this.line, refM, opcode == Opcodes.INVOKESPECIAL));
        break;
    }
    default:
        log.warn(getM() + ": Unknown method 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);/* w  ww. j  a  v a2s  . co 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.AddEmptyMethodAdapter.java

License:Open Source License

@Override
public void visitEnd() {
    MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions);
    if ((this.access & Opcodes.ACC_ABSTRACT) != 0) {
        mv.visitEnd();/*from   www. j a  v  a  2s . c o m*/
        return;
    }
    mv.visitCode();
    boolean needConstValue = true;
    if (superToCall != null) {
        needConstValue = false;
        boolean isStatic = (this.access & Opcodes.ACC_STATIC) != 0;
        int firstArgIndex = isStatic ? 0 : 1;
        if (!isStatic)
            mv.visitVarInsn(Opcodes.ALOAD, 0); // "this"
        Type[] args = Type.getArgumentTypes(desc);
        for (int i = 0, slot = firstArgIndex; i < args.length; slot += args[i++].getSize())
            mv.visitVarInsn(args[i].getOpcode(Opcodes.ILOAD), slot);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, superToCall, name, desc, false);
    }
    Type returnType = Type.getReturnType(this.desc);
    switch (returnType.getSort()) {
    case Type.VOID:
        mv.visitInsn(Opcodes.RETURN);
        break;
    case Type.INT:
    case Type.BOOLEAN:
    case Type.CHAR:
    case Type.BYTE:
    case Type.SHORT:
        if (needConstValue)
            mv.visitInsn(Opcodes.ICONST_1);
        mv.visitInsn(Opcodes.IRETURN);
        break;
    case Type.FLOAT:
        if (needConstValue)
            mv.visitInsn(Opcodes.FCONST_1);
        mv.visitInsn(Opcodes.FRETURN);
        break;
    case Type.LONG:
        if (needConstValue)
            mv.visitInsn(Opcodes.LCONST_1);
        mv.visitInsn(Opcodes.LRETURN);
        break;
    case Type.DOUBLE:
    case Type.OBJECT:
    case Type.ARRAY:
        if (needConstValue)
            mv.visitInsn(Opcodes.ACONST_NULL);
        mv.visitInsn(Opcodes.ARETURN);
        break;
    }
    mv.visitMaxs(1, maxLocals);
    mv.visitEnd();
}

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 a  2  s .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// ww  w.j  av  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 .  j  a va 2 s .  c om

    //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;
}