示例#1
0
        /// <summary> </summary>
        /// <param name="clazz">The class to instrospect
        /// </param>
        /// <param name="recursive">If true, goes does the hierarchy to try to analyse all classes
        /// </param>
        /// <param name="A">map with classname that are being introspected, to avoid recursive calls
        ///
        /// </param>
        /// <returns>
        /// </returns>
        private ClassInfoList InternalIntrospect(System.Type clazz, bool recursive, ClassInfoList classInfoList)
        {
            if (classInfoList != null)
            {
                ClassInfo existingCi = (ClassInfo)classInfoList.GetClassInfoWithName(OdbClassUtil.GetFullName(clazz));
                if (existingCi != null)
                {
                    return(classInfoList);
                }
            }

            ClassInfo classInfo = new ClassInfo(OdbClassUtil.GetFullName(clazz));

            classInfo.SetClassCategory(GetClassCategory(OdbClassUtil.GetFullName(clazz)));
            if (classInfoList == null)
            {
                classInfoList = new ClassInfoList(classInfo);
            }
            else
            {
                classInfoList.AddClassInfo(classInfo);
            }

            // Field[] fields = clazz.getDeclaredFields();
            //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Class.getName' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
            //m by cristi
            IOdbList <FieldInfo>          fields     = GetAllFields(OdbClassUtil.GetFullName(clazz));
            IOdbList <ClassAttributeInfo> attributes = new OdbArrayList <ClassAttributeInfo>(fields.Count);

            ClassInfo ci = null;

            for (int i = 0; i < fields.Count; i++)
            {
                System.Reflection.FieldInfo field = (System.Reflection.FieldInfo)fields[i];
                //Console.WriteLine("Field " + field.Name + " , type = " + field.FieldType);
                if (!ODBType.GetFromClass(field.FieldType).IsNative())
                {
                    if (recursive)
                    {
                        classInfoList = InternalIntrospect(field.FieldType, recursive, classInfoList);
                        ci            = classInfoList.GetClassInfoWithName(OdbClassUtil.GetFullName(field.FieldType));
                    }
                    else
                    {
                        ci = new ClassInfo(OdbClassUtil.GetFullName(field.FieldType));
                    }
                }
                else
                {
                    ci = null;
                }
                attributes.Add(new ClassAttributeInfo((i + 1), field.Name, field.FieldType, OdbClassUtil.GetFullName(field.FieldType), ci));
            }
            classInfo.SetAttributes(attributes);
            classInfo.SetMaxAttributeId(fields.Count);
            return(classInfoList);
        }
示例#2
0
        /// <summary>Builds a class info from a class and an existing class info
        ///
        /// <pre>
        /// The existing class info is used to make sure that fields with the same name will have
        /// the same id
        /// </pre>
        ///
        /// </summary>
        /// <param name="fullClassName">The name of the class to get info
        /// </param>
        /// <param name="existingClassInfo">
        /// </param>
        /// <returns> A ClassInfo -  a meta representation of the class
        /// </returns>
        public ClassInfo GetClassInfo(System.String fullClassName, ClassInfo existingClassInfo)
        {
            ClassInfo classInfo = new ClassInfo(fullClassName);

            classInfo.SetClassCategory(GetClassCategory(fullClassName));
            ;
            IOdbList <FieldInfo>          fields     = GetAllFields(fullClassName);
            IOdbList <ClassAttributeInfo> attributes = new OdbArrayList <ClassAttributeInfo>(fields.Count);

            int       attributeId    = -1;
            int       maxAttributeId = existingClassInfo.GetMaxAttributeId();
            ClassInfo ci             = null;

            for (int i = 0; i < fields.Count; i++)
            {
                FieldInfo field = fields[i];
                // Gets the attribute id from the existing class info
                attributeId = existingClassInfo.GetAttributeId(field.Name);
                if (attributeId == -1)
                {
                    maxAttributeId++;
                    // The attibute with field.getName() does not exist in existing class info
                    //  create a new id
                    attributeId = maxAttributeId;
                }
                if (!ODBType.GetFromClass(field.FieldType).IsNative())
                {
                    ci = new ClassInfo(OdbClassUtil.GetFullName(field.FieldType));
                }
                else
                {
                    ci = null;
                }

                attributes.Add(new ClassAttributeInfo(attributeId, field.Name, field.FieldType, OdbClassUtil.GetFullName(field.FieldType), ci));
            }
            classInfo.SetAttributes(attributes);
            classInfo.SetMaxAttributeId(maxAttributeId);
            return(classInfo);
        }
 public ClassAttributeInfo(int attributeId, string name, System.Type nativeClass,
                           string fullClassName, ClassInfo info) : base
         ()
 {
     //private transient static int nb=0;
     this.id          = attributeId;
     this.name        = name;
     this.nativeClass = nativeClass;
     SetFullClassName(fullClassName);
     if (nativeClass != null)
     {
         attributeType = ODBType.GetFromClass(nativeClass
                                              );
     }
     else
     {
         if (fullClassName != null)
         {
             attributeType = ODBType.GetFromName(fullClassName);
         }
     }
     classInfo = info;
     isIndex   = false;
 }
        /// <summary>
        /// Build a meta representation of an object
        /// <pre>
        /// warning: When an object has two fields with the same name (a private field with the same name in a parent class, the deeper field (of the parent) is ignored!)
        /// </pre>
        /// </summary>
        /// <param name="o"></param>
        /// <param name="ci"></param>
        /// <param name="recursive"></param>
        /// <returns>The ObjectInfo</returns>
        protected virtual NeoDatis.Odb.Core.Layers.Layer2.Meta.AbstractObjectInfo GetObjectInfoInternal
            (NeoDatis.Odb.Core.Layers.Layer2.Meta.AbstractObjectInfo nnoi, object o, NeoDatis.Odb.Core.Layers.Layer2.Meta.ClassInfo
            ci, bool recursive, System.Collections.Generic.IDictionary <object, NeoDatis.Odb.Core.Layers.Layer2.Meta.NonNativeObjectInfo
                                                                        > alreadyReadObjects, NeoDatis.Odb.Core.Layers.Layer1.Introspector.IIntrospectionCallback
            callback)
        {
            object value = null;

            if (o == null)
            {
                return(NeoDatis.Odb.Core.Layers.Layer2.Meta.NullNativeObjectInfo.GetInstance());
            }
            System.Type clazz = o.GetType();
            NeoDatis.Odb.Core.Layers.Layer2.Meta.ODBType type = NeoDatis.Odb.Core.Layers.Layer2.Meta.ODBType
                                                                .GetFromClass(clazz);
            string className = OdbClassUtil.GetFullName(clazz);

            if (type.IsNative())
            {
                return(GetNativeObjectInfoInternal(type, o, recursive, alreadyReadObjects,
                                                   callback));
            }
            // sometimes the clazz.getName() may not match the ci.getClassName()
            // It happens when the attribute is an interface or superclass of the
            // real attribute class
            // In this case, ci must be updated to the real class info
            if (ci != null && !clazz.FullName.Equals(ci.GetFullClassName()))
            {
                ci   = GetClassInfo(className);
                nnoi = null;
            }
            NeoDatis.Odb.Core.Layers.Layer2.Meta.NonNativeObjectInfo mainAoi = (NeoDatis.Odb.Core.Layers.Layer2.Meta.NonNativeObjectInfo
                                                                                )nnoi;
            bool isRootObject = false;

            if (alreadyReadObjects == null)
            {
                alreadyReadObjects = new NeoDatis.Tool.Wrappers.Map.OdbHashMap <object, NeoDatis.Odb.Core.Layers.Layer2.Meta.NonNativeObjectInfo
                                                                                >();
                isRootObject = true;
            }
            if (o != null)
            {
                NonNativeObjectInfo cachedNnoi = null;
                alreadyReadObjects.TryGetValue(o, out cachedNnoi);

                if (cachedNnoi != null)
                {
                    ObjectReference or = new ObjectReference(cachedNnoi);
                    return(or);
                }
                if (callback != null)
                {
                    callback.ObjectFound(o);
                }
            }
            if (mainAoi == null)
            {
                mainAoi = BuildNnoi(o, ci, null, null, null, alreadyReadObjects);
            }
            alreadyReadObjects[o] = mainAoi;
            NeoDatis.Tool.Wrappers.List.IOdbList <System.Reflection.FieldInfo> fields = classIntrospector.GetAllFields(className);
            NeoDatis.Odb.Core.Layers.Layer2.Meta.AbstractObjectInfo            aoi    = null;
            int attributeId = -1;

            // For all fields
            for (int i = 0; i < fields.Count; i++)
            {
                System.Reflection.FieldInfo field = fields[i];
                try
                {
                    value       = field.GetValue(o);
                    attributeId = ci.GetAttributeId(field.Name);
                    if (attributeId == -1)
                    {
                        throw new ODBRuntimeException(NeoDatisError.ObjectIntrospectorNoFieldWithName.AddParameter(ci.GetFullClassName()).AddParameter(field.Name));
                    }
                    ODBType valueType = null;
                    if (value == null)
                    {
                        // If value is null, take the type from the field type
                        // declared in the class
                        valueType = ODBType.GetFromClass(field.FieldType);
                    }
                    else
                    {
                        // Else take the real attribute type!
                        valueType = ODBType.GetFromClass(value.GetType());
                    }
                    // for native fields
                    if (valueType.IsNative())
                    {
                        aoi = GetNativeObjectInfoInternal(valueType, value, recursive, alreadyReadObjects, callback);
                        mainAoi.SetAttributeValue(attributeId, aoi);
                    }
                    else
                    {
                        //callback.objectFound(value);
                        // Non Native Objects
                        if (value == null)
                        {
                            ClassInfo clai = GetClassInfo(OdbClassUtil.GetFullName(field.GetType()));

                            aoi = new NeoDatis.Odb.Core.Layers.Layer2.Meta.NonNativeNullObjectInfo(clai);
                            mainAoi.SetAttributeValue(attributeId, aoi);
                        }
                        else
                        {
                            ClassInfo clai = GetClassInfo(OdbClassUtil.GetFullName(value.GetType()));
                            if (recursive)
                            {
                                aoi = GetObjectInfoInternal(null, value, clai, recursive, alreadyReadObjects, callback
                                                            );
                                mainAoi.SetAttributeValue(attributeId, aoi);
                            }
                            else
                            {
                                // When it is not recursive, simply add the object
                                // values.add(value);
                                throw new NeoDatis.Odb.ODBRuntimeException(NeoDatis.Odb.Core.NeoDatisError.InternalError
                                                                           .AddParameter("Should not enter here - ObjectIntrospector - 'simply add the object'"
                                                                                         ));
                            }
                        }
                    }
                }
                catch (System.ArgumentException e)
                {
                    throw new NeoDatis.Odb.ODBRuntimeException(NeoDatis.Odb.Core.NeoDatisError.InternalError
                                                               .AddParameter("in getObjectInfoInternal"), e);
                }
                catch (System.MemberAccessException e)
                {
                    throw new NeoDatis.Odb.ODBRuntimeException(NeoDatis.Odb.Core.NeoDatisError.InternalError
                                                               .AddParameter("getObjectInfoInternal"), e);
                }
            }
            if (isRootObject)
            {
                alreadyReadObjects.Clear();
                alreadyReadObjects = null;
            }
            return(mainAoi);
        }
示例#5
0
        /// <summary>Store an object with the specific id</summary>
        /// <param name="oid"></param>
        /// <param name="@object"></param>
        /// <returns></returns>
        /// <></>
        protected virtual NeoDatis.Odb.OID InternalStore(OID oid, object o)
        {
            if (GetSession(true).IsRollbacked())
            {
                throw new ODBRuntimeException(NeoDatisError.OdbHasBeenRollbacked.AddParameter(GetBaseIdentification().ToString()));
            }
            if (o == null)
            {
                throw new ODBRuntimeException(NeoDatisError.OdbCanNotStoreNullObject);
            }
            Type clazz = o.GetType();

            if (ODBType.IsNative(clazz))
            {
                throw new ODBRuntimeException(NeoDatisError.OdbCanNotStoreNativeObjectDirectly
                                              .AddParameter(clazz.FullName).AddParameter(ODBType.GetFromClass(clazz).GetName()).AddParameter(clazz.FullName));
            }
            // The object must be transformed into meta representation
            ClassInfo ci        = null;
            string    className = OdbClassUtil.GetFullName(clazz);

            // first checks if the class of this object already exist in the
            // metamodel
            if (GetMetaModel().ExistClass(className))
            {
                ci = GetMetaModel().GetClassInfo(className, true);
            }
            else
            {
                ClassInfoList ciList = classIntrospector.Introspect(o.GetType(), true);
                // All new classes found
                objectWriter.AddClasses(ciList);
                ci = ciList.GetMainClassInfo();
            }
            // first detects if we must perform an insert or an update
            // If object is in the cache, we must perform an update, else an insert
            bool   mustUpdate = false;
            ICache cache      = GetSession(true).GetCache();

            if (o != null)
            {
                OID cacheOid = cache.IdOfInsertingObject(o);
                if (cacheOid != null)
                {
                    return(cacheOid);
                }
                // throw new ODBRuntimeException("Inserting meta representation of
                // an object without the object itself is not yet supported");
                mustUpdate = cache.ExistObject(o);
            }
            // The introspection callback is used to execute some specific task (like calling trigger, for example) while introspecting the object
            IIntrospectionCallback callback = introspectionCallbackForInsert;

            if (mustUpdate)
            {
                callback = introspectionCallbackForUpdate;
            }
            // Transform the object into an ObjectInfo
            NonNativeObjectInfo nnoi = (NonNativeObjectInfo)objectIntrospector.GetMetaRepresentation(o, ci, true, null, callback);

            // During the introspection process, if object is to be updated, then the oid has been set
            mustUpdate = nnoi.GetOid() != null;
            if (mustUpdate)
            {
                return(objectWriter.UpdateNonNativeObjectInfo(nnoi, false));
            }
            return(objectWriter.InsertNonNativeObject(oid, nnoi, true));
        }