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


Java ClassWriter类代码示例

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


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

示例1: ClassVisitorForLabels

import jdk.internal.org.objectweb.asm.ClassWriter; //导入依赖的package包/类
public ClassVisitorForLabels(ClassWriter cw, Map<Label, Integer> lines,
                             Executable target) {
    super(Opcodes.ASM5, cw);
    this.lineNumbers = lines;

    StringBuilder builder = new StringBuilder("(");
    for (Parameter parameter : target.getParameters()) {
        builder.append(Utils.toJVMTypeSignature(parameter.getType()));
    }
    builder.append(")");
    if (target instanceof Constructor) {
        targetName = "<init>";
        builder.append("V");
    } else {
        targetName = target.getName();
        builder.append(Utils.toJVMTypeSignature(
                ((Method) target).getReturnType()));
    }
    targetDesc = builder.toString();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:CTVMUtilities.java

示例2: 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:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:24,代码来源:LambdaAsm.java

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

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

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

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

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

示例9: write

import jdk.internal.org.objectweb.asm.ClassWriter; //导入依赖的package包/类
@Override
protected ByteVector write(ClassWriter cw,
                           byte[] code,
                           int len,
                           int maxStack,
                           int maxLocals)
{
    assert packages != null;

    ByteVector attr = new ByteVector();

    // package_count
    attr.putShort(packages.size());

    // packages
    packages.stream()
        .map(p -> p.replace('.', '/'))
        .forEach(p -> attr.putShort(cw.newPackage(p)));

    return attr;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:22,代码来源:ClassFileAttributes.java

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

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

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

示例13: transform

import jdk.internal.org.objectweb.asm.ClassWriter; //导入依赖的package包/类
@Override
public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {
    //remove *.diz files as well as debug attributes.
    in.transformAndCopy((resource) -> {
        ResourcePoolEntry res = resource;
        if (resource.type().equals(ResourcePoolEntry.Type.CLASS_OR_RESOURCE)) {
            String path = resource.path();
            if (path.endsWith(".class")) {
                if (path.endsWith("module-info.class")) {
                    // XXX. Do we have debug info? Is Asm ready for module-info?
                } else {
                    ClassReader reader = new ClassReader(resource.contentBytes());
                    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
                    reader.accept(writer, ClassReader.SKIP_DEBUG);
                    byte[] content = writer.toByteArray();
                    res = resource.copyWithContent(content);
                }
            }
        } else if (predicate.test(res.path())) {
            res = null;
        }
        return res;
    }, out);

    return out.build();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:27,代码来源:StripDebugPlugin.java

示例14: generateClass

import jdk.internal.org.objectweb.asm.ClassWriter; //导入依赖的package包/类
/**
 * Generates a class file with the given class name
 */
byte[] generateClass(String className) {
    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();

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

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


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