Nitsmell : Adding new code smells and print console updated
[nit.git] / src / metrics / mclassdef_collect.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 # This module redef Mclassdef to add new collect methods.
15
16 module mclassdef_collect
17
18 # We usualy need specific phases
19 # NOTE: `frontend` is sufficent in most case (it is often too much)
20 import frontend
21 import model_views
22 import model_collect
23
24 redef class MClassDef
25 # Collect all mproperties introduced in 'self' with `visibility >= min_visibility`.
26 fun collect_intro_mproperties(view: ModelView): Set[MProperty] do
27 var set = new HashSet[MProperty]
28 for mprop in self.intro_mproperties do
29 if not view.accept_mentity(mprop) then continue
30 set.add(mprop)
31 end
32 return set
33 end
34
35 # Collect mmethods introduced in 'self' with `visibility >= min_visibility`.
36 fun collect_intro_mmethods(view: ModelView): Set[MMethod] do
37 var res = new HashSet[MMethod]
38 for mproperty in collect_intro_mproperties(view) do
39 if not view.accept_mentity(mproperty) then continue
40 if mproperty isa MMethod then res.add(mproperty)
41 end
42 return res
43 end
44
45 # Collect mmethods redefined in 'self' with `visibility >= min_visibility`.
46 fun collect_redef_mmethods(view: ModelView): Set[MMethod] do
47 var res = new HashSet[MMethod]
48 for mproperty in collect_redef_mproperties(view) do
49 if not view.accept_mentity(mproperty) then continue
50 if mproperty isa MMethod then res.add(mproperty)
51 end
52 return res
53 end
54
55 # Collect mattributes redefined in 'self' with `visibility >= min_visibility`.
56 fun collect_redef_mattributes(view: ModelView): Set[MAttribute] do
57 var res = new HashSet[MAttribute]
58 for mproperty in collect_redef_mproperties(view) do
59 if not view.accept_mentity(mproperty) then continue
60 if mproperty isa MAttribute then res.add(mproperty)
61 end
62 return res
63 end
64
65 # Collect mattributes introduced in 'self' with `visibility >= min_visibility`.
66 fun collect_intro_mattributes(view: ModelView): Set[MAttribute] do
67 var res = new HashSet[MAttribute]
68 for mproperty in collect_intro_mproperties(view) do
69 if not view.accept_mentity(mproperty) then continue
70 if mproperty isa MAttribute then res.add(mproperty)
71 end
72 return res
73 end
74
75 # Collect all mproperties redefined in 'self' with `visibility >= min_visibility`.
76 fun collect_redef_mproperties(view: ModelView): Set[MProperty] do
77 var set = new HashSet[MProperty]
78 for mpropdef in self.mpropdefs do
79 if not view.accept_mentity(mpropdef) then continue
80 if mpropdef.mproperty.intro_mclassdef.mclass == self then continue
81 set.add(mpropdef.mproperty)
82 end
83 return set
84 end
85
86 # Collect mmethods inherited by 'self' if accepted by `view`.
87 fun collect_inherited_mmethods(view: ModelView): Set[MMethod] do
88 var res = new HashSet[MMethod]
89 for mproperty in collect_inherited_mproperties(view) do
90 if not view.accept_mentity(mproperty) then continue
91 if mproperty isa MMethod then res.add(mproperty)
92 end
93 return res
94 end
95
96 # Collect mproperties introduced and redefined in 'self' with `visibility >= min_visibility`.
97 fun collect_local_mproperties(view: ModelView): Set[MProperty] do
98 var set = new HashSet[MProperty]
99 set.add_all collect_intro_mproperties(view)
100 set.add_all collect_redef_mproperties(view)
101 return set
102 end
103
104 # Collect all mproperties inehrited by 'self' with `visibility >= min_visibility`.
105 fun collect_inherited_mproperties(view: ModelView): Set[MProperty] do
106 var set = new HashSet[MProperty]
107 for parent in collect_parents(view) do
108 set.add_all(parent.collect_intro_mproperties(view))
109 set.add_all(parent.collect_inherited_mproperties(view))
110 end
111 return set
112 end
113
114 # Collect mattributes inherited by 'self' with `visibility >= min_visibility`.
115 fun collect_inherited_mattributes(view: ModelView): Set[MAttribute] do
116 var res = new HashSet[MAttribute]
117 for mproperty in collect_inherited_mproperties(view) do
118 if not view.accept_mentity(mproperty) then continue
119 if mproperty isa MAttribute then res.add(mproperty)
120 end
121 return res
122 end
123
124 # Collect all mmethod inehrited,intro and redef
125 fun collect_all_methods(view: ModelView): Set[MMethod] do
126 var set = new HashSet[MMethod]
127 set.add_all collect_intro_mmethods(view)
128 set.add_all collect_redef_mmethods(view)
129 set.add_all collect_inherited_mmethods(view)
130 return set
131 end
132
133 # Collect all mattributs inehrited,intro and redef
134 fun collect_all_mattributes(view: ModelView): Set[MAttribute] do
135 var set = new HashSet[MAttribute]
136 set.add_all collect_redef_mattributes(view)
137 set.add_all collect_intro_mattributes(view)
138 set.add_all collect_inherited_mattributes(view)
139 return set
140 end
141
142 # Collect intro and redef mmethods
143 fun collect_intro_and_redef_methods(view: ModelView): Set[MMethod] do
144 var set = new HashSet[MMethod]
145 set.add_all collect_intro_mmethods(view)
146 set.add_all collect_redef_mmethods(view)
147 return set
148 end
149
150 # Collect intro and redef mattributs
151 fun collect_intro_and_redef_mattributes(view: ModelView): Set[MAttribute] do
152 var set = new HashSet[MAttribute]
153 set.add_all collect_redef_mattributes(view)
154 set.add_all collect_intro_mattributes(view)
155 return set
156 end
157
158 # Collect intro and redef mpropdefs
159 fun collect_intro_and_redef_mpropdefs(view: ModelView): Set[MPropDef] do
160 var set = new HashSet[MPropDef]
161 set.add_all collect_intro_mpropdefs(view)
162 set.add_all collect_redef_mpropdefs(view)
163 return set
164 end
165
166 # Collect intro abstract mmethodDef
167 fun collect_abstract_methods(view: ModelView): Set[MMethodDef] do
168 var set = new HashSet[MMethodDef]
169 var mpropdefs = collect_intro_mpropdefs(view)
170 for mpropdef in mpropdefs do
171 if mpropdef isa MMethodDef then
172 if mpropdef.is_abstract then set.add(mpropdef)
173 end
174 end
175 return set
176 end
177
178 # Collect not defined properties
179 fun collect_not_define_properties(view: ModelView):Set[MMethodDef] do
180 var set = new HashSet[MMethodDef]
181 for mpropdef in collect_abstract_methods(view) do
182 var redef_count = 0
183 for mprop in mpropdef.mproperty.mpropdefs do
184 if mprop.is_abstract then continue
185 redef_count += 1
186 end
187 if redef_count == 0 then set.add(mpropdef)
188 end
189 return set
190 end
191 end