@@ -203,7 +203,19 @@ impl Cyclomatic for CppCode {
203203
204204impl Cyclomatic for PreprocCode { }
205205impl Cyclomatic for CcommentCode { }
206- impl Cyclomatic for JavaCode { }
206+
207+ impl Cyclomatic for JavaCode {
208+ fn compute ( node : & Node , stats : & mut Stats ) {
209+ use Java :: * ;
210+
211+ match node. object ( ) . kind_id ( ) . into ( ) {
212+ If | For | While | Case | Catch | TernaryExpression | AMPAMP | PIPEPIPE => {
213+ stats. cyclomatic += 1. ;
214+ }
215+ _ => { }
216+ }
217+ }
218+ }
207219
208220#[ cfg( test) ]
209221mod tests {
@@ -406,4 +418,85 @@ mod tests {
406418 ]
407419 ) ;
408420 }
421+
422+ #[ test]
423+ fn java_simple_class ( ) {
424+ check_metrics ! (
425+ "
426+ public class Example { // +2 (+1 unit space)
427+ int a = 10;
428+ boolean b = (a > 5) ? true : false; // +1
429+ boolean c = b && true; // +1
430+
431+ public void m1() { // +1
432+ if (a % 2 == 0) { // +1
433+ b = b || c; // +1
434+ }
435+ }
436+ public void m2() { // +1
437+ while (a > 3) { // +1
438+ m1();
439+ a--;
440+ }
441+ }
442+ }" ,
443+ "foo.java" ,
444+ JavaParser ,
445+ cyclomatic,
446+ [ ( cyclomatic_sum, 9 , usize ) ] ,
447+ [
448+ ( cyclomatic_average, 2.25 ) , // nspace = 4 (unit, class and 2 methods)
449+ ( cyclomatic_max, 3.0 ) ,
450+ ( cyclomatic_min, 1.0 )
451+ ]
452+ ) ;
453+ }
454+
455+ #[ test]
456+ fn java_real_class ( ) {
457+ check_metrics ! (
458+ "
459+ public class Matrix { // +2 (+1 unit space)
460+ private int[][] m = new int[5][5];
461+
462+ public void init() { // +1
463+ for (int i = 0; i < m.length; i++) { // +1
464+ for (int j = 0; j < m[i].length; j++) { // +1
465+ m[i][j] = i * j;
466+ }
467+ }
468+ }
469+ public int compute(int i, int j) { // +1
470+ try {
471+ return m[i][j] / m[j][i];
472+ } catch (ArithmeticException e) { // +1
473+ return -1;
474+ } catch (ArrayIndexOutOfBoundsException e) { // +1
475+ return -2;
476+ }
477+ }
478+ public void print(int result) { // +1
479+ switch (result) {
480+ case -1: // +1
481+ System.out.println(\" Division by zero\" );
482+ break;
483+ case -2: // +1
484+ System.out.println(\" Wrong index number\" );
485+ break;
486+ default:
487+ System.out.println(\" The result is \" + result);
488+ }
489+ }
490+ }" ,
491+ "foo.java" ,
492+ JavaParser ,
493+ cyclomatic,
494+ [ ( cyclomatic_sum, 11 , usize ) ] ,
495+ [
496+ ( cyclomatic_average, 2.2 ) , // nspace = 5 (unit, class and 3 methods)
497+ ( cyclomatic_max, 3.0 ) ,
498+ ( cyclomatic_min, 1.0 )
499+ ]
500+ ) ;
501+ }
409502}
0 commit comments