Search in sources :

Example 11 with TypeInsnNode

use of org.objectweb.asm.tree.TypeInsnNode in project runelite by runelite.

the class InstructionDeserializer method deserialize.

@Override
public AbstractInsnNode deserialize(JsonElement var1, Type var2, JsonDeserializationContext var3) {
    JsonObject var4 = (JsonObject) var1;
    int var5 = var4.get("opcode").getAsInt();
    if (var5 != 21 && var5 != 25 && var5 != 58 && var5 != 54 && var5 != 22 && var5 != 55) {
        String var7;
        String var8;
        String var10;
        if (var5 != 184 && var5 != 182 && var5 != 183) {
            if (var5 == 18) {
                try {
                    return new LdcInsnNode(Integer.valueOf(var4.get("cst").getAsInt()));
                } catch (Exception var9) {
                    return new LdcInsnNode(var4.get("cst").getAsString());
                }
            } else if (var5 != 187 && var5 != 189) {
                if (var5 != 16 && var5 != 17) {
                    if (var5 != 179 && var5 != 178 && var5 != 180 && var5 != 181) {
                        return new InsnNode(var5);
                    } else {
                        var10 = var4.get("owner").getAsString();
                        var7 = var4.get("name").getAsString();
                        var8 = var4.get("desc").getAsString();
                        return new FieldInsnNode(var5, var10, var7, var8);
                    }
                } else {
                    return new IntInsnNode(var5, var4.get("operand").getAsInt());
                }
            } else {
                return new TypeInsnNode(var5, var4.get("desc").getAsString());
            }
        } else {
            var10 = var4.get("owner").getAsString();
            var7 = var4.get("name").getAsString();
            var8 = var4.get("desc").getAsString();
            return new MethodInsnNode(var5, var10, var7, var8);
        }
    } else {
        int var6 = var4.get("var").getAsInt();
        return new VarInsnNode(var5, var6);
    }
}
Also used : FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) JsonObject(com.google.gson.JsonObject) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 12 with TypeInsnNode

use of org.objectweb.asm.tree.TypeInsnNode in project BuildCraft by BuildCraft.

the class EventBusProviderASM method generateGenerator.

private byte[] generateGenerator(Class<? extends IEventHandler<?>> handlerClass, Class<?> parClass, String clsName) {
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    ClassNode node = new ClassNode();
    node.name = clsName.replace('.', '/');
    node.version = Opcodes.V1_6;
    node.access = Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL;
    node.interfaces = Lists.newArrayList(IEventHandlerProvider.class.getName().replace('.', '/'));
    node.superName = "java/lang/Object";
    // Method:
    // public ClassName() {
    // super();
    // }
    {
        MethodNode consturctorMethod = new MethodNode();
        consturctorMethod.access = Opcodes.ACC_PUBLIC;
        consturctorMethod.desc = "()V";
        consturctorMethod.name = "<init>";
        consturctorMethod.exceptions = Lists.newArrayList();
        consturctorMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
        consturctorMethod.instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false));
        consturctorMethod.instructions.add(new InsnNode(Opcodes.RETURN));
        node.methods.add(consturctorMethod);
    }
    // Method:
    // public IEventHandler createNewHandler(Object obj) {
    // return new ClassHandler(obj);
    // }
    {
        MethodNode generationMethod = new MethodNode();
        // public final void handle(Object)
        generationMethod.access = Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL;
        generationMethod.desc = "(Ljava/lang/Object;)Lbuildcraft/core/lib/event/IEventHandler;";
        generationMethod.name = "createNewHandler";
        generationMethod.exceptions = Lists.newArrayList();
        {
            generationMethod.instructions.add(new TypeInsnNode(Opcodes.NEW, Type.getInternalName(handlerClass)));
            generationMethod.instructions.add(new InsnNode(Opcodes.DUP));
            generationMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 1));
            generationMethod.instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, Type.getInternalName(parClass)));
            generationMethod.instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, Type.getInternalName(handlerClass), "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(parClass)), false));
            generationMethod.instructions.add(new InsnNode(Opcodes.ARETURN));
        }
        node.methods.add(generationMethod);
    }
    node.accept(writer);
    return writer.toByteArray();
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) ClassWriter(org.objectweb.asm.ClassWriter)

Example 13 with TypeInsnNode

use of org.objectweb.asm.tree.TypeInsnNode in project BuildCraft by BuildCraft.

the class EventBusProviderASM method generateDirectHandler.

private byte[] generateDirectHandler(Method meth, Class<?> parClass, String clsName) {
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    ClassNode node = new ClassNode();
    node.name = clsName.replace('.', '/');
    node.access = Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL;
    node.interfaces = Lists.newArrayList(IEventHandler.class.getName().replace('.', '/'));
    node.version = Opcodes.V1_6;
    node.superName = "java/lang/Object";
    String fd = Type.getDescriptor(meth.getDeclaringClass());
    node.fields.add(new FieldNode(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "listener", fd, null, null));
    // This method does:
    // public ClassName(ListenerObject obj) {
    // super();
    // this.listener = obj;
    // }
    {
        MethodNode consturctorMethod = new MethodNode();
        consturctorMethod.access = Opcodes.ACC_PUBLIC;
        consturctorMethod.desc = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(meth.getDeclaringClass()));
        consturctorMethod.name = "<init>";
        consturctorMethod.exceptions = Lists.newArrayList();
        consturctorMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
        consturctorMethod.instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false));
        consturctorMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
        consturctorMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 1));
        consturctorMethod.instructions.add(new FieldInsnNode(Opcodes.PUTFIELD, node.name, "listener", fd));
        consturctorMethod.instructions.add(new InsnNode(Opcodes.RETURN));
        node.methods.add(consturctorMethod);
    }
    // This method does:
    // public final void handle(Object event) {
    // if (!(event instanceof EventClass)) return;
    // listener.<method_name>((EventClass) event);
    // }
    {
        MethodNode generationMethod = new MethodNode();
        // public final void handle(Object)
        generationMethod.access = Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL;
        generationMethod.desc = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(Object.class));
        generationMethod.name = "handle";
        generationMethod.exceptions = Lists.newArrayList();
        // This block does:
        // if (!(event instanceof EventClass)) return;
        {
            // ADD the first object (given as [this, event])
            // -> event
            generationMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 1));
            // event -> event instanceof PAR_CLASS -> boolean
            generationMethod.instructions.add(new TypeInsnNode(Opcodes.INSTANCEOF, Type.getInternalName(parClass)));
            LabelNode instanceLabel = new LabelNode();
            // boolean -> if (boolean) GOTO instanceLabel ->
            generationMethod.instructions.add(new JumpInsnNode(Opcodes.IFNE, instanceLabel));
            // return;
            generationMethod.instructions.add(new InsnNode(Opcodes.RETURN));
            generationMethod.instructions.add(instanceLabel);
        }
        // This block does:
        // listener.<method_name>(event);
        {
            generationMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
            // -> ListenerObject
            String desc = Type.getDescriptor(meth.getDeclaringClass());
            generationMethod.instructions.add(new FieldInsnNode(Opcodes.GETFIELD, node.name, "listener", desc));
            // -> Object
            generationMethod.instructions.add(new VarInsnNode(Opcodes.ALOAD, 1));
            // CheckCast
            generationMethod.instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, Type.getInternalName(parClass)));
            // ListenerObject, EventObject -> <method_name> ->
            generationMethod.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, Type.getInternalName(meth.getDeclaringClass()), meth.getName(), Type.getMethodDescriptor(meth), false));
        }
        // return;
        generationMethod.instructions.add(new InsnNode(Opcodes.RETURN));
        node.methods.add(generationMethod);
    }
    node.accept(writer);
    byte[] bytecode = writer.toByteArray();
    return bytecode;
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) ClassNode(org.objectweb.asm.tree.ClassNode) FieldNode(org.objectweb.asm.tree.FieldNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) ClassWriter(org.objectweb.asm.ClassWriter) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 14 with TypeInsnNode

use of org.objectweb.asm.tree.TypeInsnNode in project soot by Sable.

the class AsmMethodSource method convert.

private void convert() {
    ArrayDeque<Edge> worklist = new ArrayDeque<Edge>();
    for (LabelNode ln : trapHandlers.keySet()) {
        if (checkInlineExceptionHandler(ln))
            handleInlineExceptionHandler(ln, worklist);
        else
            worklist.add(new Edge(ln, new ArrayList<Operand>()));
    }
    worklist.add(new Edge(instructions.getFirst(), new ArrayList<Operand>()));
    conversionWorklist = worklist;
    edges = HashBasedTable.create(1, 1);
    do {
        Edge edge = worklist.pollLast();
        AbstractInsnNode insn = edge.insn;
        stack = edge.stack;
        edge.stack = null;
        do {
            int type = insn.getType();
            if (type == FIELD_INSN) {
                convertFieldInsn((FieldInsnNode) insn);
            } else if (type == IINC_INSN) {
                convertIincInsn((IincInsnNode) insn);
            } else if (type == INSN) {
                convertInsn((InsnNode) insn);
                int op = insn.getOpcode();
                if ((op >= IRETURN && op <= RETURN) || op == ATHROW) {
                    break;
                }
            } else if (type == INT_INSN) {
                convertIntInsn((IntInsnNode) insn);
            } else if (type == LDC_INSN) {
                convertLdcInsn((LdcInsnNode) insn);
            } else if (type == JUMP_INSN) {
                JumpInsnNode jmp = (JumpInsnNode) insn;
                convertJumpInsn(jmp);
                int op = jmp.getOpcode();
                if (op == JSR)
                    throw new UnsupportedOperationException("JSR!");
                if (op != GOTO) {
                    /* ifX opcode, i.e. two successors */
                    AbstractInsnNode next = insn.getNext();
                    addEdges(insn, next, Collections.singletonList(jmp.label));
                } else {
                    addEdges(insn, jmp.label, null);
                }
                break;
            } else if (type == LOOKUPSWITCH_INSN) {
                LookupSwitchInsnNode swtch = (LookupSwitchInsnNode) insn;
                convertLookupSwitchInsn(swtch);
                LabelNode dflt = swtch.dflt;
                addEdges(insn, dflt, swtch.labels);
                break;
            } else if (type == METHOD_INSN) {
                convertMethodInsn((MethodInsnNode) insn);
            } else if (type == INVOKE_DYNAMIC_INSN) {
                convertInvokeDynamicInsn((InvokeDynamicInsnNode) insn);
            } else if (type == MULTIANEWARRAY_INSN) {
                convertMultiANewArrayInsn((MultiANewArrayInsnNode) insn);
            } else if (type == TABLESWITCH_INSN) {
                TableSwitchInsnNode swtch = (TableSwitchInsnNode) insn;
                convertTableSwitchInsn(swtch);
                LabelNode dflt = swtch.dflt;
                addEdges(insn, dflt, swtch.labels);
            } else if (type == TYPE_INSN) {
                convertTypeInsn((TypeInsnNode) insn);
            } else if (type == VAR_INSN) {
                if (insn.getOpcode() == RET)
                    throw new UnsupportedOperationException("RET!");
                convertVarInsn((VarInsnNode) insn);
            } else if (type == LABEL) {
                convertLabel((LabelNode) insn);
            } else if (type == LINE) {
                convertLine((LineNumberNode) insn);
            } else if (type == FRAME) {
            // we can ignore it
            } else
                throw new RuntimeException("Unknown instruction type: " + type);
        } while ((insn = insn.getNext()) != null);
    } while (!worklist.isEmpty());
    conversionWorklist = null;
    edges = null;
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) TableSwitchInsnNode(org.objectweb.asm.tree.TableSwitchInsnNode) MultiANewArrayInsnNode(org.objectweb.asm.tree.MultiANewArrayInsnNode) ArrayList(java.util.ArrayList) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) ArrayDeque(java.util.ArrayDeque) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) IincInsnNode(org.objectweb.asm.tree.IincInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) LookupSwitchInsnNode(org.objectweb.asm.tree.LookupSwitchInsnNode)

Example 15 with TypeInsnNode

use of org.objectweb.asm.tree.TypeInsnNode in project pinpoint by naver.

the class ASMMethodVariables method loadInterceptorLocalVariables.

public void loadInterceptorLocalVariables(final InsnList instructions, final InterceptorDefinition interceptorDefinition, final boolean after) {
    assertInitializedInterceptorLocalVariables();
    loadVar(instructions, this.interceptorVarIndex);
    instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, Type.getInternalName(interceptorDefinition.getInterceptorBaseClass())));
    // target(this) object.
    loadThis(instructions);
    final InterceptorType interceptorType = interceptorDefinition.getInterceptorType();
    if (interceptorType == InterceptorType.ARRAY_ARGS) {
        // Object target, Object[] args
        loadVar(instructions, this.argsVarIndex);
    } else if (interceptorType == InterceptorType.STATIC) {
        // Object target, String declaringClassInternalName, String methodName, String parameterDescription, Object[] args
        loadVar(instructions, this.classNameVarIndex);
        loadVar(instructions, this.methodNameVarIndex);
        loadVar(instructions, this.parameterDescriptionVarIndex);
        loadVar(instructions, this.argsVarIndex);
    } else if (interceptorType == InterceptorType.API_ID_AWARE) {
        // Object target, int apiId, Object[] args
        loadInt(instructions, this.apiIdVarIndex);
        loadVar(instructions, this.argsVarIndex);
    } else if (interceptorType == InterceptorType.BASIC) {
        int interceptorMethodParameterCount = getInterceptorParameterCount(interceptorDefinition);
        int argumentCount = Math.min(this.argumentTypes.length, interceptorMethodParameterCount);
        int i = 1;
        for (; i <= argumentCount; i++) {
            // Object target, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4
            if (i == 1)
                loadVar(instructions, this.arg0VarIndex);
            else if (i == 2)
                loadVar(instructions, this.arg1VarIndex);
            else if (i == 3)
                loadVar(instructions, this.arg2VarIndex);
            else if (i == 4)
                loadVar(instructions, this.arg3VarIndex);
            else if (i == 5)
                loadVar(instructions, this.arg4VarIndex);
            else
                loadNull(instructions);
        }
        for (; i <= interceptorMethodParameterCount; i++) {
            loadNull(instructions);
        }
    }
    if (after) {
        loadVar(instructions, this.resultVarIndex);
        loadVar(instructions, this.throwableVarIndex);
    }
}
Also used : TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) InterceptorType(com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorType)

Aggregations

TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)29 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)18 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)14 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)13 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)12 InsnNode (org.objectweb.asm.tree.InsnNode)11 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)9 IntInsnNode (org.objectweb.asm.tree.IntInsnNode)8 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)8 MethodNode (org.objectweb.asm.tree.MethodNode)8 Type (org.objectweb.asm.Type)7 InsnList (org.objectweb.asm.tree.InsnList)7 LabelNode (org.objectweb.asm.tree.LabelNode)7 ClassNode (org.objectweb.asm.tree.ClassNode)5 FieldNode (org.objectweb.asm.tree.FieldNode)4 MultiANewArrayInsnNode (org.objectweb.asm.tree.MultiANewArrayInsnNode)3 JsonObject (com.google.gson.JsonObject)2 ClassWriter (org.objectweb.asm.ClassWriter)2 FrameNode (org.objectweb.asm.tree.FrameNode)2 IincInsnNode (org.objectweb.asm.tree.IincInsnNode)2