Search in sources :

Example 96 with ClassWriter

use of org.objectweb.asm.ClassWriter in project BetterRain by OreCruncher.

the class Transformer method transformWorld.

private byte[] transformWorld(final byte[] classBytes) {
    final String[] names;
    if (TransformLoader.runtimeDeobEnabled)
        names = new String[] { "updateWeatherBody" };
    else
        names = new String[] { "updateWeatherBody" };
    final String[] targetName = new String[] { "updateWeatherBody" };
    final ClassReader cr = new ClassReader(classBytes);
    final ClassNode cn = new ClassNode(ASM5);
    cr.accept(cn, 0);
    for (final MethodNode m : cn.methods) {
        if (m.name.equals(names[0])) {
            logger.debug("Hooking " + names[0]);
            InsnList list = new InsnList();
            list.add(new VarInsnNode(ALOAD, 0));
            final String sig = "(Lnet/minecraft/world/World;)V";
            list.add(new MethodInsnNode(INVOKESTATIC, "org/blockartistry/mod/DynSurround/server/WorldHandler", targetName[0], sig, false));
            list.add(new InsnNode(RETURN));
            m.instructions.insertBefore(m.instructions.getFirst(), list);
            break;
        }
    }
    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    cn.accept(cw);
    return cw.toByteArray();
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) ClassReader(org.objectweb.asm.ClassReader) InsnList(org.objectweb.asm.tree.InsnList) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) ClassWriter(org.objectweb.asm.ClassWriter)

Example 97 with ClassWriter

use of org.objectweb.asm.ClassWriter in project BetterRain by OreCruncher.

the class Transformer method transformWorldServer.

private byte[] transformWorldServer(final byte[] classBytes) {
    final String[] names;
    if (TransformLoader.runtimeDeobEnabled)
        names = new String[] { "func_73051_P" };
    else
        names = new String[] { "resetRainAndThunder" };
    final String[] targetName = new String[] { "resetRainAndThunder" };
    final ClassReader cr = new ClassReader(classBytes);
    final ClassNode cn = new ClassNode(ASM5);
    cr.accept(cn, 0);
    for (final MethodNode m : cn.methods) {
        if (m.name.equals(names[0])) {
            logger.debug("Hooking " + names[0]);
            InsnList list = new InsnList();
            list.add(new VarInsnNode(ALOAD, 0));
            final String sig = "(Lnet/minecraft/world/WorldServer;)V";
            list.add(new MethodInsnNode(INVOKESTATIC, "org/blockartistry/mod/DynSurround/server/PlayerSleepHandler", targetName[0], sig, false));
            list.add(new InsnNode(RETURN));
            m.instructions.insertBefore(m.instructions.getFirst(), list);
            break;
        }
    }
    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    cn.accept(cw);
    return cw.toByteArray();
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) ClassReader(org.objectweb.asm.ClassReader) InsnList(org.objectweb.asm.tree.InsnList) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) ClassWriter(org.objectweb.asm.ClassWriter)

Example 98 with ClassWriter

use of org.objectweb.asm.ClassWriter in project fabric8 by fabric8io.

the class TraceStrategy method transform.

@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    byte[] buffer = null;
    ClassInfo classInfo = context.getClassInfo(className);
    classInfo.setOriginalClass(classBeingRedefined);
    if (classInfo.getTransformed() == null) {
        // we haven't been transformed before
        classInfo.setOriginal(classfileBuffer);
    }
    if (!cleanUp.get()) {
        byte[] classBufferToRedefine = classInfo.getOriginal();
        if (configuration.isAudit(className)) {
            if (classInfo.isTransformed()) {
                // remove metrics from methods no longer defined
                context.resetMethods(classInfo);
            }
            ClassReader cr = new ClassReader(classBufferToRedefine);
            ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
            ApmClassVisitor visitor = new ApmClassVisitor(this, cw, classInfo);
            cr.accept(visitor, ClassReader.SKIP_FRAMES);
            buffer = cw.toByteArray();
            if (!verifyClass(className, buffer)) {
                classInfo.setCanTransform(false);
                buffer = null;
            }
            classInfo.setTransformed(buffer);
        }
    } else {
        if (classInfo.getOriginal() != null) {
            buffer = classInfo.getOriginal();
            context.resetAll(classInfo);
        }
    }
    return buffer;
}
Also used : ClassReader(org.objectweb.asm.ClassReader) ClassWriter(org.objectweb.asm.ClassWriter) ClassInfo(io.fabric8.apmagent.ClassInfo)

Example 99 with ClassWriter

use of org.objectweb.asm.ClassWriter in project wildfly-swarm by wildfly-swarm.

the class ClientServiceFactory method createImpl.

static byte[] createImpl(String implName, ClassInfo classInfo) {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;
    AnnotationVisitor av0;
    cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, implName.replace('.', '/'), null, "java/lang/Object", new String[] { classInfo.name().toString().replace('.', '/') });
    int lastDot = implName.lastIndexOf('.');
    String simpleName = implName.substring(lastDot + 1);
    cw.visitSource(simpleName + ".java", null);
    {
        av0 = cw.visitAnnotation("Ljavax/enterprise/context/ApplicationScoped;", true);
        av0.visitEnd();
    }
    cw.visitInnerClass("javax/ws/rs/client/Invocation$Builder", "javax/ws/rs/client/Invocation", "Builder", ACC_PUBLIC + ACC_STATIC + ACC_ABSTRACT + ACC_INTERFACE);
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(14, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLineNumber(15, l1);
        mv.visitInsn(RETURN);
        Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", buildTypeDef(implName), null, l0, l2, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    List<AnnotationInstance> annotations = classInfo.annotations().get(DotName.createSimple("org.wildfly.swarm.client.jaxrs.Service"));
    String baseUrl = (String) annotations.get(0).value("baseUrl").value();
    int lineNum = 18;
    classInfo.asClass().methods().stream().forEachOrdered(method -> {
        createMethod(cw, implName, classInfo.name().toString(), method, lineNum, baseUrl);
    });
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) Label(org.objectweb.asm.Label) ClassWriter(org.objectweb.asm.ClassWriter) AnnotationInstance(org.jboss.jandex.AnnotationInstance) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 100 with ClassWriter

use of org.objectweb.asm.ClassWriter in project wildfly-swarm by wildfly-swarm.

the class DefaultApplicationFactory method create.

public static byte[] create(String name, String path) throws IOException {
    ClassReader reader = new ClassReader(basicClassBytes());
    String slashName = name.replace('.', '/');
    ClassWriter writer = new ClassWriter(0);
    Remapper remapper = new Remapper() {

        @Override
        public String map(String typeName) {
            if (typeName.equals("org/wildfly/swarm/jaxrs/runtime/DefaultApplication")) {
                return slashName;
            }
            return super.map(typeName);
        }
    };
    ClassRemapper adapter = new ClassRemapper(writer, remapper);
    reader.accept(adapter, 0);
    AnnotationVisitor ann = writer.visitAnnotation("Ljavax/ws/rs/ApplicationPath;", true);
    ann.visit("value", path);
    ann.visitEnd();
    writer.visitEnd();
    return writer.toByteArray();
}
Also used : Remapper(org.objectweb.asm.commons.Remapper) ClassRemapper(org.objectweb.asm.commons.ClassRemapper) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) ClassReader(org.objectweb.asm.ClassReader) ClassRemapper(org.objectweb.asm.commons.ClassRemapper) 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