55import Designite .SourceModel .SM_Field ;
66import Designite .SourceModel .SM_Method ;
77import Designite .SourceModel .SM_Type ;
8- import Designite .visitors .MethodControlFlowVisitor ;
98
10- public class MethodMetrics implements MetricExtractor {
9+ public class MethodMetrics extends Metrics {
1110
1211 private int numOfParameters ;
1312 private int cyclomaticComplexity ;
1413 private int numOfLines ;
15-
1614 private SM_Method method ;
17-
18- public MethodMetrics (SM_Method method ) {
19- this .method = method ;
20- }
21-
22- @ Override
23- public void extractMetrics () {
24- extractNumOfParametersMetrics ();
25- extractCyclomaticComplexity ();
26- extractNumberOfLines ();
27- }
28-
29- private void extractNumOfParametersMetrics () {
30- numOfParameters = method .getParameterList ().size ();
31- }
32-
33- private void extractCyclomaticComplexity () {
34- cyclomaticComplexity = calculateCyclomaticComplexity ();
35- }
36-
37- private int calculateCyclomaticComplexity () {
38- MethodControlFlowVisitor visitor = new MethodControlFlowVisitor ();
39- method .getMethodDeclaration ().accept (visitor );
40- return visitor .getNumOfIfStatements ()
41- + visitor .getNumOfSwitchCaseStatementsWitoutDefault ()
42- + visitor .getNumOfForStatements ()
43- + visitor .getNumOfWhileStatements ()
44- + visitor .getNumOfDoStatements ()
45- + visitor .getNumOfForeachStatements ()
46- + 1 ;
47- }
48-
49- private void extractNumberOfLines () {
50- if (methodHasBody ()) {
51- String body = method .getMethodDeclaration ().getBody ().toString ();
52- int length = body .length ();
53- // long newlines = body.lines().count();
54- numOfLines = length - body .replace ("\n " , "" ).length ();
55- }
56- }
57-
58- private boolean methodHasBody () {
59- return method .getMethodDeclaration ().getBody () != null ;
60- }
61-
15+
6216 public int getNumOfParameters () {
6317 return numOfParameters ;
6418 }
@@ -71,16 +25,32 @@ public int getNumOfLines() {
7125 return numOfLines ;
7226 }
7327
28+ public void setNumOfParameters (int numOfParameters ) {
29+ this .numOfParameters = numOfParameters ;
30+ }
31+
32+ public void setCyclomaticComplexity (int cyclomaticComplexity ) {
33+ this .cyclomaticComplexity = cyclomaticComplexity ;
34+ }
35+
36+ public void setNumOfLines (int numOfLines ) {
37+ this .numOfLines = numOfLines ;
38+ }
39+
40+ public void setMethod (SM_Method method ){
41+ this .method = method ;
42+ }
43+
44+ public SM_Method getMethod () {
45+ return method ;
46+ }
47+
7448 public List <SM_Field > getDirectFieldAccesses () {
7549 return method .getDirectFieldAccesses ();
7650 }
77-
51+
7852 public List <SM_Type > getSMTypesInInstanceOf () {
7953 return method .getSMTypesInInstanceOf ();
8054 }
81-
82- public SM_Method getMethod () {
83- return method ;
84- }
8555
8656}
0 commit comments