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


Java ClassWriter.toByteArray方法代码示例

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


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

示例1: getClassFile

import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
static byte[] getClassFile(String suffix) {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;
    cw.visit(52, ACC_PUBLIC | ACC_SUPER, CLASS_NAME + suffix, null, "java/lang/Object", null);
    {
        mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, METHOD_NAME, TYPE.toMethodDescriptorString(), null, null);
        mv.visitCode();
        Handle bsm = new Handle(H_INVOKESTATIC,
                CallSiteDepContextTest.class.getName().replace(".", "/"),
                "bootstrap",
                bsmMH.type().toMethodDescriptorString());
        mv.visitInvokeDynamicInsn("methodName", TYPE.toMethodDescriptorString(), bsm);
        mv.visitInsn(IRETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    cw.visitEnd();
    return cw.toByteArray();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:20,代码来源:CallSiteDepContextTest.java

示例2: testGetModuleOnVMAnonymousClass

import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
/**
 * Exercise Class::getModule on VM anonymous classes
 */
@Test(dataProvider = "hostclasses")
public void testGetModuleOnVMAnonymousClass(Class<?> hostClass, String ignore) {

    // choose a class name in the same package as the host class
    String prefix = hostClass.getPackageName();
    if (prefix.length() > 0)
        prefix = prefix.replace('.', '/') + "/";
    String className = prefix + "Anon";

    // create the class
    String superName = "java/lang/Object";
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
                                     + ClassWriter.COMPUTE_FRAMES);
    cw.visit(V1_8, ACC_PUBLIC + ACC_FINAL + ACC_SUPER,
             className, null, superName, null);
    byte[] classBytes = cw.toByteArray();
    int cpPoolSize = constantPoolSize(classBytes);
    Class<?> anonClass
        = U.defineAnonymousClass(hostClass, classBytes, new Object[cpPoolSize]);

    assertTrue(anonClass.getModule() == hostClass.getModule());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:26,代码来源:GetModuleTest.java

示例3: getClassFile

import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
/**
 * Generates a class of the following shape:
 *     static class T {
 *         @DontInline public static int f() { return $r; }
 *     }
 */
static byte[] getClassFile(int r) {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;
    cw.visit(52, ACC_PUBLIC | ACC_SUPER, NAME, null, "java/lang/Object", null);
    {
        mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "f", "()I", null, null);
        mv.visitAnnotation("Ljdk/internal/vm/annotation/DontInline;", true);
        mv.visitCode();
        mv.visitLdcInsn(r);
        mv.visitInsn(IRETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    cw.visitEnd();
    return cw.toByteArray();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:23,代码来源:RedefineTest.java

示例4: badHostClass

import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
static public void badHostClass(Class<?> hostClass) {
    // choose a class name in the same package as the host class
    String className;
    if (hostClass != null) {
        String prefix = packageName(hostClass);
        if (prefix.length() > 0)
            prefix = prefix.replace('.', '/') + "/";
        className = prefix + "Anon";
    } else {
       className = "Anon";
    }

    // create the class
    String superName = "java/lang/Object";
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
                                     + ClassWriter.COMPUTE_FRAMES);
    cw.visit(V1_8, ACC_PUBLIC + ACC_FINAL + ACC_SUPER,
             className, null, superName, null);
    byte[] classBytes = cw.toByteArray();
    int cpPoolSize = constantPoolSize(classBytes);
    Class<?> anonClass
        = unsafe.defineAnonymousClass(hostClass, classBytes, new Object[cpPoolSize]);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:24,代码来源:TestBadHostClass.java

示例5: 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

示例6: 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:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:30,代码来源:TestAMEnotNPE.java

示例7: addClass

import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
/**
 * Add a class to the pool, if a class already exists, it is replaced.
 *
 * @param writer The class writer.
 * @throws java.io.IOException
 */
@Override
public void addClass(ClassWriter writer) {
    Objects.requireNonNull(writer);
    // Retrieve the className
    ClassReader reader = newClassReader(writer.toByteArray());
    String className = reader.getClassName();
    String path;
    if (className.endsWith("module-info")) {
        // remove the module name contained in the class name
        className = className.substring(className.indexOf("/") + 1);
        path = "/" + moduleName + "/" + className;
    } else {
        path = toClassNamePath(className);
    }

    byte[] content = writer.toByteArray();
    ModuleEntry res = ModuleEntry.create(path,
            new ByteArrayInputStream(content), content.length);
    transformedClasses.put(className, res);
}
 
开发者ID:campolake,项目名称:openjdk9,代码行数:27,代码来源:AsmPoolImpl.java

示例8: 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

示例9: loadClassData

import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
private byte[] loadClassData(String name) throws Exception {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;
    switch (name) {
        case INTERFACE_NAME:
            cw.visit(V1_8, ACC_ABSTRACT | ACC_INTERFACE | ACC_PUBLIC, INTERFACE_NAME, null, "java/lang/Object", null);
            {
                mv = cw.visitMethod(ACC_PRIVATE, "privInstance", "()I", null, null);
                mv.visitCode();
                mv.visitLdcInsn(EXPECTED);
                mv.visitInsn(IRETURN);
                mv.visitMaxs(1, 1);
                mv.visitEnd();
            }
            break;
        case CLASS_NAME:
            cw.visit(52, ACC_SUPER | ACC_PUBLIC, CLASS_NAME, null, "java/lang/Object", new String[]{INTERFACE_NAME});
            {
                mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
                mv.visitCode();
                mv.visitVarInsn(ALOAD, 0);
                mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
                mv.visitInsn(RETURN);
                mv.visitMaxs(1, 1);
                mv.visitEnd();
            }
            break;
        default:
            break;
    }
    cw.visitEnd();

    return cw.toByteArray();
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:35,代码来源:TestPrivateInterfaceMethodReflect.java

示例10: toModuleInfo

import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
/**
 * Writes the given module descriptor to a module-info.class file,
 * returning it in a byte array.
 */
private static byte[] toModuleInfo(ModuleDescriptor md, ModuleTarget target) {
    ClassWriter cw = new ClassWriter(0);
    cw.visit(Opcodes.V1_9, ACC_MODULE, "module-info", null, null, null);
    cw.visitAttribute(new ModuleAttribute(md));

    // for tests: write the ModulePackages attribute when there are packages
    // that aren't exported or open
    Stream<String> exported = md.exports().stream()
            .map(ModuleDescriptor.Exports::source);
    Stream<String> open = md.opens().stream()
            .map(ModuleDescriptor.Opens::source);
    long exportedOrOpen = Stream.concat(exported, open).distinct().count();
    if (md.packages().size() > exportedOrOpen)
        cw.visitAttribute(new ModulePackagesAttribute(md.packages()));

    // write ModuleMainClass if the module has a main class
    md.mainClass().ifPresent(mc -> cw.visitAttribute(new ModuleMainClassAttribute(mc)));

    // write ModuleTarget if there is a target platform
    if (target != null) {
        cw.visitAttribute(new ModuleTargetAttribute(target.targetPlatform()));
    }

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

示例11: generateCodeBytesForLFs

import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
private static byte[] generateCodeBytesForLFs(String className,
        String[] names, LambdaForm[] forms) {

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
    cw.visit(Opcodes.V1_8, Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER,
            className, null, InvokerBytecodeGenerator.INVOKER_SUPER_NAME, null);
    cw.visitSource(className.substring(className.lastIndexOf('/') + 1), null);

    for (int i = 0; i < forms.length; i++) {
        addMethod(className, names[i], forms[i],
                forms[i].methodType(), cw);
    }
    return cw.toByteArray();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:15,代码来源:GenerateJLIClassesHelper.java

示例12: generateRunner

import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
/**
 * Generate a class file with the given class name. The class implements Runnable
 * with a run method to invokestatic the given targetClass/targetMethod.
 */
byte[] generateRunner(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",
            new String[] { "java/lang/Runnable" });

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

    // run()
    String tc = targetClass.replace(".", "/");
    mv = cw.visitMethod(ACC_PUBLIC, "run", "()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

示例13: loadClassData

import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
private byte[] loadClassData(String name) throws Exception {
    ClassWriter cw = new ClassWriter(
            ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    if (name.equals(BOOTSTRAP_METHOD_CLASS_NAME)) {
        defineIndyBootstrapMethodClass(cw);
        return cw.toByteArray();
    }
    else if (name.equals("Exec")) {
        defineIndyCallingClass(cw);
        return cw.toByteArray();
    }
    return null;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:14,代码来源:BootstrapMethodErrorTest.java

示例14: 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

示例15: generateTestClass

import jdk.internal.org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
private static byte[] generateTestClass() {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);

    cw.visit(TestOSRWithNonEmptyStack.CLASS_FILE_VERSION, ACC_PUBLIC,
            TestOSRWithNonEmptyStack.CLASS_NAME, null, "java/lang/Object",
            null);

    TestOSRWithNonEmptyStack.generateConstructor(cw);
    TestOSRWithNonEmptyStack.generateTestMethod(cw);

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


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