Search in sources :

Example 31 with CodeEmitter

use of org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter in project powermock by powermock.

the class FieldProviderTransformer method getField.

// TODO: if this is used to enhance class files SWITCH_STYLE_TRIE should be used
// to avoid JVM hashcode implementation incompatibilities
private void getField(String[] names) throws Exception {
    final CodeEmitter e = begin_method(Constants.ACC_PUBLIC, PROVIDER_GET, null);
    e.load_this();
    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.getfield((String) key);
            e.box(type);
            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 32 with CodeEmitter

use of org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter in project powermock by powermock.

the class FieldProviderTransformer method initFieldProvider.

private void initFieldProvider(String[] names) {
    CodeEmitter e = getStaticHook();
    EmitUtils.push_object(e, names);
    e.putstatic(getClassType(), FIELD_NAMES, Constants.TYPE_STRING_ARRAY);
    e.push(names.length);
    e.newarray(Constants.TYPE_CLASS);
    e.dup();
    for (int i = 0; i < names.length; i++) {
        e.dup();
        e.push(i);
        Type type = (Type) fields.get(names[i]);
        EmitUtils.load_class(e, type);
        e.aastore();
    }
    e.putstatic(getClassType(), FIELD_TYPES, Constants.TYPE_CLASS_ARRAY);
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) Type(org.powermock.api.mockito.repackaged.asm.Type)

Example 33 with CodeEmitter

use of org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter in project powermock by powermock.

the class FieldProviderTransformer method setByIndex.

private void setByIndex(final String[] names, final int[] indexes) throws Exception {
    final CodeEmitter e = super.begin_method(Constants.ACC_PUBLIC, PROVIDER_SET_BY_INDEX, null);
    e.load_this();
    e.load_arg(1);
    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.unbox(type);
            e.putfield(names[key]);
            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 34 with CodeEmitter

use of org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter in project powermock by powermock.

the class FieldProviderTransformer method getTypes.

private void getTypes() {
    CodeEmitter e = super.begin_method(Constants.ACC_PUBLIC, PROVIDER_GET_TYPES, null);
    e.getstatic(getClassType(), FIELD_TYPES, Constants.TYPE_CLASS_ARRAY);
    e.return_value();
    e.end_method();
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter)

Example 35 with CodeEmitter

use of org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter in project powermock by powermock.

the class InterceptFieldTransformer method begin_method.

public CodeEmitter begin_method(int access, Signature sig, Type[] exceptions) {
    return new CodeEmitter(super.begin_method(access, sig, exceptions)) {

        public void visitFieldInsn(int opcode, String owner, String name, String desc) {
            Type towner = TypeUtils.fromInternalName(owner);
            switch(opcode) {
                case Constants.GETFIELD:
                    if (filter.acceptRead(towner, name)) {
                        helper(towner, readMethodSig(name, desc));
                        return;
                    }
                    break;
                case Constants.PUTFIELD:
                    if (filter.acceptWrite(towner, name)) {
                        helper(towner, writeMethodSig(name, desc));
                        return;
                    }
                    break;
            }
            super.visitFieldInsn(opcode, owner, name, desc);
        }

        private void helper(Type owner, Signature sig) {
            invoke_virtual(owner, sig);
        }
    };
}
Also used : CodeEmitter(org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter) Type(org.powermock.api.mockito.repackaged.asm.Type) Signature(org.powermock.api.mockito.repackaged.cglib.core.Signature)

Aggregations

CodeEmitter (org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter)47 Label (org.powermock.api.mockito.repackaged.asm.Label)17 MethodInfo (org.powermock.api.mockito.repackaged.cglib.core.MethodInfo)13 Type (org.powermock.api.mockito.repackaged.asm.Type)9 Iterator (java.util.Iterator)8 ObjectSwitchCallback (org.powermock.api.mockito.repackaged.cglib.core.ObjectSwitchCallback)7 Signature (org.powermock.api.mockito.repackaged.cglib.core.Signature)6 Local (org.powermock.api.mockito.repackaged.cglib.core.Local)5 CodeGenerationException (org.powermock.api.mockito.repackaged.cglib.core.CodeGenerationException)4 ProcessSwitchCallback (org.powermock.api.mockito.repackaged.cglib.core.ProcessSwitchCallback)4 PropertyDescriptor (java.beans.PropertyDescriptor)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Block (org.powermock.api.mockito.repackaged.cglib.core.Block)3 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 MethodInfoTransformer (org.powermock.api.mockito.repackaged.cglib.core.MethodInfoTransformer)2