-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetSubclasses.m
104 lines (91 loc) · 3.39 KB
/
getSubclasses.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
function L = getSubclasses(ofClass,withinScope)
% getSubclasses List subclasses of a class
% GETSUBCLASSES lists all subclasses of a class.
%
% Syntax
% L = GETSUBCLASSES(ofClass,withinScope)
%
% Description
% L = GETSUBCLASSES(ofClass,withinScope) lists all subclasses of class
% ofClass limiting the search to the folder 'withinScope' and its
% subfolders.
%
% Example(s)
% L = GETSUBCLASSES('GearKit.gearDeployment','~/Toolboxes/Dingi')
%
%
% Input Arguments
% ofClass - superclass name
% char
% Superclass name in dot notation (e.g. '<PackageName>.<ClassName>').
%
% withinScope - folder to limit search to
% char
% Limits the search for subclasses to folder 'withinScope' and all
% subfolders.
%
%
% Output Arguments
% L - list of subclasses
% struct
% Struct listing all subclasses with fields class name 'Class', full
% path 'Path' and superclass name 'Superclasses'.
%
%
% Name-Value Pair Arguments
%
%
% See also
%
% Copyright (c) 2021-2022 David Clemens (dclemens@geomar.de)
%
global classes
classes = struct('Class',char.empty,'Path',char.empty,'Superclasses',cell.empty);
narginchk(2,2)
% Check that the scope is a valid folder
if exist(withinScope,'dir') ~= 7
error('Dingi:getSubclasses:invalidScope',...
'''%s'' is not a valid folder.',withinScope)
end
listClasses(withinScope);
maskClasses = cellfun(@(cl) ismember({ofClass},cl),cat(1,classes.Superclasses));
L = classes(maskClasses);
L = L(:);
function subpaths = listClasses(path)
if ischar(path)
path = cellstr(path);
end
nPath = numel(path);
for pp = 1:nPath
tmp = what(path{pp});
pathHasPackages = ~isempty(tmp.packages);
pathHasClasses = ~isempty(tmp.classes);
if pathHasClasses
classNames = strsplit(tmp.path,'+');
classNames = strrep(classNames,'/','');
classNames = strcat(strjoin(classNames(2:end),'.'),{'.'},tmp.classes);
nClasses = numel(classNames);
ind = numel(classes) + 1:numel(classes) + nClasses;
superClassList = cell(nClasses,1);
for cc = 1:nClasses
a = eval(['?',classNames{cc}]);
if isempty(a)
% The class folder exists, but no classdef .m file is found.
continue
end
superClassList{cc} = arrayfun(@(cl) cl.Name, a.SuperclassList,'un',0);
end
superClassList(cellfun(@isempty,superClassList)) = {{''}};
classes(ind) = struct('Class',classNames,'Path',repmat({tmp.path},nClasses,1),'Superclasses',superClassList);
end
if pathHasPackages
subpaths = fullfile(tmp.path,strcat({'+'},tmp.packages));
pathMask = cellfun(@isempty,regexp(subpaths,'/Dingi/\+Tests'));
subpaths = subpaths(pathMask);
subpaths = listClasses(subpaths);
else
subpaths = {''};
end
end
end
end