[NUI] Add file comment and end empty line
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.XamlBuild / src / public / XamlBuild / TypeDefinitionExtensions.cs
1 /*
2  * Copyright(c) 2022 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 using System;
18 using System.Collections.Generic;
19 using System.Linq;
20 using System.Reflection;
21 using Mono.Cecil;
22 using Mono.Cecil.Cil;
23 using MethodAttributes = Mono.Cecil.MethodAttributes;
24 using MethodImplAttributes = Mono.Cecil.MethodImplAttributes;
25
26 namespace Tizen.NUI.Xaml.Build.Tasks
27 {
28     static class TypeDefinitionExtensions
29     {
30         public static MethodDefinition AddDefaultConstructor(this TypeDefinition targetType)
31         {
32             var module = targetType.Module;
33             var parentType = module.ImportReference(("mscorlib", "System", "Object"));
34
35             return AddDefaultConstructor(targetType, parentType);
36         }
37
38         public static MethodDefinition AddDefaultConstructor(this TypeDefinition targetType, TypeReference parentType)
39         {
40             var module = targetType.Module;
41             var voidType = module.ImportReference(("mscorlib", "System", "Void"));
42             var methodAttributes = MethodAttributes.Public |
43                                    MethodAttributes.HideBySig |
44                                    MethodAttributes.SpecialName |
45                                    MethodAttributes.RTSpecialName;
46
47             var parentctor = module.ImportCtorReference(parentType, paramCount: 0) ?? module.ImportCtorReference(("mscorlib", "System", "Object"), parameterTypes: null);
48
49             var ctor = new MethodDefinition(".ctor", methodAttributes, voidType)
50             {
51                 CallingConvention = MethodCallingConvention.Default,
52                 ImplAttributes = (MethodImplAttributes.IL | MethodImplAttributes.Managed)
53             };
54             ctor.Body.InitLocals = true;
55
56             var IL = ctor.Body.GetILProcessor();
57
58             IL.Emit(OpCodes.Ldarg_0);
59             IL.Emit(OpCodes.Call, parentctor);
60             IL.Emit(OpCodes.Ret);
61
62             targetType.Methods.Add(ctor);
63             return ctor;
64         }
65
66         public static IEnumerable<MethodDefinition> AllMethods(this TypeDefinition self)
67         {
68             while (self != null)
69             {
70                 foreach (var md in self.Methods)
71                     yield return md;
72                 self = self.BaseType == null ? null : self.BaseType.ResolveCached();
73             }
74         }
75
76         public static FieldDefinition GetOrCreateField(this TypeDefinition self, string name, Mono.Cecil.FieldAttributes attributes, TypeReference fieldType)
77         {
78             var field = self.Fields.FirstOrDefault(a => a.Name == name);
79
80             if (null == field)
81             {
82                 field = new FieldDefinition(name, attributes, fieldType);
83                 self.Fields.Add(field);
84             }
85
86             return field;
87         }
88
89         public static MethodDefinition GetOrCreateMethod(this TypeDefinition self, string name, MethodAttributes attributes, Type type)
90         {
91             MethodDefinition method = self.Methods.FirstOrDefault(a => a.Name == name);
92             if (null == method)
93             {
94                 method = new MethodDefinition(name, MethodAttributes.Public, self.Module.ImportReference(type));
95                 self.Methods.Add(method);
96             }
97
98             return method;
99         }
100     }
101 }
102