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:com.mebigfatguy.junitflood.jvm.OperandStack.java

License:Apache License

public void performMethodInsn(int opcode, String owner, String name, String desc) {
    switch (opcode) {
    case Opcodes.INVOKEVIRTUAL:
    case Opcodes.INVOKESPECIAL:
    case Opcodes.INVOKESTATIC:
    case Opcodes.INVOKEINTERFACE:
        String[] parmSigs = SignatureUtils.splitMethodParameterSignatures(desc);
        int numParms = parmSigs.length;
        while ((numParms--) > 0) {
            pop();/*from w  w  w .j av a2  s.c  o m*/
        }
        if (opcode != Opcodes.INVOKESTATIC) {
            pop();
        }

        String returnSig = SignatureUtils.getReturnSignature(desc);
        if (!"V".equals(returnSig)) {
            Operand op = new Operand();
            op.setSignature(returnSig);
            push(op);
        }
        break;

    case Opcodes.INVOKEDYNAMIC:
        //no idea
        break;
    }
}

From source file:com.mogujie.instantrun.IncrementalChangeVisitor.java

License:Apache License

/**
 * Turns this class into an override class that can be loaded by our custom class loader:
 * <ul>//ww w  . j  a v a 2 s  .  c  o m
 * <li>Make the class name be OriginalName$override</li>
 * <li>Ensure the class derives from java.lang.Object, no other inheritance</li>
 * <li>Ensure the class has a public parameterless constructor that is a noop.</li>
 * </ul>
 */
@Override
public void visit(int version, int access, String name, String signature, String superName,
        String[] interfaces) {
    super.visit(version, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, name + OVERRIDE_SUFFIX, signature,
            "java/lang/Object", new String[] { CHANGE_TYPE.getInternalName() });

    visitedClassName = name;
    visitedSuperName = superName;
    instanceToStaticDescPrefix = "(L" + visitedClassName + ";";

    // Create empty constructor
    MethodVisitor mv = super.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();

    //        super.visitField(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_STATIC,
    //                "$obsolete", "Z", null, null);
}

From source file:com.mogujie.instantrun.IncrementalChangeVisitor.java

License:Apache License

private void insnMachine(AbstractInsnNode node, List<AbstractInsnNode> removeList) {
    if (node.getOpcode() == Opcodes.NEW) {
        if (status == INIT) {
            status = FIND_NEW;/*from  w w w.  j a v a  2s.co  m*/
            removeList.add(node);
        } else {
            throw new RuntimeException("find new in status " + status);
        }
    }

    if (node.getOpcode() == Opcodes.DUP) {
        if (status == FIND_NEW) {
            status = INIT;
            removeList.add(node);
        }
    }

    if (node.getOpcode() == Opcodes.INVOKESPECIAL && node instanceof MethodInsnNode) {
        if (status == INIT) {
            MethodInsnNode methodInsnNode = (MethodInsnNode) node;
            if ("<init>".equals(methodInsnNode.name)) {
                if (getMethodAccessRight(methodInsnNode.owner, methodInsnNode.name,
                        methodInsnNode.desc) == AccessRight.PUBLIC) {
                    int removeSize = removeList.size();
                    removeList.remove(removeSize - 1);
                    removeList.remove(removeSize - 2);
                }
            }

        }
    }

}

From source file:com.mogujie.instantrun.IncrementalTool.java

License:Apache License

public static byte[] getPatchFileContents(ImmutableList<String> patchFileContents,
        ImmutableList<Integer> patchIndexContents) {
    if (patchFileContents.size() != patchIndexContents.size()) {
        throw new GradleException("patchFileContents's size is " + patchFileContents.size()
                + ", but patchIndexContents's size is " + patchIndexContents.size()
                + ", please check the changed classes.");
    }//ww  w .j  a va 2  s.co m
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, IncrementalVisitor.APP_PATCHES_LOADER_IMPL,
            null, IncrementalVisitor.ABSTRACT_PATCHES_LOADER_IMPL, null);

    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, IncrementalVisitor.ABSTRACT_PATCHES_LOADER_IMPL, "<init>",
                "()V", false);
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getPatchedClasses", "()[Ljava/lang/String;", null, null);
        mv.visitCode();

        mv.visitIntInsn(Opcodes.SIPUSH, patchFileContents.size());
        mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/String");
        for (int index = 0; index < patchFileContents.size(); index++) {
            mv.visitInsn(Opcodes.DUP);
            mv.visitIntInsn(Opcodes.SIPUSH, index);
            mv.visitLdcInsn(patchFileContents.get(index));
            mv.visitInsn(Opcodes.AASTORE);
        }
        mv.visitInsn(Opcodes.ARETURN);
        mv.visitMaxs(4, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getPatchedClassIndexes", "()[I", null, null);
        mv.visitCode();

        mv.visitIntInsn(Opcodes.SIPUSH, patchIndexContents.size());
        mv.visitIntInsn(Opcodes.NEWARRAY, Opcodes.T_INT);
        for (int index = 0; index < patchIndexContents.size(); index++) {
            mv.visitInsn(Opcodes.DUP);
            mv.visitIntInsn(Opcodes.SIPUSH, index);
            mv.visitLdcInsn(patchIndexContents.get(index));
            mv.visitInsn(Opcodes.IASTORE);
        }
        mv.visitInsn(Opcodes.ARETURN);
        mv.visitMaxs(4, 1);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();

}

From source file:com.navercorp.pinpoint.profiler.instrument.ASMMethodNodeAdapter.java

License:Apache License

public void addDelegator(final String superClassInternalName) {
    if (superClassInternalName == null) {
        throw new IllegalArgumentException("super class internal name must not be null.");
    }/*w  w  w . j  av  a  2 s  . c  o m*/

    final InsnList instructions = this.methodNode.instructions;
    if (isStatic()) {
        // load parameters
        this.methodVariables.loadArgs(instructions);
        // invoke static
        instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, superClassInternalName, this.methodNode.name,
                this.methodNode.desc, false));
    } else {
        // load this
        this.methodVariables.loadVar(instructions, 0);
        // load parameters
        this.methodVariables.loadArgs(instructions);
        // invoke special
        instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, superClassInternalName, this.methodNode.name,
                this.methodNode.desc, false));
    }
    // return
    this.methodVariables.returnValue(instructions);
}

From source file:com.navercorp.pinpoint.profiler.instrument.ASMMethodVariables.java

License:Apache License

AbstractInsnNode findInitConstructorInstruction() {
    int nested = 0;
    for (AbstractInsnNode insnNode = this.methodNode.instructions
            .getFirst(); insnNode != null; insnNode = insnNode.getNext()) {
        if (insnNode instanceof TypeInsnNode) {
            if (insnNode.getOpcode() == Opcodes.NEW) {
                // new object().
                nested++;//w ww  .  j  a v a 2  s  . co m
            }
        } else if (insnNode instanceof MethodInsnNode) {
            final MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode;
            if (methodInsnNode.getOpcode() == Opcodes.INVOKESPECIAL && methodInsnNode.name.equals("<init>")) {
                if (--nested < 0) {
                    // find this() or super().
                    return insnNode.getNext();
                }
            }
        }
    }

    return null;
}

From source file:com.navercorp.pinpoint.profiler.instrument.ASMMethodVariables.java

License:Apache License

void invokeConstructor(final InsnList instructions, final Type type, final Method method) {
    String owner = type.getSort() == Type.ARRAY ? type.getDescriptor() : type.getInternalName();
    instructions.add(/*from w ww . j  a  v a 2s .  c om*/
            new MethodInsnNode(Opcodes.INVOKESPECIAL, owner, method.getName(), method.getDescriptor(), false));
}

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

License:Apache License

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

    visitor.visitTypeInsn(Opcodes.NEW, intBeanClazzName);
    visitor.visitInsn(Opcodes.DUP);
    visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, intBeanClazzName, "<init>", "()V");
    visitor.visitVarInsn(Opcodes.ASTORE, 3);
    return visitor;
}

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

License:Apache License

/**
 * Creates bytecode for serializing a bean property which returns a collection of opaque objects.
 * //from   ww w .  jav a  2 s  .  c  o m
 * @param visitor method visitor used for creating bytecode
 * @param returnType return type of get method in bean
 * @param intBeanClazzName binary name of bean
 * @param methodName binary name of get method in bean
 */
private void createObjectCollectionSerializationCode(MethodVisitor visitor, Class<?> returnType,
        String intBeanClazzName, String methodName, String propertyName) {
    visitor.visitTypeInsn(Opcodes.NEW, "com/nginious/http/serialize/JsonObjectCollectionSerializer");
    visitor.visitInsn(Opcodes.DUP);
    visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/nginious/http/serialize/JsonObjectCollectionSerializer",
            "<init>", "()V");
    visitor.visitVarInsn(Opcodes.ASTORE, 4);

    visitor.visitVarInsn(Opcodes.ALOAD, 4);
    String intReturnClazzName = returnType.getName().replace('.', '/');
    visitor.visitVarInsn(Opcodes.ALOAD, 3);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, intBeanClazzName, methodName,
            "()L" + intReturnClazzName + ";");
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "com/nginious/http/serialize/JsonObjectCollectionSerializer",
            "serialize", "(Ljava/util/Collection;)Lorg/json/JSONArray;");
    visitor.visitVarInsn(Opcodes.ASTORE, 4);

    visitor.visitVarInsn(Opcodes.ALOAD, 1);
    visitor.visitLdcInsn(propertyName);
    visitor.visitVarInsn(Opcodes.ALOAD, 4);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/json/JSONObject", "put",
            "(Ljava/lang/String;Ljava/lang/Object;)Lorg/json/JSONObject;");
}

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

License:Apache License

/**
 * Creates bytecode for serializing a bean property which returns a collection of beans that are serializable. Bean serializability
 * is determined as described in the class description.
 * //from  w  w  w. jav  a2 s .  c om
 * @param visitor method visitor used for creating bytecode
 * @param intBeanClazzName binary class name of bean
 * @param methodName binary name of get method in bean returning collection
 * @param returnType return type of get method in bean
 * @param collectionBeanType class of serializable bean found in collection
 */
private void createBeanCollectionSerializationCode(MethodVisitor visitor, String intBeanClazzName,
        String methodName, String propertyName, Class<?> returnType, Class<?> collectionBeanType) {
    visitor.visitVarInsn(Opcodes.ALOAD, 0);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "com/nginious/http/serialize/JsonSerializer",
            "getSerializerFactory", "()Lcom/nginious/http/serialize/SerializerFactoryImpl;");
    visitor.visitLdcInsn(collectionBeanType.getName());
    visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Class", "forName",
            "(Ljava/lang/String;)Ljava/lang/Class;");
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "com/nginious/http/serialize/SerializerFactoryImpl",
            "createJsonSerializer", "(Ljava/lang/Class;)Lcom/nginious/http/serialize/JsonSerializer;");
    visitor.visitVarInsn(Opcodes.ASTORE, 4);

    visitor.visitTypeInsn(Opcodes.NEW, "com/nginious/http/serialize/JsonBeanCollectionSerializer");
    visitor.visitInsn(Opcodes.DUP);
    visitor.visitVarInsn(Opcodes.ALOAD, 4);
    visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/nginious/http/serialize/JsonBeanCollectionSerializer",
            "<init>", "(Lcom/nginious/http/serialize/JsonSerializer;)V");
    visitor.visitVarInsn(Opcodes.ASTORE, 5);

    visitor.visitVarInsn(Opcodes.ALOAD, 5);
    String intReturnClazzName = returnType.getName().replace('.', '/');
    visitor.visitVarInsn(Opcodes.ALOAD, 3);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, intBeanClazzName, methodName,
            "()L" + intReturnClazzName + ";");
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "com/nginious/http/serialize/JsonBeanCollectionSerializer",
            "serialize", "(Ljava/util/Collection;)Lorg/json/JSONArray;");
    visitor.visitVarInsn(Opcodes.ASTORE, 5);

    visitor.visitVarInsn(Opcodes.ALOAD, 1);
    visitor.visitLdcInsn(propertyName);
    visitor.visitVarInsn(Opcodes.ALOAD, 5);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/json/JSONObject", "put",
            "(Ljava/lang/String;Ljava/lang/Object;)Lorg/json/JSONObject;");
}