Serializable::inspect
to show more useful information
nitc :: modelbuilder
more_collections :: more_collections
Highly specific, but useful, collections-related classes.serialization :: serialization_core
Abstract services to serialize Nit objects to different formatsnitc :: toolcontext
Common command-line tool infrastructure than handle options and error messagescore :: union_find
union–find algorithm using an efficient disjoint-set data structurenitc :: api_metrics
nitc :: nitmetrics
A program that collects various metrics on nit programs and libraries
# Metrics on the usage of explicit static types.
module static_types_metrics
import metrics_base
import modelize
redef class ToolContext
var static_types_metrics_phase: Phase = new StaticTypesMetricsPhase(self, null)
end
private class StaticTypesMetricsPhase
super Phase
redef fun process_mainmodule(mainmodule, given_mmodules)
do
if not toolcontext.opt_static_types.value and not toolcontext.opt_all.value then return
compute_static_types_metrics(toolcontext.modelbuilder)
end
end
# The job of this visitor is to resolve all types found
private class ATypeCounterVisitor
super Visitor
var modelbuilder: ModelBuilder
var nclassdef: AClassdef
var typecount: Counter[MType]
redef fun visit(n)
do
if n isa AAnnotation then return
if n isa AType then do
var mclassdef = self.nclassdef.mclassdef
if mclassdef == null then break
var mtype = modelbuilder.resolve_mtype(mclassdef, n)
if mtype != null then
self.typecount.inc(mtype)
end
end
n.visit_all(self)
end
end
# Visit the AST and print metrics on the usage of explicit static types.
fun compute_static_types_metrics(modelbuilder: ModelBuilder)
do
# Count each occurence of a specific static type
var typecount = new Counter[MType]
# Visit all the source code to collect data
for nmodule in modelbuilder.nmodules do
for nclassdef in nmodule.n_classdefs do
var visitor = new ATypeCounterVisitor(modelbuilder, nclassdef, typecount)
visitor.enter_visit(nclassdef)
end
end
# Display data
print "--- Metrics of the explitic static types ---"
print "Total number of explicit static types: {typecount.sum}"
if typecount.sum == 0 then return
print "Statistics of type usage:"
typecount.print_summary
typecount.print_elements(10)
end
src/metrics/static_types_metrics.nit:17,1--82,3