Search in sources :

Example 46 with ClassWriter

use of org.objectweb.asm.ClassWriter in project bytecode-viewer by Konloch.

the class JarArchive method write.

public void write(File target) {
    try (JarOutputStream output = (manifest != null ? new JarOutputStream(new FileOutputStream(target), manifest) : new JarOutputStream(new FileOutputStream(target)))) {
        for (Map.Entry<String, ClassNode> entry : build().entrySet()) {
            output.putNextEntry(new JarEntry(entry.getKey().replaceAll("\\.", "/") + ".class"));
            ClassWriter writer = new ClassWriter(0);
            entry.getValue().accept(writer);
            output.write(writer.toByteArray());
            output.closeEntry();
        }
        output.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) HashMap(java.util.HashMap) Map(java.util.Map) ClassWriter(org.objectweb.asm.ClassWriter)

Example 47 with ClassWriter

use of org.objectweb.asm.ClassWriter in project gradle by gradle.

the class ApiClassExtractor method extractApiClassFrom.

/**
 * 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. Returns null when class should not be included, due to some reason that is not known until visited.
 */
@Nullable
public byte[] extractApiClassFrom(ClassReader originalClassReader) {
    ClassWriter apiClassWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    ApiMemberSelector visitor = new ApiMemberSelector(originalClassReader.getClassName(), new MethodStubbingApiMemberAdapter(apiClassWriter), apiIncludesPackagePrivateMembers);
    originalClassReader.accept(visitor, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
    if (visitor.isPrivateInnerClass()) {
        return null;
    }
    return apiClassWriter.toByteArray();
}
Also used : ClassWriter(org.objectweb.asm.ClassWriter) Nullable(javax.annotation.Nullable)

Example 48 with ClassWriter

use of org.objectweb.asm.ClassWriter in project ignite by apache.

the class HadoopHelperImpl method loadReplace.

/**
 * {@inheritDoc}
 */
@Override
public byte[] loadReplace(InputStream in, final String originalName, final String replaceName) {
    ClassReader rdr;
    try {
        rdr = new ClassReader(in);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    ClassWriter w = new ClassWriter(Opcodes.ASM4);
    rdr.accept(new RemappingClassAdapter(w, new Remapper() {

        /**
         */
        String replaceType = replaceName.replace('.', '/');

        /**
         */
        String nameType = originalName.replace('.', '/');

        @Override
        public String map(String type) {
            if (type.equals(replaceType))
                return nameType;
            return type;
        }
    }), ClassReader.EXPAND_FRAMES);
    return w.toByteArray();
}
Also used : RemappingClassAdapter(org.objectweb.asm.commons.RemappingClassAdapter) Remapper(org.objectweb.asm.commons.Remapper) ClassReader(org.objectweb.asm.ClassReader) IOException(java.io.IOException) ClassWriter(org.objectweb.asm.ClassWriter)

Example 49 with ClassWriter

use of org.objectweb.asm.ClassWriter in project Lucee by lucee.

the class ClassRenamer method rename.

public static byte[] rename(byte[] src, String newName) {
    ClassReader cr = new ClassReader(src);
    ClassWriter cw = ASMUtil.getClassWriter();
    ClassVisitor ca = new ClassRenamer(cw, newName);
    cr.accept(ca, 0);
    return cw.toByteArray();
}
Also used : ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter)

Example 50 with ClassWriter

use of org.objectweb.asm.ClassWriter in project Lucee by lucee.

the class MethodCleaner method modifie.

public static byte[] modifie(byte[] src, String methodName, Class[] args, Class rtn, String msg) {
    ClassReader cr = new ClassReader(src);
    ClassWriter cw = ASMUtil.getClassWriter();
    ClassVisitor ca = new MethodCleaner(cw, methodName, args, rtn, msg);
    cr.accept(ca, 0);
    return cw.toByteArray();
}
Also used : ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter)

Aggregations

ClassWriter (org.objectweb.asm.ClassWriter)502 ClassReader (org.objectweb.asm.ClassReader)285 MethodVisitor (org.objectweb.asm.MethodVisitor)127 Test (org.junit.Test)99 ClassVisitor (org.objectweb.asm.ClassVisitor)88 ClassNode (org.objectweb.asm.tree.ClassNode)70 IOException (java.io.IOException)62 Label (org.objectweb.asm.Label)55 SemanticVersioningClassVisitor (org.apache.aries.versioning.utils.SemanticVersioningClassVisitor)52 HashSet (java.util.HashSet)37 Method (java.lang.reflect.Method)36 Type (org.objectweb.asm.Type)33 BinaryCompatibilityStatus (org.apache.aries.versioning.utils.BinaryCompatibilityStatus)32 File (java.io.File)30 InvocationTargetException (java.lang.reflect.InvocationTargetException)28 FieldVisitor (org.objectweb.asm.FieldVisitor)28 FileOutputStream (java.io.FileOutputStream)27 InputStream (java.io.InputStream)26 MethodNode (org.objectweb.asm.tree.MethodNode)25 OuterClass (com.android.tools.layoutlib.create.dataclass.OuterClass)23