Example usage for org.objectweb.asm Opcodes GETFIELD

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

Introduction

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

Prototype

int GETFIELD

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

Click Source Link

Usage

From source file:bytecode.InstructionExporter.java

License:Apache License

/**
 * Outputs a read operation, choosing both the correct opcode family (variable
 * read, array load, field get, etc.) and type.
 *
 * @param instruction Read instruction./*from  ww  w  . j a va 2 s .com*/
 * @return            <code>null</code>
 */
@Override
public Void visit(Read instruction) {
    // Variable Reads
    if (instruction.getState() instanceof Variable) {
        Variable v = (Variable) instruction.getState();

        switch (v.getType().getSort()) {
        case LONG:
            mv.visitVarInsn(Opcodes.LLOAD, v.getIndex());
            break;
        case FLOAT:
            mv.visitVarInsn(Opcodes.FLOAD, v.getIndex());
            break;
        case DOUBLE:
            mv.visitVarInsn(Opcodes.DLOAD, v.getIndex());
            break;
        case REF:
            mv.visitVarInsn(Opcodes.ALOAD, v.getIndex());
            break;
        default:
            mv.visitVarInsn(Opcodes.ILOAD, v.getIndex());
            break;
        }
        // Array Loads
    } else if (instruction.getState() instanceof ArrayElement) {
        switch (instruction.getState().getType().getSort()) {
        case INT:
            mv.visitInsn(Opcodes.IALOAD);
            break;
        case LONG:
            mv.visitInsn(Opcodes.LALOAD);
            break;
        case FLOAT:
            mv.visitInsn(Opcodes.FALOAD);
            break;
        case DOUBLE:
            mv.visitInsn(Opcodes.DALOAD);
            break;
        case REF:
            mv.visitInsn(Opcodes.AALOAD);
            break;
        case BYTE:
            mv.visitInsn(Opcodes.BALOAD);
            break;
        case BOOL:
            mv.visitInsn(Opcodes.BALOAD);
            break;
        case CHAR:
            mv.visitInsn(Opcodes.CALOAD);
            break;
        case SHORT:
            mv.visitInsn(Opcodes.SALOAD);
            break;
        }
        // Static Reads
    } else if (instruction.getState() instanceof Field) {
        Field f = (Field) instruction.getState();

        mv.visitFieldInsn(Opcodes.GETSTATIC, f.getOwner().getName(), f.getName(), f.getType().getDescriptor());
        // Field Reads
    } else if (instruction.getState() instanceof InstanceField) {
        Field f = (Field) ((InstanceField) instruction.getState()).getField();

        mv.visitFieldInsn(Opcodes.GETFIELD, f.getOwner().getName(), f.getName(), f.getType().getDescriptor());
    }

    return null;
}

From source file:bytecode.MethodImporter.java

License:Apache License

/**
 * Imports field instructions (put and get), both for static and instance
 * fields./*from  ww  w .j  ava 2 s. c  o m*/
 *
 * @param opcode Opcode.
 * @param owner  Class containing the field.
 * @param name   Name of field.
 * @param desc   Type descriptor of field.
 */
@Override
public void visitFieldInsn(final int opcode, final String owner, final String name, final String desc) {
    Field f = ClassNode.getClass(owner).getField(name, desc);

    if (((opcode == Opcodes.GETSTATIC) || (opcode == Opcodes.PUTSTATIC)) != f.getModifiers()
            .contains(Modifier.STATIC)) {
        throw new RuntimeException("Field staticness conflicts with instruction");
    }

    switch (opcode) {
    // Loads
    case Opcodes.GETSTATIC:
    case Opcodes.GETFIELD:
        createFieldRead(f);
        break;
    // Stores
    case Opcodes.PUTSTATIC:
    case Opcodes.PUTFIELD:
        createFieldWrite(f);
        break;
    }
}

From source file:Client.JClassPatcher.java

License:Open Source License

private void patchClient(ClassNode node) {
    Logger.Info("Patching client (" + node.name + ".class)");

    Iterator<MethodNode> methodNodeList = node.methods.iterator();
    while (methodNodeList.hasNext()) {
        MethodNode methodNode = methodNodeList.next();

        // I (I)V is where most of the interface is processed
        if (methodNode.name.equals("I") && methodNode.desc.equals("(I)V")) {
            // Show combat menu
            Iterator<AbstractInsnNode> insnNodeList = methodNode.instructions.iterator();
            while (insnNodeList.hasNext()) {
                AbstractInsnNode insnNode = insnNodeList.next();

                if (insnNode.getOpcode() == Opcodes.BIPUSH) {
                    IntInsnNode bipush = (IntInsnNode) insnNode;

                    if (bipush.operand == -9) {
                        AbstractInsnNode findNode = insnNode;
                        while (findNode.getOpcode() != Opcodes.IF_ICMPEQ)
                            findNode = findNode.getNext();

                        LabelNode label = ((JumpInsnNode) findNode).label;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Client/Settings", "COMBAT_MENU", "Z"));
                        methodNode.instructions.insertBefore(insnNode, new JumpInsnNode(Opcodes.IFGT, label));
                        break;
                    }/*from   w ww.j  ava  2s  .  co  m*/
                }
            }
        } else if (methodNode.name.equals("J") && methodNode.desc.equals("(I)V")) {
            Iterator<AbstractInsnNode> insnNodeList = methodNode.instructions.iterator();
            while (insnNodeList.hasNext()) {
                AbstractInsnNode insnNode = insnNodeList.next();

                // Chat command patch
                if (insnNode.getOpcode() == Opcodes.SIPUSH) {
                    IntInsnNode call = (IntInsnNode) insnNode;
                    if (call.operand == 627) {
                        AbstractInsnNode jmpNode = insnNode;
                        while (jmpNode.getOpcode() != Opcodes.IFEQ)
                            jmpNode = jmpNode.getNext();

                        AbstractInsnNode insertNode = insnNode;
                        while (insertNode.getOpcode() != Opcodes.INVOKEVIRTUAL)
                            insertNode = insertNode.getPrevious();

                        JumpInsnNode jmp = (JumpInsnNode) jmpNode;
                        methodNode.instructions.insert(insertNode, new VarInsnNode(Opcodes.ASTORE, 2));
                        methodNode.instructions.insert(insertNode, new MethodInsnNode(Opcodes.INVOKESTATIC,
                                "Game/Client", "processChatCommand", "(Ljava/lang/String;)Ljava/lang/String;"));
                        methodNode.instructions.insert(insertNode, new VarInsnNode(Opcodes.ALOAD, 2));
                    }
                }
            }
        } else if (methodNode.name.equals("h") && methodNode.desc.equals("(B)V")) {
            Iterator<AbstractInsnNode> insnNodeList = methodNode.instructions.iterator();
            while (insnNodeList.hasNext()) {
                AbstractInsnNode insnNode = insnNodeList.next();

                // Private chat command patch
                if (insnNode.getOpcode() == Opcodes.GETFIELD) {
                    FieldInsnNode field = (FieldInsnNode) insnNode;
                    if (field.owner.equals("client") && field.name.equals("Ob")
                            && insnNode.getPrevious().getPrevious().getOpcode() != Opcodes.INVOKEVIRTUAL) {
                        insnNode = insnNode.getPrevious().getPrevious();
                        methodNode.instructions.insert(insnNode,
                                new FieldInsnNode(Opcodes.PUTFIELD, "client", "Ob", "Ljava/lang/String;"));
                        methodNode.instructions.insert(insnNode,
                                new MethodInsnNode(Opcodes.INVOKESTATIC, "Game/Client", "processPrivateCommand",
                                        "(Ljava/lang/String;)Ljava/lang/String;"));
                        methodNode.instructions.insert(insnNode,
                                new FieldInsnNode(Opcodes.GETFIELD, "client", "Ob", "Ljava/lang/String;"));
                        methodNode.instructions.insert(insnNode, new VarInsnNode(Opcodes.ALOAD, 0));
                        methodNode.instructions.insert(insnNode, new VarInsnNode(Opcodes.ALOAD, 0));
                        break;
                    }
                }
            }
        } else if (methodNode.name.equals("a") && methodNode.desc.equals("(IIIIIIII)V")) {
            // Draw NPC hook
            AbstractInsnNode insnNode = methodNode.instructions.getLast();
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 8));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 1));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 7));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 4));

            methodNode.instructions.insertBefore(insnNode,
                    new FieldInsnNode(Opcodes.GETSTATIC, "e", "Mb", "[Ljava/lang/String;"));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ALOAD, 0));
            methodNode.instructions.insertBefore(insnNode,
                    new FieldInsnNode(Opcodes.GETFIELD, "client", "Tb", "[Lta;"));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 6));
            methodNode.instructions.insertBefore(insnNode, new InsnNode(Opcodes.AALOAD));
            methodNode.instructions.insertBefore(insnNode, new FieldInsnNode(Opcodes.GETFIELD, "ta", "t", "I"));
            methodNode.instructions.insertBefore(insnNode, new InsnNode(Opcodes.AALOAD));
            methodNode.instructions.insertBefore(insnNode, new MethodInsnNode(Opcodes.INVOKESTATIC,
                    "Game/Client", "drawNPC", "(IIIILjava/lang/String;)V"));
        } else if (methodNode.name.equals("b") && methodNode.desc.equals("(IIIIIIII)V")) {
            // Draw Player hook
            AbstractInsnNode insnNode = methodNode.instructions.getLast();
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 5));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 6));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 2));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 7));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ALOAD, 0));
            methodNode.instructions.insertBefore(insnNode,
                    new FieldInsnNode(Opcodes.GETFIELD, "client", "rg", "[Lta;"));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 8));
            methodNode.instructions.insertBefore(insnNode, new InsnNode(Opcodes.AALOAD));
            methodNode.instructions.insertBefore(insnNode,
                    new FieldInsnNode(Opcodes.GETFIELD, "ta", "C", "Ljava/lang/String;"));
            methodNode.instructions.insertBefore(insnNode, new MethodInsnNode(Opcodes.INVOKESTATIC,
                    "Game/Client", "drawPlayer", "(IIIILjava/lang/String;)V"));
        } else if (methodNode.name.equals("b") && methodNode.desc.equals("(IIIIIII)V")) {
            // Draw Item hook
            // ILOAD 4 is item id
            AbstractInsnNode insnNode = methodNode.instructions.getLast();
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 3));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 7));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 5));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 1));
            methodNode.instructions.insertBefore(insnNode, new VarInsnNode(Opcodes.ILOAD, 4));
            methodNode.instructions.insertBefore(insnNode,
                    new MethodInsnNode(Opcodes.INVOKESTATIC, "Game/Client", "drawItem", "(IIIII)V"));
        } else if (methodNode.name.equals("L") && methodNode.desc.equals("(I)V")) {
            Iterator<AbstractInsnNode> insnNodeList = methodNode.instructions.iterator();
            while (insnNodeList.hasNext()) {
                AbstractInsnNode insnNode = insnNodeList.next();

                // Right click bounds fix
                if (insnNode.getOpcode() == Opcodes.SIPUSH) {
                    IntInsnNode call = (IntInsnNode) insnNode;
                    AbstractInsnNode nextNode = insnNode.getNext();

                    if (call.operand == 510) {
                        call.operand = 512 - call.operand;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "width", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.ISUB));
                    } else if (call.operand == 315) {
                        call.operand = 334 - call.operand;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "height_client", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.ISUB));
                    } else if (call.operand == -316) {
                        call.operand = 334 - (call.operand * -1);
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "height_client", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.INEG));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.ISUB));
                    }
                }
            }
        } else if (methodNode.name.equals("a") && methodNode.desc.equals("(ZZ)V")) {
            Iterator<AbstractInsnNode> insnNodeList = methodNode.instructions.iterator();
            while (insnNodeList.hasNext()) {
                AbstractInsnNode insnNode = insnNodeList.next();

                // Friends chat mouse fix
                if (insnNode.getOpcode() == Opcodes.SIPUSH) {
                    IntInsnNode call = (IntInsnNode) insnNode;
                    if (call.operand == 489 || call.operand == 429) {
                        call.operand = 512 - call.operand;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "width", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.ISUB));
                    }
                    if (call.operand == -430) {
                        call.operand = 512 - (call.operand * -1);
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "width", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.INEG));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.ISUB));
                    }
                }
            }
        } else if (methodNode.name.equals("i") && methodNode.desc.equals("(I)V")) {
            AbstractInsnNode lastNode = methodNode.instructions.getLast().getPrevious();

            // Send combat style option
            LabelNode label = new LabelNode();

            methodNode.instructions.insert(lastNode, label);

            // Format
            methodNode.instructions.insert(lastNode,
                    new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "da", "b", "(I)V", false));
            methodNode.instructions.insert(lastNode, new IntInsnNode(Opcodes.SIPUSH, 21294));
            methodNode.instructions.insert(lastNode,
                    new FieldInsnNode(Opcodes.GETFIELD, "client", "Jh", "Lda;"));
            methodNode.instructions.insert(lastNode, new VarInsnNode(Opcodes.ALOAD, 0));

            // Write byte
            methodNode.instructions.insert(lastNode,
                    new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "ja", "c", "(II)V", false));
            methodNode.instructions.insert(lastNode, new IntInsnNode(Opcodes.BIPUSH, -80));
            methodNode.instructions.insert(lastNode,
                    new FieldInsnNode(Opcodes.GETSTATIC, "Client/Settings", "COMBAT_STYLE", "I"));
            methodNode.instructions.insert(lastNode, new FieldInsnNode(Opcodes.GETFIELD, "da", "f", "Lja;"));
            methodNode.instructions.insert(lastNode,
                    new FieldInsnNode(Opcodes.GETFIELD, "client", "Jh", "Lda;"));
            methodNode.instructions.insert(lastNode, new VarInsnNode(Opcodes.ALOAD, 0));

            // Create Packet
            methodNode.instructions.insert(lastNode,
                    new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "da", "b", "(II)V", false));
            methodNode.instructions.insert(lastNode, new InsnNode(Opcodes.ICONST_0));
            methodNode.instructions.insert(lastNode, new IntInsnNode(Opcodes.BIPUSH, 29));
            methodNode.instructions.insert(lastNode,
                    new FieldInsnNode(Opcodes.GETFIELD, "client", "Jh", "Lda;"));
            methodNode.instructions.insert(lastNode, new VarInsnNode(Opcodes.ALOAD, 0));

            // Skip combat packet if style is already controlled
            methodNode.instructions.insert(lastNode, new JumpInsnNode(Opcodes.IF_ICMPLE, label));
            methodNode.instructions.insert(lastNode, new InsnNode(Opcodes.ICONST_0));
            methodNode.instructions.insert(lastNode,
                    new FieldInsnNode(Opcodes.GETSTATIC, "Client/Settings", "COMBAT_STYLE", "I"));

            // Client init_game
            methodNode.instructions.insert(lastNode,
                    new MethodInsnNode(Opcodes.INVOKESTATIC, "Game/Client", "init_game", "()V", false));
        } else if (methodNode.name.equals("o") && methodNode.desc.equals("(I)V")) {
            // Client.init_login patch
            AbstractInsnNode findNode = methodNode.instructions.getLast();
            methodNode.instructions.insertBefore(findNode,
                    new MethodInsnNode(Opcodes.INVOKESTATIC, "Game/Client", "init_login", "()V", false));
        } else if (methodNode.name.equals("a") && methodNode.desc.equals("(B)V")) {
            Iterator<AbstractInsnNode> insnNodeList = methodNode.instructions.iterator();
            while (insnNodeList.hasNext()) {
                AbstractInsnNode insnNode = insnNodeList.next();

                // Camera view distance crash fix
                if (insnNode.getOpcode() == Opcodes.SIPUSH) {
                    IntInsnNode call = (IntInsnNode) insnNode;
                    if (call.operand == 15000) {
                        call.operand = 32767;
                    }
                }
            }

            // Client.init patch
            AbstractInsnNode findNode = methodNode.instructions.getFirst();
            methodNode.instructions.insertBefore(findNode, new VarInsnNode(Opcodes.ALOAD, 0));
            methodNode.instructions.insertBefore(findNode,
                    new FieldInsnNode(Opcodes.PUTSTATIC, "Game/Client", "instance", "Ljava/lang/Object;"));
            methodNode.instructions.insertBefore(findNode,
                    new MethodInsnNode(Opcodes.INVOKESTATIC, "Game/Client", "init", "()V", false));
        } else if (methodNode.name.equals("G") && methodNode.desc.equals("(I)V")) {
            // TODO: This can be shortened, I'll fix it another time

            // NPC Dialogue keyboard
            AbstractInsnNode lastNode = methodNode.instructions.getLast().getPrevious();

            LabelNode label = new LabelNode();

            methodNode.instructions.insert(lastNode, label);

            // Hide dialogue
            methodNode.instructions.insert(lastNode, new FieldInsnNode(Opcodes.PUTFIELD, "client", "Ph", "Z"));
            methodNode.instructions.insert(lastNode, new InsnNode(Opcodes.ICONST_0));
            methodNode.instructions.insert(lastNode, new VarInsnNode(Opcodes.ALOAD, 0));

            // Format
            methodNode.instructions.insert(lastNode,
                    new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "da", "b", "(I)V", false));
            methodNode.instructions.insert(lastNode, new IntInsnNode(Opcodes.SIPUSH, 21294));
            methodNode.instructions.insert(lastNode,
                    new FieldInsnNode(Opcodes.GETFIELD, "client", "Jh", "Lda;"));
            methodNode.instructions.insert(lastNode, new VarInsnNode(Opcodes.ALOAD, 0));

            // Write byte
            methodNode.instructions.insert(lastNode,
                    new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "ja", "c", "(II)V", false));
            methodNode.instructions.insert(lastNode, new IntInsnNode(Opcodes.BIPUSH, 115));
            methodNode.instructions.insert(lastNode,
                    new FieldInsnNode(Opcodes.GETSTATIC, "Game/KeyboardHandler", "dialogue_option", "I"));
            methodNode.instructions.insert(lastNode, new FieldInsnNode(Opcodes.GETFIELD, "da", "f", "Lja;"));
            methodNode.instructions.insert(lastNode,
                    new FieldInsnNode(Opcodes.GETFIELD, "client", "Jh", "Lda;"));
            methodNode.instructions.insert(lastNode, new VarInsnNode(Opcodes.ALOAD, 0));

            // Create Packet
            methodNode.instructions.insert(lastNode,
                    new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "da", "b", "(II)V", false));
            methodNode.instructions.insert(lastNode, new InsnNode(Opcodes.ICONST_0));
            methodNode.instructions.insert(lastNode, new IntInsnNode(Opcodes.BIPUSH, 116));
            methodNode.instructions.insert(lastNode,
                    new FieldInsnNode(Opcodes.GETFIELD, "client", "Jh", "Lda;"));
            methodNode.instructions.insert(lastNode, new VarInsnNode(Opcodes.ALOAD, 0));

            // Check if dialogue option is pressed
            methodNode.instructions.insert(lastNode, new JumpInsnNode(Opcodes.IF_ICMPGE, label));
            // Menu option count
            methodNode.instructions.insert(lastNode, new FieldInsnNode(Opcodes.GETFIELD, "client", "Id", "I"));
            methodNode.instructions.insert(lastNode, new VarInsnNode(Opcodes.ALOAD, 0));
            methodNode.instructions.insert(lastNode,
                    new FieldInsnNode(Opcodes.GETSTATIC, "Game/KeyboardHandler", "dialogue_option", "I"));
            methodNode.instructions.insert(lastNode, new JumpInsnNode(Opcodes.IFLT, label));
            methodNode.instructions.insert(lastNode,
                    new FieldInsnNode(Opcodes.GETSTATIC, "Game/KeyboardHandler", "dialogue_option", "I"));
        } else if (methodNode.name.equals("f") && methodNode.desc.equals("(I)V")) {
            Iterator<AbstractInsnNode> insnNodeList = methodNode.instructions.iterator();
            while (insnNodeList.hasNext()) {
                AbstractInsnNode insnNode = insnNodeList.next();

                // Hide Roof option
                if (insnNode.getOpcode() == Opcodes.GETFIELD) {
                    FieldInsnNode field = (FieldInsnNode) insnNode;

                    if (field.owner.equals("client") && field.name.equals("yj")) {
                        AbstractInsnNode nextNode = insnNode.getNext();
                        if (nextNode.getOpcode() == Opcodes.IFNE) {
                            LabelNode label = ((JumpInsnNode) nextNode).label;
                            methodNode.instructions.insert(nextNode, new JumpInsnNode(Opcodes.IFGT, label));
                            methodNode.instructions.insert(nextNode,
                                    new FieldInsnNode(Opcodes.GETSTATIC, "Client/Settings", "HIDE_ROOFS", "Z"));
                        }
                    }
                }

                // Move wilderness skull
                if (insnNode.getOpcode() == Opcodes.SIPUSH) {
                    IntInsnNode call = (IntInsnNode) insnNode;
                    if (call.operand == 465 || call.operand == 453) {
                        call.operand = 512 - call.operand;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "width", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.ISUB));
                    }
                }
            }
        } else if (methodNode.name.equals("a") && methodNode.desc.equals("(IIZ)Z")) {
            Iterator<AbstractInsnNode> insnNodeList = methodNode.instructions.iterator();
            while (insnNodeList.hasNext()) {
                AbstractInsnNode insnNode = insnNodeList.next();

                // Move the load screen text dialogue
                if (insnNode.getOpcode() == Opcodes.SIPUSH) {
                    IntInsnNode call = (IntInsnNode) insnNode;
                    if (call.operand == 256) {
                        call.operand = 2;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "width", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IDIV));
                    } else if (call.operand == 192) {
                        call.operand = 2;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "height", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IADD));
                        methodNode.instructions.insert(insnNode, new IntInsnNode(Opcodes.SIPUSH, 19));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IDIV));
                    }
                }
            }
        } else if (methodNode.name.equals("d") && methodNode.desc.equals("(B)V")) {
            Iterator<AbstractInsnNode> insnNodeList = methodNode.instructions.iterator();
            while (insnNodeList.hasNext()) {
                AbstractInsnNode insnNode = insnNodeList.next();

                // Center logout dialogue
                if (insnNode.getOpcode() == Opcodes.SIPUSH || insnNode.getOpcode() == Opcodes.BIPUSH) {
                    IntInsnNode call = (IntInsnNode) insnNode;
                    if (call.operand == 256) {
                        call.operand = 2;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "width", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IDIV));
                    } else if (call.operand == 173) {
                        call.operand = 2;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "height", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IDIV));
                    } else if (call.operand == 126) {
                        call.operand = 2;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "width", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.ISUB));
                        methodNode.instructions.insert(insnNode, new IntInsnNode(Opcodes.SIPUSH, 130));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IDIV));
                    } else if (call.operand == 137) {
                        call.operand = 2;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "height", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.ISUB));
                        methodNode.instructions.insert(insnNode, new IntInsnNode(Opcodes.SIPUSH, 36));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IDIV));
                    }
                }
            }
        } else if (methodNode.name.equals("j") && methodNode.desc.equals("(I)V")) {
            Iterator<AbstractInsnNode> insnNodeList = methodNode.instructions.iterator();
            while (insnNodeList.hasNext()) {
                AbstractInsnNode insnNode = insnNodeList.next();

                // Center welcome box
                if (insnNode.getOpcode() == Opcodes.SIPUSH || insnNode.getOpcode() == Opcodes.BIPUSH) {
                    IntInsnNode call = (IntInsnNode) insnNode;
                    if (call.operand == 256) {
                        call.operand = 2;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "width", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IDIV));
                    } else if (call.operand == 167) {
                        call.operand = 2;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "height", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.ISUB));
                        methodNode.instructions.insert(insnNode, new IntInsnNode(Opcodes.SIPUSH, 6));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IDIV));
                    } else if (call.operand == 56) {
                        call.operand = 2;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "width", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.ISUB));
                        methodNode.instructions.insert(insnNode, new IntInsnNode(Opcodes.SIPUSH, 200));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IDIV));
                    } else if (call.operand == -87) {
                        call.operand = 2;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "width", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.INEG));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.ISUB));
                        methodNode.instructions.insert(insnNode, new IntInsnNode(Opcodes.SIPUSH, 169));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IDIV));
                    } else if (call.operand == 426) {
                        call.operand = 2;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "width", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IADD));
                        methodNode.instructions.insert(insnNode, new IntInsnNode(Opcodes.SIPUSH, 170));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IDIV));
                    } else if (call.operand == 106) {
                        call.operand = 2;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "width", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.ISUB));
                        methodNode.instructions.insert(insnNode, new IntInsnNode(Opcodes.SIPUSH, 150));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IDIV));
                    } else if (call.operand == 406) {
                        call.operand = 2;
                        methodNode.instructions.insertBefore(insnNode,
                                new FieldInsnNode(Opcodes.GETSTATIC, "Game/Renderer", "width", "I"));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IADD));
                        methodNode.instructions.insert(insnNode, new IntInsnNode(Opcodes.SIPUSH, 150));
                        methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.IDIV));
                    }
                }
            }
        } else if (methodNode.name.equals("k") && methodNode.desc.equals("(B)V")) {
            Iterator<AbstractInsnNode> insnNodeList = methodNode.instructions.iterator();
            while (insnNodeList.hasNext()) {
                AbstractInsnNode insnNode = insnNodeList.next();

                // Save settings from combat menu
                if (insnNode.getOpcode() == Opcodes.PUTFIELD) {
                    FieldInsnNode field = (FieldInsnNode) insnNode;

                    if (field.owner.equals("client") && field.name.equals("Fg")) {
                        methodNode.instructions.insert(insnNode, new MethodInsnNode(Opcodes.INVOKESTATIC,
                                "Client/Settings", "save", "()V", false));
                        methodNode.instructions.insert(insnNode,
                                new FieldInsnNode(Opcodes.PUTSTATIC, "Client/Settings", "COMBAT_STYLE", "I"));
                        methodNode.instructions.insert(insnNode,
                                new FieldInsnNode(Opcodes.GETFIELD, "client", "Fg", "I"));
                        methodNode.instructions.insert(insnNode, new VarInsnNode(Opcodes.ALOAD, 0));
                    }
                }
            }
        } else if (methodNode.name.equals("a") && methodNode.desc
                .equals("(ZLjava/lang/String;ILjava/lang/String;IILjava/lang/String;Ljava/lang/String;)V")) {
            AbstractInsnNode first = methodNode.instructions.getFirst();
            methodNode.instructions.insertBefore(first, new VarInsnNode(Opcodes.ALOAD, 7));
            methodNode.instructions.insertBefore(first, new VarInsnNode(Opcodes.ALOAD, 4));
            methodNode.instructions.insertBefore(first, new VarInsnNode(Opcodes.ILOAD, 5));
            methodNode.instructions.insertBefore(first, new MethodInsnNode(Opcodes.INVOKESTATIC, "Game/Client",
                    "messageHook", "(Ljava/lang/String;Ljava/lang/String;I)V"));
        }
    }
}

From source file:Client.JClassPatcher.java

License:Open Source License

private void patchRenderer(ClassNode node) {
    Logger.Info("Patching renderer (" + node.name + ".class)");

    Iterator<MethodNode> methodNodeList = node.methods.iterator();
    while (methodNodeList.hasNext()) {
        MethodNode methodNode = methodNodeList.next();

        // Renderer present hook
        if (methodNode.desc.equals("(Ljava/awt/Graphics;III)V")) {
            AbstractInsnNode findNode = methodNode.instructions.getFirst();
            FieldInsnNode imageNode = null;

            while (findNode.getOpcode() != Opcodes.POP) {
                findNode = findNode.getNext();
                if (findNode == null) {
                    Logger.Error("Unable to find present hook");
                    break;
                }/*from  w  w w.java 2s. c o  m*/
            }

            while (findNode.getOpcode() != Opcodes.INVOKESPECIAL) {
                if (findNode.getOpcode() == Opcodes.GETFIELD)
                    imageNode = (FieldInsnNode) findNode;

                AbstractInsnNode prev = findNode.getPrevious();
                methodNode.instructions.remove(findNode);
                findNode = prev;
            }

            methodNode.instructions.insert(findNode, new MethodInsnNode(Opcodes.INVOKESTATIC, "Game/Renderer",
                    "present", "(Ljava/awt/Graphics;Ljava/awt/Image;)V", false));
            methodNode.instructions.insert(findNode,
                    new FieldInsnNode(Opcodes.GETFIELD, node.name, imageNode.name, imageNode.desc));
            methodNode.instructions.insert(findNode, new VarInsnNode(Opcodes.ALOAD, 0));
            methodNode.instructions.insert(findNode, new VarInsnNode(Opcodes.ALOAD, 1));
        } else if (methodNode.name.equals("a") && methodNode.desc.equals("(IILjava/lang/String;IIBI)V")) {
            AbstractInsnNode start = methodNode.instructions.getFirst();
            while (start != null) {
                if (start.getOpcode() == Opcodes.ALOAD && start.getNext().getOpcode() == Opcodes.ILOAD
                        && start.getNext().getNext().getOpcode() == Opcodes.INVOKEVIRTUAL
                        && start.getNext().getNext().getNext().getOpcode() == Opcodes.ISTORE) {
                    break;
                }

                start = start.getNext();
            }
            start = start.getPrevious();

            LabelNode finishLabel = ((JumpInsnNode) start.getPrevious().getPrevious()).label;
            LabelNode failLabel = new LabelNode();

            methodNode.instructions.insertBefore(start, new IntInsnNode(Opcodes.BIPUSH, 126));
            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ALOAD, 3));
            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ILOAD, 10));
            methodNode.instructions.insertBefore(start,
                    new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/String", "charAt", "(I)C"));
            methodNode.instructions.insertBefore(start, new JumpInsnNode(Opcodes.IF_ICMPNE, failLabel));

            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ILOAD, 10));
            methodNode.instructions.insertBefore(start, new InsnNode(Opcodes.ICONST_5));
            methodNode.instructions.insertBefore(start, new InsnNode(Opcodes.IADD));
            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ALOAD, 3));
            methodNode.instructions.insertBefore(start,
                    new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/String", "length", "()I"));
            methodNode.instructions.insertBefore(start, new JumpInsnNode(Opcodes.IF_ICMPGE, failLabel));

            methodNode.instructions.insertBefore(start, new IntInsnNode(Opcodes.BIPUSH, 126));
            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ALOAD, 3));
            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ILOAD, 10));
            methodNode.instructions.insertBefore(start, new InsnNode(Opcodes.ICONST_5));
            methodNode.instructions.insertBefore(start, new InsnNode(Opcodes.IADD));
            methodNode.instructions.insertBefore(start,
                    new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/String", "charAt", "(I)C"));
            methodNode.instructions.insertBefore(start, new JumpInsnNode(Opcodes.IF_ICMPNE, failLabel));

            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ALOAD, 3));
            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ILOAD, 10));
            methodNode.instructions.insertBefore(start, new InsnNode(Opcodes.ICONST_1));
            methodNode.instructions.insertBefore(start, new InsnNode(Opcodes.IADD));
            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ILOAD, 10));
            methodNode.instructions.insertBefore(start, new InsnNode(Opcodes.ICONST_5));
            methodNode.instructions.insertBefore(start, new InsnNode(Opcodes.IADD));
            methodNode.instructions.insertBefore(start, new MethodInsnNode(Opcodes.INVOKEVIRTUAL,
                    "java/lang/String", "substring", "(II)Ljava/lang/String;"));
            methodNode.instructions.insertBefore(start, new MethodInsnNode(Opcodes.INVOKESTATIC,
                    "java/lang/Integer", "parseInt", "(Ljava/lang/String;)I"));
            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ISTORE, 4));
            methodNode.instructions.insertBefore(start, new IincInsnNode(10, 5));

            methodNode.instructions.insertBefore(start, new JumpInsnNode(Opcodes.GOTO, finishLabel));

            methodNode.instructions.insertBefore(start, failLabel);
        }
    }
}

From source file:Client.JClassPatcher.java

License:Open Source License

/**
 * TODO: Complete JavaDoc//from w ww .j  av a 2  s .co m
 * 
 * @param methodNode
 * @param owner The class of the variable to be hooked
 * @param var The variable to be hooked
 * @param desc
 * @param newClass The class the hooked variable will be stored in
 * @param newVar The variable name the hooked variable will be stored in
 * @param newDesc
 * @param canRead Specifies if the hooked variable should be readable
 * @param canWrite Specifies if the hooked variable should be writable
 */
private void hookClassVariable(MethodNode methodNode, String owner, String var, String desc, String newClass,
        String newVar, String newDesc, boolean canRead, boolean canWrite) {
    Iterator<AbstractInsnNode> insnNodeList = methodNode.instructions.iterator();
    while (insnNodeList.hasNext()) {
        AbstractInsnNode insnNode = insnNodeList.next();

        int opcode = insnNode.getOpcode();
        if (opcode == Opcodes.GETFIELD || opcode == Opcodes.PUTFIELD) {
            FieldInsnNode field = (FieldInsnNode) insnNode;
            if (field.owner.equals(owner) && field.name.equals(var) && field.desc.equals(desc)) {
                if (opcode == Opcodes.GETFIELD && canWrite) {
                    methodNode.instructions.insert(insnNode,
                            new FieldInsnNode(Opcodes.GETSTATIC, newClass, newVar, newDesc));
                    methodNode.instructions.insert(insnNode, new InsnNode(Opcodes.POP));
                } else if (opcode == Opcodes.PUTFIELD && canRead) {
                    methodNode.instructions.insertBefore(insnNode, new InsnNode(Opcodes.DUP_X1));
                    methodNode.instructions.insert(insnNode,
                            new FieldInsnNode(Opcodes.PUTSTATIC, newClass, newVar, newDesc));
                }
            }
        }
    }
}

From source file:com.alibaba.hotswap.processor.constructor.modifier.FieldHolderInitModifier.java

License:Open Source License

@Override
public void visitCode() {
    super.visitCode();

    // this.__$$hotswap_field_holder$$__ = new ConcurrentHashMap();
    mv.visitVarInsn(Opcodes.ALOAD, 0);/*from  w  w  w . j a  v a2  s.c  om*/
    mv.visitFieldInsn(Opcodes.GETFIELD, className, HotswapConstants.FIELD_HOLDER,
            "Ljava/util/concurrent/ConcurrentHashMap;");
    Label end = newLabel();
    ifNonNull(end);

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitTypeInsn(Opcodes.NEW, "java/util/concurrent/ConcurrentHashMap");
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/concurrent/ConcurrentHashMap", "<init>", "()V");
    mv.visitFieldInsn(Opcodes.PUTFIELD, className, HotswapConstants.FIELD_HOLDER,
            "Ljava/util/concurrent/ConcurrentHashMap;");
    mark(end);
}

From source file:com.alibaba.hotswap.processor.field.access.FieldAccessModifier.java

License:Open Source License

@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
    if (HotswapRuntime.getClassInitialized(className) && !name.equals(HotswapConstants.STATIC_FIELD_HOLDER)
            && !name.equals(HotswapConstants.FIELD_HOLDER)) {

        Queue<String> owners = new LinkedList<String>();
        owners.offer(owner);//from   www . jav  a 2 s.  com
        String tmpOwner = owners.poll();
        while (tmpOwner != null) {
            if (HotswapRuntime.hasClassMeta(tmpOwner)) {
                // Try to reload owner.
                HotswapRuntime.try2Reload(tmpOwner);

                ClassMeta classMeta = HotswapRuntime.getClassMeta(tmpOwner);
                String fmKey = HotswapFieldUtil.getFieldKey(name, desc);
                FieldMeta fm = classMeta.getFieldMeta(fmKey);

                if (classMeta.isInterface) {
                    if (fm != null && !fm.isDeleted(classMeta.loadedIndex)
                            && (opcode == Opcodes.PUTSTATIC || opcode == Opcodes.GETSTATIC)) {
                        super.visitFieldInsn(opcode, Type.getInternalName(classMeta.vClass), fm.name, fm.desc);
                        return;
                    }
                }

                if (fm != null && classMeta.primaryFieldKeyList.contains(fmKey)
                        && !fm.isDeleted(classMeta.loadedIndex)) {
                    break;
                } else if (fm != null && classMeta.primaryFieldKeyList.contains(fmKey)
                        && fm.isDeleted(classMeta.loadedIndex)) {
                    // If this accessed field is primary and deleted, it perhaps is a alias field
                    fm = classMeta.getFieldMeta(HotswapFieldUtil
                            .getFieldKey(HotswapConstants.PREFIX_FIELD_ALIAS + fm.name, fm.desc));
                }

                if (fm != null && fm.isAdded() && !fm.isDeleted(classMeta.loadedIndex)) {
                    if (opcode == Opcodes.PUTSTATIC) {
                        // put static
                        box(Type.getType(fm.desc));
                        mv.visitFieldInsn(Opcodes.GETSTATIC, tmpOwner, HotswapConstants.STATIC_FIELD_HOLDER,
                                "Ljava/util/concurrent/ConcurrentHashMap;");
                        mv.visitLdcInsn(fm.name);
                        mv.visitLdcInsn(fm.desc);
                        mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HotswapFieldUtil.class),
                                "setFieldValue",
                                "(Ljava/lang/Object;Ljava/util/concurrent/ConcurrentHashMap;Ljava/lang/String;Ljava/lang/String;)V");
                        return;
                    } else if (opcode == Opcodes.GETSTATIC) {
                        // get static
                        mv.visitFieldInsn(Opcodes.GETSTATIC, tmpOwner, HotswapConstants.STATIC_FIELD_HOLDER,
                                "Ljava/util/concurrent/ConcurrentHashMap;");
                        mv.visitLdcInsn(fm.name);
                        mv.visitLdcInsn(fm.desc);
                        mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HotswapFieldUtil.class),
                                "getFieldValue",
                                "(Ljava/util/concurrent/ConcurrentHashMap;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;");
                        unbox(Type.getType(fm.desc));
                        return;
                    } else if (opcode == Opcodes.PUTFIELD) {
                        // put field
                        // stack: obj fieldValue
                        box(Type.getType(fm.desc));
                        mv.visitInsn(Opcodes.SWAP);
                        mv.visitFieldInsn(Opcodes.GETFIELD, tmpOwner, HotswapConstants.FIELD_HOLDER,
                                "Ljava/util/concurrent/ConcurrentHashMap;");
                        mv.visitLdcInsn(fm.name);
                        mv.visitLdcInsn(fm.desc);
                        mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HotswapFieldUtil.class),
                                "setFieldValue",
                                "(Ljava/lang/Object;Ljava/util/concurrent/ConcurrentHashMap;Ljava/lang/String;Ljava/lang/String;)V");

                        return;
                    } else if (opcode == Opcodes.GETFIELD) {
                        // get field
                        // stack: obj
                        mv.visitFieldInsn(Opcodes.GETFIELD, tmpOwner, HotswapConstants.FIELD_HOLDER,
                                "Ljava/util/concurrent/ConcurrentHashMap;");
                        mv.visitLdcInsn(fm.name);
                        mv.visitLdcInsn(fm.desc);
                        mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HotswapFieldUtil.class),
                                "getFieldValue",
                                "(Ljava/util/concurrent/ConcurrentHashMap;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;");

                        Label notnull = newLabel();
                        Type type = Type.getType(fm.desc);

                        mv.visitInsn(Opcodes.DUP);
                        mv.visitJumpInsn(Opcodes.IFNONNULL, notnull);

                        Label end = newLabel();
                        switch (type.getSort()) {
                        case Type.BOOLEAN:
                            mv.visitInsn(Opcodes.POP);
                            mv.visitLdcInsn(new Boolean(false));
                            break;

                        case Type.CHAR:
                            mv.visitInsn(Opcodes.POP);
                            mv.visitLdcInsn(new Character(' '));
                            break;

                        case Type.BYTE:
                            mv.visitInsn(Opcodes.POP);
                            mv.visitLdcInsn(new Byte((byte) 0));
                            break;

                        case Type.SHORT:
                            mv.visitInsn(Opcodes.POP);
                            mv.visitLdcInsn(new Short((short) 0));
                            break;

                        case Type.INT:
                            mv.visitInsn(Opcodes.POP);
                            mv.visitLdcInsn(new Integer(0));
                            break;

                        case Type.FLOAT:
                            mv.visitInsn(Opcodes.POP);
                            mv.visitLdcInsn(new Float(0));
                            break;

                        case Type.LONG:
                            mv.visitInsn(Opcodes.POP);
                            mv.visitLdcInsn(new Long(0));
                            break;

                        case Type.DOUBLE:
                            mv.visitInsn(Opcodes.POP);
                            mv.visitLdcInsn(new Double(0));
                            break;

                        default:
                            break;
                        }
                        mv.visitJumpInsn(Opcodes.GOTO, end);

                        mark(notnull);
                        unbox(type);

                        mark(end);
                        return;
                    }
                }

                if (classMeta.clazz != null) {
                    Class<?> superClass = classMeta.clazz.getSuperclass();
                    if (superClass != null) {
                        owners.offer(HotswapUtil.getInternalClassName(superClass.getName()));
                    }
                    Class<?>[] superInterfaces = classMeta.clazz.getInterfaces();
                    if (superInterfaces != null) {
                        for (Class<?> itf : superInterfaces) {
                            owners.offer(HotswapUtil.getInternalClassName(itf.getName()));
                        }
                    }
                }
            }
            tmpOwner = owners.poll();
        }
    }
    super.visitFieldInsn(opcode, owner, name, desc);
}

From source file:com.alibaba.hotswap.processor.jdk.reflect.modifier.ConstructorNewInstanceModifier.java

License:Open Source License

@Override
public void visitCode() {
    super.visitCode();
    Label old = new Label();

    mv.visitVarInsn(Opcodes.ALOAD, 0);/* w w  w  .ja  va 2  s .co m*/
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/lang/reflect/Constructor", "parameterTypes",
            "[Ljava/lang/Class;");
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(MethodReflectHelper.class),
            "isUniformConstructorArgsType", "([Ljava/lang/Class;)Z");
    ifZCmp(NE, old);

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/lang/reflect/Constructor", "clazz", "Ljava/lang/Class;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;");
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HotswapRuntime.class), "hasClassMeta",
            "(Ljava/lang/String;)Z");
    mv.visitJumpInsn(Opcodes.IFEQ, old);

    // check it as a primary constructor
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/lang/reflect/Constructor", "clazz", "Ljava/lang/Class;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;");
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HotswapRuntime.class), "getClassMeta",
            "(Ljava/lang/String;)Lcom/alibaba/hotswap/meta/ClassMeta;");
    mv.visitLdcInsn(HotswapConstants.INIT);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Type.class), "getConstructorDescriptor",
            "(Ljava/lang/reflect/Constructor;)Ljava/lang/String;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(ClassMeta.class), "isPrimaryInitMethod",
            "(Ljava/lang/String;Ljava/lang/String;)Z");
    mv.visitJumpInsn(Opcodes.IFNE, old);

    // get uniform constructor
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/lang/reflect/Constructor", "clazz", "Ljava/lang/Class;");
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HotswapMethodUtil.class),
            "getConstructorParamTypes", "()[Ljava/lang/Class;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getConstructor",
            "([Ljava/lang/Class;)Ljava/lang/reflect/Constructor;");

    // index and objs
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/lang/reflect/Constructor", "clazz", "Ljava/lang/Class;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;");
    mv.visitLdcInsn(HotswapConstants.INIT);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Type.class), "getConstructorDescriptor",
            "(Ljava/lang/reflect/Constructor;)Ljava/lang/String;");
    loadArgs();
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(HotswapMethodUtil.class), "getMethodParams",
            "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)[Ljava/lang/Object;");

    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/Constructor", "newInstance",
            "([Ljava/lang/Object;)Ljava/lang/Object;");
    mv.visitInsn(Opcodes.ARETURN);

    mv.visitLabel(old);
}

From source file:com.android.ide.eclipse.apt.internal.analysis.InnerClassAnalyzer.java

License:Apache License

/**
 * Checks if a method of an inner class accesses the private content of an outer class (field or method)
 * @param methodNode/* w  ww . j  a  v  a2s.c o m*/
 * @param method
 * @return True if the outer access is confirmed, false otherwise.
 */
private boolean confirmParentClassAccess(final MethodNode methodNode, final MethodInsnNode method) {
    final String methodNodeName = methodNode.name;
    AbstractInsnNode prev = method.getPrevious();
    boolean result = false;
    while (!result) {
        if (methodNodeName.startsWith("<init>")) {
            if (prev.getOpcode() == Opcodes.ALOAD) {
                final VarInsnNode varInsn = (VarInsnNode) prev;
                result = varInsn.var == 1;
            }
        } else {
            if (prev.getOpcode() == Opcodes.GETFIELD) {
                final FieldInsnNode getField = (FieldInsnNode) prev;
                final String field = getField.owner + getField.name;
                final String testField = mInnerClass.name + "this$0";
                result = field.equals(testField);
            }
        }
        if (prev.getType() == AbstractInsnNode.LINE) {
            break;
        } else {
            prev = prev.getPrevious();
        }
    }
    return result;
}

From source file:com.android.ide.eclipse.apt.internal.analysis.InternalGetSetAnalyzer.java

License:Apache License

/**
 * Checks if a method is a getter//ww  w  . j  ava 2 s.  co  m
 * @param methodTest The method to test
 * @return True if the method is a getter, false otherwise
 */
private boolean isGetter(final MethodNode methodTest) {
    boolean getter = false;
    final String desc = methodTest.desc;
    final Type[] arguments = Type.getArgumentTypes(desc);
    final Type returnType = Type.getReturnType(desc);
    if (arguments.length == 0 && returnType.getSort() != Type.VOID) {
        final InsnList instructions = methodTest.instructions;
        //three next to skip label and line number instructions
        final AbstractInsnNode first = instructions.getFirst().getNext().getNext();
        final int returnOp = returnType.getOpcode(Opcodes.IRETURN);
        final int firstOp = first.getOpcode();
        //check for static getter
        if ((Opcodes.ACC_STATIC & methodTest.access) == 0) {
            if (firstOp == Opcodes.ALOAD) {
                final AbstractInsnNode second = first.getNext();
                if (second.getOpcode() == Opcodes.GETFIELD) {
                    final AbstractInsnNode third = second.getNext();
                    if (third.getOpcode() == returnOp) {
                        getter = true;
                    }
                }
            }
        } else {
            if (firstOp == Opcodes.GETSTATIC) {
                final AbstractInsnNode second = first.getNext();
                if (second.getOpcode() == returnOp) {
                    getter = true;
                }
            }
        }
    }
    return getter;
}