-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathClass-Graph.pl
More file actions
156 lines (138 loc) · 4.08 KB
/
Class-Graph.pl
File metadata and controls
156 lines (138 loc) · 4.08 KB
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
use Understand;
my $path = $ENV{'PATH'};
my @temp;
@paths = split(";",$path);
foreach $pa(@paths){
if(($pa=~m/jdk/)!=1){
push(@temp,$pa);
}
}
$ENV{'PATH'} = join(";",@temp);
print $ENV{'PATH'};
system( "und -quiet create -db temp.udb -languages java add " . $ARGV[0] . " analyze" );
$project_name=$ARGV[0];
( $db, $status ) = Understand::open("temp.udb");
die "Error status: ", $status, "\n" if $status;
$output_file=">classgraph.dot";
open( OUTFILE, $output_file );
print OUTFILE "digraph G{\n";
my %hashmap;
$output_file2=">CountLine-".$project_name.".csv";
open(OUTFILE2, $output_file2);
sub printClassGraph{
my $category = shift(@_);
my $callee = shift(@_);
my $caller = shift(@_);
$callee_name=$callee->longname();
$caller_name=$caller->longname();
$Anon="Anon";
if(($callee_name=~/$Anon/)||($caller_name=~/$Anon/)){
last;
}
if ( !exists $hashmap{$caller_name} ) {
$hashmap{$caller_name} = 1;
print OUTFILE "\t" . "\"" . $caller_name . "\"" . ";" . "\n";
}
if ( !exists $hashmap{$callee_name} ) {
$hashmap{$callee_name} = 1;
print OUTFILE "\t" . "\"" . $callee_name . "\"" . ";" . "\n";
}
print OUTFILE "\t" . "\""
. $caller_name . "\"" . "->" . "\""
. $callee_name . "\""
. "["
. $category . "]"
. ";" . "\n";
}
foreach $interface ($db->ents("Interface")){
$loc=$interface->metric("CountLineCode");
if($loc==0){
next;
}
foreach $class ($interface->ents("Implementby","Class")){
$loc_class=$class->metric("CountLineCode");
if($loc_class==0){
next;
}
printClassGraph("I",$interface,$class);#实例化接口关系用I表示。
}
}
@abstract_class_list=$db->ents("Abstract Class");
@class_list=$db->ents("Class");
@interface_list=$db->ents("Interface");
@all_class_list=(@abstract_class_list,@class_list,@interface_list);
$all_class_length=@all_class_list;
my %count;
@all_class_unique_list=grep { ++$count{ $_ } < 2; } @all_class_list;
$all_class_unique_length=@all_class_unique_list;
my %class_name_list;
$class_count=0;
foreach $class (@all_class_unique_list){
$loc=$class->metric("CountLineCode");
if($loc==0){
next;
}
$Anon="Anon";
if($class->longname()=~/$Anon/){
next;
}
$class_count=$class_count+1;
foreach $child_class ($class->ents("Extendby","Class")){
$loc_class=$child_class->metric("CountLineCode");
if($loc_class==0){
next;
}
printClassGraph("C",$class,$child_class);#继承关系用C表示,代表Child的意思。
}
foreach $aggre_class ($class->ents("Coupleby","Class")){
$loc_class=$aggre_class->metric("CountLineCode");
if($loc_class==0){
next;
}
printClassGraph("A",$class,$aggre_class);#聚合关系用A表示,代表Aggregation的意思。
}
foreach $method ($class->ents('Define','Method')){
$loc_method=$method->metric("CountLineCode");
if($loc_method==0){
print "This method's loc==0:",$method->longname(),"\n";
next;
}
}
$class_name=$class->name();
@names=split(/\./,$class_name);
$class_name_list{$names[@names-1]}=$class;
}
foreach $class (@all_class_unique_list){
$loc=$class->metric("CountLineCode");
if($loc==0){
next;
}
foreach $method ($class->ents('Define','Method')){
$loc_method=$method->metric("CountLineCode");
if($loc_method==0){
print "This method's loc==0:",$method->longname(),"\n";
next;
}
$method_return_type=$method->type();
if(exists $class_name_list{$method_return_type}){
printClassGraph("R",$class_name_list{$method_return_type},$class);#方法返回值用R表示,代表Return的意思。
}
$method_parameters=$method->parameters();
@parameter_list=split(/\,/,$method_parameters);
@parameter_type_list=();
foreach $parameter (@parameter_list){
@parameter_one=split(/\ /,$parameter);
@parameter_one_strip=split(/\[/,@parameter_one[0]);
@parameter_type_list=(@parameter_type_list,@parameter_one_strip[0]);
}
foreach $parameter_type (@parameter_type_list){
if(exists $class_name_list{$parameter_type}){
printClassGraph("P",$class_name_list{$parameter_type},$class);#参数类型用P表示,代表Parameter的意思。
last;
}
}
}
}
print OUTFILE "}";
print OUTFILE2 $db->metric("CountLineCode"),",";
print OUTFILE2 $class_count;