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:com.android.builder.testing.MockableJarGenerator.java

License:Apache License

private static InsnList throwExceptionsList(MethodNode methodNode, ClassNode classNode) {
    try {/*www. java 2  s  .  co  m*/
        String runtimeException = Type.getInternalName(RuntimeException.class);
        Constructor<RuntimeException> constructor = RuntimeException.class.getConstructor(String.class);

        InsnList instructions = new InsnList();
        instructions.add(new TypeInsnNode(Opcodes.NEW, runtimeException));
        instructions.add(new InsnNode(Opcodes.DUP));

        String className = classNode.name.replace('/', '.');
        instructions.add(new LdcInsnNode("Method " + methodNode.name + " in " + className + " not mocked. "
                + "See http://g.co/androidstudio/not-mocked for details."));
        instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, runtimeException, CONSTRUCTOR,
                Type.getType(constructor).getDescriptor(), false));
        instructions.add(new InsnNode(Opcodes.ATHROW));

        return instructions;
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.android.mkstubs.stubber.MethodStubber.java

License:Apache License

@Override
public void visitCode() {
    Label l0 = new Label();
    mv.visitLabel(l0);//from ww  w  . jav a 2s .co  m
    mv.visitLineNumber(36, l0);
    mv.visitTypeInsn(Opcodes.NEW, "java/lang/RuntimeException");
    mv.visitInsn(Opcodes.DUP);
    mv.visitLdcInsn("stub");
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, // opcode
            "java/lang/RuntimeException", // owner
            "<init>", // name
            "(Ljava/lang/String;)V", // desc
            false);
    mv.visitInsn(Opcodes.ATHROW);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", // name
            "Lcom/android/mkstubs/stubber/MethodStubber;", // desc
            null, // signature
            l0, // label start
            l1, // label end
            0); // index
    mv.visitMaxs(3, 1); // maxStack, maxLocals
}

From source file:com.android.tools.klint.checks.ControlFlowGraph.java

License:Apache License

private static String dotDescribe(Node node) {
    AbstractInsnNode instruction = node.instruction;
    if (instruction instanceof LabelNode) {
        return "Label";
    } else if (instruction instanceof LineNumberNode) {
        LineNumberNode lineNode = (LineNumberNode) instruction;
        return "Line " + lineNode.line;
    } else if (instruction instanceof FrameNode) {
        return "Stack Frame";
    } else if (instruction instanceof MethodInsnNode) {
        MethodInsnNode method = (MethodInsnNode) instruction;
        String cls = method.owner.substring(method.owner.lastIndexOf('/') + 1);
        cls = cls.replace('$', '.');
        return "Call " + cls + "#" + method.name;
    } else if (instruction instanceof FieldInsnNode) {
        FieldInsnNode field = (FieldInsnNode) instruction;
        String cls = field.owner.substring(field.owner.lastIndexOf('/') + 1);
        cls = cls.replace('$', '.');
        return "Field " + cls + "#" + field.name;
    } else if (instruction instanceof TypeInsnNode && instruction.getOpcode() == Opcodes.NEW) {
        return "New " + ((TypeInsnNode) instruction).desc;
    }//www .  j a  v a 2 s .com
    StringBuilder sb = new StringBuilder();
    String opcodeName = getOpcodeName(instruction.getOpcode());
    sb.append(opcodeName);

    if (instruction instanceof IntInsnNode) {
        IntInsnNode in = (IntInsnNode) instruction;
        sb.append(" ").append(Integer.toString(in.operand));
    } else if (instruction instanceof LdcInsnNode) {
        LdcInsnNode ldc = (LdcInsnNode) instruction;
        sb.append(" ");
        if (ldc.cst instanceof String) {
            sb.append("\\\"");
        }
        sb.append(ldc.cst);
        if (ldc.cst instanceof String) {
            sb.append("\\\"");
        }
    }
    return sb.toString();
}

From source file:com.android.tools.lint.checks.JavaScriptInterfaceDetector.java

License:Apache License

@Nullable
private String findFirstArgType(ClassContext context, ClassNode classNode, MethodNode method,
        MethodInsnNode call) {// ww w.ja v  a  2s .co  m
    // Find object being passed in as the first argument
    Analyzer analyzer = new Analyzer(new SourceInterpreter() {
        @Override
        public SourceValue newOperation(AbstractInsnNode insn) {
            if (insn.getOpcode() == Opcodes.NEW) {
                String desc = ((TypeInsnNode) insn).desc;
                return new TypeValue(1, desc);
            }
            return super.newOperation(insn);
        }

        @Override
        public SourceValue newValue(Type type) {
            if (type != null && type.getSort() == Type.VOID) {
                return null;
            } else if (type != null) {
                return new TypeValue(1, type.getInternalName());
            }
            return super.newValue(type);
        }

        @Override
        public SourceValue copyOperation(AbstractInsnNode insn, SourceValue value) {
            return value;
        }
    });
    try {
        Frame[] frames = analyzer.analyze(classNode.name, method);
        InsnList instructions = method.instructions;
        Frame frame = frames[instructions.indexOf(call)];
        if (frame.getStackSize() <= 1) {
            return null;
        }
        SourceValue stackValue = (SourceValue) frame.getStack(1);
        if (stackValue instanceof TypeValue) {
            return ((TypeValue) stackValue).getFqcn();
        }
    } catch (AnalyzerException e) {
        context.log(e, null);
    }

    return null;
}

From source file:com.asakusafw.dag.compiler.builtin.SummarizeOperatorGenerator.java

License:Apache License

private static void defineCheckNull(ClassWriter writer, UserOperator operator, DataModelReference inputType) {

    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC, METHOD_CHECK_NON_NULL,
            Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(ValueOption.class), typeOf(Object.class),
                    typeOf(String.class)),
            null, null);/*w  ww . j a v a 2 s. com*/

    LocalVarRef optionVar = new LocalVarRef(Opcodes.ALOAD, 0);
    LocalVarRef objectVar = new LocalVarRef(Opcodes.ALOAD, 1);
    LocalVarRef nameVar = new LocalVarRef(Opcodes.ALOAD, 2);

    // if (option.isNull()) {
    Label ifEnd = new Label();
    optionVar.load(method);
    getNullity(method, VALUE_DESC);
    method.visitJumpInsn(Opcodes.IFEQ, ifEnd);

    // new NullPointerException ...
    method.visitTypeInsn(Opcodes.NEW, typeOf(NullPointerException.class).getInternalName());
    method.visitInsn(Opcodes.DUP);

    // str = String.format("<type>.%s must not be null (in <operator>): %s", name, object)
    getConst(method,
            String.format("%s.%%s must not be null (in %s.%s): %%s", inputType.getDeclaration().getSimpleName(),
                    operator.getMethod().getDeclaringClass().getSimpleName(), operator.getMethod().getName()));

    getArray(method, typeOf(Object.class), new LocalVarRef[] { nameVar, objectVar });
    method.visitMethodInsn(Opcodes.INVOKESTATIC, typeOf(String.class).getInternalName(), "format",
            Type.getMethodDescriptor(typeOf(String.class), typeOf(String.class), typeOf(Object[].class)),
            false);

    // throw new NullPointerException(str)
    method.visitMethodInsn(Opcodes.INVOKESPECIAL, typeOf(NullPointerException.class).getInternalName(),
            CONSTRUCTOR_NAME, Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(String.class)), false);

    method.visitInsn(Opcodes.ATHROW);

    method.visitLabel(ifEnd);
    // }
    method.visitInsn(Opcodes.RETURN);
    method.visitMaxs(0, 0);
    method.visitEnd();
}

From source file:com.asakusafw.dag.compiler.codegen.AsmUtil.java

License:Apache License

/**
 * Adds an new instance of the target type on to the top of the stack.
 * @param method the current method visitor
 * @param type the target type/*from w  w w .  j  av a 2s . c o m*/
 */
public static void getNew(MethodVisitor method, TypeDescription type) {
    Type t = typeOf(type);
    method.visitTypeInsn(Opcodes.NEW, t.getInternalName());
    method.visitInsn(Opcodes.DUP);
    method.visitMethodInsn(Opcodes.INVOKESPECIAL, t.getInternalName(), CONSTRUCTOR_NAME,
            Type.getMethodDescriptor(Type.VOID_TYPE), false);
}

From source file:com.asakusafw.dag.compiler.codegen.OperationGenerator.java

License:Apache License

private static void getClass(MethodVisitor method, ClassDescription target, ClassNode element,
        Function<VertexElement, String> ids) {
    method.visitTypeInsn(Opcodes.NEW, element.getImplementationType().getInternalName());
    method.visitInsn(Opcodes.DUP);/*w  w  w .jav  a 2  s  .co  m*/
    List<Type> parameterTypes = new ArrayList<>();
    for (VertexElement dep : element.getDependencies()) {
        parameterTypes.add(typeOf(dep.getRuntimeType()));
        get(method, target, dep, ids);
    }
    method.visitMethodInsn(Opcodes.INVOKESPECIAL, element.getImplementationType().getInternalName(),
            CONSTRUCTOR_NAME,
            Type.getMethodDescriptor(Type.VOID_TYPE, parameterTypes.stream().toArray(Type[]::new)), false);
}

From source file:com.codename1.tools.translator.bytecodes.TypeInstruction.java

License:Open Source License

@Override
public void appendInstruction(StringBuilder b, List<Instruction> l) {
    type = type.replace('.', '_').replace('/', '_').replace('$', '_');
    b.append("    ");
    switch (opcode) {
    case Opcodes.NEW:
        b.append("PUSH_POINTER(__NEW_");
        b.append(type);/*ww  w. j  a v  a  2 s  . com*/
        b.append("(threadStateData)); /* NEW */\n");
        break;
    case Opcodes.ANEWARRAY:
        if (type.startsWith("[")) {
            int dim = 2;
            String t = type.substring(1);
            while (t.startsWith("[")) {
                t = t.substring(1);
                dim++;
            }

            b.append(" SP--;\n    PUSH_POINTER(allocArray(threadStateData, (*SP).data.i, &class_array");
            b.append(dim);
            b.append("__");
            b.append(actualType);
            b.append(", sizeof(JAVA_OBJECT), ");
            b.append(dim);
            b.append("));\n    SP[-1].data.o->__codenameOneParentClsReference = &class_array");
            b.append(dim);
            b.append("__");
            b.append(actualType);
            b.append("; /* ANEWARRAY multi */\n");
            break;
        }
        b.append("SP--;\n    PUSH_POINTER(__NEW_ARRAY_");
        b.append(actualType);
        b.append("(threadStateData, SP[0].data.i));\n");
        break;
    case Opcodes.CHECKCAST:
        b.append("BC_CHECKCAST(");
        b.append(type);
        b.append(");\n");
        break;
    case Opcodes.INSTANCEOF:
        int pos = type.indexOf('[');
        if (pos > -1) {
            int count = 1;
            while (type.charAt(pos + 1) == '[') {
                count++;
                pos++;
            }
            b.append("BC_INSTANCEOF(cn1_array_");
            b.append(count);
            b.append("_id_");
            b.append(actualType);
        } else {
            b.append("BC_INSTANCEOF(cn1_class_id_");
            b.append(actualType);
        }
        b.append(");\n");
        break;
    }
}

From source file:com.devexperts.aprof.transformer.AbstractMethodVisitor.java

License:Open Source License

@Override
public void visitTypeInsn(final int opcode, final String desc) {
    switch (opcode) {
    case Opcodes.NEW:
        visitAllocateBefore(desc);/* ww  w . j a v a2s  . c  om*/
        mv.visitTypeInsn(opcode, desc);
        visitAllocateAfter(desc);
        break;
    case Opcodes.ANEWARRAY:
        if (!context.isIntrinsicArraysCopyOf()) {
            String arrayDesc = desc.startsWith("[") ? "[" + desc : "[L" + desc + ";";
            visitAllocateArrayBefore(arrayDesc);
            mv.visitTypeInsn(opcode, desc);
            visitAllocateArrayAfter(arrayDesc);
            break;
        }
        // ELSE -- FALLS THROUGH !!!
    default:
        mv.visitTypeInsn(opcode, desc);
    }
}

From source file:com.geeksaga.light.profiler.util.ASMUtil.java

License:Apache License

public static TypeInsnNode createNewNode(String className) {
    // TODO use to Type.getInternalName
    return new TypeInsnNode(Opcodes.NEW, getInternalName(className));
}