e5c8baa7a9cd3227985655381dacdfe6ac584d08
[platform/core/csapi/tizenfx.git] / tools / src / ABIChecker / Utilities / ObjectUtil.cs
1 using System;
2 using System.Reflection;
3
4 namespace Checker_ABI.Utilities
5 {
6     /// -----------------------------------------------------------------------
7     /// <summary>  
8     /// This utility class contains a rich sets of utility methods that perform operations 
9     /// on objects during runtime such as copying of property and field values
10     /// between 2 objects, deep cloning of objects, etc.  
11     /// </summary>
12     /// -----------------------------------------------------------------------
13     public abstract class ObjectUtils
14     {
15         /// <summary>
16         /// Deep Comparison two objects if they are alike. The objects are consider alike if 
17         /// they are:
18         /// <list type="ordered">
19         ///    <item>of the same <see cref="System.Type"/>,</item>
20         ///    <item>have the same number of methods, properties and fields</item>
21         ///    <item>the public and private properties and fields values reflect each other's. </item>
22         /// </list>
23         /// </summary>
24         /// <param name="original"></param>
25         /// <param name="comparedToObject"></param>
26         /// <returns></returns>
27         public static bool IsALike(object original, object comparedToObject)
28         {
29
30             if (original.GetType() != comparedToObject.GetType())
31                 return false;
32
33             // ...............................................
34             // Compare Number of Private and public Methods
35             // ...............................................
36             MethodInfo[] originalMethods = original
37               .GetType()
38               .GetMethods(BindingFlags.Instance |
39               BindingFlags.NonPublic |
40               BindingFlags.Public);
41
42             MethodInfo[] comparedMethods = comparedToObject
43               .GetType()
44               .GetMethods(BindingFlags.Instance |
45               BindingFlags.NonPublic |
46               BindingFlags.Public);
47
48             if (comparedMethods.Length != originalMethods.Length)
49                 return false;
50
51             // ...............................................
52             // Compare Number of Private and public Properties
53             // ................................................
54             PropertyInfo[] originalProperties = original
55               .GetType()
56               .GetProperties(BindingFlags.Instance |
57               BindingFlags.NonPublic |
58               BindingFlags.Public);
59
60             PropertyInfo[] comparedProperties = comparedToObject
61               .GetType()
62               .GetProperties(BindingFlags.Instance |
63               BindingFlags.NonPublic |
64               BindingFlags.Public);
65
66
67             if (comparedProperties.Length != originalProperties.Length)
68                 return false;
69
70
71             // ...........................................
72             // Compare number of public and private fields
73             // ...........................................
74             FieldInfo[] originalFields = original
75               .GetType()
76               .GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
77
78             FieldInfo[] comparedToFields = comparedToObject
79               .GetType()
80               .GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
81
82
83             if (comparedToFields.Length != originalFields.Length)
84                 return false;
85
86             // ........................................
87             // compare field values
88             // ........................................
89             foreach (FieldInfo fi in originalFields)
90             {
91
92                 // check to see if the object to contains the field          
93                 FieldInfo fiComparedObj = comparedToObject.GetType().GetField(fi.Name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
94
95                 if (fiComparedObj == null)
96                     return false;
97
98                 // Get the value of the field from the original object        
99                 Object srcValue = original.GetType().InvokeMember(fi.Name,
100                   BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
101                   null,
102                   original,
103                   null);
104
105
106
107                 // Get the value of the field
108                 object comparedObjFieldValue = comparedToObject
109                   .GetType()
110                   .InvokeMember(fiComparedObj.Name,
111                   BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
112                   null,
113                   comparedToObject,
114                   null);
115
116
117                 // -------------------------------
118                 // now compare the field values
119                 // -------------------------------
120
121                 if (srcValue == null)
122                 {
123                     if (comparedObjFieldValue != null)
124                         return false;
125                     else
126                         return true;
127                 }
128
129                 if (srcValue.GetType() != comparedObjFieldValue.GetType())
130                     return false;
131
132                 if (!srcValue.ToString().Equals(comparedObjFieldValue.ToString()))
133                     return false;
134             }
135
136             // ........................................
137             // compare each Property values
138             // ........................................
139             foreach (PropertyInfo pi in originalProperties)
140             {
141
142                 // check to see if the object to contains the field          
143                 PropertyInfo piComparedObj = comparedToObject
144                   .GetType()
145                   .GetProperty(pi.Name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
146
147                 if (piComparedObj == null)
148                     return false;
149
150                 // Get the value of the property from the original object        
151                 Object srcValue = original
152                   .GetType()
153                   .InvokeMember(pi.Name,
154                   BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
155                   null,
156                   original,
157                   null);
158
159                 // Get the value of the property
160                 object comparedObjValue = comparedToObject
161                   .GetType()
162                   .InvokeMember(piComparedObj.Name,
163                   BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
164                   null,
165                   comparedToObject,
166                   null);
167
168
169                 // -------------------------------
170                 // now compare the property values
171                 // -------------------------------
172                 if (srcValue.GetType() != comparedObjValue.GetType())
173                     return false;
174
175                 if (!srcValue.ToString().Equals(comparedObjValue.ToString()))
176                     return false;
177             }
178             return true;
179         }
180     }
181 }