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:apb.processors.NotNullClassInstrumenter.java

License:Apache License

public MethodVisitor visitMethod(final int access, final String name, String desc, String signature,
        String[] exceptions) {// ww  w  .  j  ava  2s . c o m
    final Type[] args = Type.getArgumentTypes(desc);
    final Type returnType = Type.getReturnType(desc);

    MethodVisitor v = cv.visitMethod(access, name, desc, signature, exceptions);
    return new MethodAdapter(v) {
        @Override
        public AnnotationVisitor visitParameterAnnotation(int parameter, String anno, boolean visible) {
            final AnnotationVisitor result = mv.visitParameterAnnotation(parameter, anno, visible);

            if (NotNullClassInstrumenter.isReferenceType(args[parameter])
                    && anno.equals(NOT_NULL_ANNOATATION_SIGNATURE)) {
                notNullParams.add(parameter);
            }

            return result;
        }

        @Override
        public AnnotationVisitor visitAnnotation(String anno, boolean isRuntime) {
            final AnnotationVisitor av = mv.visitAnnotation(anno, isRuntime);

            if (isReferenceType(returnType) && anno.equals(NOT_NULL_ANNOATATION_SIGNATURE)) {
                isResultNotNull = true;
            }

            return av;
        }

        @Override
        public void visitCode() {
            if (isResultNotNull || !notNullParams.isEmpty()) {
                startGeneratedCodeLabel = new Label();
                mv.visitLabel(startGeneratedCodeLabel);
            }

            for (int nullParam : notNullParams) {
                int var = (access & 8) != 0 ? 0 : 1;

                for (int i = 0; i < nullParam; i++) {
                    var += args[i].getSize();
                }

                mv.visitVarInsn(Opcodes.ALOAD, var);
                Label end = new Label();
                mv.visitJumpInsn(Opcodes.IFNONNULL, end);
                generateThrow(
                        ILLEGAL_STATE_EXCEPTION_SIGNATURE, "Argument " + nullParam
                                + " for @NotNull parameter of " + className + "." + name + " must not be null",
                        end);
            }

            if (isResultNotNull) {
                final Label codeStart = new Label();
                mv.visitJumpInsn(Opcodes.GOTO, codeStart);
                throwLabel = new Label();
                mv.visitLabel(throwLabel);
                generateThrow(ILLEGAL_STATE_EXCEPTION_SIGNATURE,
                        "@NotNull method " + className + "." + name + " must not return null", codeStart);
            }
        }

        @Override
        public void visitLocalVariable(String name, String desc, String signature, Label start, Label end,
                int index) {
            boolean isStatic = (access & 8) != 0;
            boolean isParameter = isStatic ? index < args.length : index <= args.length;
            mv.visitLocalVariable(name, desc, signature,
                    !isParameter || startGeneratedCodeLabel == null ? start : startGeneratedCodeLabel, end,
                    index);
        }

        public void visitInsn(int opcode) {
            if (opcode == Opcodes.ARETURN && isResultNotNull) {
                mv.visitInsn(Opcodes.DUP);
                mv.visitJumpInsn(Opcodes.IFNULL, throwLabel);
            }

            mv.visitInsn(opcode);
        }

        private void generateThrow(String exceptionClass, String descr, Label end) {
            mv.visitTypeInsn(Opcodes.NEW, exceptionClass);
            mv.visitInsn(Opcodes.DUP);
            mv.visitLdcInsn(descr);

            final String exceptionParamClass = "(Ljava/lang/String;)V";
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, exceptionClass, "<init>", exceptionParamClass);
            mv.visitInsn(Opcodes.ATHROW);
            mv.visitLabel(end);
            isModified = true;
        }

        private final List<Integer> notNullParams = new ArrayList<Integer>();
        private boolean isResultNotNull = false;
        public Label throwLabel;
        private Label startGeneratedCodeLabel;
    };
}

From source file:bytecode.InstructionExporter.java

License:Apache License

/**
 * Outputs an object allocation instruction.
 *
 * @param instruction Object allocation instruction.
 * @return            <code>null</code>
 *//*from w w  w.j  a v  a  2  s .c  o m*/
@Override
public Void visit(NewObject instruction) {
    mv.visitTypeInsn(Opcodes.NEW, instruction.getType().getInternalName());

    return null;
}

From source file:bytecode.MethodImporter.java

License:Apache License

/**
 * Imports instructions with a single type operand (allocation of reference
 * arrays, allocation of objects, casting and type checks).
 *
 * @param opcode     Opcode./* w w  w. j  a  v a 2 s  . co m*/
 * @param descriptor String descriptor of the type operand.
 */
@Override
public void visitTypeInsn(final int opcode, final String descriptor) {
    Type type = Type.getObjectType(descriptor);

    switch (opcode) {
    // News
    case Opcodes.ANEWARRAY:
        ordered.add(stack.push(new NewArray(type, stack.pop())));
        break;
    case Opcodes.NEW:
        ordered.add(stack.push(new NewObject(type)));
        break;

    // Type Checks
    case Opcodes.CHECKCAST:
        ordered.add(stack.push(new CheckCast(stack.pop(), type)));
        break;
    case Opcodes.INSTANCEOF:
        ordered.add(stack.push(new InstanceOf(stack.pop(), type)));
        break;
    }
}

From source file:Client.JClassPatcher.java

License:Open Source License

private void patchRandom(ClassNode node) {
    Logger.Info("Patching random (" + node.name + ".class)");

    Iterator<MethodNode> methodNodeList = node.methods.iterator();
    while (methodNodeList.hasNext()) {
        MethodNode methodNode = methodNodeList.next();

        if (methodNode.name.equals("a")) {
            // System.out.println(methodNode.desc);
            if (methodNode.desc.equals("(ILtb;)V")) {
                Iterator<AbstractInsnNode> insnNodeList = methodNode.instructions.iterator();
                while (insnNodeList.hasNext()) {
                    AbstractInsnNode insnNode = insnNodeList.next();
                    AbstractInsnNode nextNode = insnNode.getNext();

                    if (nextNode == null)
                        break;

                    if (insnNode.getOpcode() == Opcodes.ALOAD && nextNode.getOpcode() == Opcodes.ICONST_0) {
                        VarInsnNode call = (VarInsnNode) insnNode;
                        System.out.println("Patching validation...");

                        methodNode.instructions.insert(insnNode, new MethodInsnNode(Opcodes.INVOKEVIRTUAL,
                                "java/util/Random", "nextBytes", "([B)V"));
                        methodNode.instructions.insert(insnNode, new VarInsnNode(Opcodes.ALOAD, 2));
                        methodNode.instructions.insert(insnNode,
                                new MethodInsnNode(Opcodes.INVOKESPECIAL, "java/util/Random", "<init>", "()V"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.DUP));
                        methodNode.instructions.insert(insnNode,
                                new TypeInsnNode(Opcodes.NEW, "java/util/Random"));
                    }/*from  ww  w  .j a  v a2 s .co m*/
                }
            }
        }
    }
}

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

License:Open Source License

private void println(MethodVisitor mv, String prefix, int refVar) {
    mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");
    mv.visitTypeInsn(Opcodes.NEW, "java/lang/StringBuilder");
    mv.visitInsn(Opcodes.DUP);//from www  .j  a v  a 2  s.c o m
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V");
    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);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
    mv.visitLdcInsn(" var " + refVar + ":");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(Ljava/lang/String;)Ljava/lang/StringBuilder;");

    mv.visitVarInsn(Opcodes.ALOAD, refVar);

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

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);/*w  w  w  . j  a  v  a2s  . c o  m*/
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/concurrent/ConcurrentHashMap", "<init>", "()V");
        mv.visitFieldInsn(Opcodes.PUTSTATIC, className, HotswapConstants.STATIC_FIELD_HOLDER,
                "Ljava/util/concurrent/ConcurrentHashMap;");
    }
}

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 ww  w. j av a 2  s . 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);//from   ww w.j a v a 2 s. 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.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  ww. j  a v  a 2  s. co  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();
        }
    };
}