forked from dreamer-89-zz/complexity-measure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Automated_Calculation_Cyclomatic_Complexity.java
68 lines (65 loc) · 2.55 KB
/
Automated_Calculation_Cyclomatic_Complexity.java
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package software_testing_meaures;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
/**
*
* @author Suraj Singh
*/
class Automated_Calculation_Cyclomatic_Complexity {
static String[] matching_Expression = new String[]{""};
public static void main(String arfs[]) throws IOException
{
Scanner s = new Scanner(System.in);
String line,path;
int counter=0,isSwitch=0;// store the number of decision nodes in the DD graph
char choice;
StringBuffer inputFile, matcherFile;
do
{
counter = 0;
System.out.print("Enter file name : ");
path = "C:\\Users\\Suraj Singh\\Desktop\\"+s.next()+".java";
System.out.print("Do you want source code : ");
choice = s.next().charAt(0);
try
{
BufferedReader r = new BufferedReader(new FileReader(path));
if(choice=='y'||choice=='Y')
System.out.println("\n*************************************SOURCE CODE*************************************");
while( (line=r.readLine())!=null)
{
if(choice=='y'||choice=='Y') System.out.println(line);
if(line.matches("(^\\s*)(.*)(if|for|while)(\\(.*)"))
counter++;
else if(line.matches("(^\\s*)switch(\\(.*)"))
isSwitch++; // Beginning of switch statement
else if(line.matches("(^\\s*)case(\\s+)(\\w+):(.*)") && isSwitch>0) // support nested sw
counter++;
else if(line.matches("(^\\s*)default(\\s*):(.*)") && isSwitch>0)//end of switch further case will not be cosidered
{
isSwitch--;
counter++;
}
}
}
catch(FileNotFoundException e)
{
System.out.println(e.getMessage());
return;
}
if(choice=='y'||choice=='Y') System.out.println("**************************************************************************************");
System.out.println("\nCYCLOMATIC COMPLEXITY => "+counter);
System.out.print("\nDo you want to continue (y/n)? ");
choice = s.next().charAt(0);
System.out.println();
}while(choice=='Y'||choice=='y');
}
}