Example usage for org.objectweb.asm Opcodes DUP

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

Introduction

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

Prototype

int DUP

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

Click Source Link

Usage

From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java

License:Open Source License

private void println(MethodVisitor mv, String prefix) {
    mv.visitInsn(Opcodes.DUP); // S1 S1

    mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;"); // PrintStream S1 S1

    mv.visitInsn(Opcodes.SWAP); // S1 PrintStream S1

    mv.visitTypeInsn(Opcodes.NEW, "java/lang/StringBuilder"); // StringBuilder S1 PrintStream S1
    mv.visitInsn(Opcodes.DUP); // StringBuilder StringBuilder S1 PrintStream S1
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V"); // StringBuilder S1 PrintStream S1
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Thread", "currentThread", "()Ljava/lang/Thread;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Thread", "getName", "()Ljava/lang/String;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
    mv.visitLdcInsn(" " + prefix); // prefix StringBuilder S1 PrintStream S1
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(Ljava/lang/String;)Ljava/lang/StringBuilder;"); // StringBuilder S1 PrintStream S1

    mv.visitInsn(Opcodes.SWAP); // S1 StringBuilder PrintStream S1

    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(Ljava/lang/Object;)Ljava/lang/StringBuilder;"); // StringBuilder PrintStream S1
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;"); // PrintStream S1
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V"); // S1
}

From source file:com.alibaba.hotswap.processor.clinit.ClinitModifier.java

License:Open Source License

@Override
public void visitCode() {
    super.visitCode();
    if (!isInterface) {
        // initial __hotswap_static_field_holder__
        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;");
    }/*from   w  w  w .j  a  v a 2s. co  m*/
}

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 w w.  ja  v a 2s  .c o  m*/
                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.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.  j ava2s  .  com
    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.field.access.FieldAccessModifier.java

License:Open Source License

@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
    if (HotswapRuntime.getClassInitialized(className) && !name.equals(HotswapConstants.STATIC_FIELD_HOLDER)
            && !name.equals(HotswapConstants.FIELD_HOLDER)) {

        Queue<String> owners = new LinkedList<String>();
        owners.offer(owner);/*from  www .ja  v a 2  s  . c  o  m*/
        String tmpOwner = owners.poll();
        while (tmpOwner != null) {
            if (HotswapRuntime.hasClassMeta(tmpOwner)) {
                // Try to reload owner.
                HotswapRuntime.try2Reload(tmpOwner);

                ClassMeta classMeta = HotswapRuntime.getClassMeta(tmpOwner);
                String fmKey = HotswapFieldUtil.getFieldKey(name, desc);
                FieldMeta fm = classMeta.getFieldMeta(fmKey);

                if (classMeta.isInterface) {
                    if (fm != null && !fm.isDeleted(classMeta.loadedIndex)
                            && (opcode == Opcodes.PUTSTATIC || opcode == Opcodes.GETSTATIC)) {
                        super.visitFieldInsn(opcode, Type.getInternalName(classMeta.vClass), fm.name, fm.desc);
                        return;
                    }
                }

                if (fm != null && classMeta.primaryFieldKeyList.contains(fmKey)
                        && !fm.isDeleted(classMeta.loadedIndex)) {
                    break;
                } else if (fm != null && classMeta.primaryFieldKeyList.contains(fmKey)
                        && fm.isDeleted(classMeta.loadedIndex)) {
                    // If this accessed field is primary and deleted, it perhaps is a alias field
                    fm = classMeta.getFieldMeta(HotswapFieldUtil
                            .getFieldKey(HotswapConstants.PREFIX_FIELD_ALIAS + fm.name, fm.desc));
                }

                if (fm != null && fm.isAdded() && !fm.isDeleted(classMeta.loadedIndex)) {
                    if (opcode == Opcodes.PUTSTATIC) {
                        // put static
                        box(Type.getType(fm.desc));
                        mv.visitFieldInsn(Opcodes.GETSTATIC, tmpOwner, HotswapConstants.STATIC_FIELD_HOLDER,
                                "Ljava/util/concurrent/ConcurrentHashMap;");
                        mv.visitLdcInsn(fm.name);
                        mv.visitLdcInsn(fm.desc);
                        mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HotswapFieldUtil.class),
                                "setFieldValue",
                                "(Ljava/lang/Object;Ljava/util/concurrent/ConcurrentHashMap;Ljava/lang/String;Ljava/lang/String;)V");
                        return;
                    } else if (opcode == Opcodes.GETSTATIC) {
                        // get static
                        mv.visitFieldInsn(Opcodes.GETSTATIC, tmpOwner, HotswapConstants.STATIC_FIELD_HOLDER,
                                "Ljava/util/concurrent/ConcurrentHashMap;");
                        mv.visitLdcInsn(fm.name);
                        mv.visitLdcInsn(fm.desc);
                        mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HotswapFieldUtil.class),
                                "getFieldValue",
                                "(Ljava/util/concurrent/ConcurrentHashMap;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;");
                        unbox(Type.getType(fm.desc));
                        return;
                    } else if (opcode == Opcodes.PUTFIELD) {
                        // put field
                        // stack: obj fieldValue
                        box(Type.getType(fm.desc));
                        mv.visitInsn(Opcodes.SWAP);
                        mv.visitFieldInsn(Opcodes.GETFIELD, tmpOwner, HotswapConstants.FIELD_HOLDER,
                                "Ljava/util/concurrent/ConcurrentHashMap;");
                        mv.visitLdcInsn(fm.name);
                        mv.visitLdcInsn(fm.desc);
                        mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HotswapFieldUtil.class),
                                "setFieldValue",
                                "(Ljava/lang/Object;Ljava/util/concurrent/ConcurrentHashMap;Ljava/lang/String;Ljava/lang/String;)V");

                        return;
                    } else if (opcode == Opcodes.GETFIELD) {
                        // get field
                        // stack: obj
                        mv.visitFieldInsn(Opcodes.GETFIELD, tmpOwner, HotswapConstants.FIELD_HOLDER,
                                "Ljava/util/concurrent/ConcurrentHashMap;");
                        mv.visitLdcInsn(fm.name);
                        mv.visitLdcInsn(fm.desc);
                        mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HotswapFieldUtil.class),
                                "getFieldValue",
                                "(Ljava/util/concurrent/ConcurrentHashMap;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;");

                        Label notnull = newLabel();
                        Type type = Type.getType(fm.desc);

                        mv.visitInsn(Opcodes.DUP);
                        mv.visitJumpInsn(Opcodes.IFNONNULL, notnull);

                        Label end = newLabel();
                        switch (type.getSort()) {
                        case Type.BOOLEAN:
                            mv.visitInsn(Opcodes.POP);
                            mv.visitLdcInsn(new Boolean(false));
                            break;

                        case Type.CHAR:
                            mv.visitInsn(Opcodes.POP);
                            mv.visitLdcInsn(new Character(' '));
                            break;

                        case Type.BYTE:
                            mv.visitInsn(Opcodes.POP);
                            mv.visitLdcInsn(new Byte((byte) 0));
                            break;

                        case Type.SHORT:
                            mv.visitInsn(Opcodes.POP);
                            mv.visitLdcInsn(new Short((short) 0));
                            break;

                        case Type.INT:
                            mv.visitInsn(Opcodes.POP);
                            mv.visitLdcInsn(new Integer(0));
                            break;

                        case Type.FLOAT:
                            mv.visitInsn(Opcodes.POP);
                            mv.visitLdcInsn(new Float(0));
                            break;

                        case Type.LONG:
                            mv.visitInsn(Opcodes.POP);
                            mv.visitLdcInsn(new Long(0));
                            break;

                        case Type.DOUBLE:
                            mv.visitInsn(Opcodes.POP);
                            mv.visitLdcInsn(new Double(0));
                            break;

                        default:
                            break;
                        }
                        mv.visitJumpInsn(Opcodes.GOTO, end);

                        mark(notnull);
                        unbox(type);

                        mark(end);
                        return;
                    }
                }

                if (classMeta.clazz != null) {
                    Class<?> superClass = classMeta.clazz.getSuperclass();
                    if (superClass != null) {
                        owners.offer(HotswapUtil.getInternalClassName(superClass.getName()));
                    }
                    Class<?>[] superInterfaces = classMeta.clazz.getInterfaces();
                    if (superInterfaces != null) {
                        for (Class<?> itf : superInterfaces) {
                            owners.offer(HotswapUtil.getInternalClassName(itf.getName()));
                        }
                    }
                }
            }
            tmpOwner = owners.poll();
        }
    }
    super.visitFieldInsn(opcode, owner, name, desc);
}

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  a2  s  . c  o m*/
        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.classloader.modifier.DefineClassMethodModifier.java

License:Open Source License

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

    mv.visitVarInsn(Opcodes.ALOAD, 1);/*from w  w w .jav a  2  s . c o m*/
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HotswapConfiguration.class),
            "getClassPathInWorkspace", "(Ljava/lang/String;)Ljava/lang/String;");
    mv.visitInsn(Opcodes.DUP);
    Label old = new Label();
    mv.visitJumpInsn(Opcodes.IFNULL, old);

    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HotswapRuntime.class), "updateClassMeta",
            "(Ljava/lang/String;Ljava/lang/ClassLoader;)V");

    mv.visitVarInsn(Opcodes.ALOAD, 1); // className
    mv.visitInsn(Opcodes.SWAP);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(CustomerLoadClassBytes.class),
            "loadBytesFromPath", "(Ljava/lang/String;Ljava/lang/String;)[B");
    mv.visitVarInsn(Opcodes.ASTORE, 2);// store class bytes into 2
    mv.visitVarInsn(Opcodes.ALOAD, 2);// load class bytes
    mv.visitInsn(Opcodes.ARRAYLENGTH); // length of the class bytes
    mv.visitVarInsn(Opcodes.ISTORE, 4);// store length into 4

    Label end = new Label();
    mv.visitJumpInsn(Opcodes.GOTO, end);

    mv.visitLabel(old);
    mv.visitInsn(Opcodes.POP);

    mv.visitLabel(end);
}

From source file:com.alibaba.hotswap.processor.jdk.classloader.modifier.DefineClassMethodModifier.java

License:Open Source License

@Override
public void visitInsn(int opcode) {
    if (opcode == Opcodes.ARETURN) {
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HotswapRuntime.class), "hasClassMeta",
                "(Ljava/lang/String;)Z");
        Label end = new Label();
        mv.visitJumpInsn(Opcodes.IFEQ, end);

        mv.visitInsn(Opcodes.DUP);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitInsn(Opcodes.SWAP);/* www  .  jav a  2  s .  c  om*/
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HotswapRuntime.class),
                "updateClassMetaClass", "(Ljava/lang/String;Ljava/lang/Class;)V");

        mv.visitLabel(end);
    }

    super.visitInsn(opcode);
}

From source file:com.alibaba.hotswap.processor.jdk.classloader.modifier.FindClassMethodModifier.java

License:Open Source License

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

    Label normal = new Label();

    mv.visitVarInsn(Opcodes.ALOAD, 0);/*  w w  w.  j a va  2s  .c  om*/
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(ClassLoaderHelper.class), "tryLoadVClass",
            "(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;");
    mv.visitInsn(Opcodes.DUP);
    mv.visitJumpInsn(Opcodes.IFNULL, normal);
    mv.visitInsn(Opcodes.ARETURN);

    mv.visitLabel(normal);
    mv.visitInsn(Opcodes.POP);
}

From source file:com.android.build.gradle.internal.transforms.InstantRunTransform.java

License:Apache License

/**
 * Use asm to generate a concrete subclass of the AppPathLoaderImpl class.
 * It only implements one method :/*  w  ww . j a  v a  2s.c  om*/
 *      String[] getPatchedClasses();
 *
 * The method is supposed to return the list of classes that were patched in this iteration.
 * This will be used by the InstantRun runtime to load all patched classes and register them
 * as overrides on the original classes.2 class files.
 *
 * @param patchFileContents list of patched class names.
 * @param outputDir output directory where to generate the .class file in.
 */
private static void writePatchFileContents(@NonNull ImmutableList<String> patchFileContents,
        @NonNull File outputDir, long buildId) {

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

    // Add the build ID to force the patch file to be repackaged.
    cw.visitField(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, "BUILD_ID", "J", null, buildId);

    {
        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.BIPUSH, patchFileContents.size());
        mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/String");
        for (int index = 0; index < patchFileContents.size(); index++) {
            mv.visitInsn(Opcodes.DUP);
            mv.visitIntInsn(Opcodes.BIPUSH, index);
            mv.visitLdcInsn(patchFileContents.get(index));
            mv.visitInsn(Opcodes.AASTORE);
        }
        mv.visitInsn(Opcodes.ARETURN);
        mv.visitMaxs(4, 1);
        mv.visitEnd();
    }
    cw.visitEnd();

    byte[] classBytes = cw.toByteArray();
    File outputFile = new File(outputDir, IncrementalVisitor.APP_PATCHES_LOADER_IMPL + ".class");
    try {
        Files.createParentDirs(outputFile);
        Files.write(classBytes, outputFile);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}