@@ -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,66 @@ 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 = 1;
428+ int b = (a == 1) ? 1 : 0; // +1
429+ int c = (a == 2) ? 1 : 0; // +1
430+ public void m1() { // +1
431+ if (a == 1) { // +1
432+
433+ }
434+ }
435+ public void m2() { // +1
436+ if (a == 2) { // +1
437+
438+ }
439+ }
440+ }" ,
441+ "foo.java" ,
442+ JavaParser ,
443+ cyclomatic,
444+ [ ( cyclomatic_sum, 8 , usize ) ] ,
445+ [
446+ ( cyclomatic_average, 2.0 ) , // nspace = 4 (unit, class and 2 methods)
447+ ( cyclomatic_max, 3.0 ) ,
448+ ( cyclomatic_min, 1.0 )
449+ ]
450+ ) ;
451+ }
452+
453+ #[ test]
454+ fn java_real_class ( ) {
455+ check_metrics ! (
456+ "
457+ public class Matrix { // +2 (+1 unit space)
458+ private int[][] m = new int[5][5];
459+
460+ public void init() { // +1
461+ for (int i = 0; i < m.length; i++) { // +1
462+ for (int j = 0; j < m[i].length; j++) { // +1
463+ if (i == j) { // +1
464+ m[i][j] = i + j;
465+ } else {
466+ m[i][j] = i * j;
467+ }
468+ }
469+ }
470+ }
471+ }" ,
472+ "foo.java" ,
473+ JavaParser ,
474+ cyclomatic,
475+ [ ( cyclomatic_sum, 6 , usize ) ] ,
476+ [
477+ ( cyclomatic_average, 2.0 ) , // nspace = 3 (unit, class and method)
478+ ( cyclomatic_max, 4.0 ) ,
479+ ( cyclomatic_min, 1.0 )
480+ ]
481+ ) ;
482+ }
409483}
0 commit comments