当前位置: 首页>>代码示例>>Java>>正文


Java ClassWriter.visitMethod方法代码示例

本文整理汇总了Java中jdk.internal.org.objectweb.asm.ClassWriter.visitMethod方法的典型用法代码示例。如果您正苦于以下问题:Java ClassWriter.visitMethod方法的具体用法?Java ClassWriter.visitMethod怎么用?Java ClassWriter.visitMethod使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在jdk.internal.org.objectweb.asm.ClassWriter的用法示例。


在下文中一共展示了ClassWriter.visitMethod方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: genModuleDescriptorsMethod

import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
/**
 * Generate bytecode for moduleDescriptors method
 */
private void genModuleDescriptorsMethod(ClassWriter cw) {
    this.mv = cw.visitMethod(ACC_PUBLIC,
                             "moduleDescriptors",
                             "()" + MODULE_DESCRIPTOR_ARRAY_SIGNATURE,
                             "()" + MODULE_DESCRIPTOR_ARRAY_SIGNATURE,
                             null);
    mv.visitCode();
    pushInt(mv, moduleInfos.size());
    mv.visitTypeInsn(ANEWARRAY, "java/lang/module/ModuleDescriptor");
    mv.visitVarInsn(ASTORE, MD_VAR);

    for (int index = 0; index < moduleInfos.size(); index++) {
        ModuleInfo minfo = moduleInfos.get(index);
        new ModuleDescriptorBuilder(minfo.descriptor(),
                                    minfo.packages(),
                                    index).build();
    }
    mv.visitVarInsn(ALOAD, MD_VAR);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:26,代码来源:SystemModulesPlugin.java

示例2: genIncubatorModules

import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
/**
 * Generate bytecode for hasIncubatorModules method
 */
private void genIncubatorModules(ClassWriter cw) {
    boolean hasIncubatorModules = moduleInfos.stream()
            .map(ModuleInfo::moduleResolution)
            .filter(mres -> (mres != null && mres.hasIncubatingWarning()))
            .findFirst()
            .isPresent();

    mv = cw.visitMethod(ACC_PUBLIC,
                        "hasIncubatorModules",
                        "()Z",
                        "()Z",
                        null);
    mv.visitCode();
    if (hasIncubatorModules) {
        mv.visitInsn(ICONST_1);
    } else {
        mv.visitInsn(ICONST_0);
    }
    mv.visitInsn(IRETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:26,代码来源:SystemModulesPlugin.java

示例3: getAnonClass

import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
static Class<?> getAnonClass(Class<?> hostClass, final String className) {
    final String superName = "java/lang/Object";
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
    cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER, className, null, superName, null);

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_STATIC | Opcodes.ACC_PUBLIC, "test", "()V", null, null);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "p1/T", "test0", "()V", false);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "p1/T", "test1", "()V", false);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "p1/T", "test2", "()V", false);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "p1/T", "test3", "()V", false);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();

    final byte[] classBytes = cw.toByteArray();
    Class<?> invokerClass = UNSAFE.defineAnonymousClass(hostClass, classBytes, new Object[0]);
    UNSAFE.ensureClassInitialized(invokerClass);
    return invokerClass;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:20,代码来源:DefineAnon.java

示例4: verifyASM

import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
static void verifyASM() throws Exception {
    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_8, ACC_PUBLIC, "X", null, "java/lang/Object", null);
    MethodVisitor mv = cw.visitMethod(ACC_STATIC, "foo",
            "()V", null, null);
    mv.visitMaxs(2, 1);
    mv.visitMethodInsn(INVOKESTATIC,
            "java/util/function/Function.class",
            "identity", "()Ljava/util/function/Function;", true);
    mv.visitInsn(RETURN);
    cw.visitEnd();
    byte[] carray = cw.toByteArray();
    // for debugging
    // write((new File("X.class")).toPath(), carray, CREATE, TRUNCATE_EXISTING);

    // verify using javap/classfile reader
    ClassFile cf = ClassFile.read(new ByteArrayInputStream(carray));
    int mcount = checkMethod(cf, "foo");
    if (mcount < 1) {
        throw new RuntimeException("unexpected method count, expected 1" +
                "but got " + mcount);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:24,代码来源:LambdaAsm.java

示例5: genHasSplitPackages

import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
/**
 * Generate bytecode for hasSplitPackages method
 */
private void genHasSplitPackages(ClassWriter cw) {
    boolean distinct = moduleInfos.stream()
            .map(ModuleInfo::packages)
            .flatMap(Set::stream)
            .allMatch(new HashSet<>()::add);
    boolean hasSplitPackages = !distinct;

    mv = cw.visitMethod(ACC_PUBLIC,
                        "hasSplitPackages",
                        "()Z",
                        "()Z",
                        null);
    mv.visitCode();
    if (hasSplitPackages) {
        mv.visitInsn(ICONST_1);
    } else {
        mv.visitInsn(ICONST_0);
    }
    mv.visitInsn(IRETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:26,代码来源:SystemModulesPlugin.java

示例6: writeClassFile

import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
public static void writeClassFile() throws Exception {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    cw.visit(99, ACC_PUBLIC + ACC_SUPER, "ClassFile", 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();

    try (FileOutputStream fos = new FileOutputStream(new File("ClassFile.class"))) {
         fos.write(cw.toByteArray());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:19,代码来源:UnsupportedClassFileVersion.java

示例7: dumpT2

import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
public static byte[] dumpT2() {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    cw.visit(52, ACC_PUBLIC | ACC_SUPER, "p1/T2", null, "p1/T1", null);
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "p1/T1", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "m", "()I", null, null);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}
 
开发者ID:campolake,项目名称:openjdk9,代码行数:23,代码来源:TestConcreteClassWithAbstractMethod.java

示例8: bytesForD

import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
/**
 * The bytes for D, a NOT abstract class extending abstract class C without
 * supplying an implementation for abstract method m. There is a default
 * method in the interface I, but it should lose to the abstract class.
 *
 * @return
 * @throws Exception
 */
public static byte[] bytesForD() throws Exception {

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES
            | ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;

    cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, "p/D", null, "p/C", null);

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

    return cw.toByteArray();
}
 
开发者ID:ojdkbuild,项目名称:lookaside_java-1.8.0-openjdk,代码行数:30,代码来源:TestAMEnotNPE.java

示例9: generateClassWithInitializer

import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
/**
 * Generate a class file with the given class name. The class will initializer
 * to invokestatic the given targetClass/targetMethod.
 */
byte[] generateClassWithInitializer(String className,
                                    String targetClass,
                                    String targetMethod) throws Exception {

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
                                     + ClassWriter.COMPUTE_FRAMES);
    cw.visit(V1_9,
            ACC_PUBLIC + ACC_SUPER,
            className.replace(".", "/"),
            null,
            "java/lang/Object",
            null);

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

    // <clinit>
    String tc = targetClass.replace(".", "/");
    mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
    mv.visitMethodInsn(INVOKESTATIC, tc, targetMethod, "()V", false);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();

    cw.visitEnd();
    return cw.toByteArray();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:37,代码来源:DefineClassTest.java

示例10: generateInvokerTemplate

import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
/** Produces byte code for a class that is used as an injected invoker. */
private static byte[] generateInvokerTemplate() {
    ClassWriter cw = new ClassWriter(0);

    // private static class InjectedInvoker {
    //     @Hidden
    //     static Object invoke_V(MethodHandle vamh, Object[] args) throws Throwable {
    //        return vamh.invokeExact(args);
    //     }
    // }
    cw.visit(52, ACC_PRIVATE | ACC_SUPER, "InjectedInvoker", null, "java/lang/Object", null);

    MethodVisitor mv = cw.visitMethod(ACC_STATIC, "invoke_V",
                  "(Ljava/lang/invoke/MethodHandle;[Ljava/lang/Object;)Ljava/lang/Object;",
                  null, null);

    // Suppress invoker method in stack traces.
    AnnotationVisitor av0 = mv.visitAnnotation("Ljava/lang/invoke/LambdaForm$Hidden;", true);
    av0.visitEnd();

    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invokeExact",
                       "([Ljava/lang/Object;)Ljava/lang/Object;", false);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();

    cw.visitEnd();
    return cw.toByteArray();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:33,代码来源:MethodHandleImpl.java

示例11: generateConstructor

import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
private static void generateConstructor(ClassWriter classWriter) {
    MethodVisitor mv = classWriter.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(0, 0);
    mv.visitEnd();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:15,代码来源:TestOSRWithNonEmptyStack.java

示例12: dumpT1

import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
public static byte[] dumpT1() {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    cw.visit(52, ACC_PUBLIC | ACC_SUPER, "p1/T1", 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();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "m", "()I", null, null);
        mv.visitCode();
        mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
        mv.visitLdcInsn("p1/T1.m()");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V", false);
        mv.visitIntInsn(BIPUSH, 3);
        mv.visitInsn(IRETURN);
        mv.visitMaxs(2, 1);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:30,代码来源:TestConcreteClassWithAbstractMethod.java

示例13: defineIndyBootstrapMethodClass

import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
void defineIndyBootstrapMethodClass(ClassWriter cw) {
    cw.visit(52, ACC_SUPER | ACC_PUBLIC,
             BOOTSTRAP_METHOD_CLASS_NAME, null, "java/lang/Object", null);
    // Bootstrap method is declared to be private
    MethodVisitor mv = cw.visitMethod(ACC_PRIVATE | ACC_STATIC,
                                      BOOTSTRAP_METHOD_NAME, BOOTSTRAP_METHOD_DESC, null, null);
    mv.visitCode();
    defineIndyBootstrapMethodBody(mv);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:12,代码来源:BootstrapMethodErrorTest.java

示例14: generateTestMethod

import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
private static void generateTestMethod(ClassWriter classWriter) {
    MethodVisitor mv = classWriter.visitMethod(ACC_PUBLIC,
            TestOSRWithNonEmptyStack.METHOD_NAME, "()V", null, null);
    Label osrEntryPoint = new Label();

    mv.visitCode();
    // Push 'this' into stack before OSR entry point to bail out compilation
    mv.visitVarInsn(ALOAD, 0);
    // Setup loop counter
    mv.visitInsn(ICONST_0);
    mv.visitVarInsn(ISTORE, 1);
    // Begin loop
    mv.visitLabel(osrEntryPoint);
    // Increment loop counter
    mv.visitVarInsn(ILOAD, 1);
    mv.visitInsn(ICONST_1);
    mv.visitInsn(IADD);
    // Duplicate it for loop condition check
    mv.visitInsn(DUP);
    mv.visitVarInsn(ISTORE, 1);
    // Check loop condition
    mv.visitLdcInsn(TestOSRWithNonEmptyStack.ITERATIONS);
    mv.visitJumpInsn(IF_ICMPLT, osrEntryPoint);
    // Pop 'this'.
    mv.visitInsn(POP);
    mv.visitInsn(RETURN);

    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:31,代码来源:TestOSRWithNonEmptyStack.java

示例15: genModuleHashesMethod

import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
/**
 * Generate bytecode for moduleHashes method
 */
private void genModuleHashesMethod(ClassWriter cw) {
    MethodVisitor hmv =
        cw.visitMethod(ACC_PUBLIC,
                       "moduleHashes",
                       "()" + MODULE_HASHES_ARRAY_SIGNATURE,
                       "()" + MODULE_HASHES_ARRAY_SIGNATURE,
                       null);
    hmv.visitCode();
    pushInt(hmv, moduleInfos.size());
    hmv.visitTypeInsn(ANEWARRAY, "jdk/internal/module/ModuleHashes");
    hmv.visitVarInsn(ASTORE, MH_VAR);

    for (int index = 0; index < moduleInfos.size(); index++) {
        ModuleInfo minfo = moduleInfos.get(index);
        if (minfo.recordedHashes() != null) {
            new ModuleHashesBuilder(minfo.recordedHashes(),
                                    index,
                                    hmv).build();
        }
    }

    hmv.visitVarInsn(ALOAD, MH_VAR);
    hmv.visitInsn(ARETURN);
    hmv.visitMaxs(0, 0);
    hmv.visitEnd();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:30,代码来源:SystemModulesPlugin.java


注:本文中的jdk.internal.org.objectweb.asm.ClassWriter.visitMethod方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。