Search in sources :

Example 1 with Type

use of org.powermock.api.mockito.repackaged.asm.Type in project powermock by powermock.

the class FieldProviderTransformer method getByIndex.

private void getByIndex(final String[] names, final int[] indexes) throws Exception {
    final CodeEmitter e = super.begin_method(Constants.ACC_PUBLIC, PROVIDER_GET_BY_INDEX, null);
    e.load_this();
    e.load_arg(0);
    e.process_switch(indexes, new ProcessSwitchCallback() {

        public void processCase(int key, Label end) throws Exception {
            Type type = (Type) fields.get(names[key]);
            e.getfield(names[key]);
            e.box(type);
            e.return_value();
        }

        public void processDefault() throws Exception {
            e.throw_exception(ILLEGAL_ARGUMENT_EXCEPTION, "Unknown field index");
        }
    });
    e.end_method();
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) Type(org.powermock.api.mockito.repackaged.asm.Type) ProcessSwitchCallback(org.powermock.api.mockito.repackaged.cglib.core.ProcessSwitchCallback) Label(org.powermock.api.mockito.repackaged.asm.Label) CodeGenerationException(org.powermock.api.mockito.repackaged.cglib.core.CodeGenerationException)

Example 2 with Type

use of org.powermock.api.mockito.repackaged.asm.Type in project powermock by powermock.

the class FieldProviderTransformer method setField.

private void setField(String[] names) throws Exception {
    final CodeEmitter e = begin_method(Constants.ACC_PUBLIC, PROVIDER_SET, null);
    e.load_this();
    e.load_arg(1);
    e.load_arg(0);
    EmitUtils.string_switch(e, names, Constants.SWITCH_STYLE_HASH, new ObjectSwitchCallback() {

        public void processCase(Object key, Label end) {
            Type type = (Type) fields.get(key);
            e.unbox(type);
            e.putfield((String) key);
            e.return_value();
        }

        public void processDefault() {
            e.throw_exception(ILLEGAL_ARGUMENT_EXCEPTION, "Unknown field name");
        }
    });
    e.end_method();
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) Type(org.powermock.api.mockito.repackaged.asm.Type) Label(org.powermock.api.mockito.repackaged.asm.Label) ObjectSwitchCallback(org.powermock.api.mockito.repackaged.cglib.core.ObjectSwitchCallback)

Example 3 with Type

use of org.powermock.api.mockito.repackaged.asm.Type in project powermock by powermock.

the class ReflectUtils method getClassInfo.

public static ClassInfo getClassInfo(final Class clazz) {
    final Type type = Type.getType(clazz);
    final Type sc = (clazz.getSuperclass() == null) ? null : Type.getType(clazz.getSuperclass());
    return new ClassInfo() {

        public Type getType() {
            return type;
        }

        public Type getSuperType() {
            return sc;
        }

        public Type[] getInterfaces() {
            return TypeUtils.getTypes(clazz.getInterfaces());
        }

        public int getModifiers() {
            return clazz.getModifiers();
        }
    };
}
Also used : Type(org.powermock.api.mockito.repackaged.asm.Type)

Example 4 with Type

use of org.powermock.api.mockito.repackaged.asm.Type in project powermock by powermock.

the class Analyzer method analyze.

/**
     * Analyzes the given method.
     * 
     * @param owner the internal name of the class to which the method belongs.
     * @param m the method to be analyzed.
     * @return the symbolic state of the execution stack frame at each bytecode
     *         instruction of the method. The size of the returned array is
     *         equal to the number of instructions (and labels) of the method. A
     *         given frame is <tt>null</tt> if and only if the corresponding
     *         instruction cannot be reached (dead code).
     * @throws AnalyzerException if a problem occurs during the analysis.
     */
public Frame[] analyze(final String owner, final MethodNode m) throws AnalyzerException {
    if ((m.access & (ACC_ABSTRACT | ACC_NATIVE)) != 0) {
        frames = new Frame[0];
        return frames;
    }
    n = m.instructions.size();
    insns = m.instructions;
    handlers = new List[n];
    frames = new Frame[n];
    subroutines = new Subroutine[n];
    queued = new boolean[n];
    queue = new int[n];
    top = 0;
    // computes exception handlers for each instruction
    for (int i = 0; i < m.tryCatchBlocks.size(); ++i) {
        TryCatchBlockNode tcb = (TryCatchBlockNode) m.tryCatchBlocks.get(i);
        int begin = insns.indexOf(tcb.start);
        int end = insns.indexOf(tcb.end);
        for (int j = begin; j < end; ++j) {
            List insnHandlers = handlers[j];
            if (insnHandlers == null) {
                insnHandlers = new ArrayList();
                handlers[j] = insnHandlers;
            }
            insnHandlers.add(tcb);
        }
    }
    // computes the subroutine for each instruction:
    Subroutine main = new Subroutine(null, m.maxLocals, null);
    List subroutineCalls = new ArrayList();
    Map subroutineHeads = new HashMap();
    findSubroutine(0, main, subroutineCalls);
    while (!subroutineCalls.isEmpty()) {
        JumpInsnNode jsr = (JumpInsnNode) subroutineCalls.remove(0);
        Subroutine sub = (Subroutine) subroutineHeads.get(jsr.label);
        if (sub == null) {
            sub = new Subroutine(jsr.label, m.maxLocals, jsr);
            subroutineHeads.put(jsr.label, sub);
            findSubroutine(insns.indexOf(jsr.label), sub, subroutineCalls);
        } else {
            sub.callers.add(jsr);
        }
    }
    for (int i = 0; i < n; ++i) {
        if (subroutines[i] != null && subroutines[i].start == null) {
            subroutines[i] = null;
        }
    }
    // initializes the data structures for the control flow analysis
    Frame current = newFrame(m.maxLocals, m.maxStack);
    Frame handler = newFrame(m.maxLocals, m.maxStack);
    Type[] args = Type.getArgumentTypes(m.desc);
    int local = 0;
    if ((m.access & ACC_STATIC) == 0) {
        Type ctype = Type.getObjectType(owner);
        current.setLocal(local++, interpreter.newValue(ctype));
    }
    for (int i = 0; i < args.length; ++i) {
        current.setLocal(local++, interpreter.newValue(args[i]));
        if (args[i].getSize() == 2) {
            current.setLocal(local++, interpreter.newValue(null));
        }
    }
    while (local < m.maxLocals) {
        current.setLocal(local++, interpreter.newValue(null));
    }
    merge(0, current, null);
    // control flow analysis
    while (top > 0) {
        int insn = queue[--top];
        Frame f = frames[insn];
        Subroutine subroutine = subroutines[insn];
        queued[insn] = false;
        try {
            AbstractInsnNode insnNode = m.instructions.get(insn);
            int insnOpcode = insnNode.getOpcode();
            int insnType = insnNode.getType();
            if (insnType == AbstractInsnNode.LABEL || insnType == AbstractInsnNode.LINE || insnType == AbstractInsnNode.FRAME) {
                merge(insn + 1, f, subroutine);
                newControlFlowEdge(insn, insn + 1);
            } else {
                current.init(f).execute(insnNode, interpreter);
                subroutine = subroutine == null ? null : subroutine.copy();
                if (insnNode instanceof JumpInsnNode) {
                    JumpInsnNode j = (JumpInsnNode) insnNode;
                    if (insnOpcode != GOTO && insnOpcode != JSR) {
                        merge(insn + 1, current, subroutine);
                        newControlFlowEdge(insn, insn + 1);
                    }
                    int jump = insns.indexOf(j.label);
                    if (insnOpcode == JSR) {
                        merge(jump, current, new Subroutine(j.label, m.maxLocals, j));
                    } else {
                        merge(jump, current, subroutine);
                    }
                    newControlFlowEdge(insn, jump);
                } else if (insnNode instanceof LookupSwitchInsnNode) {
                    LookupSwitchInsnNode lsi = (LookupSwitchInsnNode) insnNode;
                    int jump = insns.indexOf(lsi.dflt);
                    merge(jump, current, subroutine);
                    newControlFlowEdge(insn, jump);
                    for (int j = 0; j < lsi.labels.size(); ++j) {
                        LabelNode label = (LabelNode) lsi.labels.get(j);
                        jump = insns.indexOf(label);
                        merge(jump, current, subroutine);
                        newControlFlowEdge(insn, jump);
                    }
                } else if (insnNode instanceof TableSwitchInsnNode) {
                    TableSwitchInsnNode tsi = (TableSwitchInsnNode) insnNode;
                    int jump = insns.indexOf(tsi.dflt);
                    merge(jump, current, subroutine);
                    newControlFlowEdge(insn, jump);
                    for (int j = 0; j < tsi.labels.size(); ++j) {
                        LabelNode label = (LabelNode) tsi.labels.get(j);
                        jump = insns.indexOf(label);
                        merge(jump, current, subroutine);
                        newControlFlowEdge(insn, jump);
                    }
                } else if (insnOpcode == RET) {
                    if (subroutine == null) {
                        throw new AnalyzerException("RET instruction outside of a sub routine");
                    }
                    for (int i = 0; i < subroutine.callers.size(); ++i) {
                        Object caller = subroutine.callers.get(i);
                        int call = insns.indexOf((AbstractInsnNode) caller);
                        if (frames[call] != null) {
                            merge(call + 1, frames[call], current, subroutines[call], subroutine.access);
                            newControlFlowEdge(insn, call + 1);
                        }
                    }
                } else if (insnOpcode != ATHROW && (insnOpcode < IRETURN || insnOpcode > RETURN)) {
                    if (subroutine != null) {
                        if (insnNode instanceof VarInsnNode) {
                            int var = ((VarInsnNode) insnNode).var;
                            subroutine.access[var] = true;
                            if (insnOpcode == LLOAD || insnOpcode == DLOAD || insnOpcode == LSTORE || insnOpcode == DSTORE) {
                                subroutine.access[var + 1] = true;
                            }
                        } else if (insnNode instanceof IincInsnNode) {
                            int var = ((IincInsnNode) insnNode).var;
                            subroutine.access[var] = true;
                        }
                    }
                    merge(insn + 1, current, subroutine);
                    newControlFlowEdge(insn, insn + 1);
                }
            }
            List insnHandlers = handlers[insn];
            if (insnHandlers != null) {
                for (int i = 0; i < insnHandlers.size(); ++i) {
                    TryCatchBlockNode tcb = (TryCatchBlockNode) insnHandlers.get(i);
                    Type type;
                    if (tcb.type == null) {
                        type = Type.getObjectType("java/lang/Throwable");
                    } else {
                        type = Type.getObjectType(tcb.type);
                    }
                    int jump = insns.indexOf(tcb.handler);
                    if (newControlFlowExceptionEdge(insn, jump)) {
                        handler.init(f);
                        handler.clearStack();
                        handler.push(interpreter.newValue(type));
                        merge(jump, handler, subroutine);
                    }
                }
            }
        } catch (AnalyzerException e) {
            throw new AnalyzerException("Error at instruction " + insn + ": " + e.getMessage(), e);
        } catch (Exception e) {
            throw new AnalyzerException("Error at instruction " + insn + ": " + e.getMessage(), e);
        }
    }
    return frames;
}
Also used : LabelNode(org.powermock.api.mockito.repackaged.asm.tree.LabelNode) TryCatchBlockNode(org.powermock.api.mockito.repackaged.asm.tree.TryCatchBlockNode) TableSwitchInsnNode(org.powermock.api.mockito.repackaged.asm.tree.TableSwitchInsnNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AbstractInsnNode(org.powermock.api.mockito.repackaged.asm.tree.AbstractInsnNode) Type(org.powermock.api.mockito.repackaged.asm.Type) JumpInsnNode(org.powermock.api.mockito.repackaged.asm.tree.JumpInsnNode) IincInsnNode(org.powermock.api.mockito.repackaged.asm.tree.IincInsnNode) InsnList(org.powermock.api.mockito.repackaged.asm.tree.InsnList) ArrayList(java.util.ArrayList) List(java.util.List) LookupSwitchInsnNode(org.powermock.api.mockito.repackaged.asm.tree.LookupSwitchInsnNode) VarInsnNode(org.powermock.api.mockito.repackaged.asm.tree.VarInsnNode) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with Type

use of org.powermock.api.mockito.repackaged.asm.Type in project powermock by powermock.

the class BasicVerifier method naryOperation.

public Value naryOperation(final AbstractInsnNode insn, final List values) throws AnalyzerException {
    int opcode = insn.getOpcode();
    if (opcode == MULTIANEWARRAY) {
        for (int i = 0; i < values.size(); ++i) {
            if (values.get(i) != BasicValue.INT_VALUE) {
                throw new AnalyzerException(null, BasicValue.INT_VALUE, (Value) values.get(i));
            }
        }
    } else {
        int i = 0;
        int j = 0;
        if (opcode != INVOKESTATIC) {
            Type owner = Type.getObjectType(((MethodInsnNode) insn).owner);
            if (!isSubTypeOf((Value) values.get(i++), newValue(owner))) {
                throw new AnalyzerException("Method owner", newValue(owner), (Value) values.get(0));
            }
        }
        Type[] args = Type.getArgumentTypes(((MethodInsnNode) insn).desc);
        while (i < values.size()) {
            Value expected = newValue(args[j++]);
            Value encountered = (Value) values.get(i++);
            if (!isSubTypeOf(encountered, expected)) {
                throw new AnalyzerException("Argument " + j, expected, encountered);
            }
        }
    }
    return super.naryOperation(insn, values);
}
Also used : Type(org.powermock.api.mockito.repackaged.asm.Type)

Aggregations

Type (org.powermock.api.mockito.repackaged.asm.Type)29 Label (org.powermock.api.mockito.repackaged.asm.Label)9 CodeEmitter (org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter)9 List (java.util.List)6 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 CodeGenerationException (org.powermock.api.mockito.repackaged.cglib.core.CodeGenerationException)3 Signature (org.powermock.api.mockito.repackaged.cglib.core.Signature)3 TryCatchBlockNode (org.powermock.api.mockito.repackaged.asm.tree.TryCatchBlockNode)2 ClassEmitter (org.powermock.api.mockito.repackaged.cglib.core.ClassEmitter)2 ObjectSwitchCallback (org.powermock.api.mockito.repackaged.cglib.core.ObjectSwitchCallback)2 ProcessSwitchCallback (org.powermock.api.mockito.repackaged.cglib.core.ProcessSwitchCallback)2 Method (java.lang.reflect.Method)1 Iterator (java.util.Iterator)1 AbstractInsnNode (org.powermock.api.mockito.repackaged.asm.tree.AbstractInsnNode)1 ClassNode (org.powermock.api.mockito.repackaged.asm.tree.ClassNode)1 IincInsnNode (org.powermock.api.mockito.repackaged.asm.tree.IincInsnNode)1 InsnList (org.powermock.api.mockito.repackaged.asm.tree.InsnList)1 JumpInsnNode (org.powermock.api.mockito.repackaged.asm.tree.JumpInsnNode)1