[NUI] Fix Svace issues of dereferenced nullable variables
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / EXaml / Operation / GetStaticObject.cs
1 /*
2  * Copyright(c) 2021 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.Reflection;
20 using System.Text;
21 using Tizen.NUI.BaseComponents;
22 using Tizen.NUI.Binding;
23 using Tizen.NUI.Binding.Internals;
24 using Tizen.NUI.Xaml;
25
26 namespace Tizen.NUI.EXaml
27 {
28     internal class GetStaticObject : Operation
29     {
30         public GetStaticObject(GlobalDataList globalDataList, List<object> operationInfo)
31         {
32             typeIndex = (int)operationInfo[0];
33             propertyName = operationInfo[1] as string;
34             fieldName = operationInfo[2] as string;
35             this.globalDataList = globalDataList;
36         }
37
38         private string propertyName;
39         private string fieldName;
40
41         private GlobalDataList globalDataList;
42
43         public void Do()
44         {
45             object obj = null;
46
47             if (0 == globalDataList.GatheredInstances.Count && null != globalDataList.Root)
48             {
49                 obj = globalDataList.Root;
50             }
51             else
52             {
53                 var type = globalDataList.GatheredTypes[typeIndex];
54
55                 if (null != fieldName)
56                 {
57                     obj = type.GetField(fieldName, BindingFlags.Static | BindingFlags.Public)?.GetValue(null);
58                 }
59                 else
60                 {
61                     obj = type.GetProperty(propertyName, BindingFlags.Static | BindingFlags.Public)?.GetValue(null);
62                 }
63             }
64
65             if (null != obj)
66             {
67                 globalDataList.GatheredInstances.Add(obj);
68
69                 if (obj is BindableObject bindableObject)
70                 {
71                     bindableObject.IsCreateByXaml = true;
72
73                     if (1 == globalDataList.GatheredInstances.Count)
74                     {
75                         NameScope nameScope = new NameScope();
76                         NameScope.SetNameScope(bindableObject, nameScope);
77                     }
78                 }
79             }
80             else
81             {
82                 string name = null;
83                 if (null != fieldName)
84                 {
85                     name = fieldName;
86                 }
87                 else
88                 {
89                     name = propertyName;
90                 }
91
92                 throw new Exception($"Can't gather static instance typeIndex:{typeIndex}, name:{name}");
93             }
94         }
95
96         private int typeIndex;
97     }
98 }