/// <summary>Gets all the persistent classes that are subclasses or equal to the parameter class
        ///     </summary>
        /// <param name="fullClassName"></param>
        /// <returns>The list of class info of persistent classes that are subclasses or equal to the class
        ///     </returns>
        public virtual IOdbList <ClassInfo> GetPersistentSubclassesOf(string fullClassName)
        {
            IOdbList <ClassInfo> result     = new OdbArrayList <ClassInfo>();
            IEnumerator <string> classNames = rapidAccessForUserClassesByName.Keys.GetEnumerator();
            string oneClassName             = null;

            System.Type theClass = classPool.GetClass(fullClassName);
            System.Type oneClass = null;
            while (classNames.MoveNext())
            {
                oneClassName = classNames.Current;
                if (oneClassName.Equals(fullClassName))
                {
                    result.Add(GetClassInfo(oneClassName, true));
                }
                else
                {
                    oneClass = classPool.GetClass(oneClassName);
                    if (theClass.IsAssignableFrom(oneClass))
                    {
                        result.Add(GetClassInfo(oneClassName, true));
                    }
                }
            }
            return(result);
        }
Exemple #2
0
        public IOdbList <FieldInfo> GetAllFields(string fullClassName)
        {
            IOdbList <FieldInfo> result = null;

            fields.TryGetValue(fullClassName, out result);

            if (result != null)
            {
                return(result);
            }
            System.Collections.IDictionary attributesNames = new System.Collections.Hashtable();
            result = new OdbArrayList <FieldInfo>(50);
            System.Reflection.FieldInfo[] superClassfields = null;
            System.Collections.IList      classes          = GetSuperClasses(fullClassName, true);
            for (int i = 0; i < classes.Count; i++)
            {
                System.Type clazz1 = (System.Type)classes[i];

                superClassfields = clazz1.GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Static);
                for (int j = 0; j < superClassfields.Length; j++)
                {
                    // Only adds the attribute if it does not exist one with same name
                    if (attributesNames[superClassfields[j].Name] == null)
                    {
                        result.Add(superClassfields[j]);
                        attributesNames[superClassfields[j].Name] = superClassfields[j].Name;
                    }
                }
            }
            result = RemoveUnnecessaryFields(result);
            fields[fullClassName] = result;
            attributesNames.Clear();
            attributesNames = null;
            return(result);
        }
Exemple #3
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);
        }
Exemple #4
0
        public IOdbList <FieldInfo> RemoveUnnecessaryFields(IOdbList <FieldInfo> fields)
        {
            IOdbList <FieldInfo> fieldsToRemove = new OdbArrayList <FieldInfo> (fields.Count);

            // Remove static fields
            for (int i = 0; i < fields.Count; i++)
            {
                System.Reflection.FieldInfo field = (System.Reflection.FieldInfo)fields[i];

                // by osmadja
                if (field.IsNotSerialized || field.IsStatic)
                {
                    fieldsToRemove.Add(field);
                }
                //by cristi
                if (field.FieldType == typeof(IntPtr))
                {
                    fieldsToRemove.Add(field);
                }
                object[] oattr           = field.GetCustomAttributes(true);
                bool     isNonPersistent = false;
                foreach (object at in oattr)
                {
                    NonPersistentAttribute npat = at as NonPersistentAttribute;
                    if (npat != null)
                    {
                        isNonPersistent = true;
                        break;
                    }
                }
                if (isNonPersistent || field.IsStatic)
                {
                    fieldsToRemove.Add(field);
                }
                // Remove inner class fields
                if (field.Name.StartsWith("this$"))
                {
                    fieldsToRemove.Add(field);
                }
            }

            fields.RemoveAll(fieldsToRemove);
            return(fields);
        }
Exemple #5
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 override IOdbList<string> GetAllInvolvedFields
			()
		{
			IEnumerator iterator = criteria.GetEnumerator();
			ICriterion criterion = null;
			IOdbList<string> fields = new OdbArrayList<string>(10);
			while (iterator.MoveNext())
			{
				criterion = (ICriterion)iterator.Current;
                IOdbList<string> l = criterion.GetAllInvolvedFields();
                // check duplicate
                for (int i = 0; i < l.Count; i++)
                {
                    string f = l.Get(i);
                    if(!fields.Contains(f))
                    {
                        fields.Add(f);
                    }
                }
			}
			return fields;
		}
        public override IOdbList <string> GetAllInvolvedFields
            ()
        {
            IEnumerator       iterator  = criteria.GetEnumerator();
            ICriterion        criterion = null;
            IOdbList <string> fields    = new OdbArrayList <string>(10);

            while (iterator.MoveNext())
            {
                criterion = (ICriterion)iterator.Current;
                IOdbList <string> l = criterion.GetAllInvolvedFields();
                // check duplicate
                for (int i = 0; i < l.Count; i++)
                {
                    string f = l.Get(i);
                    if (!fields.Contains(f))
                    {
                        fields.Add(f);
                    }
                }
            }
            return(fields);
        }
		public virtual ClassInfoCompareResult ExtractDifferences(ClassInfo newCI, bool update)
		{
			string attributeName = null;
			ClassAttributeInfo cai1 = null;
			ClassAttributeInfo cai2 = null;
			Meta.ClassInfoCompareResult result = new ClassInfoCompareResult(GetFullClassName());
			bool isCompatible = true;
			IOdbList<ClassAttributeInfo> attributesToRemove = new OdbArrayList<ClassAttributeInfo>(10);
			IOdbList<ClassAttributeInfo> attributesToAdd = new OdbArrayList<ClassAttributeInfo>(10);
			int nbAttributes = attributes.Count;
			for (int id = 0; id < nbAttributes; id++)
			{
				// !!!WARNING : ID start with 1 and not 0
				cai1 = attributes[id];
				if (cai1 == null)
				{
					continue;
				}
				attributeName = cai1.GetName();
				cai2 = newCI.GetAttributeInfoFromId(cai1.GetId());
				if (cai2 == null)
				{
					result.AddCompatibleChange("Field '" + attributeName + "' has been removed");
					if (update)
					{
						// Simply remove the attribute from meta-model
						attributesToRemove.Add(cai1);
					}
				}
				else
				{
					if (!ODBType.TypesAreCompatible(cai1.GetAttributeType(), cai2.GetAttributeType()))
					{
						result.AddIncompatibleChange("Type of Field '" + attributeName + "' has changed : old='"
							 + cai1.GetFullClassname() + "' - new='" + cai2.GetFullClassname() + "'");
						isCompatible = false;
					}
				}
			}
			int nbNewAttributes = newCI.attributes.Count;
			for (int id = 0; id < nbNewAttributes; id++)
			{
				// !!!WARNING : ID start with 1 and not 0
				cai2 = newCI.attributes[id];
				if (cai2 == null)
				{
					continue;
				}
				attributeName = cai2.GetName();
				cai1 = GetAttributeInfoFromId(cai2.GetId());
				if (cai1 == null)
				{
					result.AddCompatibleChange("Field '" + attributeName + "' has been added");
					if (update)
					{
						// Sets the right id of attribute
						cai2.SetId(maxAttributeId + 1);
						maxAttributeId++;
						// Then adds the new attribute to the meta-model
						attributesToAdd.Add(cai2);
					}
				}
			}
			attributes.RemoveAll(attributesToRemove);
			attributes.AddAll(attributesToAdd);
			FillAttributesMap();
			return result;
		}
		/// <summary>Gets all the persistent classes that are subclasses or equal to the parameter class
		/// 	</summary>
		/// <param name="fullClassName"></param>
		/// <returns>The list of class info of persistent classes that are subclasses or equal to the class
		/// 	</returns>
		public virtual IOdbList<ClassInfo > GetPersistentSubclassesOf(string fullClassName)
		{
			IOdbList<ClassInfo> result = new OdbArrayList<ClassInfo>();
			IEnumerator<string> classNames = rapidAccessForUserClassesByName.Keys.GetEnumerator();
			string oneClassName = null;
			System.Type theClass = classPool.GetClass(fullClassName);
			System.Type oneClass = null;
			while (classNames.MoveNext())
			{
				oneClassName = classNames.Current;
				if (oneClassName.Equals(fullClassName))
				{
					result.Add(GetClassInfo(oneClassName, true));
				}
				else
				{
					oneClass = classPool.GetClass(oneClassName);
					if (theClass.IsAssignableFrom(oneClass))
					{
						result.Add(GetClassInfo(oneClassName, true));
					}
				}
			}
			return result;
		}
				public IOdbList<FieldInfo> RemoveUnnecessaryFields(IOdbList<FieldInfo> fields)
				{
					IOdbList<FieldInfo> fieldsToRemove = new OdbArrayList<FieldInfo> (fields.Count);
					
					// Remove static fields
					for (int i = 0; i < fields.Count; i++)
					{
						System.Reflection.FieldInfo field = (System.Reflection.FieldInfo) fields[i];
		
		                // by osmadja
		                if (field.IsNotSerialized || field.IsStatic )
		                {
		                    fieldsToRemove.Add(field);
		                }
		                //by cristi
		                if (field.FieldType == typeof(IntPtr))
		                {
		                    fieldsToRemove.Add(field);
		                }
		                object[] oattr = field.GetCustomAttributes(true);
		                bool isNonPersistent = false;
		                foreach (object at in oattr)
		                {
		                    NonPersistentAttribute npat = at as NonPersistentAttribute;
		                    if (npat != null)
		                    {
		                        isNonPersistent = true;
		                        break;
		                    }
		                }
		                if (isNonPersistent || field.IsStatic)
		                {
		                    fieldsToRemove.Add(field);
		                }
						// Remove inner class fields
						if (field.Name.StartsWith("this$"))
						{
							fieldsToRemove.Add(field);
						}
					}
					
					fields.RemoveAll(fieldsToRemove);
					return fields;
				}
            public IOdbList<FieldInfo> GetAllFields(string fullClassName)
            {
                IOdbList<FieldInfo> result = null;
                fields.TryGetValue(fullClassName, out result);

                if (result != null)
                {
                    return result;
                }
                System.Collections.IDictionary attributesNames = new System.Collections.Hashtable();
                result = new OdbArrayList<FieldInfo>(50);
                System.Reflection.FieldInfo[] superClassfields = null;
                System.Collections.IList classes = GetSuperClasses(fullClassName, true);
                for (int i = 0; i < classes.Count; i++)
                {
                    System.Type clazz1 = (System.Type)classes[i];

                    superClassfields = clazz1.GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Static);
                    for (int j = 0; j < superClassfields.Length; j++)
                    {
                        // Only adds the attribute if it does not exist one with same name
                        if (attributesNames[superClassfields[j].Name] == null)
                        {
                            result.Add(superClassfields[j]);
                            attributesNames[superClassfields[j].Name] = superClassfields[j].Name;
                        }
                    }
                }
                result = RemoveUnnecessaryFields(result);
                fields[fullClassName] = result;
                attributesNames.Clear();
                attributesNames = null;
                return result;
            }
				/// <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;
				}
				/// <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;
				}
        public virtual ClassInfoCompareResult ExtractDifferences(ClassInfo newCI, bool update)
        {
            string             attributeName = null;
            ClassAttributeInfo cai1          = null;
            ClassAttributeInfo cai2          = null;

            Meta.ClassInfoCompareResult result = new ClassInfoCompareResult(GetFullClassName());
            bool isCompatible = true;
            IOdbList <ClassAttributeInfo> attributesToRemove = new OdbArrayList <ClassAttributeInfo>(10);
            IOdbList <ClassAttributeInfo> attributesToAdd    = new OdbArrayList <ClassAttributeInfo>(10);
            int nbAttributes = attributes.Count;

            for (int id = 0; id < nbAttributes; id++)
            {
                // !!!WARNING : ID start with 1 and not 0
                cai1 = attributes[id];
                if (cai1 == null)
                {
                    continue;
                }
                attributeName = cai1.GetName();
                cai2          = newCI.GetAttributeInfoFromId(cai1.GetId());
                if (cai2 == null)
                {
                    result.AddCompatibleChange("Field '" + attributeName + "' has been removed");
                    if (update)
                    {
                        // Simply remove the attribute from meta-model
                        attributesToRemove.Add(cai1);
                    }
                }
                else
                {
                    if (!ODBType.TypesAreCompatible(cai1.GetAttributeType(), cai2.GetAttributeType()))
                    {
                        result.AddIncompatibleChange("Type of Field '" + attributeName + "' has changed : old='"
                                                     + cai1.GetFullClassname() + "' - new='" + cai2.GetFullClassname() + "'");
                        isCompatible = false;
                    }
                }
            }
            int nbNewAttributes = newCI.attributes.Count;

            for (int id = 0; id < nbNewAttributes; id++)
            {
                // !!!WARNING : ID start with 1 and not 0
                cai2 = newCI.attributes[id];
                if (cai2 == null)
                {
                    continue;
                }
                attributeName = cai2.GetName();
                cai1          = GetAttributeInfoFromId(cai2.GetId());
                if (cai1 == null)
                {
                    result.AddCompatibleChange("Field '" + attributeName + "' has been added");
                    if (update)
                    {
                        // Sets the right id of attribute
                        cai2.SetId(maxAttributeId + 1);
                        maxAttributeId++;
                        // Then adds the new attribute to the meta-model
                        attributesToAdd.Add(cai2);
                    }
                }
            }
            attributes.RemoveAll(attributesToRemove);
            attributes.AddAll(attributesToAdd);
            FillAttributesMap();
            return(result);
        }
 public void AddOid(OID oid)
 {
     oids.Add(oid);
 }