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;
}
}
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();
}
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);
}
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();
}
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();
}
Aggregations