Example usage for org.objectweb.asm Opcodes BIPUSH

List of usage examples for org.objectweb.asm Opcodes BIPUSH

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes BIPUSH.

Prototype

int BIPUSH

To view the source code for org.objectweb.asm Opcodes BIPUSH.

Click Source Link

Usage

From source file:serianalyzer.JVMImpl.java

License:Open Source License

/**
 * @param opcode//from ww w  . j  a v  a  2s  . co  m
 * @param operand
 * @param s
 */
static void handleJVMIntInsn(int opcode, int operand, JVMStackState s) {
    switch (opcode) {
    case Opcodes.BIPUSH:
        s.push(new BasicConstant(Type.BYTE_TYPE, operand));
        break;
    case Opcodes.SIPUSH:
        s.push(new BasicConstant(Type.SHORT_TYPE, operand));
        break;
    case Opcodes.NEWARRAY:
        s.pop();
        s.push(new BasicVariable(makeBasicArrayType(operand), "array", false)); //$NON-NLS-1$
    }
}

From source file:sg.atom.core.actor.internal.codegenerator.ActorProxyCreator.java

License:Apache License

/**
 * Create or get a MessageCaller implementation for the given method.
 *
 * @param ownerClass the class that owns the message
 * @param method the method to invoke/*w  w w. j a  va  2s  . co m*/
 * @return the message caller
 * @throws NoSuchMethodException
 * @throws SecurityException
 */
@SuppressWarnings("unchecked")
public static Class<MessageCaller<?>> createMessageCaller(Class<?> ownerClass, Method method)
        throws SecurityException, NoSuchMethodException {

    String className = String.format("%s_%s_%d__MESSAGECALLER", ownerClass.getName(), method.getName(),
            getMethodNumber(method));
    String classNameInternal = className.replace('.', '/');
    java.lang.reflect.Type fullReturnType = method.getGenericReturnType();
    if ((!(fullReturnType instanceof ParameterizedType)) && AsyncResult.class
            .isAssignableFrom(((Class) ((ParameterizedType) fullReturnType).getRawType()))) {
        throw new RuntimeException("Something's wrong here: should not be called for such a method");
    }
    String returnSignature = GenericTypeHelper
            .getSignature(((ParameterizedType) fullReturnType).getActualTypeArguments()[0]);

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    MethodVisitor mv;

    cw.visit(codeVersion, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER + Opcodes.ACC_SYNTHETIC,
            classNameInternal, "L" + classNameInternal + "<" + returnSignature + ">;",
            "org/actorsguildframework/internal/MessageCaller", null);
    cw.visitSource(null, null);

    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "org/actorsguildframework/internal/MessageCaller", "<init>",
                "()V");
        mv.visitInsn(Opcodes.RETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + classNameInternal + ";", null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "invoke",
                "(Lorg/actorsguildframework/Actor;[Ljava/lang/Object;)Lorg/actorsguildframework/AsyncResult;",
                "(Lorg/actorsguildframework/Actor;[Ljava/lang/Object;)Lorg/actorsguildframework/AsyncResult<"
                        + returnSignature + ">;",
                null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);

        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(method.getDeclaringClass()) + "__ACTORPROXY");

        int idx = 0;
        for (Class<?> t : method.getParameterTypes()) {
            mv.visitVarInsn(Opcodes.ALOAD, 2);
            mv.visitIntInsn(Opcodes.BIPUSH, idx);
            mv.visitInsn(Opcodes.AALOAD);
            if (t.isPrimitive()) {
                String wrapperDescr = GenerationUtils.getWrapperInternalName(t);
                mv.visitTypeInsn(Opcodes.CHECKCAST, wrapperDescr);
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, wrapperDescr, t.getName() + "Value",
                        "()" + Type.getDescriptor(t));
            } else {
                if (isArgumentFreezingRequired(method, idx, t)) {
                    mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(SerializableFreezer.class));
                    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(SerializableFreezer.class),
                            "get", Type.getMethodDescriptor(SerializableFreezer.class.getMethod("get")));
                }
                if (!t.equals(Object.class)) {
                    mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(t));
                }
            }
            idx++;
        }
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                Type.getInternalName(method.getDeclaringClass()) + "__ACTORPROXY",
                String.format(SUPER_CALLER_NAME_FORMAT, method.getName()), Type.getMethodDescriptor(method));

        mv.visitInsn(Opcodes.ARETURN);

        Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", "L" + classNameInternal + ";", null, l0, l2, 0);
        mv.visitLocalVariable("instance", "Lorg/actorsguildframework/Actor;", null, l0, l2, 1);
        mv.visitLocalVariable("arguments", "[Ljava/lang/Object;", null, l0, l2, 2);

        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getMessageName", "()Ljava/lang/String;", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLdcInsn(method.getName());
        mv.visitInsn(Opcodes.ARETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + classNameInternal + ";", null, l0, l1, 0);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    cw.visitEnd();

    return (Class<MessageCaller<?>>) GenerationUtils.loadClass(className, cw.toByteArray());
}

From source file:sg.atom.core.actor.internal.codegenerator.ActorProxyCreator.java

License:Apache License

/**
 * Writes a proxy method for messages.//from  w  w w .  j  a  v a2  s . co  m
 *
 * @param classNameInternal the internal class name
 * @param classNameDescriptor the class name descriptor
 * @param cw the ClassWriter
 * @param index the message index
 * @param type the ActorState type to use
 * @param concurrencyModel the concurrency model of the message
 * @param messageDescriptor the message's descriptor
 * @param method the method to override
 * @param simpleDescriptor a simple descriptor of the message
 * @param genericSignature the signature of the message
 */
private static void writeProxyMethod(String classNameInternal, String classNameDescriptor, ClassWriter cw,
        int index, Type actorState, ConcurrencyModel concurrencyModel, MessageImplDescriptor messageDescriptor,
        Method method, String simpleDescriptor, String genericSignature) throws NoSuchMethodException {
    MethodVisitor mv;
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, method.getName(), simpleDescriptor, genericSignature, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitIntInsn(Opcodes.BIPUSH, method.getParameterTypes().length);
        mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
        for (int j = 0; j < method.getParameterTypes().length; j++) {
            mv.visitInsn(Opcodes.DUP);
            mv.visitIntInsn(Opcodes.BIPUSH, j);
            Class<?> paraType = method.getParameterTypes()[j];
            if (paraType.isPrimitive()) {
                String wrapperClass = GenerationUtils.getWrapperInternalName(paraType);
                Type primType = Type.getType(paraType);
                mv.visitVarInsn(primType.getOpcode(Opcodes.ILOAD), j + 1);
                mv.visitMethodInsn(Opcodes.INVOKESTATIC, wrapperClass, "valueOf",
                        "(" + primType.getDescriptor() + ")" + "L" + wrapperClass + ";");
            } else if (isArgumentFreezingRequired(method, j, paraType)) {
                mv.visitVarInsn(Opcodes.ALOAD, j + 1);
                mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(SerializableFreezer.class),
                        "freeze",
                        Type.getMethodDescriptor(SerializableFreezer.class.getMethod("freeze", Object.class)));
            } else if (paraType.isInterface()) {
                mv.visitVarInsn(Opcodes.ALOAD, j + 1);
                mv.visitInsn(Opcodes.DUP);
                mv.visitTypeInsn(Opcodes.INSTANCEOF, "org/actorsguildframework/Actor");
                Label lEndif = new Label();
                mv.visitJumpInsn(Opcodes.IFNE, lEndif);
                mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(ActorRuntimeException.class));
                mv.visitInsn(Opcodes.DUP);
                mv.visitLdcInsn(String.format(
                        "Argument %d is an non-Serializable interface, but you did not give an Actor. If a message's argument type is an interface that does not extend Serializable, only Actors are acceptable as argument.",
                        j));
                mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(ActorRuntimeException.class),
                        "<init>", "(Ljava/lang/String;)V");
                mv.visitInsn(Opcodes.ATHROW);
                mv.visitLabel(lEndif);
            } else {
                mv.visitVarInsn(Opcodes.ALOAD, j + 1);
            }

            mv.visitInsn(Opcodes.AASTORE);
        }
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitVarInsn(Opcodes.ASTORE, method.getParameterTypes().length + 1); // method.getParameterTypes().length+1 ==> 'args' local variable
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, classNameInternal, "actorState__ACTORPROXY",
                actorState.getDescriptor());
        mv.visitFieldInsn(Opcodes.GETSTATIC, classNameInternal,
                String.format(MESSAGE_CALLER_NAME_FORMAT, index),
                "Lorg/actorsguildframework/internal/MessageCaller;");
        mv.visitFieldInsn(Opcodes.GETSTATIC, "org/actorsguildframework/annotations/ThreadUsage",
                messageDescriptor.getThreadUsage().name(),
                "Lorg/actorsguildframework/annotations/ThreadUsage;");
        mv.visitVarInsn(Opcodes.ALOAD, method.getParameterTypes().length + 1);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, actorState.getInternalName(), "queueMessage",
                "(Lorg/actorsguildframework/internal/MessageCaller;Lorg/actorsguildframework/annotations/ThreadUsage;[Ljava/lang/Object;)Lorg/actorsguildframework/internal/AsyncResultImpl;");
        mv.visitInsn(Opcodes.ARETURN);
        Label l4 = new Label();
        mv.visitLabel(l4);
        mv.visitLocalVariable("this", classNameDescriptor, null, l0, l4, 0);
        for (int j = 0; j < method.getParameterTypes().length; j++) {
            mv.visitLocalVariable("arg" + j, Type.getDescriptor(method.getParameterTypes()[j]),
                    GenericTypeHelper.getSignatureIfGeneric(method.getGenericParameterTypes()[j]), l0, l4,
                    j + 1);
        }
        mv.visitLocalVariable("args", "[Ljava/lang/Object;", null, l1, l4,
                method.getParameterTypes().length + 1);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
}

From source file:tk.wurst_client.hooks.injector.MethodHookInjector.java

License:Open Source License

@Override
public void visitCode() {
    super.visitCode();
    if (methodData.hasHookAt(HookPosition.METHOD_START)) {
        HookData hookData = methodData.getHook(HookPosition.METHOD_START);
        super.visitLdcInsn(className + "." + methodName + "|start");

        if (hookData.collectsParams()) {
            super.visitIntInsn(Opcodes.BIPUSH, paramCount);
            super.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
            for (byte i = 0; i < paramCount; i++) {
                super.visitInsn(Opcodes.DUP);
                super.visitIntInsn(Opcodes.BIPUSH, i);
                super.visitVarInsn(Opcodes.ALOAD, i);
                super.visitInsn(Opcodes.AASTORE);
            }/*from  w  w  w  .ja  v  a  2 s  . c  o m*/
        }

        // TODO: Custom class path
        super.visitMethodInsn(Opcodes.INVOKESTATIC, "tk/wurst_client/hooks/HookManager", "hook",
                "(Ljava/lang/String;" + (hookData.collectsParams() ? "[Ljava/lang/Object;" : "") + ")V", false);
    }
}

From source file:tk.wurst_client.hooks.injector.MethodHookInjector.java

License:Open Source License

@Override
public void visitInsn(int opcode) {
    if (methodData.hasHookAt(HookPosition.METHOD_END) && opcode >= 172 && opcode <= 177) {
        HookData hookData = methodData.getHook(HookPosition.METHOD_END);
        super.visitLdcInsn(className + "." + methodName + "|end");

        if (hookData.collectsParams()) {

            super.visitIntInsn(Opcodes.BIPUSH, paramCount);
            super.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
            for (byte i = 0; i < paramCount; i++) {
                super.visitInsn(Opcodes.DUP);
                super.visitIntInsn(Opcodes.BIPUSH, i);
                super.visitVarInsn(Opcodes.ALOAD, i);
                super.visitInsn(Opcodes.AASTORE);
            }//from  ww  w  .  j a  v a  2 s .co m
        }

        // TODO: Custom class path
        super.visitMethodInsn(Opcodes.INVOKESTATIC, "tk/wurst_client/hooks/HookManager", "hook",
                "(Ljava/lang/String;" + (hookData.collectsParams() ? "[Ljava/lang/Object;" : "") + ")V", false);
    }
    super.visitInsn(opcode);
}

From source file:vazkii.quark.base.asm.ClassTransformer.java

License:Creative Commons License

private static byte[] transformTransferStackInSlot(byte[] basicClass, int min, int max, String hook) {
    MethodSignature sig = new MethodSignature("transferStackInSlot", "func_82846_b", "b",
            "(Lnet/minecraft/entity/player/EntityPlayer;I)Lnet/minecraft/item/ItemStack;");

    bipushCount = 0;//w  ww  . j  a va  2 s .co m
    return transform(basicClass, Pair.of(sig, combine((AbstractInsnNode node) -> { // Filte
        return node.getOpcode() == Opcodes.BIPUSH;
    }, (MethodNode method, AbstractInsnNode node) -> { // Action
        InsnList newInstructions = new InsnList();
        bipushCount++;

        if (bipushCount != min && bipushCount != max)
            return false;

        log("Adding invokestatic to " + ((IntInsnNode) node).operand + "/" + bipushCount);
        newInstructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, ASM_HOOKS, hook, "(I)I"));

        method.instructions.insert(node, newInstructions);
        return bipushCount == max;
    })));
}