Exemplo n.º 1
0
        internal FastFieldReflector(java.io.ObjectStreamField[] fields)
        {
            this.fields = fields;
            TypeWrapper tw = null;

            foreach (java.io.ObjectStreamField field in fields)
            {
                FieldWrapper fw = GetFieldWrapper(field);
                if (fw != null)
                {
                    if (tw == null)
                    {
                        tw = fw.DeclaringType;
                    }
                    else if (tw != fw.DeclaringType)
                    {
                        // pre-condition is that all fields are from the same Type!
                        throw new java.lang.InternalError();
                    }
                }
            }
            if (tw == null)
            {
                objFieldGetter  = objFieldSetter = objDummy;
                primFieldGetter = primFieldSetter = primDummy;
            }
            else
            {
                try
                {
                    tw.Finish();
                }
                catch (RetargetableJavaException x)
                {
                    throw x.ToJava();
                }
                DynamicMethod dmObjGetter     = DynamicMethodUtils.Create("__<ObjFieldGetter>", tw.TypeAsBaseType, true, null, new Type[] { typeof(object), typeof(object[]) });
                DynamicMethod dmPrimGetter    = DynamicMethodUtils.Create("__<PrimFieldGetter>", tw.TypeAsBaseType, true, null, new Type[] { typeof(object), typeof(byte[]) });
                DynamicMethod dmObjSetter     = DynamicMethodUtils.Create("__<ObjFieldSetter>", tw.TypeAsBaseType, true, null, new Type[] { typeof(object), typeof(object[]) });
                DynamicMethod dmPrimSetter    = DynamicMethodUtils.Create("__<PrimFieldSetter>", tw.TypeAsBaseType, true, null, new Type[] { typeof(object), typeof(byte[]) });
                CodeEmitter   ilgenObjGetter  = CodeEmitter.Create(dmObjGetter);
                CodeEmitter   ilgenPrimGetter = CodeEmitter.Create(dmPrimGetter);
                CodeEmitter   ilgenObjSetter  = CodeEmitter.Create(dmObjSetter);
                CodeEmitter   ilgenPrimSetter = CodeEmitter.Create(dmPrimSetter);

                // we want the getters to be verifiable (because writeObject can be used from partial trust),
                // so we create a local to hold the properly typed object reference
                CodeEmitterLocal objGetterThis  = ilgenObjGetter.DeclareLocal(tw.TypeAsBaseType);
                CodeEmitterLocal primGetterThis = ilgenPrimGetter.DeclareLocal(tw.TypeAsBaseType);
                ilgenObjGetter.Emit(OpCodes.Ldarg_0);
                ilgenObjGetter.Emit(OpCodes.Castclass, tw.TypeAsBaseType);
                ilgenObjGetter.Emit(OpCodes.Stloc, objGetterThis);
                ilgenPrimGetter.Emit(OpCodes.Ldarg_0);
                ilgenPrimGetter.Emit(OpCodes.Castclass, tw.TypeAsBaseType);
                ilgenPrimGetter.Emit(OpCodes.Stloc, primGetterThis);

                foreach (java.io.ObjectStreamField field in fields)
                {
                    FieldWrapper fw = GetFieldWrapper(field);
                    if (fw == null)
                    {
                        continue;
                    }
                    fw.ResolveField();
                    TypeWrapper fieldType = fw.FieldTypeWrapper;
                    try
                    {
                        fieldType = fieldType.EnsureLoadable(tw.GetClassLoader());
                        fieldType.Finish();
                    }
                    catch (RetargetableJavaException x)
                    {
                        throw x.ToJava();
                    }
                    if (fieldType.IsPrimitive)
                    {
                        // Getter
                        ilgenPrimGetter.Emit(OpCodes.Ldarg_1);
                        ilgenPrimGetter.EmitLdc_I4(field.getOffset());
                        ilgenPrimGetter.Emit(OpCodes.Ldloc, primGetterThis);
                        fw.EmitGet(ilgenPrimGetter);
                        if (fieldType == PrimitiveTypeWrapper.BYTE)
                        {
                            ilgenPrimGetter.Emit(OpCodes.Call, WriteByteMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.BOOLEAN)
                        {
                            ilgenPrimGetter.Emit(OpCodes.Call, WriteBooleanMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.CHAR)
                        {
                            ilgenPrimGetter.Emit(OpCodes.Call, WriteCharMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.SHORT)
                        {
                            ilgenPrimGetter.Emit(OpCodes.Call, WriteShortMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.INT)
                        {
                            ilgenPrimGetter.Emit(OpCodes.Call, WriteIntMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.FLOAT)
                        {
                            ilgenPrimGetter.Emit(OpCodes.Call, WriteFloatMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.LONG)
                        {
                            ilgenPrimGetter.Emit(OpCodes.Call, WriteLongMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.DOUBLE)
                        {
                            ilgenPrimGetter.Emit(OpCodes.Call, WriteDoubleMethod);
                        }
                        else
                        {
                            throw new java.lang.InternalError();
                        }

                        // Setter
                        ilgenPrimSetter.Emit(OpCodes.Ldarg_0);
                        ilgenPrimSetter.Emit(OpCodes.Castclass, tw.TypeAsBaseType);
                        ilgenPrimSetter.Emit(OpCodes.Ldarg_1);
                        ilgenPrimSetter.EmitLdc_I4(field.getOffset());
                        if (fieldType == PrimitiveTypeWrapper.BYTE)
                        {
                            ilgenPrimSetter.Emit(OpCodes.Call, ReadByteMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.BOOLEAN)
                        {
                            ilgenPrimSetter.Emit(OpCodes.Call, ReadBooleanMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.CHAR)
                        {
                            ilgenPrimSetter.Emit(OpCodes.Call, ReadCharMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.SHORT)
                        {
                            ilgenPrimSetter.Emit(OpCodes.Call, ReadShortMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.INT)
                        {
                            ilgenPrimSetter.Emit(OpCodes.Call, ReadIntMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.FLOAT)
                        {
                            ilgenPrimSetter.Emit(OpCodes.Call, ReadFloatMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.LONG)
                        {
                            ilgenPrimSetter.Emit(OpCodes.Call, ReadLongMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.DOUBLE)
                        {
                            ilgenPrimSetter.Emit(OpCodes.Call, ReadDoubleMethod);
                        }
                        else
                        {
                            throw new java.lang.InternalError();
                        }
                        fw.EmitSet(ilgenPrimSetter);
                    }
                    else
                    {
                        // Getter
                        ilgenObjGetter.Emit(OpCodes.Ldarg_1);
                        ilgenObjGetter.EmitLdc_I4(field.getOffset());
                        ilgenObjGetter.Emit(OpCodes.Ldloc, objGetterThis);
                        fw.EmitGet(ilgenObjGetter);
                        fieldType.EmitConvSignatureTypeToStackType(ilgenObjGetter);
                        ilgenObjGetter.Emit(OpCodes.Stelem_Ref);

                        // Setter
                        ilgenObjSetter.Emit(OpCodes.Ldarg_0);
                        ilgenObjSetter.Emit(OpCodes.Ldarg_1);
                        ilgenObjSetter.EmitLdc_I4(field.getOffset());
                        ilgenObjSetter.Emit(OpCodes.Ldelem_Ref);
                        fieldType.EmitCheckcast(ilgenObjSetter);
                        fieldType.EmitConvStackTypeToSignatureType(ilgenObjSetter, null);
                        fw.EmitSet(ilgenObjSetter);
                    }
                }
                ilgenObjGetter.Emit(OpCodes.Ret);
                ilgenPrimGetter.Emit(OpCodes.Ret);
                ilgenObjSetter.Emit(OpCodes.Ret);
                ilgenPrimSetter.Emit(OpCodes.Ret);
                ilgenObjGetter.DoEmit();
                ilgenPrimGetter.DoEmit();
                ilgenObjSetter.DoEmit();
                ilgenPrimSetter.DoEmit();
                objFieldGetter  = (ObjFieldGetterSetter)dmObjGetter.CreateDelegate(typeof(ObjFieldGetterSetter));
                primFieldGetter = (PrimFieldGetterSetter)dmPrimGetter.CreateDelegate(typeof(PrimFieldGetterSetter));
                objFieldSetter  = (ObjFieldGetterSetter)dmObjSetter.CreateDelegate(typeof(ObjFieldGetterSetter));
                primFieldSetter = (PrimFieldGetterSetter)dmPrimSetter.CreateDelegate(typeof(PrimFieldGetterSetter));
            }
        }
Exemplo n.º 2
0
        internal FastFieldReflector(java.io.ObjectStreamField[] fields)
        {
            this.fields = fields;
            TypeWrapper tw = null;
            foreach (java.io.ObjectStreamField field in fields)
            {
                FieldWrapper fw = GetFieldWrapper(field);
                if (fw != null)
                {
                    if (tw == null)
                    {
                        tw = fw.DeclaringType;
                    }
                    else if (tw != fw.DeclaringType)
                    {
                        // pre-condition is that all fields are from the same Type!
                        throw new java.lang.InternalError();
                    }
                }
            }
            if (tw == null)
            {
                objFieldGetter = objFieldSetter = objDummy;
                primFieldGetter = primFieldSetter = primDummy;
            }
            else
            {
                try
                {
                    tw.Finish();
                }
                catch (RetargetableJavaException x)
                {
                    throw x.ToJava();
                }
                DynamicMethod dmObjGetter = DynamicMethodUtils.Create("__<ObjFieldGetter>", tw.TypeAsBaseType, true, null, new Type[] { typeof(object), typeof(object[]) });
                DynamicMethod dmPrimGetter = DynamicMethodUtils.Create("__<PrimFieldGetter>", tw.TypeAsBaseType, true, null, new Type[] { typeof(object), typeof(byte[]) });
                DynamicMethod dmObjSetter = DynamicMethodUtils.Create("__<ObjFieldSetter>", tw.TypeAsBaseType, true, null, new Type[] { typeof(object), typeof(object[]) });
                DynamicMethod dmPrimSetter = DynamicMethodUtils.Create("__<PrimFieldSetter>", tw.TypeAsBaseType, true, null, new Type[] { typeof(object), typeof(byte[]) });
                CodeEmitter ilgenObjGetter = CodeEmitter.Create(dmObjGetter);
                CodeEmitter ilgenPrimGetter = CodeEmitter.Create(dmPrimGetter);
                CodeEmitter ilgenObjSetter = CodeEmitter.Create(dmObjSetter);
                CodeEmitter ilgenPrimSetter = CodeEmitter.Create(dmPrimSetter);

                // we want the getters to be verifiable (because writeObject can be used from partial trust),
                // so we create a local to hold the properly typed object reference
                CodeEmitterLocal objGetterThis = ilgenObjGetter.DeclareLocal(tw.TypeAsBaseType);
                CodeEmitterLocal primGetterThis = ilgenPrimGetter.DeclareLocal(tw.TypeAsBaseType);
                ilgenObjGetter.Emit(OpCodes.Ldarg_0);
                ilgenObjGetter.Emit(OpCodes.Castclass, tw.TypeAsBaseType);
                ilgenObjGetter.Emit(OpCodes.Stloc, objGetterThis);
                ilgenPrimGetter.Emit(OpCodes.Ldarg_0);
                ilgenPrimGetter.Emit(OpCodes.Castclass, tw.TypeAsBaseType);
                ilgenPrimGetter.Emit(OpCodes.Stloc, primGetterThis);

                foreach (java.io.ObjectStreamField field in fields)
                {
                    FieldWrapper fw = GetFieldWrapper(field);
                    if (fw == null)
                    {
                        continue;
                    }
                    fw.ResolveField();
                    TypeWrapper fieldType = fw.FieldTypeWrapper;
                    try
                    {
                        fieldType = fieldType.EnsureLoadable(tw.GetClassLoader());
                        fieldType.Finish();
                    }
                    catch (RetargetableJavaException x)
                    {
                        throw x.ToJava();
                    }
                    if (fieldType.IsPrimitive)
                    {
                        // Getter
                        ilgenPrimGetter.Emit(OpCodes.Ldarg_1);
                        ilgenPrimGetter.EmitLdc_I4(field.getOffset());
                        ilgenPrimGetter.Emit(OpCodes.Ldloc, primGetterThis);
                        fw.EmitGet(ilgenPrimGetter);
                        if (fieldType == PrimitiveTypeWrapper.BYTE)
                        {
                            ilgenPrimGetter.Emit(OpCodes.Call, WriteByteMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.BOOLEAN)
                        {
                            ilgenPrimGetter.Emit(OpCodes.Call, WriteBooleanMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.CHAR)
                        {
                            ilgenPrimGetter.Emit(OpCodes.Call, WriteCharMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.SHORT)
                        {
                            ilgenPrimGetter.Emit(OpCodes.Call, WriteShortMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.INT)
                        {
                            ilgenPrimGetter.Emit(OpCodes.Call, WriteIntMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.FLOAT)
                        {
                            ilgenPrimGetter.Emit(OpCodes.Call, WriteFloatMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.LONG)
                        {
                            ilgenPrimGetter.Emit(OpCodes.Call, WriteLongMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.DOUBLE)
                        {
                            ilgenPrimGetter.Emit(OpCodes.Call, WriteDoubleMethod);
                        }
                        else
                        {
                            throw new java.lang.InternalError();
                        }

                        // Setter
                        ilgenPrimSetter.Emit(OpCodes.Ldarg_0);
                        ilgenPrimSetter.Emit(OpCodes.Castclass, tw.TypeAsBaseType);
                        ilgenPrimSetter.Emit(OpCodes.Ldarg_1);
                        ilgenPrimSetter.EmitLdc_I4(field.getOffset());
                        if (fieldType == PrimitiveTypeWrapper.BYTE)
                        {
                            ilgenPrimSetter.Emit(OpCodes.Call, ReadByteMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.BOOLEAN)
                        {
                            ilgenPrimSetter.Emit(OpCodes.Call, ReadBooleanMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.CHAR)
                        {
                            ilgenPrimSetter.Emit(OpCodes.Call, ReadCharMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.SHORT)
                        {
                            ilgenPrimSetter.Emit(OpCodes.Call, ReadShortMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.INT)
                        {
                            ilgenPrimSetter.Emit(OpCodes.Call, ReadIntMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.FLOAT)
                        {
                            ilgenPrimSetter.Emit(OpCodes.Call, ReadFloatMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.LONG)
                        {
                            ilgenPrimSetter.Emit(OpCodes.Call, ReadLongMethod);
                        }
                        else if (fieldType == PrimitiveTypeWrapper.DOUBLE)
                        {
                            ilgenPrimSetter.Emit(OpCodes.Call, ReadDoubleMethod);
                        }
                        else
                        {
                            throw new java.lang.InternalError();
                        }
                        fw.EmitSet(ilgenPrimSetter);
                    }
                    else
                    {
                        // Getter
                        ilgenObjGetter.Emit(OpCodes.Ldarg_1);
                        ilgenObjGetter.EmitLdc_I4(field.getOffset());
                        ilgenObjGetter.Emit(OpCodes.Ldloc, objGetterThis);
                        fw.EmitGet(ilgenObjGetter);
                        fieldType.EmitConvSignatureTypeToStackType(ilgenObjGetter);
                        ilgenObjGetter.Emit(OpCodes.Stelem_Ref);

                        // Setter
                        ilgenObjSetter.Emit(OpCodes.Ldarg_0);
                        ilgenObjSetter.Emit(OpCodes.Ldarg_1);
                        ilgenObjSetter.EmitLdc_I4(field.getOffset());
                        ilgenObjSetter.Emit(OpCodes.Ldelem_Ref);
                        fieldType.EmitCheckcast(ilgenObjSetter);
                        fieldType.EmitConvStackTypeToSignatureType(ilgenObjSetter, null);
                        fw.EmitSet(ilgenObjSetter);
                    }
                }
                ilgenObjGetter.Emit(OpCodes.Ret);
                ilgenPrimGetter.Emit(OpCodes.Ret);
                ilgenObjSetter.Emit(OpCodes.Ret);
                ilgenPrimSetter.Emit(OpCodes.Ret);
                ilgenObjGetter.DoEmit();
                ilgenPrimGetter.DoEmit();
                ilgenObjSetter.DoEmit();
                ilgenPrimSetter.DoEmit();
                objFieldGetter = (ObjFieldGetterSetter)dmObjGetter.CreateDelegate(typeof(ObjFieldGetterSetter));
                primFieldGetter = (PrimFieldGetterSetter)dmPrimGetter.CreateDelegate(typeof(PrimFieldGetterSetter));
                objFieldSetter = (ObjFieldGetterSetter)dmObjSetter.CreateDelegate(typeof(ObjFieldGetterSetter));
                primFieldSetter = (PrimFieldGetterSetter)dmPrimSetter.CreateDelegate(typeof(PrimFieldGetterSetter));
            }
        }