-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallgraph.pl
executable file
·206 lines (151 loc) · 3.58 KB
/
callgraph.pl
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#! /usr/bin/perl
use strict;
use warnings;
use File::Find;
use Getopt::Long;
my ($roll,$dot,$csv,$project_prefix,$output_file);
GetOptions ('roll' => \$roll, 'prefix=s' => \$project_prefix, 'o=s' => \$output_file, 'dot' => \$dot, 'csv' => \$csv);
my $command = "javap -v";
my $extension = "class";
my $temp_dir = "/tmp";
my $temp_file = "temp.tmp";
unless ($csv xor $dot){
die "Please specify dot or csv format.\n";
}
my $input_dir = shift or die "Input folder missing.\n";
&load_files;
if ($dot){
˙
}
if ($csv){
&csv;
}
my @lines;
my @class_files;
sub load_files{
my ($result, $class, @aux);
find(\&wanted, $input_dir);
foreach $class (@class_files) {
$class=~ s/(.*)\..*/$1/;
$result= `$command $class 2> /dev/null | grep " = class" 2> /dev/null`;
$result=~ s/.*\/\/ */\"$class\" -> /g;
@aux = split /\n/, $result;
push (@lines,@aux);
}
}
sub wanted {
if ($_ = /\.$extension$/)
{
push (@class_files, $File::Find::name);
}
}
sub csv {
my $_line;
my $_body;
if ($output_file){
open(F,">$output_file");
print F "\"From\" , \"To\" , \"Calls\" \n";
} else {
print "\"From\" , \"To\" , \"Calls\" \n";
}
foreach $_line (@lines){
$_line=~ s/"$input_dir\//"/g;
$_line=~ s/\[[A-Z]?//g;
$_line=~ s/\;//g;
$_line=~ s/->\s*(.*)/, \"$1\"/g;
$_line=~ s/\"\"/\"/g;
$_line=~ s/\$[^\"]*//g;
$_line=~ s/\"[^\"]*\"\s*,\s*\"\s*\"\s*//g;
if ($roll){
$_line=~ s/\/[^\"\/]*\"/\"/g;
}
if (defined $project_prefix){
if ($_line=~ m{,\s+"$project_prefix}){
$_body.=$_line."\n";
}
} else {
$_body.=$_line."\n";
}
}
open(T,">$temp_dir/$temp_file");
print T "$_body";
close T;
$_body= `sort $temp_dir/$temp_file | uniq -c | sort -n`;
$_body=~ s/\s*(\d+)\s*(.*)/$2 , $1\n/g;
if ($output_file){
print F "$_body";
close F;
} else {
print "$_body";
}
}
sub dot {
my $_line;
my $_body;
if ($output_file){
open(F,">$output_file");
print F "digraph G\n{\n";
print F "node [shape=box]\n";
} else {
print "digraph G\n{\n";
print "node [shape=box]\n";
}
foreach $_line (@lines){
$_line=~ s/"$input_dir\//"/g;
$_line=~ s/\[[A-Z]?//g;
$_line=~ s/\;//g;
$_line=~ s/->\s*(.*)/-> \"$1\"/g;
$_line=~ s/\"\"/\"/g;
$_line=~ s/\$[^\"]*//g;
$_line=~ s/\"[^\"]*\"\s*->\s*\"\s*\"\s*//g;
if ($roll){
$_line=~ s/\/[^\"\/]*\"/\"/g;
}
if (defined $project_prefix){
if($_line=~ m{->\s+"$project_prefix}){
$_body.=$_line."\n";
}
} else {
$_body.=$_line."\n";
}
}
open(T,">/tmp/$temp_file");
print T "$_body";
close T;
$_body= `sort /tmp/$temp_file | uniq -c | sort -n`;
$_body=~ s/\s*(\d+)\s*(.*)/$2 [label=$1]\n/g;
if ($output_file){
print F "$_body";
print F "}\n";
close F;
} else {
print "$_body";
print "}\n";
}
}
__END__
=head1 NAME
callgraph - Java class dependency graph generator.
=head1 SYNOPSIS
callgraph [B<-roll>|B<-dot>|B<-csv>] [B<-prefix> F<project_prefix>] [B<-o> F<output_file>] F<root_dir>
=head1 DESCRIPTION
This tool generates a class dependency graph for Java projects. It is capable of outputing in either B<dot> or B<csv> files. Thes files can then be processed by approriate tools.
=head1 OPTIONS
=over 2
=item B<-roll>
rolls up information to the package level.
=item B<-dot>
outputs a B<dot> file.
=item B<-csv>
outputs a B<csv> file.
=item B<-o> F<filename>
outputs to the specified file.
=item B<-prefix> F<project_prefix>
define the project prefix.
=back
=head1 AUTHORS
Tiago Veloso and Marcio Coelho.
=head1 COPYRIGHT
Permission is granted to copy, distribute and/or modify this
document under the terms of the GNU Public Licence.
=cut