Search in sources :

Example 91 with ClassWriter

use of org.objectweb.asm.ClassWriter in project LanternServer by LanternPowered.

the class FinalFieldClassTransformer method transform.

@Override
public byte[] transform(ClassLoader loader, String className, byte[] byteCode) {
    final ClassReader classReader = new ClassReader(byteCode);
    final ClassWriter classWriter = new ClassWriter(0);
    try {
        final FinalFieldClassVisitor classVisitor = new FinalFieldClassVisitor(classWriter);
        classReader.accept(classVisitor, 0);
        return classWriter.toByteArray();
    } catch (StopTransformationException e) {
        // Fail fast in case we don't need transformation
        return byteCode;
    }
}
Also used : ClassReader(org.objectweb.asm.ClassReader) ClassWriter(org.objectweb.asm.ClassWriter)

Example 92 with ClassWriter

use of org.objectweb.asm.ClassWriter in project LanternServer by LanternPowered.

the class FastValueContainerClassTransformer method transform.

@Override
public byte[] transform(ClassLoader classLoader, String className, byte[] byteCode) {
    // we can assume that everything in that package is optimized already or should be.
    if (className.startsWith("org.lanternpowered.server.data.") || className.startsWith("org.spongepowered.api.data.")) {
        return byteCode;
    }
    final ClassReader classReader = new ClassReader(byteCode);
    final ClassWriter classWriter = new ClassWriter(Opcodes.ASM5);
    final FastValueContainerClassVisitor classVisitor = new FastValueContainerClassVisitor(classWriter);
    classReader.accept(classVisitor, 0);
    return classWriter.toByteArray();
}
Also used : ClassReader(org.objectweb.asm.ClassReader) ClassWriter(org.objectweb.asm.ClassWriter)

Example 93 with ClassWriter

use of org.objectweb.asm.ClassWriter in project LanternServer by LanternPowered.

the class ScriptFunctionGenerator method generateScriptFunctionClass.

/**
 * Generates a {@link ScriptFunction} class for the specified {@link ScriptFunctionMethod}.
 *
 * @param functionMethod The script function method
 * @return The script function class
 */
private Class<? extends ScriptFunction> generateScriptFunctionClass(ScriptFunctionMethod<?> functionMethod) {
    final Class<?> functionClass = functionMethod.getFunctionClass();
    final String name = functionClass.getName() + ScriptFunction.class.getSimpleName() + "Impl35962493";
    final String internalName = name.replace('.', '/');
    final String superClass;
    final String[] interfaces;
    if (functionMethod.getFunctionClass().isInterface()) {
        interfaces = new String[2];
        interfaces[1] = Type.getInternalName(functionClass);
        superClass = Type.getInternalName(Object.class);
    } else {
        interfaces = new String[1];
        superClass = Type.getInternalName(functionClass);
    }
    interfaces[0] = Type.getInternalName(ScriptFunction.class);
    // public class <I>ScriptReferencedImpl35962493 implements <I>, ScriptFunction {
    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, internalName, null, superClass, interfaces);
    // private final LanternScript script;
    FieldVisitor fv = cw.visitField(ACC_PRIVATE + ACC_FINAL, "script", Type.getDescriptor(LanternScript.class), null, null);
    fv.visitEnd();
    // public <I>ScriptReferencedImpl35962493(LanternScript script) {
    // this.script = script;
    // }
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(" + Type.getDescriptor(LanternScript.class) + ")V", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(Object.class), "<init>", "()V", false);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitFieldInsn(PUTFIELD, internalName, "script", Type.getDescriptor(LanternScript.class));
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    // @Override
    // public LanternScript getScript() {
    // return this.script;
    // }
    mv = cw.visitMethod(ACC_PUBLIC, "getScript", "()" + Type.getDescriptor(LanternScript.class), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, internalName, "script", Type.getDescriptor(LanternScript.class));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    // The script method
    @SuppressWarnings("ConstantConditions") @Nonnull final Method method = functionMethod.getMethod();
    mv = cw.visitMethod(ACC_PUBLIC, method.getName(), Type.getMethodDescriptor(method), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, internalName, "script", Type.getDescriptor(LanternScript.class));
    mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(LanternScript.class), "getRaw", "()" + Type.getDescriptor(Object.class), false);
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(functionClass));
    // Load all the method parameters to pass them through the delegated method
    for (int i = 1; i <= method.getParameterTypes().length; i++) {
        mv.visitVarInsn(ALOAD, i);
    }
    mv.visitMethodInsn(getInvokeMethodInsnOpcode(method), Type.getInternalName(method.getDeclaringClass()), method.getName(), Type.getMethodDescriptor(method), true);
    final Class<?> returnType = method.getReturnType();
    mv.visitInsn(getReturnInsnOpcode(returnType));
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    cw.visitEnd();
    final byte[] byteCode = cw.toByteArray();
    // noinspection unchecked
    return this.classLoader.defineClass(name, byteCode);
}
Also used : Nonnull(javax.annotation.Nonnull) Method(java.lang.reflect.Method) FieldVisitor(org.objectweb.asm.FieldVisitor) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 94 with ClassWriter

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

the class Transformer method transformSoundManager.

private byte[] transformSoundManager(final byte[] classBytes) {
    final String[] names;
    if (TransformLoader.runtimeDeobEnabled)
        names = new String[] { "func_148594_a" };
    else
        names = new String[] { "getNormalizedVolume" };
    final String[] targetName = new String[] { "getNormalizedVolume" };
    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]);
            final InsnList list = new InsnList();
            list.add(new VarInsnNode(ALOAD, 1));
            list.add(new VarInsnNode(ALOAD, 2));
            list.add(new VarInsnNode(ALOAD, 3));
            final String sig = "(Lnet/minecraft/client/audio/ISound;Lnet/minecraft/client/audio/SoundPoolEntry;Lnet/minecraft/client/audio/SoundCategory;)F";
            list.add(new MethodInsnNode(INVOKESTATIC, "org/blockartistry/mod/DynSurround/client/sound/SoundManager", targetName[0], sig, false));
            list.add(new InsnNode(FRETURN));
            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 95 with ClassWriter

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

the class Transformer method transformEntityRenderer.

private byte[] transformEntityRenderer(final byte[] classBytes) {
    final String[] names;
    if (TransformLoader.runtimeDeobEnabled)
        names = new String[] { "func_78474_d", "func_78484_h" };
    else
        names = new String[] { "renderRainSnow", "addRainParticles" };
    final String[] targetName = new String[] { "renderRainSnow", "addRainParticles" };
    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));
            list.add(new VarInsnNode(FLOAD, 1));
            final String sig = "(Lnet/minecraft/client/renderer/EntityRenderer;F)V";
            list.add(new MethodInsnNode(INVOKESTATIC, "org/blockartistry/mod/DynSurround/client/RenderWeather", targetName[0], sig, false));
            list.add(new InsnNode(RETURN));
            m.instructions.insertBefore(m.instructions.getFirst(), list);
        } else if (m.name.equals(names[1])) {
            logger.debug("Hooking " + names[1]);
            InsnList list = new InsnList();
            list.add(new VarInsnNode(ALOAD, 0));
            final String sig = "(Lnet/minecraft/client/renderer/EntityRenderer;)V";
            list.add(new MethodInsnNode(INVOKESTATIC, "org/blockartistry/mod/DynSurround/client/RenderWeather", targetName[1], sig, false));
            list.add(new InsnNode(RETURN));
            m.instructions.insertBefore(m.instructions.getFirst(), list);
        }
    }
    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)

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