Example usage for org.objectweb.asm MethodVisitor visitMethodInsn

List of usage examples for org.objectweb.asm MethodVisitor visitMethodInsn

Introduction

In this page you can find the example usage for org.objectweb.asm MethodVisitor visitMethodInsn.

Prototype

public void visitMethodInsn(final int opcode, final String owner, final String name, final String descriptor,
        final boolean isInterface) 

Source Link

Document

Visits a method instruction.

Usage

From source file:com.android.build.gradle.shrinker.TestClasses.java

License:Apache License

static byte[] emptyClass(String name) throws Exception {

    ClassWriter cw = new ClassWriter(0);
    FieldVisitor fv;//from w  ww. j a v a  2 s . c  o  m
    MethodVisitor mv;
    AnnotationVisitor av0;

    cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, "test/" + name, null, "java/lang/Object", null);

    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}

From source file:com.android.build.gradle.shrinker.TestClasses.java

License:Apache License

static byte[] classWithEmptyMethods(String className, String... namesAndDescriptors) throws Exception {

    ClassWriter cw = new ClassWriter(0);
    FieldVisitor fv;//  w ww .j ava 2s.  c  o  m
    MethodVisitor mv;
    AnnotationVisitor av0;

    cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, "test/" + className, null, "java/lang/Object", null);

    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    for (String namesAndDescriptor : namesAndDescriptors) {
        int colon = namesAndDescriptor.indexOf(':');
        String methodName = namesAndDescriptor.substring(0, colon);
        String descriptor = namesAndDescriptor.substring(colon + 1, namesAndDescriptor.length());
        {
            mv = cw.visitMethod(ACC_PUBLIC, methodName, descriptor, null, null);
            mv.visitCode();
            // This bytecode is only valid for some signatures (void methods). This class is used
            // for testing the parser, we don't ever load these classes to a running VM anyway.
            mv.visitInsn(RETURN);
            mv.visitMaxs(0, 1);
            mv.visitEnd();
        }
    }
    cw.visitEnd();

    return cw.toByteArray();
}

From source file:com.android.build.gradle.tasks.fd.GenerateInstantRunAppInfoTask.java

License:Apache License

void writeAppInfoClass(@NonNull String applicationId, @Nullable String applicationClass, long token)
        throws IOException {
    ClassWriter cw = new ClassWriter(0);
    FieldVisitor fv;//from   w  w w. java  2s.com
    MethodVisitor mv;

    String appInfoOwner = "com/android/tools/fd/runtime/AppInfo";
    cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, appInfoOwner, null, "java/lang/Object", null);

    fv = cw.visitField(ACC_PUBLIC + ACC_STATIC, "applicationId", "Ljava/lang/String;", null, null);
    fv.visitEnd();
    fv = cw.visitField(ACC_PUBLIC + ACC_STATIC, "applicationClass", "Ljava/lang/String;", null, null);
    fv.visitEnd();
    fv = cw.visitField(ACC_PUBLIC + ACC_STATIC, "token", "J", null, null);
    fv.visitEnd();
    fv = cw.visitField(ACC_PUBLIC + ACC_STATIC, "usingApkSplits", "Z", null, null);
    fv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    mv.visitInsn(RETURN);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", "L" + appInfoOwner + ";", null, l0, l1, 0);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
    mv.visitCode();
    mv.visitLdcInsn(applicationId);
    mv.visitFieldInsn(PUTSTATIC, appInfoOwner, "applicationId", "Ljava/lang/String;");
    if (applicationClass != null) {
        mv.visitLdcInsn(applicationClass);
    } else {
        mv.visitInsn(ACONST_NULL);
    }
    mv.visitFieldInsn(PUTSTATIC, appInfoOwner, "applicationClass", "Ljava/lang/String;");
    if (token != 0L) {
        mv.visitLdcInsn(token);
    } else {
        mv.visitInsn(LCONST_0);
    }
    mv.visitFieldInsn(PUTSTATIC, appInfoOwner, "token", "J");
    if (isUsingMultiApks()) {
        mv.visitInsn(ICONST_1);
    } else {
        mv.visitInsn(ICONST_0);
    }
    mv.visitFieldInsn(PUTSTATIC, appInfoOwner, "usingApkSplits", "Z");

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

    byte[] bytes = cw.toByteArray();

    try (JarOutputStream outputStream = new JarOutputStream(
            new BufferedOutputStream(new FileOutputStream(getOutputFile())))) {
        outputStream.putNextEntry(new ZipEntry("com/android/tools/fd/runtime/AppInfo.class"));
        outputStream.write(bytes);
        outputStream.closeEntry();
    }
}

From source file:com.android.build.gradle.tasks.ir.GenerateInstantRunAppInfoTask.java

License:Apache License

void writeAppInfoClass(@NonNull String applicationId, long token) throws IOException {
    ClassWriter cw = new ClassWriter(0);
    FieldVisitor fv;/*from ww w . j  a  va2s .  c  o  m*/
    MethodVisitor mv;

    String appInfoOwner = SERVER_PACKAGE + "/AppInfo";
    cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, appInfoOwner, null, "java/lang/Object", null);

    fv = cw.visitField(ACC_PUBLIC + ACC_STATIC, "applicationId", "Ljava/lang/String;", null, null);
    fv.visitEnd();
    fv = cw.visitField(ACC_PUBLIC + ACC_STATIC, "token", "J", null, null);
    fv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    mv.visitInsn(RETURN);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", "L" + appInfoOwner + ";", null, l0, l1, 0);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
    mv.visitCode();
    mv.visitLdcInsn(applicationId);
    mv.visitFieldInsn(PUTSTATIC, appInfoOwner, "applicationId", "Ljava/lang/String;");
    if (token != 0L) {
        mv.visitLdcInsn(token);
    } else {
        mv.visitInsn(LCONST_0);
    }
    mv.visitFieldInsn(PUTSTATIC, appInfoOwner, "token", "J");

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

    byte[] bytes = cw.toByteArray();

    try (JarOutputStream outputStream = new JarOutputStream(
            new BufferedOutputStream(new FileOutputStream(getOutputFile())))) {
        outputStream.putNextEntry(new ZipEntry(SERVER_PACKAGE + "/AppInfo.class"));
        outputStream.write(bytes);
        outputStream.closeEntry();
    }
}

From source file:com.android.builder.shrinker.TestClasses.java

License:Apache License

public static byte[] emptyClass(String name) throws Exception {

    ClassWriter cw = new ClassWriter(0);
    FieldVisitor fv;//ww w  . java 2  s.com
    MethodVisitor mv;
    AnnotationVisitor av0;

    cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, "test/" + name, null, "java/lang/Object", null);

    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}

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

License:Apache License

static void branch(MethodVisitor method, Context context, UserOperator operator, LocalVarRef input,
        Map<OperatorProperty, FieldRef> dependencies) {
    OperatorOutput[] outputs = outputs(context, operator);
    Label[] caseLabels = Stream.of(outputs).map(o -> new Label()).toArray(Label[]::new);
    Label defaultLabel = new Label();
    Label endLabel = new Label();

    method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(Enum.class).getInternalName(), "ordinal",
            Type.getMethodDescriptor(Type.INT_TYPE), false);
    method.visitTableSwitchInsn(0, caseLabels.length - 1, defaultLabel, caseLabels);

    for (int i = 0; i < outputs.length; i++) {
        method.visitLabel(caseLabels[i]);
        FieldRef ref = Invariants.requireNonNull(dependencies.get(outputs[i]));
        ref.load(method);/*  www .  j a v a2s.  co  m*/
        input.load(method);
        invokeResultAdd(method);
        method.visitJumpInsn(Opcodes.GOTO, endLabel);
    }
    method.visitLabel(defaultLabel);
    getNew(method, Descriptions.typeOf(AssertionError.class));
    method.visitInsn(Opcodes.ATHROW);

    method.visitLabel(endLabel);
}

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

License:Apache License

private static void defineSelection(Context context, ClassWriter writer, UserOperator operator, FieldRef impl,
        Map<OperatorProperty, FieldRef> dependencies) {
    Method selector = Invariants.safe(() -> {
        return MasterJoinOperatorUtil.getSelection(context.getClassLoader(), operator);
    });//from w ww .j  av  a2 s  .  com
    if (selector == null) {
        return;
    }
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PROTECTED | Opcodes.ACC_FINAL, "selectMaster",
            Type.getMethodDescriptor(typeOf(Object.class), typeOf(List.class), typeOf(Object.class)), null,
            null);
    cast(method, 2, MasterJoinOperatorUtil.getTransactionInput(operator).getDataType());

    List<ValueRef> arguments = new ArrayList<>();
    impl.load(method);
    arguments.add(new LocalVarRef(Opcodes.ALOAD, 1));
    arguments.add(new LocalVarRef(Opcodes.ALOAD, 2));
    arguments
            .addAll(Lang.project(getExtraViews(operator), v -> Invariants.requireNonNull(dependencies.get(v))));
    arguments
            .addAll(Lang.project(operator.getArguments(), v -> Invariants.requireNonNull(dependencies.get(v))));
    for (int i = 0, n = selector.getParameterCount(); i < n; i++) {
        arguments.get(i).load(method);
    }
    method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(selector.getDeclaringClass()).getInternalName(),
            selector.getName(), Type.getMethodDescriptor(selector), false);
    method.visitInsn(Opcodes.ARETURN);
    method.visitMaxs(0, 0);
    method.visitEnd();
}

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

License:Apache License

private static void performJoin(MethodVisitor method, Context context, UserOperator operator,
        LocalVarRef masterRef, LocalVarRef txRef, FieldRef impl, Map<OperatorProperty, FieldRef> dependencies,
        ClassDescription target) {//  ww  w  .  j  a v a2 s  . com
    OperatorInput master = operator.getInput(MasterJoin.ID_INPUT_MASTER);
    OperatorInput tx = operator.getInput(MasterJoin.ID_INPUT_TRANSACTION);
    OperatorOutput joined = operator.getOutput(MasterJoin.ID_OUTPUT_JOINED);

    method.visitVarInsn(Opcodes.ALOAD, 0);
    getField(method, target, FIELD_BUFFER, typeOf(joined.getDataType()));
    LocalVarRef bufferVar = putLocalVar(method, Type.OBJECT, 3);

    bufferVar.load(method);
    method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(joined.getDataType()).getInternalName(), "reset",
            Type.getMethodDescriptor(Type.VOID_TYPE), false);

    List<PropertyMapping> mappings = Invariants
            .safe(() -> JoinedModelUtil.getPropertyMappings(context.getClassLoader(), operator));
    Map<OperatorInput, ValueRef> inputs = new HashMap<>();
    Map<OperatorOutput, ValueRef> outputs = new HashMap<>();
    inputs.put(master, masterRef);
    inputs.put(tx, txRef);
    outputs.put(joined, bufferVar);
    mapping(method, context.getDataModelLoader(), mappings, inputs, outputs);
    bufferVar.load(method);
}

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

License:Apache License

private static void countMapper(MethodVisitor method, PropertyReference src, PropertyReference dst,
        LocalVarRef srcVar, LocalVarRef dstVar) {
    Invariants.require(dst.getType().equals(LONG_DESC));
    dstVar.load(method);//from   w  w  w.  java 2s .co m
    getOption(method, dst);
    getConst(method, 1L);
    method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(LONG_DESC).getInternalName(), "modify",
            Type.getMethodDescriptor(typeOf(LONG_DESC), Type.LONG_TYPE), false);
}

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

License:Apache License

private static void sumMapper(MethodVisitor method, ClassDescription target, PropertyReference src,
        PropertyReference dst, LocalVarRef srcVar, LocalVarRef dstVar, Set<PropertyReference> nullChecked) {
    dstVar.load(method);// ww  w.j  a  v a  2 s . c o  m
    getOption(method, dst);
    srcVar.load(method);
    getOption(method, src);
    checkNull(method, target, src, srcVar, nullChecked);
    TypeDescription srcOptionType = src.getType();
    Type srcEntityType = Invariants.requireNonNull(ENTITY_TYPE_MAP.get(srcOptionType));
    method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(srcOptionType).getInternalName(), "get",
            Type.getMethodDescriptor(srcEntityType), false);
    if (srcOptionType.equals(BYTE_DESC) || srcOptionType.equals(SHORT_DESC) || srcOptionType.equals(INT_DESC)) {
        method.visitInsn(Opcodes.I2L);
    } else if (srcOptionType.equals(FLOAT_DESC)) {
        method.visitInsn(Opcodes.F2D);
    }
    TypeDescription dstOptionType = dst.getType();
    Type dstEntityType = Invariants.requireNonNull(ENTITY_TYPE_MAP.get(dstOptionType));
    method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, typeOf(dstOptionType).getInternalName(), "modify",
            Type.getMethodDescriptor(typeOf(dstOptionType), dstEntityType), false);
}