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.alibaba.hotswap.processor.clinit.ClinitVisitor.java

License:Open Source License

@Override
public void visitEnd() {
    // If no clinit method, then add it
    if (!hasClinitMethod) {
        if (!isInterface) {
            int access = Opcodes.ACC_STATIC + Opcodes.ACC_PUBLIC;
            String name = HotswapConstants.HOTSWAP_CLINIT;
            String desc = "()V";
            MethodVisitor mv = super.visitMethod(access, name, desc, null, null);
            if (mv != null) {
                mv.visitCode();/*from w ww .j a v a2s . c om*/
                mv.visitTypeInsn(Opcodes.NEW, "java/util/concurrent/ConcurrentHashMap");
                mv.visitInsn(Opcodes.DUP);
                mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/concurrent/ConcurrentHashMap", "<init>",
                        "()V");
                mv.visitFieldInsn(Opcodes.PUTSTATIC, className, HotswapConstants.STATIC_FIELD_HOLDER,
                        "Ljava/util/concurrent/ConcurrentHashMap;");

                mv.visitInsn(Opcodes.RETURN);
                mv.visitMaxs(2, 0);
                mv.visitEnd();
            }

            generateClinit();
        } else {
            generateDefaultClinit();
        }
    } else {
        if (!isInterface) {
            generateClinit();
        }
    }

    super.visitEnd();
}

From source file:com.alibaba.hotswap.processor.constructor.modifier.ConstructorInvokeModifier.java

License:Open Source License

@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc) {
    if (opcode == Opcodes.INVOKESPECIAL && name.equals(HotswapConstants.INIT)) {
        if (HotswapRuntime.hasClassMeta(owner)) {
            // Try to reload owner.
            ClassMeta classMeta = HotswapRuntime.getClassMeta(owner);
            String mk = HotswapMethodUtil.getMethodKey(name, desc);
            MethodMeta mm = classMeta.initMetas.get(mk);
            if (mm != null && mm.isAdded()) {
                Type[] argsType = Type.getArgumentTypes(desc);
                push(argsType.length);//w  w  w .  ja  v a 2s  .com
                newArray(Type.getType(Object.class));

                for (int i = argsType.length - 1; i >= 0; i--) {
                    Type type = argsType[i];
                    swap();
                    box(type);
                    push(i);
                    swap();
                    // arrayStore(Type.getType(Object.class));
                    mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HotswapMethodUtil.class),
                            "processConstructorArgs",
                            "([Ljava/lang/Object;ILjava/lang/Object;)[Ljava/lang/Object;");
                }

                mv.visitLdcInsn(Opcodes.ACONST_NULL);
                swap();
                mv.visitLdcInsn(mm.getIndex());
                swap();
                super.visitMethodInsn(opcode, owner, name, HotswapConstants.UNIFORM_CONSTRUCTOR_DESC);

                return;
            }
        }
    }
    super.visitMethodInsn(opcode, owner, name, desc);
}

From source file:com.alibaba.hotswap.processor.constructor.modifier.FieldHolderInitModifier.java

License:Open Source License

@Override
public void visitCode() {
    super.visitCode();

    // this.__$$hotswap_field_holder$$__ = new ConcurrentHashMap();
    mv.visitVarInsn(Opcodes.ALOAD, 0);/* ww  w  . jav a2  s .  c  o  m*/
    mv.visitFieldInsn(Opcodes.GETFIELD, className, HotswapConstants.FIELD_HOLDER,
            "Ljava/util/concurrent/ConcurrentHashMap;");
    Label end = newLabel();
    ifNonNull(end);

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitTypeInsn(Opcodes.NEW, "java/util/concurrent/ConcurrentHashMap");
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/concurrent/ConcurrentHashMap", "<init>", "()V");
    mv.visitFieldInsn(Opcodes.PUTFIELD, className, HotswapConstants.FIELD_HOLDER,
            "Ljava/util/concurrent/ConcurrentHashMap;");
    mark(end);
}

From source file:com.alibaba.hotswap.processor.front.compile.CompilerErrorVisitor.java

License:Open Source License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);

    return new GeneratorAdapter(mv, access, name, desc) {

        private int status = 0;

        @Override/*w w w .j a  v a2s.c om*/
        public void visitCode() {
            super.visitCode();
            status = 1;
        }

        @Override
        public void visitTypeInsn(int opcode, String type) {
            if (status == 1 && opcode == Opcodes.NEW) {
                status = 2;
            } else {
                status = 0;
            }
            super.visitTypeInsn(opcode, type);
        }

        @Override
        public void visitMethodInsn(int opcode, String owner, String name, String desc) {
            if (status == 4 && opcode == Opcodes.INVOKESPECIAL) {
                status = 5;
            } else {
                status = 0;
            }
            super.visitMethodInsn(opcode, owner, name, desc);
        }

        @Override
        public void visitLdcInsn(Object cst) {
            if (status == 3) {
                status = 4;
            } else {
                status = 0;
            }
            super.visitLdcInsn(cst);
        }

        @Override
        public void visitInsn(int opcode) {
            if (status == 2 && opcode == Opcodes.DUP) {
                status = 3;
            } else if (status == 5 && opcode == Opcodes.ATHROW) {
                status = 6;
            } else {
                status = 0;
            }

            super.visitInsn(opcode);
        }

        @Override
        public void visitEnd() {
            if (status == 6) {
                throw new HotswapException(
                        "Class file is compiled from Java source file which has compile error");
            }
            super.visitEnd();
        }
    };
}

From source file:com.alibaba.hotswap.processor.jdk.lang.modifier.PrivateGetDeclaredConstructors.java

License:Open Source License

@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc) {

    if (opcode == Opcodes.INVOKESPECIAL && owner.equals("java/lang/Class")
            && name.equals("getDeclaredConstructors0") && desc.equals("(Z)[Ljava/lang/reflect/Constructor;")) {
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;");
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HotswapRuntime.class), "hasClassMeta",
                "(Ljava/lang/String;)Z");
        Label old = new Label();
        mv.visitJumpInsn(Opcodes.IFEQ, old);

        mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(MethodReflectHelper.class),
                "getDeclaredConstructors0", "(Ljava/lang/Class;Z)[Ljava/lang/reflect/Constructor;");
        Label end = new Label();
        mv.visitJumpInsn(Opcodes.GOTO, end);
        mv.visitLabel(old);/*  ww w . j  av a2s  . co  m*/
        super.visitMethodInsn(opcode, owner, name, desc);
        mv.visitLabel(end);
    } else {
        super.visitMethodInsn(opcode, owner, name, desc);
    }
}

From source file:com.alibaba.hotswap.processor.jdk.lang.modifier.PrivateGetDeclaredFieldsModifier.java

License:Open Source License

@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc) {

    if (opcode == Opcodes.INVOKESPECIAL && owner.equals("java/lang/Class") && name.equals("getDeclaredFields0")
            && desc.equals("(Z)[Ljava/lang/reflect/Field;")) {
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;");
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HotswapRuntime.class), "hasClassMeta",
                "(Ljava/lang/String;)Z");
        Label old = new Label();
        mv.visitJumpInsn(Opcodes.IFEQ, old);

        // mv.visitInsn(Opcodes.POP2);
        // mv.visitVarInsn(Opcodes.ALOAD, 0);
        // mv.visitVarInsn(Opcodes.ILOAD, 1);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(FieldReflectHelper.class),
                "privateGetDeclaredFields0", "(Ljava/lang/Class;Z)[Ljava/lang/reflect/Field;");
        Label end = new Label();
        mv.visitJumpInsn(Opcodes.GOTO, end);
        mv.visitLabel(old);/*from   w w w  . ja  v a 2  s.  c o  m*/
        super.visitMethodInsn(opcode, owner, name, desc);
        mv.visitLabel(end);
    } else {
        super.visitMethodInsn(opcode, owner, name, desc);
    }
}

From source file:com.android.build.gradle.integration.packaging.NativeSoPackagingFromJarTest.java

License:Apache License

/**
 * Creates a class and returns the byte[] with the class
 * @return//from w  ww  .j  a va  2s .co  m
 */
private static byte[] getDummyClassByteCode() {
    ClassWriter cw = new ClassWriter(0);
    FieldVisitor fv;
    MethodVisitor mv;
    AnnotationVisitor av0;

    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, "com/foo/Foo", null, "java/lang/Object",
            null);

    mv = cw.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(1, 1);
    mv.visitEnd();

    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "aaa", "()V", null, null);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "test/Aaa", "bbb", "()V", false);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();

    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "bbb", "()V", null, null);
    mv.visitCode();
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 1);
    mv.visitEnd();

    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "ccc", "()V", null, null);
    mv.visitCode();
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 1);
    mv.visitEnd();

    cw.visitEnd();

    return cw.toByteArray();
}

From source file:com.android.build.gradle.internal.incremental.ConstructorArgsRedirection.java

License:Apache License

@Override
protected void restore(GeneratorAdapter mv, List<Type> args) {
    // At this point, init$args has been called and the result Object is on the stack.
    // The value of that Object is Object[] with exactly n + 1 elements.
    // The first element is a string with the qualified name of the constructor to call.
    // The remaining elements are the constructtor arguments.

    // Create a new local that holds the result of init$args call.
    mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;");
    int constructorArgs = mv.newLocal(Type.getType("[Ljava/lang/Object;"));
    mv.storeLocal(constructorArgs);//  w  w w .j  av  a 2 s. c  om

    // Reinstate local values
    mv.loadLocal(locals);
    int stackIndex = 0;
    for (int arrayIndex = 0; arrayIndex < args.size(); arrayIndex++) {
        Type arg = args.get(arrayIndex);
        // Do not restore "this"
        if (arrayIndex > 0) {
            // duplicates the array
            mv.dup();
            // index in the array of objects to restore the boxed parameter.
            mv.push(arrayIndex);
            // get it from the array
            mv.arrayLoad(Type.getType(Object.class));
            // unbox the argument
            ByteCodeUtils.unbox(mv, arg);
            // restore the argument
            mv.visitVarInsn(arg.getOpcode(Opcodes.ISTORE), stackIndex);
        }
        // stack index must progress according to the parameter type we just processed.
        stackIndex += arg.getSize();
    }
    // pops the array
    mv.pop();

    // Push a null for the marker parameter.
    mv.loadLocal(constructorArgs);
    mv.visitInsn(Opcodes.ACONST_NULL);

    // Invoke the constructor
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, thisClassName, "<init>", DISPATCHING_THIS_SIGNATURE, false);

    mv.goTo(end.getLabel());
}

From source file:com.android.build.gradle.internal.incremental.ConstructorRedirection.java

License:Apache License

@Override
protected void doRedirect(GeneratorAdapter mv, int change) {
    mv.loadLocal(change);/*from   w  w w .j  a  va2s  .  co  m*/
    mv.push("init$args." + constructor.args.desc);

    Type arrayType = Type.getType("[Ljava/lang/Object;");
    // init$args args (including this) + locals
    mv.push(types.size() + 1);
    mv.newArray(Type.getType(Object.class));

    int array = mv.newLocal(arrayType);
    mv.dup();
    mv.storeLocal(array);

    // "this" is not ready yet, use null instead.
    mv.dup();
    mv.push(0);
    mv.visitInsn(Opcodes.ACONST_NULL);
    mv.arrayStore(Type.getType(Object.class));

    // Set the arguments in positions 1..(n-1);
    ByteCodeUtils.loadVariableArray(mv, ByteCodeUtils.toLocalVariables(types), 1); // Skip the this value

    // Add the locals array at the last position.
    mv.dup();
    // The index of the last position of the array.
    mv.push(types.size());
    // Create the array with all the local variables declared up to this point.
    ByteCodeUtils.newVariableArray(mv, constructor.variables.subList(0, constructor.localsAtLoadThis));
    mv.arrayStore(Type.getType(Object.class));

    mv.invokeInterface(IncrementalVisitor.CHANGE_TYPE,
            Method.getMethod("Object access$dispatch(String, Object[])"));
    mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;");
    //// At this point, init$args has been called and the result Object is on the stack.
    //// The value of that Object is Object[] with exactly n + 2 elements.
    //// The first element is the resulting local variables
    //// The second element is a string with the qualified name of the constructor to call.
    //// The remaining elements are the constructor arguments.

    // Keep a reference to the new locals array
    mv.dup();
    mv.push(0);
    mv.arrayLoad(Type.getType("[Ljava/lang/Object;"));
    mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;");
    mv.storeLocal(array);

    // Call super constructor
    // Put this behind the returned array
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.swap();
    // Push a null for the marker parameter.
    mv.visitInsn(Opcodes.ACONST_NULL);
    // Invoke the constructor
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, constructor.owner, "<init>", DISPATCHING_THIS_SIGNATURE, false);

    // Dispatch to init$body
    mv.loadLocal(change);
    mv.push("init$body." + constructor.body.desc);
    mv.loadLocal(array);

    // Now "this" can be set
    mv.dup();
    mv.push(0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.arrayStore(Type.getType(Object.class));

    mv.invokeInterface(IncrementalVisitor.CHANGE_TYPE,
            Method.getMethod("Object access$dispatch(String, Object[])"));
    mv.pop();
}

From source file:com.android.build.gradle.internal.incremental.IncrementalChangeVisitor.java

License:Apache License

/**
 * Turns this class into an override class that can be loaded by our custom class loader:
 *<ul>/*from   w  w w . j a va  2s.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() });

    if (DEBUG) {
        System.out.println(">>>>>>>> Processing " + name + "<<<<<<<<<<<<<");
    }

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