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


Java ClassWriter.toByteArray方法代码示例

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


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

示例1: handler

import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
@Override
public byte[] handler(byte[] pClass) {
    ClassReader cr=new ClassReader(pClass);
    ClassNode cn=new ClassNode();
    cr.accept(cn, 0);
    for (MethodNode mv:cn.methods){
        if(mv.name.equals("accept")){
            AbstractInsnNode node=mv.instructions.getFirst();
            do{
                if(node instanceof MethodInsnNode){
                    if(node.getOpcode()==INVOKESTATIC){
                        if(((MethodInsnNode) node).name.equals("revertToFrozen")){
                            mv.instructions.remove(node);
                            break;
                        }
                    }
                }
            }while ((node=node.getNext())!=null);
            break;
        }
    }
    ClassWriter cw=new ClassWriter(0);
    cn.accept(cw);
    return cw.toByteArray();
}
 
开发者ID:xjboss,项目名称:MCRenderCrashFix,代码行数:26,代码来源:FMLHS$7Handler.java

示例2: transform

import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
    List<ITransformer> transformers = getTransformers(transformedName);

    if (!transformers.isEmpty()) {
        ClassNode cn = getClassNode(basicClass);
        if (cn == null)
            return basicClass;

        // Run all transformers on the Class
        transformers.forEach(transformer -> transformer.transform(cn));

        // Return transformed class bytecode
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
        cn.accept(cw);
        return cw.toByteArray();
    }

    return basicClass;
}
 
开发者ID:ImpactDevelopment,项目名称:ClientAPI,代码行数:21,代码来源:ClientTransformer.java

示例3: run

import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
@Override
public void run() {
    if (!hasWorkingJavac()) {
        ClassWriter w = new ClassWriter(0);
        w.visit(Opcodes.V1_8, Opcodes.ACC_ABSTRACT | Opcodes.ACC_PUBLIC, "com/sun/tools/javac/code/Scope$WriteableScope", null, "com/sun/tools/javac/code/Scope", null);
        byte[] classData = w.toByteArray();
        try {
            Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
            theUnsafe.setAccessible(true);
            Unsafe unsafe = (Unsafe) theUnsafe.get(null);
            Class scopeClass = Class.forName("com.sun.tools.javac.code.Scope");
            unsafe.defineClass("com.sun.tools.javac.code.Scope$WriteableScope", classData, 0, classData.length, scopeClass.getClassLoader(), scopeClass.getProtectionDomain());
        } catch (Throwable t) {
            //ignore...
            Logger.getLogger(NoJavacHelper.class.getName()).log(Level.FINE, null, t);
        }
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:19,代码来源:NoJavacHelper.java

示例4: write

import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
@Override
public void write(File file) throws IOException {
    JarOutputStream jos = new JarOutputStream(new FileOutputStream(file));
    try {
        for (Iterator<ClassNode> itr = iterator(); itr.hasNext(); ) {
            ClassNode   node   = itr.next();
            ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
            node.accept(new CheckClassAdapter(writer, true));
            byte[] bytes = writer.toByteArray();
            jos.putNextEntry(new JarEntry(node.name.concat(".class")));
            jos.write(bytes);
        }
    } finally {
        jos.close();
    }
}
 
开发者ID:Parabot,项目名称:Parabot-317-API-Minified-OS-Scape,代码行数:17,代码来源:JarArchive.java

示例5: generateClass

import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
public static byte[] generateClass() {

            ClassWriter cw = new ClassWriter(0);

            cw.visit(52, ACC_SUPER | ACC_PUBLIC, INNER_CLASS_NAME_INTERNAL, null, "java/lang/Object", null);

            cw.visitSource("UnbalancedMonitorsTest.java", null);

            cw.visitInnerClass(INNER_CLASS_NAME_INTERNAL, CLASS_NAME_INTERNAL, "UnbalancedMonitors", ACC_STATIC);

            visitConstructor(cw);
            visitWrongOrder(cw);
            visitBlockStructured(cw, true, false);
            visitBlockStructured(cw, true, true);
            visitBlockStructured(cw, false, false);
            visitBlockStructured(cw, false, true);
            cw.visitEnd();

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

示例6: classNodeToBytes

import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
private byte[] classNodeToBytes(ClassNode classNode) {
  ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
  classNode.accept(writer);
  byte[] bytes = writer.toByteArray();

  // verify bytecode

  if (verifyAndPrint) {
    ClassReader reader = new ClassReader(bytes);
    ClassVisitor tracer = new TraceClassVisitor(new PrintWriter(System.out));
    ClassVisitor checker = new CheckClassAdapter(tracer, true);
    reader.accept(checker, 0);
  }

  return bytes;
}
 
开发者ID:kroepke,项目名称:luna,代码行数:17,代码来源:ASMBytecodeEmitter.java

示例7: extractApiClassFrom

import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
/**
 * Extracts an API class from a given original class.
 *
 * @param originalClassReader the reader containing the original class
 * @return bytecode of the API class extracted from the original class
 */
public byte[] extractApiClassFrom(ClassReader originalClassReader) {
    ClassWriter apiClassWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    originalClassReader.accept(
        new ApiMemberSelector(new MethodStubbingApiMemberAdapter(apiClassWriter), apiIncludesPackagePrivateMembers),
        ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
    return apiClassWriter.toByteArray();
}
 
开发者ID:lxxlxx888,项目名称:Reer,代码行数:14,代码来源:ApiClassExtractor.java

示例8: transformBytes

import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
/**
 * Get the raw bytes of instrumented class with name {@code className}
 *
 * @param classLoader
 * @param className
 * @param reader
 * @return
 */
public byte[] transformBytes(ClassLoader classLoader, ClassName className, ClassReader reader) {
    Objects.requireNonNull(classLoader);
    Objects.requireNonNull(className);
    Objects.requireNonNull(reader);

    if (!canInstrumentForCoverage(className)) {
        throw new IllegalArgumentException("Cannot instrument " + className);
    }


    int asmFlags = ClassWriter.COMPUTE_FRAMES;
    ClassWriter writer = new ComputeClassWriter(asmFlags);
    ClassVisitor cv = writer;

    //this is very we hook our instrumentation
    cv = new CoverageClassVisitor(cv, className);

    //avoid reading frames, as we re-compute them
    int readFlags = ClassReader.SKIP_FRAMES;

    /*
        This is using the "Visitor Pattern".
        In a nutshell, we scan the whole class definition, and at each
        element we "print" in the "writer" buffer.
        On such buffer we also add our instrumentation.
        Then, once we are done traversing the "reader", we convert
        the buffer in the "writer" into byte[] data
     */
    reader.accept(cv, readFlags);

    return writer.toByteArray();
}
 
开发者ID:arcuri82,项目名称:testing_security_development_enterprise_systems,代码行数:41,代码来源:Instrumentator.java

示例9: transform

import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
    if(!name.equals(authClassNameObf) && !name.equals(authClassNameDeobf)) return bytes;

    ClassNode clazz = new ClassNode();
    ClassReader reader = new ClassReader(bytes);
    reader.accept(clazz, 0);

    boolean patched = false;

    for(MethodNode method : clazz.methods) {
        String mn = method.name;
        if(!mn.equals(acceptMethodNameSrg)) {
            if(!mn.equals(acceptMethodNameObf) && !mn.equals(acceptMethodNameDeobf)) continue;
            if(!method.desc.equals(acceptMethodDesc)) continue;
        }

        for(int i = 0; i < method.instructions.size(); i++) {
            AbstractInsnNode node = method.instructions.get(i);
            if(node instanceof LabelNode) {
                patchMethod(clazz, method, (LabelNode)node);
                patched = true;
                break;
            }
        }
    }

    if(!patched) return bytes;

    ClassWriter writer = new ClassWriter(0);
    clazz.accept(writer);
    return writer.toByteArray();
}
 
开发者ID:Guichaguri,项目名称:BOHA,代码行数:34,代码来源:BlockTransformer.java

示例10: transform

import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
@Override
protected byte[] transform(String className, byte[] bytes) {
    ClassReader classReader = new Java9ClassReader(bytes);
    ClassWriter classWriter = new ClassWriter(0);
    classReader.accept(new TransformingAdapter(classWriter), 0);
    bytes = classWriter.toByteArray();
    return bytes;
}
 
开发者ID:lxxlxx888,项目名称:Reer,代码行数:9,代码来源:MixInLegacyTypesClassLoader.java

示例11: transform

import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
@Override
public byte[] transform(String name, String transformedName, byte[] bytes)
{
    if (bytes == null || name.equals("net.minecraftforge.fml.common.eventhandler.Event") || name.startsWith("net.minecraft.") || name.indexOf('.') == -1)
    {
        return bytes;
    }
    ClassReader cr = new ClassReader(bytes);
    ClassNode classNode = new ClassNode();
    cr.accept(classNode, 0);

    try
    {
        if (buildEvents(classNode))
        {
            ClassWriter cw = new ClassWriter(COMPUTE_FRAMES);
            classNode.accept(cw);
            return cw.toByteArray();
        }
        return bytes;
    }
    catch (ClassNotFoundException ex)
    {
        // Discard silently- it's just noise
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return bytes;
}
 
开发者ID:F1r3w477,项目名称:CustomWorldGen,代码行数:33,代码来源:EventSubscriptionTransformer.java

示例12: transform

import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
private static byte[] transform(byte[] serverClass, boolean isObfuscated, transformType type)
{
    System.out.println("MALMO: Attempting to transform MinecraftServer");
    try
    {
        ClassNode cnode = new ClassNode();
        ClassReader creader = new ClassReader(serverClass);
        creader.accept(cnode, 0);
        
        switch (type)
        {
        case SERVER:
            overclockServer(cnode, isObfuscated);
            break;
        case RENDERER:
            overclockRenderer(cnode, isObfuscated);
            break;
        }
        
        ClassWriter cwriter = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
        cnode.accept(cwriter);
        return cwriter.toByteArray();
    }
    catch (Exception e)
    {
        System.out.println("MALMO FAILED to transform MinecraftServer - overclocking not available!");
    }
    return serverClass;
}
 
开发者ID:Yarichi,项目名称:Proyecto-DASI,代码行数:30,代码来源:OverclockingClassTransformer.java

示例13: transform

import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
@Override
public byte[] transform(String name, String realName, byte[] bytes) {
    if (transformingClasses.containsKey(realName)) {
        ClassTransformer transformer = transformingClasses.get(realName);
        try {
            LOGGER.info("Transforming class " + realName);

            ClassNode classNode = new ClassNode();
            ClassReader classReader = new ClassReader(bytes);
            classReader.accept(classNode, 0);

            transformer.transform(classNode);

            ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
            classNode.accept(classWriter);

            // let gc clean this up
            transformingClasses.remove(realName);

            return classWriter.toByteArray();
        } catch (Exception e) {
            LOGGER.error(e.getClass().getSimpleName() + " thrown from transforming class " + realName + ": " + e.getMessage());
            ASMStackLogger.printStackTrace(e);
        }
    }
    return bytes;
}
 
开发者ID:fr1kin,项目名称:ForgeHax,代码行数:28,代码来源:ForgeHaxTransformer.java

示例14: transform

import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
@Override
public byte[] transform(String name, String transformedName, byte[] basicClass)
{
    if (basicClass == null) return null;

    ClassNode classNode = new ClassNode();
    new ClassReader(basicClass).accept(classNode, 0);

    boolean isSubscriber = false;

    for (MethodNode methodNode : classNode.methods)
    {
        List<AnnotationNode> anns = methodNode.visibleAnnotations;

        if (anns != null && Iterables.any(anns, SubscribeEventPredicate.INSTANCE))
        {
            if (Modifier.isPrivate(methodNode.access))
            {
                String msg = "Cannot apply @SubscribeEvent to private method %s/%s%s";
                throw new RuntimeException(String.format(msg, classNode.name, methodNode.name, methodNode.desc));
            }

            methodNode.access = toPublic(methodNode.access);
            isSubscriber = true;
        }
    }

    if (isSubscriber)
    {
        classNode.access = toPublic(classNode.access);

        ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        classNode.accept(writer);
        return writer.toByteArray();
    }

    return basicClass;
}
 
开发者ID:F1r3w477,项目名称:CustomWorldGen,代码行数:39,代码来源:EventSubscriberTransformer.java

示例15: transform

import org.objectweb.asm.ClassWriter; //导入方法依赖的package包/类
@Override
public byte[] transform(String name, String transformedName, byte[] bytes)
{
    if (bytes == null) { return null; }

    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);

    if (remove(classNode.visibleAnnotations, SIDE))
    {
        if (DEBUG)
        {
            System.out.println(String.format("Attempted to load class %s for invalid side %s", classNode.name, SIDE));
        }
        throw new RuntimeException(String.format("Attempted to load class %s for invalid side %s", classNode.name, SIDE));
    }

    Iterator<FieldNode> fields = classNode.fields.iterator();
    while(fields.hasNext())
    {
        FieldNode field = fields.next();
        if (remove(field.visibleAnnotations, SIDE))
        {
            if (DEBUG)
            {
                System.out.println(String.format("Removing Field: %s.%s", classNode.name, field.name));
            }
            fields.remove();
        }
    }
    Iterator<MethodNode> methods = classNode.methods.iterator();
    while(methods.hasNext())
    {
        MethodNode method = methods.next();
        if (remove(method.visibleAnnotations, SIDE))
        {
            if (DEBUG)
            {
                System.out.println(String.format("Removing Method: %s.%s%s", classNode.name, method.name, method.desc));
            }
            methods.remove();
        }
    }

    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(writer);
    return writer.toByteArray();
}
 
开发者ID:F1r3w477,项目名称:CustomWorldGen,代码行数:50,代码来源:SideTransformer.java


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