Search in sources :

Example 26 with Type

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

the class EmitUtils method member_switch_helper.

private static void member_switch_helper(final CodeEmitter e, List members, final ObjectSwitchCallback callback, boolean useName) {
    try {
        final Map cache = new HashMap();
        final ParameterTyper cached = new ParameterTyper() {

            public Type[] getParameterTypes(MethodInfo member) {
                Type[] types = (Type[]) cache.get(member);
                if (types == null) {
                    cache.put(member, types = member.getSignature().getArgumentTypes());
                }
                return types;
            }
        };
        final Label def = e.make_label();
        final Label end = e.make_label();
        if (useName) {
            e.swap();
            final Map buckets = CollectionUtils.bucket(members, new Transformer() {

                public Object transform(Object value) {
                    return ((MethodInfo) value).getSignature().getName();
                }
            });
            String[] names = (String[]) buckets.keySet().toArray(new String[buckets.size()]);
            EmitUtils.string_switch(e, names, Constants.SWITCH_STYLE_HASH, new ObjectSwitchCallback() {

                public void processCase(Object key, Label dontUseEnd) throws Exception {
                    member_helper_size(e, (List) buckets.get(key), callback, cached, def, end);
                }

                public void processDefault() throws Exception {
                    e.goTo(def);
                }
            });
        } else {
            member_helper_size(e, members, callback, cached, def, end);
        }
        e.mark(def);
        e.pop();
        callback.processDefault();
        e.mark(end);
    } catch (RuntimeException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new CodeGenerationException(ex);
    }
}
Also used : HashMap(java.util.HashMap) Label(org.powermock.api.mockito.repackaged.asm.Label) Type(org.powermock.api.mockito.repackaged.asm.Type) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 27 with Type

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

the class SimpleVerifier method merge.

public Value merge(final Value v, final Value w) {
    if (!v.equals(w)) {
        Type t = ((BasicValue) v).getType();
        Type u = ((BasicValue) w).getType();
        if (t != null && (t.getSort() == Type.OBJECT || t.getSort() == Type.ARRAY)) {
            if (u != null && (u.getSort() == Type.OBJECT || u.getSort() == Type.ARRAY)) {
                if ("Lnull;".equals(t.getDescriptor())) {
                    return w;
                }
                if ("Lnull;".equals(u.getDescriptor())) {
                    return v;
                }
                if (isAssignableFrom(t, u)) {
                    return v;
                }
                if (isAssignableFrom(u, t)) {
                    return w;
                }
                // interfaces
                do {
                    if (t == null || isInterface(t)) {
                        return BasicValue.REFERENCE_VALUE;
                    }
                    t = getSuperClass(t);
                    if (isAssignableFrom(t, u)) {
                        return newValue(t);
                    }
                } while (true);
            }
        }
        return BasicValue.UNINITIALIZED_VALUE;
    }
    return v;
}
Also used : Type(org.powermock.api.mockito.repackaged.asm.Type)

Example 28 with Type

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

the class CheckClassAdapter method verify.

/**
     * Checks a given class
     *
     * @param cr a {@code ClassReader} that contains bytecode for the
     *        analysis.
     * @param dump true if bytecode should be printed out not only when errors
     *        are found.
     * @param pw write where results going to be printed
     */
public static void verify(final ClassReader cr, final boolean dump, final PrintWriter pw) {
    ClassNode cn = new ClassNode();
    cr.accept(new CheckClassAdapter(cn), ClassReader.SKIP_DEBUG);
    Type syperType = cn.superName == null ? null : Type.getObjectType(cn.superName);
    List methods = cn.methods;
    for (int i = 0; i < methods.size(); ++i) {
        MethodNode method = (MethodNode) methods.get(i);
        Analyzer a = new Analyzer(new SimpleVerifier(Type.getObjectType(cn.name), syperType, false));
        try {
            a.analyze(cn.name, method);
            if (!dump) {
                continue;
            }
        } catch (Exception e) {
            e.printStackTrace(pw);
        }
        Frame[] frames = a.getFrames();
        TraceMethodVisitor mv = new TraceMethodVisitor();
        pw.println(method.name + method.desc);
        for (int j = 0; j < method.instructions.size(); ++j) {
            method.instructions.get(j).accept(mv);
            StringBuilder s = new StringBuilder();
            Frame f = frames[j];
            if (f == null) {
                s.append('?');
            } else {
                for (int k = 0; k < f.getLocals(); ++k) {
                    s.append(getShortName(f.getLocal(k).toString())).append(' ');
                }
                s.append(" : ");
                for (int k = 0; k < f.getStackSize(); ++k) {
                    s.append(getShortName(f.getStack(k).toString())).append(' ');
                }
            }
            while (s.length() < method.maxStack + method.maxLocals + 1) {
                s.append(' ');
            }
            pw.print(Integer.toString(j + 100000).substring(1));
            // mv.text.get(j));
            pw.print(" " + s + " : " + mv.buf);
        }
        for (int j = 0; j < method.tryCatchBlocks.size(); ++j) {
            ((TryCatchBlockNode) method.tryCatchBlocks.get(j)).accept(mv);
            pw.print(" " + mv.buf);
        }
        pw.println();
    }
    pw.flush();
}
Also used : ClassNode(org.powermock.api.mockito.repackaged.asm.tree.ClassNode) TryCatchBlockNode(org.powermock.api.mockito.repackaged.asm.tree.TryCatchBlockNode) Frame(org.powermock.api.mockito.repackaged.asm.tree.analysis.Frame) SimpleVerifier(org.powermock.api.mockito.repackaged.asm.tree.analysis.SimpleVerifier) Analyzer(org.powermock.api.mockito.repackaged.asm.tree.analysis.Analyzer) Type(org.powermock.api.mockito.repackaged.asm.Type) MethodNode(org.powermock.api.mockito.repackaged.asm.tree.MethodNode) List(java.util.List)

Example 29 with Type

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

the class ParallelSorterEmitter method generateSwap.

private void generateSwap(final Object[] arrays) {
    CodeEmitter e = begin_method(Constants.ACC_PUBLIC, SWAP, null);
    for (int i = 0; i < arrays.length; i++) {
        Type type = Type.getType(arrays[i].getClass());
        Type component = TypeUtils.getComponentType(type);
        Local T = e.make_local(type);
        e.load_this();
        e.getfield(getFieldName(i));
        e.store_local(T);
        e.load_local(T);
        e.load_arg(0);
        e.load_local(T);
        e.load_arg(1);
        e.array_load(component);
        e.load_local(T);
        e.load_arg(1);
        e.load_local(T);
        e.load_arg(0);
        e.array_load(component);
        e.array_store(component);
        e.array_store(component);
    }
    e.return_value();
    e.end_method();
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) Type(org.powermock.api.mockito.repackaged.asm.Type) Local(org.powermock.api.mockito.repackaged.cglib.core.Local)

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