From c98bc453a9cd42d854b664ce464786d3ed4a961d Mon Sep 17 00:00:00 2001 From: Devon Burriss Date: Mon, 25 Oct 2021 07:38:16 +0200 Subject: [PATCH 01/19] WIP of Java LoC metric --- src/metrics/loc.rs | 88 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/src/metrics/loc.rs b/src/metrics/loc.rs index 7a9aaf2fd..11b6b54d1 100644 --- a/src/metrics/loc.rs +++ b/src/metrics/loc.rs @@ -810,9 +810,32 @@ impl Loc for CppCode { } } +impl Loc for JavaCode { + fn compute(node: &Node, stats: &mut Stats, is_func_space: bool, is_unit: bool) { + use Java::*; + + let (start, end) = init(node, stats, is_func_space, is_unit); + + match node.object().kind_id().into() { + Program => {} + Comment => { + add_cloc_lines(stats, start, end); + } + AssertStatement | BreakStatement | ContinueStatement | DoStatement + | ExpressionStatement | ForStatement | IfStatement | ReturnStatement | Statement + | SwitchStatement | ThrowStatement | TryStatement | WhileStatement => { + stats.logical_lines += 1; + } + _ => { + check_comment_ends_on_code_line(stats, start); + stats.lines.insert(start); + } + } + } +} + impl Loc for PreprocCode {} impl Loc for CcommentCode {} -impl Loc for JavaCode {} #[cfg(test)] mod tests { @@ -1914,4 +1937,67 @@ mod tests { ] ); } + + #[test] + fn java_single_statement_loc() { + check_metrics!( + "int max = 10;", + "foo.java", + JavaParser, + loc, + [ + (sloc, 1, usize), // The number of lines is 1 + // (ploc, 1, usize), // The number of code lines is 1 + // (lloc, 1, usize), // The number of statements is 1 + // (cloc, 0, usize), // The number of comments is 0 + // (blank, 0, usize) // The number of blank lines is 0 + ] + ); + } + + // #[test] + fn java_statement_inline_loc() { + check_metrics!( + "for (i = 0; i < 100; i++) { printf(\"hello\"); }", + "foo.java", + JavaParser, + loc, + [ + (sloc, 1, usize), // The number of lines is 1 + (ploc, 1, usize), // The number of code lines is 1 + (lloc, 2, usize), // The number of statements is 1 + (cloc, 0, usize), // The number of comments is 0 + (blank, 0, usize) // The number of blank lines is 0 + ] + ); + } + + // #[test] + fn java_general_loc() { + check_metrics!( + " + int max = 10; + + /* + Loop: + from: 0 + to: max + and print each value of i + */ + for (int i = 0; i <= max; i = i = 1) { + // Print the value i + System.out.println(i); + }", + "foo.java", + JavaParser, + loc, + [ + (sloc, 13, usize), // The number of lines is 13 + (ploc, 4, usize), // The number of code lines is 4 + (lloc, 2, usize), // The number of statements is 2 + (cloc, 7, usize), // The number of comments is 7 + (blank, 2, usize) // The number of blank lines is 2 + ] + ); + } } From 22190bab0b55eb99af74a8d56040f4c144dae168 Mon Sep 17 00:00:00 2001 From: Devon Burriss Date: Mon, 25 Oct 2021 08:59:26 +0200 Subject: [PATCH 02/19] Enables 2 passing simple tests --- src/metrics/loc.rs | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/src/metrics/loc.rs b/src/metrics/loc.rs index 11b6b54d1..2b8577c69 100644 --- a/src/metrics/loc.rs +++ b/src/metrics/loc.rs @@ -815,14 +815,15 @@ impl Loc for JavaCode { use Java::*; let (start, end) = init(node, stats, is_func_space, is_unit); - - match node.object().kind_id().into() { - Program => {} + let kind_id = node.object().kind_id().into(); + match kind_id { + Block | Program => {} Comment => { add_cloc_lines(stats, start, end); } AssertStatement | BreakStatement | ContinueStatement | DoStatement - | ExpressionStatement | ForStatement | IfStatement | ReturnStatement | Statement + | ExpressionStatement | ForStatement | IfStatement | LocalVariableDeclaration + | ReturnStatement | Statement | SwitchStatement | ThrowStatement | TryStatement | WhileStatement => { stats.logical_lines += 1; } @@ -1946,34 +1947,37 @@ mod tests { JavaParser, loc, [ - (sloc, 1, usize), // The number of lines is 1 - // (ploc, 1, usize), // The number of code lines is 1 - // (lloc, 1, usize), // The number of statements is 1 - // (cloc, 0, usize), // The number of comments is 0 - // (blank, 0, usize) // The number of blank lines is 0 + (lloc, 1, usize), // The number of statements is 1 + (cloc, 0, usize), // The number of comments is 0 ] ); } - // #[test] + #[test] fn java_statement_inline_loc() { check_metrics!( - "for (i = 0; i < 100; i++) { printf(\"hello\"); }", + "for (i = 0; i < 100; i++) { System.out.println(\"hello\"); }", "foo.java", JavaParser, loc, [ - (sloc, 1, usize), // The number of lines is 1 (ploc, 1, usize), // The number of code lines is 1 - (lloc, 2, usize), // The number of statements is 1 + (lloc, 2, usize), // The number of statements is 2 (cloc, 0, usize), // The number of comments is 0 - (blank, 0, usize) // The number of blank lines is 0 ] ); } - // #[test] + #[test] + #[ignore] // lloc incorrect fn java_general_loc() { + let nr_ploc = 4; + let nr_statements = 3; + let nr_comments = 7; + let nr_blanks = 2; + + let expected_sloc = nr_statements + nr_comments + nr_blanks + 1; // ? sloc always seems +1 higher + check_metrics!( " int max = 10; @@ -1984,7 +1988,7 @@ mod tests { to: max and print each value of i */ - for (int i = 0; i <= max; i = i = 1) { + for (int i = 0; i <= max; i = i + 1) { // Print the value i System.out.println(i); }", @@ -1992,11 +1996,11 @@ mod tests { JavaParser, loc, [ - (sloc, 13, usize), // The number of lines is 13 - (ploc, 4, usize), // The number of code lines is 4 - (lloc, 2, usize), // The number of statements is 2 - (cloc, 7, usize), // The number of comments is 7 - (blank, 2, usize) // The number of blank lines is 2 + (sloc, expected_sloc, usize), // The number of lines is 11 (should be 10?) + (ploc, nr_ploc, usize), // The number of code lines is 4 + (lloc, nr_statements, usize), // The number of statements is 3 + (cloc, nr_comments, usize), // The number of comments is 7 + (blank, nr_blanks, usize) // The number of blank lines is 2 ] ); } From d14a724e1f7a037be3e3980662a9f26e36a344e0 Mon Sep 17 00:00:00 2001 From: Devon Burriss Date: Tue, 26 Oct 2021 07:36:18 +0200 Subject: [PATCH 03/19] Adds more individual loc tests --- src/metrics/loc.rs | 131 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 106 insertions(+), 25 deletions(-) diff --git a/src/metrics/loc.rs b/src/metrics/loc.rs index 2b8577c69..b4fca6c24 100644 --- a/src/metrics/loc.rs +++ b/src/metrics/loc.rs @@ -817,14 +817,24 @@ impl Loc for JavaCode { let (start, end) = init(node, stats, is_func_space, is_unit); let kind_id = node.object().kind_id().into(); match kind_id { - Block | Program => {} + Program => {} Comment => { add_cloc_lines(stats, start, end); } - AssertStatement | BreakStatement | ContinueStatement | DoStatement - | ExpressionStatement | ForStatement | IfStatement | LocalVariableDeclaration - | ReturnStatement | Statement - | SwitchStatement | ThrowStatement | TryStatement | WhileStatement => { + AssertStatement + | BreakStatement + | ContinueStatement + | DoStatement + | ExpressionStatement + | ForStatement + | IfStatement + | LocalVariableDeclaration + | ReturnStatement + | Statement + | SwitchStatement + | ThrowStatement + | TryStatement + | WhileStatement => { stats.logical_lines += 1; } _ => { @@ -1939,6 +1949,36 @@ mod tests { ); } + #[test] + fn java_sloc() { + check_metrics!( + "for (i = 0; i < 100; i++) { + System.out.println(i); + }", + "foo.java", + JavaParser, + loc, + [ + (sloc, 3, usize), // The number of lines is 3 + ] + ); + } + + #[test] + fn java_ploc() { + check_metrics!( + "for (i = 0; i < 100; i++) { + System.out.println(i); + }", + "foo.java", + JavaParser, + loc, + [ + (ploc, 3, usize), // The number of code lines is 3 + ] + ); + } + #[test] fn java_single_statement_loc() { check_metrics!( @@ -1947,8 +1987,57 @@ mod tests { JavaParser, loc, [ - (lloc, 1, usize), // The number of statements is 1 - (cloc, 0, usize), // The number of comments is 0 + (lloc, 1, usize), // The number of statements is 1 + ] + ); + } + + #[test] + fn java_lloc() { + check_metrics!( + "for (i = 0; i < 100; i++) { \ + System.out.println(\"hello\"); \ + }", + "foo.java", + JavaParser, + loc, + [ + (lloc, 2, usize), // The number of statements is 2 + ] + ); + } + + #[test] + fn java_comments() { + check_metrics!( + "for (i = 0; i < 100; i++) { \ + // Print hello + System.out.println(\"hello\"); \ + // Print world + System.out.println(\"hello\"); \ + }", + "foo.java", + JavaParser, + loc, + [ + (cloc, 2, usize), // The number of comments is 2 + ] + ); + } + + #[test] + fn java_blank() { + check_metrics!( + " + int x = 1; + + + int y = 2;", + "foo.java", + JavaParser, + loc, + [ + (blank, 3, usize), // The number of blank lines is 3 ] ); } @@ -1961,26 +2050,18 @@ mod tests { JavaParser, loc, [ - (ploc, 1, usize), // The number of code lines is 1 - (lloc, 2, usize), // The number of statements is 2 - (cloc, 0, usize), // The number of comments is 0 + (ploc, 1, usize), // The number of code lines is 1 + (lloc, 2, usize), // The number of statements is 2 + (cloc, 0, usize), // The number of comments is 0 ] ); } #[test] - #[ignore] // lloc incorrect + // #[ignore] // failing test fn java_general_loc() { - let nr_ploc = 4; - let nr_statements = 3; - let nr_comments = 7; - let nr_blanks = 2; - - let expected_sloc = nr_statements + nr_comments + nr_blanks + 1; // ? sloc always seems +1 higher - check_metrics!( - " - int max = 10; + "int max = 10; /* Loop: @@ -1996,11 +2077,11 @@ mod tests { JavaParser, loc, [ - (sloc, expected_sloc, usize), // The number of lines is 11 (should be 10?) - (ploc, nr_ploc, usize), // The number of code lines is 4 - (lloc, nr_statements, usize), // The number of statements is 3 - (cloc, nr_comments, usize), // The number of comments is 7 - (blank, nr_blanks, usize) // The number of blank lines is 2 + //(sloc, 12, usize), // The number of lines is 12 + (ploc, 4, usize), // The number of code lines is 4 + (lloc, 3, usize), // The number of statements is 3 + (cloc, 7, usize), // The number of comments is 7 + (blank, 1, usize) // The number of blank lines is 2 ] ); } From b98869606922ac22951851d5dd37a5e15c7b808c Mon Sep 17 00:00:00 2001 From: Devon Burriss Date: Tue, 26 Oct 2021 12:19:41 +0200 Subject: [PATCH 04/19] Initial impl of JavaCode Getter trait get_space_kind was needed to fix an off by one issue with ploc --- src/getter.rs | 15 +++++- src/metrics/loc.rs | 127 +++++++++++++++++++++++++++++++++++---------- 2 files changed, 114 insertions(+), 28 deletions(-) diff --git a/src/getter.rs b/src/getter.rs index 3d3bcafc2..d232abfa4 100644 --- a/src/getter.rs +++ b/src/getter.rs @@ -509,4 +509,17 @@ impl Getter for CppCode { impl Getter for PreprocCode {} impl Getter for CcommentCode {} -impl Getter for JavaCode {} +impl Getter for JavaCode { + fn get_space_kind(node: &Node) -> SpaceKind { + use Java::*; + + let typ = node.object().kind_id(); + match typ.into() { + MethodDeclaration | LambdaExpression => SpaceKind::Function, + Class | ClassDeclaration => SpaceKind::Class, + PackageDeclaration => SpaceKind::Namespace, + Program => SpaceKind::Unit, + _ => SpaceKind::Unknown, + } + } +} diff --git a/src/metrics/loc.rs b/src/metrics/loc.rs index b4fca6c24..e4895a5d3 100644 --- a/src/metrics/loc.rs +++ b/src/metrics/loc.rs @@ -823,6 +823,7 @@ impl Loc for JavaCode { } AssertStatement | BreakStatement + | MethodInvocation | ContinueStatement | DoStatement | ExpressionStatement @@ -832,6 +833,7 @@ impl Loc for JavaCode { | ReturnStatement | Statement | SwitchStatement + | TernaryExpression | ThrowStatement | TryStatement | WhileStatement => { @@ -1952,7 +1954,7 @@ mod tests { #[test] fn java_sloc() { check_metrics!( - "for (i = 0; i < 100; i++) { + "for (int i = 0; i < 100; i++) { System.out.println(i); }", "foo.java", @@ -1965,9 +1967,22 @@ mod tests { } #[test] - fn java_ploc() { + fn java_single_ploc() { check_metrics!( - "for (i = 0; i < 100; i++) { + "int x = 1;", + "foo.java", + JavaParser, + loc, + [ + (ploc, 1, usize), // The number of code lines is 1 + ] + ); + } + + #[test] + fn java_simple_ploc() { + check_metrics!( + "for (int i = 0; i < 100; i = i++) { System.out.println(i); }", "foo.java", @@ -1980,7 +1995,23 @@ mod tests { } #[test] - fn java_single_statement_loc() { + fn java_multi_ploc() { + check_metrics!( + "int x = 1; + for (int i = 0; i < 100; i++) { + System.out.println(i); + }", + "foo.java", + JavaParser, + loc, + [ + (ploc, 4, usize), // The number of code lines is 4 + ] + ); + } + + #[test] + fn java_single_statement_lloc() { check_metrics!( "int max = 10;", "foo.java", @@ -1993,16 +2024,33 @@ mod tests { } #[test] - fn java_lloc() { + fn java_simple_lloc() { check_metrics!( - "for (i = 0; i < 100; i++) { \ - System.out.println(\"hello\"); \ + "for (int i = 0; i < 100; i++) { + System.out.println(i); }", "foo.java", JavaParser, loc, [ - (lloc, 2, usize), // The number of statements is 2 + (lloc, 4, usize), // The number of statements is 4 + ] + ); + } + + #[test] + fn java_multi_lloc() { + check_metrics!( + "int max = 10; + + for (int i = 0; i < max; i++) { + System.out.println(i); + }", + "foo.java", + JavaParser, + loc, + [ + (lloc, 5, usize), // The number of statements is 5 ] ); } @@ -2010,7 +2058,7 @@ mod tests { #[test] fn java_comments() { check_metrics!( - "for (i = 0; i < 100; i++) { \ + "for (int i = 0; i < 100; i++) { \ // Print hello System.out.println(\"hello\"); \ // Print world @@ -2026,10 +2074,9 @@ mod tests { } #[test] - fn java_blank() { + fn java_double_blank() { check_metrics!( - " - int x = 1; + "int x = 1; int y = 2;", @@ -2037,7 +2084,7 @@ mod tests { JavaParser, loc, [ - (blank, 3, usize), // The number of blank lines is 3 + (blank, 2, usize), // The number of blank lines is 2 ] ); } @@ -2045,43 +2092,69 @@ mod tests { #[test] fn java_statement_inline_loc() { check_metrics!( - "for (i = 0; i < 100; i++) { System.out.println(\"hello\"); }", + "for (int i = 0; i < 100; i++) { System.out.println(\"hello\"); }", "foo.java", JavaParser, loc, [ (ploc, 1, usize), // The number of code lines is 1 - (lloc, 2, usize), // The number of statements is 2 + (lloc, 4, usize), // The number of statements is 4 (cloc, 0, usize), // The number of comments is 0 ] ); } #[test] - // #[ignore] // failing test fn java_general_loc() { check_metrics!( - "int max = 10; - + "int max = 100; + /* - Loop: + Loop through and print from: 0 to: max - and print each value of i */ - for (int i = 0; i <= max; i = i + 1) { - // Print the value i + for (int i = 0; i < max; i++) { + // Print the value System.out.println(i); - }", + }", "foo.java", JavaParser, loc, [ - //(sloc, 12, usize), // The number of lines is 12 + (sloc, 11, usize), // The number of lines is 11 (ploc, 4, usize), // The number of code lines is 4 - (lloc, 3, usize), // The number of statements is 3 - (cloc, 7, usize), // The number of comments is 7 - (blank, 1, usize) // The number of blank lines is 2 + (lloc, 5, usize), // The number of statements is 3 + (cloc, 6, usize), // The number of comments is 6 + (blank, 1, usize) // The number of blank lines is 1 + ] + ); + } + + #[test] + #[ignore] + fn java_class_loc() { + check_metrics!( + " + /** + * The HelloWorldApp class implements an application that + * simply prints \"Hello World!\" to standard output. + */ + + class HelloWorldApp { + public static void main(String[] args) { + System.out.println(\"Hello World!\"); // Display the string. + } + }", + "foo.java", + JavaParser, + loc, + [ + (sloc, 10, usize), // The number of lines is 10 + (ploc, 5, usize), // The number of code lines is 5 + (lloc, 1, usize), // The number of statements is 3 + (cloc, 5, usize), // The number of comments is 6 + (blank, 1, usize) // The number of blank lines is 1 ] ); } From 3ce6450fb8bd2d1215bd9a283f62edc24abb420f Mon Sep 17 00:00:00 2001 From: Devon Burriss Date: Tue, 26 Oct 2021 15:04:13 +0200 Subject: [PATCH 05/19] Test cleanup --- src/metrics/loc.rs | 89 ++++++++++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 34 deletions(-) diff --git a/src/metrics/loc.rs b/src/metrics/loc.rs index e4895a5d3..acc5de047 100644 --- a/src/metrics/loc.rs +++ b/src/metrics/loc.rs @@ -1951,6 +1951,40 @@ mod tests { ); } + #[test] + fn java_comments() { + check_metrics!( + "for (int i = 0; i < 100; i++) { \ + // Print hello + System.out.println(\"hello\"); \ + // Print world + System.out.println(\"hello\"); \ + }", + "foo.java", + JavaParser, + loc, + [ + (cloc, 2, usize), // The number of comments is 2 + ] + ); + } + + #[test] + fn java_blank() { + check_metrics!( + "int x = 1; + + + int y = 2;", + "foo.java", + JavaParser, + loc, + [ + (blank, 2, usize), // The number of blank lines is 2 + ] + ); + } + #[test] fn java_sloc() { check_metrics!( @@ -2056,35 +2090,23 @@ mod tests { } #[test] - fn java_comments() { - check_metrics!( - "for (int i = 0; i < 100; i++) { \ - // Print hello - System.out.println(\"hello\"); \ - // Print world - System.out.println(\"hello\"); \ - }", - "foo.java", - JavaParser, - loc, - [ - (cloc, 2, usize), // The number of comments is 2 - ] - ); - } - - #[test] - fn java_double_blank() { + fn java_class_loc() { check_metrics!( - "int x = 1; - - - int y = 2;", + " + public class Person { + private String name; + public Person(String name){ + this.name = name; // +1 + } + public String getName() { + return name; // +1 + } + }", "foo.java", JavaParser, loc, [ - (blank, 2, usize), // The number of blank lines is 2 + (lloc, 2, usize), // The number of statements is 2 ] ); } @@ -2124,7 +2146,7 @@ mod tests { [ (sloc, 11, usize), // The number of lines is 11 (ploc, 4, usize), // The number of code lines is 4 - (lloc, 5, usize), // The number of statements is 3 + (lloc, 5, usize), // The number of statements is 5 (cloc, 6, usize), // The number of comments is 6 (blank, 1, usize) // The number of blank lines is 1 ] @@ -2132,28 +2154,27 @@ mod tests { } #[test] - #[ignore] - fn java_class_loc() { + fn java_main_class_loc() { check_metrics!( - " + "package com.company; /** * The HelloWorldApp class implements an application that * simply prints \"Hello World!\" to standard output. */ class HelloWorldApp { - public static void main(String[] args) { - System.out.println(\"Hello World!\"); // Display the string. - } + public void main(String[] args) { + System.out.println(\"Hello World!\"); // Display the string. + } }", "foo.java", JavaParser, loc, [ - (sloc, 10, usize), // The number of lines is 10 + (sloc, 11, usize), // The number of lines is 11 (ploc, 5, usize), // The number of code lines is 5 - (lloc, 1, usize), // The number of statements is 3 - (cloc, 5, usize), // The number of comments is 6 + (lloc, 2, usize), // The number of statements is 2 + (cloc, 5, usize), // The number of comments is 5 (blank, 1, usize) // The number of blank lines is 1 ] ); From f28b458e351bf9d1f3103395ae048770acd6fa8c Mon Sep 17 00:00:00 2001 From: Devon Burriss Date: Tue, 26 Oct 2021 15:42:34 +0200 Subject: [PATCH 06/19] Fix test --- src/metrics/loc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/metrics/loc.rs b/src/metrics/loc.rs index acc5de047..a3e8c06c5 100644 --- a/src/metrics/loc.rs +++ b/src/metrics/loc.rs @@ -2172,7 +2172,7 @@ mod tests { loc, [ (sloc, 11, usize), // The number of lines is 11 - (ploc, 5, usize), // The number of code lines is 5 + (ploc, 6, usize), // The number of code lines is 5 (lloc, 2, usize), // The number of statements is 2 (cloc, 5, usize), // The number of comments is 5 (blank, 1, usize) // The number of blank lines is 1 From 2d11dbff2d5b56a729350b618859c413deaa1975 Mon Sep 17 00:00:00 2001 From: Devon Burriss Date: Sat, 20 Nov 2021 16:17:15 +0100 Subject: [PATCH 07/19] Fixes handling of for loops --- src/metrics/loc.rs | 58 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/src/metrics/loc.rs b/src/metrics/loc.rs index a3e8c06c5..8c2073e5f 100644 --- a/src/metrics/loc.rs +++ b/src/metrics/loc.rs @@ -810,12 +810,19 @@ impl Loc for CppCode { } } +// impl fmt::Display for Java { +// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { +// write!(f, "{:?}", self) +// } +// } + impl Loc for JavaCode { fn compute(node: &Node, stats: &mut Stats, is_func_space: bool, is_unit: bool) { use Java::*; let (start, end) = init(node, stats, is_func_space, is_unit); - let kind_id = node.object().kind_id().into(); + let kind_id : Java = node.object().kind_id().into(); + //println!("KINDID {}", kind_id.to_string()); match kind_id { Program => {} Comment => { @@ -823,11 +830,11 @@ impl Loc for JavaCode { } AssertStatement | BreakStatement - | MethodInvocation + | BinaryExpression | ContinueStatement | DoStatement + | Declaration | ExpressionStatement - | ForStatement | IfStatement | LocalVariableDeclaration | ReturnStatement @@ -836,9 +843,21 @@ impl Loc for JavaCode { | TernaryExpression | ThrowStatement | TryStatement + | UpdateExpression | WhileStatement => { stats.logical_lines += 1; } + For => { + if count_specific_ancestors!( + node, + ForStatement, Block + ) == 0 + { + // handle for(int i:arr) + // otherwise the statements in the for are counted elsewhere + stats.logical_lines += 1; + } + } _ => { check_comment_ends_on_code_line(stats, start); stats.lines.insert(start); @@ -2058,10 +2077,10 @@ mod tests { } #[test] - fn java_simple_lloc() { + fn java_for_lloc() { check_metrics!( - "for (int i = 0; i < 100; i++) { - System.out.println(i); + "for (int i = 0; i < 100; i++) { // + 3 + System.out.println(i); // + 1 }", "foo.java", JavaParser, @@ -2072,13 +2091,30 @@ mod tests { ); } + #[test] + fn java_foreach_lloc() { + check_metrics!( + " + int arr[]={12,13,14,44}; // +1 + for (int i:arr) { // +1 + System.out.println(i); // +1 + }", + "foo.java", + JavaParser, + loc, + [ + (lloc, 3, usize), // The number of statements is 3 + ] + ); + } + #[test] fn java_multi_lloc() { check_metrics!( - "int max = 10; + "int max = 10; // +1 - for (int i = 0; i < max; i++) { - System.out.println(i); + for (int i = 0; i < max; i++) { // +3 + System.out.println(i); // +1 }", "foo.java", JavaParser, @@ -2164,7 +2200,7 @@ mod tests { class HelloWorldApp { public void main(String[] args) { - System.out.println(\"Hello World!\"); // Display the string. + System.out.println(\"Hello World!\"); // Display the string. Only statement. +1 lloc } }", "foo.java", @@ -2173,7 +2209,7 @@ mod tests { [ (sloc, 11, usize), // The number of lines is 11 (ploc, 6, usize), // The number of code lines is 5 - (lloc, 2, usize), // The number of statements is 2 + (lloc, 1, usize), // The number of statements is 1 (cloc, 5, usize), // The number of comments is 5 (blank, 1, usize) // The number of blank lines is 1 ] From 36cf32df9cb89a0013ccb93789a74360dda119f0 Mon Sep 17 00:00:00 2001 From: Devon Burriss Date: Sat, 20 Nov 2021 17:01:35 +0100 Subject: [PATCH 08/19] Adds while --- src/metrics/loc.rs | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/src/metrics/loc.rs b/src/metrics/loc.rs index 8c2073e5f..f665ecb77 100644 --- a/src/metrics/loc.rs +++ b/src/metrics/loc.rs @@ -810,11 +810,11 @@ impl Loc for CppCode { } } -// impl fmt::Display for Java { -// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { -// write!(f, "{:?}", self) -// } -// } +impl fmt::Display for Java { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{:?}", self) + } +} impl Loc for JavaCode { fn compute(node: &Node, stats: &mut Stats, is_func_space: bool, is_unit: bool) { @@ -822,29 +822,30 @@ impl Loc for JavaCode { let (start, end) = init(node, stats, is_func_space, is_unit); let kind_id : Java = node.object().kind_id().into(); - //println!("KINDID {}", kind_id.to_string()); + println!("KINDID {}", kind_id.to_string()); match kind_id { Program => {} Comment => { add_cloc_lines(stats, start, end); } AssertStatement + | AssignmentExpression | BreakStatement | BinaryExpression | ContinueStatement | DoStatement | Declaration - | ExpressionStatement | IfStatement | LocalVariableDeclaration + | MethodInvocation | ReturnStatement | Statement | SwitchStatement | TernaryExpression | ThrowStatement | TryStatement - | UpdateExpression - | WhileStatement => { + | UpdateExpression => { + println!(" lloc increased by {}", kind_id.to_string()); stats.logical_lines += 1; } For => { @@ -855,6 +856,7 @@ impl Loc for JavaCode { { // handle for(int i:arr) // otherwise the statements in the for are counted elsewhere + println!(" lloc increased by {}", kind_id.to_string()); stats.logical_lines += 1; } } @@ -2108,6 +2110,24 @@ mod tests { ); } + #[test] + fn java_while_lloc() { + check_metrics!( + " + int i=0; // +1 + while(i < 10) { // +1 + i++; // +1 + System.out.println(i); // +1 + }", + "foo.java", + JavaParser, + loc, + [ + (lloc, 4, usize), // The number of statements is 4 + ] + ); + } + #[test] fn java_multi_lloc() { check_metrics!( From 210824405e017aef6bddadda5bd88445abaacdd4 Mon Sep 17 00:00:00 2001 From: Devon Burriss Date: Fri, 11 Feb 2022 07:41:07 +0100 Subject: [PATCH 09/19] Remove println --- src/metrics/loc.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/metrics/loc.rs b/src/metrics/loc.rs index f665ecb77..30c954882 100644 --- a/src/metrics/loc.rs +++ b/src/metrics/loc.rs @@ -822,7 +822,6 @@ impl Loc for JavaCode { let (start, end) = init(node, stats, is_func_space, is_unit); let kind_id : Java = node.object().kind_id().into(); - println!("KINDID {}", kind_id.to_string()); match kind_id { Program => {} Comment => { @@ -845,7 +844,6 @@ impl Loc for JavaCode { | ThrowStatement | TryStatement | UpdateExpression => { - println!(" lloc increased by {}", kind_id.to_string()); stats.logical_lines += 1; } For => { @@ -856,7 +854,6 @@ impl Loc for JavaCode { { // handle for(int i:arr) // otherwise the statements in the for are counted elsewhere - println!(" lloc increased by {}", kind_id.to_string()); stats.logical_lines += 1; } } @@ -2228,7 +2225,7 @@ mod tests { loc, [ (sloc, 11, usize), // The number of lines is 11 - (ploc, 6, usize), // The number of code lines is 5 + (ploc, 6, usize), // The number of code lines is 6 (lloc, 1, usize), // The number of statements is 1 (cloc, 5, usize), // The number of comments is 5 (blank, 1, usize) // The number of blank lines is 1 From ca1b35c4010d7f97d54a37d9bdee167d30c86ea7 Mon Sep 17 00:00:00 2001 From: Devon Burriss Date: Sat, 12 Feb 2022 11:10:41 +0100 Subject: [PATCH 10/19] Ran fmt --- src/metrics/loc.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/metrics/loc.rs b/src/metrics/loc.rs index 30c954882..c379368c9 100644 --- a/src/metrics/loc.rs +++ b/src/metrics/loc.rs @@ -812,7 +812,7 @@ impl Loc for CppCode { impl fmt::Display for Java { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{:?}", self) + write!(f, "{:?}", self) } } @@ -821,7 +821,7 @@ impl Loc for JavaCode { use Java::*; let (start, end) = init(node, stats, is_func_space, is_unit); - let kind_id : Java = node.object().kind_id().into(); + let kind_id: Java = node.object().kind_id().into(); match kind_id { Program => {} Comment => { @@ -847,11 +847,7 @@ impl Loc for JavaCode { stats.logical_lines += 1; } For => { - if count_specific_ancestors!( - node, - ForStatement, Block - ) == 0 - { + if count_specific_ancestors!(node, ForStatement, Block) == 0 { // handle for(int i:arr) // otherwise the statements in the for are counted elsewhere stats.logical_lines += 1; From 70a04eda52a17d9775f8f23ba11627345e1649c7 Mon Sep 17 00:00:00 2001 From: Devon Burriss Date: Tue, 22 Feb 2022 08:29:38 +0100 Subject: [PATCH 11/19] Reduce to handle only SLOC off by 1 --- src/getter.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/getter.rs b/src/getter.rs index d232abfa4..cbcbe8e1c 100644 --- a/src/getter.rs +++ b/src/getter.rs @@ -515,9 +515,6 @@ impl Getter for JavaCode { let typ = node.object().kind_id(); match typ.into() { - MethodDeclaration | LambdaExpression => SpaceKind::Function, - Class | ClassDeclaration => SpaceKind::Class, - PackageDeclaration => SpaceKind::Namespace, Program => SpaceKind::Unit, _ => SpaceKind::Unknown, } From aa42b7c5c28bf9b2a8b80a5516aec68cd42a3b53 Mon Sep 17 00:00:00 2001 From: Devon Burriss Date: Tue, 22 Feb 2022 08:30:41 +0100 Subject: [PATCH 12/19] Adds ternary lloc to test --- src/metrics/loc.rs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/metrics/loc.rs b/src/metrics/loc.rs index c379368c9..c1becec6a 100644 --- a/src/metrics/loc.rs +++ b/src/metrics/loc.rs @@ -840,7 +840,6 @@ impl Loc for JavaCode { | ReturnStatement | Statement | SwitchStatement - | TernaryExpression | ThrowStatement | TryStatement | UpdateExpression => { @@ -2014,6 +2013,21 @@ mod tests { ); } + #[test] + fn java_module_sloc() { + check_metrics!( + "module helloworld{ + exports com.test; + }", + "foo.java", + JavaParser, + loc, + [ + (sloc, 3, usize), // The number of lines is 3 + ] + ); + } + #[test] fn java_single_ploc() { check_metrics!( @@ -2213,17 +2227,18 @@ mod tests { class HelloWorldApp { public void main(String[] args) { - System.out.println(\"Hello World!\"); // Display the string. Only statement. +1 lloc + String message = args.length == 0 ? \"Hello empty world\" : \"Hello world\"; // +2 lloc : 1 var assignment + binary exp + System.out.println(message); // Display the string. +1 lloc } }", "foo.java", JavaParser, loc, [ - (sloc, 11, usize), // The number of lines is 11 - (ploc, 6, usize), // The number of code lines is 6 - (lloc, 1, usize), // The number of statements is 1 - (cloc, 5, usize), // The number of comments is 5 + (sloc, 12, usize), // The number of lines is 11 + (ploc, 7, usize), // The number of code lines is 7 + (lloc, 3, usize), // The number of statements is 3 + (cloc, 6, usize), // The number of comments is 6 (blank, 1, usize) // The number of blank lines is 1 ] ); From 7d4f9f1f3fe204e3926ff34b10b4278d227d70f6 Mon Sep 17 00:00:00 2001 From: Devon Burriss Date: Mon, 28 Feb 2022 19:54:21 +0100 Subject: [PATCH 13/19] Adds expression grammar lloc --- src/metrics/loc.rs | 63 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 13 deletions(-) diff --git a/src/metrics/loc.rs b/src/metrics/loc.rs index c1becec6a..b778bea6f 100644 --- a/src/metrics/loc.rs +++ b/src/metrics/loc.rs @@ -822,6 +822,7 @@ impl Loc for JavaCode { let (start, end) = init(node, stats, is_func_space, is_unit); let kind_id: Java = node.object().kind_id().into(); + println!("{}", kind_id); match kind_id { Program => {} Comment => { @@ -831,27 +832,28 @@ impl Loc for JavaCode { | AssignmentExpression | BreakStatement | BinaryExpression + | CastExpression | ContinueStatement | DoStatement | Declaration + | EnhancedForStatement | IfStatement + | InstanceofExpression + | LambdaExpression | LocalVariableDeclaration | MethodInvocation + | ParenthesizedExpression + | PrimaryExpression | ReturnStatement | Statement | SwitchStatement + | TernaryExpression | ThrowStatement | TryStatement - | UpdateExpression => { + | UpdateExpression + | UnaryExpression => { stats.logical_lines += 1; } - For => { - if count_specific_ancestors!(node, ForStatement, Block) == 0 { - // handle for(int i:arr) - // otherwise the statements in the for are counted elsewhere - stats.logical_lines += 1; - } - } _ => { check_comment_ends_on_code_line(stats, start); stats.lines.insert(start); @@ -2122,7 +2124,7 @@ mod tests { check_metrics!( " int i=0; // +1 - while(i < 10) { // +1 + while(i < 10) { // +2 paren exp + binary i++; // +1 System.out.println(i); // +1 }", @@ -2130,7 +2132,7 @@ mod tests { JavaParser, loc, [ - (lloc, 4, usize), // The number of statements is 4 + (lloc, 5, usize), // The number of statements is 5 ] ); } @@ -2174,6 +2176,41 @@ mod tests { ); } + // copy in 1 line at a time + // + // + // + // + // + // + // + // + // + // + #[test] + fn java_expressions_lloc() { + check_metrics!( + "int x = 10; // +1 + x=+90; // +2 var + unary + int y = x * 2; // +2 var + binary + IntFunction double = (n) -> n*2; // +3 var + lambda + binary + int y2 = double(x); // +2 var + paren expression + System.out.println(\"double \" + x + \" = \" + y2); // +4 method + binary x 3 + String message = (x % 2) == 0 ? \"Evenly done.\" : \"Oddly done.\"; // +5 lloc : 1 var assignment + ternary + binary + param exp + binary + Object done = (Runnable) () -> { System.out.println(\"Done!\"); }; // +4 var + cast + lamda + method invoc + String s = \"string\"; // +1 + boolean isS = (s instanceof String); // +3 var assignment + paren exp + instanceof + done.run(); // +1 method invoc + ", + "foo.java", + JavaParser, + loc, + [ + (lloc, 28, usize), // The number of statements is 28 + ] + ); + } + #[test] fn java_statement_inline_loc() { check_metrics!( @@ -2227,7 +2264,7 @@ mod tests { class HelloWorldApp { public void main(String[] args) { - String message = args.length == 0 ? \"Hello empty world\" : \"Hello world\"; // +2 lloc : 1 var assignment + binary exp + String message = args.length == 0 ? \"Hello empty world\" : \"Hello world\"; // +3 lloc : 1 var assignment + ternary + binary exp System.out.println(message); // Display the string. +1 lloc } }", @@ -2235,9 +2272,9 @@ mod tests { JavaParser, loc, [ - (sloc, 12, usize), // The number of lines is 11 + (sloc, 12, usize), // The number of lines is 12 (ploc, 7, usize), // The number of code lines is 7 - (lloc, 3, usize), // The number of statements is 3 + (lloc, 4, usize), // The number of statements is 4 (cloc, 6, usize), // The number of comments is 6 (blank, 1, usize) // The number of blank lines is 1 ] From 9c030c26d721722dbf5e484a5c83c078e38f730e Mon Sep 17 00:00:00 2001 From: Devon Burriss Date: Wed, 2 Mar 2022 07:49:07 +0100 Subject: [PATCH 14/19] Removes expressions from count --- src/metrics/loc.rs | 148 ++++++++++++++++++++++++++++++++------------- 1 file changed, 106 insertions(+), 42 deletions(-) diff --git a/src/metrics/loc.rs b/src/metrics/loc.rs index b778bea6f..e4818fc52 100644 --- a/src/metrics/loc.rs +++ b/src/metrics/loc.rs @@ -829,30 +829,25 @@ impl Loc for JavaCode { add_cloc_lines(stats, start, end); } AssertStatement - | AssignmentExpression | BreakStatement - | BinaryExpression - | CastExpression | ContinueStatement | DoStatement - | Declaration | EnhancedForStatement + | ExpressionStatement | IfStatement - | InstanceofExpression - | LambdaExpression | LocalVariableDeclaration - | MethodInvocation - | ParenthesizedExpression - | PrimaryExpression | ReturnStatement - | Statement | SwitchStatement - | TernaryExpression | ThrowStatement | TryStatement - | UpdateExpression - | UnaryExpression => { + | WhileStatement => { stats.logical_lines += 1; + println!("+1"); + } + ForStatement => { + // handle binary check + increment + stats.logical_lines += 2; + println!("+2"); } _ => { check_comment_ends_on_code_line(stats, start); @@ -2087,6 +2082,20 @@ mod tests { ); } + #[test] + fn java_update_expression_lloc() { + check_metrics!( + "int i = 10; + i++;", + "foo.java", + JavaParser, + loc, + [ + (lloc, 2, usize), // The number of statements is 2 + ] + ); + } + #[test] fn java_for_lloc() { check_metrics!( @@ -2124,7 +2133,7 @@ mod tests { check_metrics!( " int i=0; // +1 - while(i < 10) { // +2 paren exp + binary + while(i < 10) { // +1 i++; // +1 System.out.println(i); // +1 }", @@ -2132,22 +2141,88 @@ mod tests { JavaParser, loc, [ - (lloc, 5, usize), // The number of statements is 5 + (lloc, 4, usize), // The number of statements is 4 + ] + ); + } + + #[test] + fn java_do_while_lloc() { + check_metrics!( + " + int i=0; // +1 + do { // +1 + i++; // +1 + System.out.println(i); // +1 + } while(i < 10)", + "foo.java", + JavaParser, + loc, + [ + (lloc, 4, usize), // The number of statements is 4 ] ); } #[test] - fn java_multi_lloc() { + fn java_switch_lloc() { + check_metrics!( + "switch(grade) { // +1 + case 'A' : + System.out.println(\"Pass with distinction\"); // +1 + break; // +1 + case 'B' : + case 'C' : + System.out.println(\"Pass\"); // +1 + break; // +1 + case 'D' : + System.out.println(\"At risk\"); // +1 + case 'F' : + System.out.println(\"Fail\"); // +1 + break; // +1 + default : + System.out.println(\"Invalid grade\"); // +1 + }", + "foo.java", + JavaParser, + loc, + [ + (lloc, 9, usize), // The number of statements is 6 + ] + ); + } + + #[test] + fn java_continue_lloc() { check_metrics!( "int max = 10; // +1 for (int i = 0; i < max; i++) { // +3 - System.out.println(i); // +1 + if(i % 2 == 0) { continue;} + 2 + System.out.println(i); // +1 }", "foo.java", JavaParser, loc, + [ + (lloc, 7, usize), // The number of statements is 7 + ] + ); + } + + #[test] + fn java_try_lloc() { + check_metrics!( + "try { // +1 + int[] myNumbers = {1, 2, 3}; // +1 + System.out.println(myNumbers[10]); // +1 + } catch (Exception e) { + System.out.println(e.getMessage()); // +1 + throw e; // +1 + }", + "foo.java", + JavaParser, + loc, [ (lloc, 5, usize), // The number of statements is 5 ] @@ -2176,37 +2251,26 @@ mod tests { ); } - // copy in 1 line at a time - // - // - // - // - // - // - // - // - // - // #[test] fn java_expressions_lloc() { check_metrics!( - "int x = 10; // +1 - x=+90; // +2 var + unary - int y = x * 2; // +2 var + binary - IntFunction double = (n) -> n*2; // +3 var + lambda + binary - int y2 = double(x); // +2 var + paren expression - System.out.println(\"double \" + x + \" = \" + y2); // +4 method + binary x 3 - String message = (x % 2) == 0 ? \"Evenly done.\" : \"Oddly done.\"; // +5 lloc : 1 var assignment + ternary + binary + param exp + binary - Object done = (Runnable) () -> { System.out.println(\"Done!\"); }; // +4 var + cast + lamda + method invoc - String s = \"string\"; // +1 - boolean isS = (s instanceof String); // +3 var assignment + paren exp + instanceof - done.run(); // +1 method invoc + "int x = 10; // +1 local var declaration + x=+90; // +1 expression statement + int y = x * 2; // +1 local var declaration + IntFunction double = (n) -> n*2; // +1 local var declaration + int y2 = double(x); // +1 local var declaration + System.out.println(\"double \" + x + \" = \" + y2); // +1 expression statement + String message = (x % 2) == 0 ? \"Evenly done.\" : \"Oddly done.\"; // +1 local var declaration + Object done = (Runnable) () -> { System.out.println(\"Done!\"); }; // +2 local var declaration + expression statement + String s = \"string\"; // +1 local var declaration + boolean isS = (s instanceof String); // +1 local var declaration + done.run(); // +1 expression statement ", "foo.java", JavaParser, loc, [ - (lloc, 28, usize), // The number of statements is 28 + (lloc, 12, usize), // The number of statements is 12 ] ); } @@ -2264,7 +2328,7 @@ mod tests { class HelloWorldApp { public void main(String[] args) { - String message = args.length == 0 ? \"Hello empty world\" : \"Hello world\"; // +3 lloc : 1 var assignment + ternary + binary exp + String message = args.length == 0 ? \"Hello empty world\" : \"Hello world\"; // +1 lloc : 1 var assignment System.out.println(message); // Display the string. +1 lloc } }", @@ -2274,7 +2338,7 @@ mod tests { [ (sloc, 12, usize), // The number of lines is 12 (ploc, 7, usize), // The number of code lines is 7 - (lloc, 4, usize), // The number of statements is 4 + (lloc, 2, usize), // The number of statements is 2 (cloc, 6, usize), // The number of comments is 6 (blank, 1, usize) // The number of blank lines is 1 ] From c8112e1759164fa9a729a17f939eacf35afa797a Mon Sep 17 00:00:00 2001 From: Devon Burriss Date: Wed, 2 Mar 2022 19:43:15 +0100 Subject: [PATCH 15/19] Do not count expressions in for loop --- src/metrics/loc.rs | 56 ++++++++++++++++------------------------------ 1 file changed, 19 insertions(+), 37 deletions(-) diff --git a/src/metrics/loc.rs b/src/metrics/loc.rs index e4818fc52..dd6922a75 100644 --- a/src/metrics/loc.rs +++ b/src/metrics/loc.rs @@ -823,31 +823,27 @@ impl Loc for JavaCode { let (start, end) = init(node, stats, is_func_space, is_unit); let kind_id: Java = node.object().kind_id().into(); println!("{}", kind_id); + // LLOC in Java is counted for statements only + // https://docs.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html match kind_id { Program => {} Comment => { add_cloc_lines(stats, start, end); } - AssertStatement - | BreakStatement - | ContinueStatement - | DoStatement - | EnhancedForStatement - | ExpressionStatement - | IfStatement - | LocalVariableDeclaration - | ReturnStatement - | SwitchStatement - | ThrowStatement - | TryStatement + AssertStatement | BreakStatement | ContinueStatement | DoStatement + | EnhancedForStatement | ExpressionStatement | ForStatement | IfStatement + | ReturnStatement | SwitchStatement | ThrowStatement | TryStatement | WhileStatement => { stats.logical_lines += 1; println!("+1"); } - ForStatement => { - // handle binary check + increment - stats.logical_lines += 2; - println!("+2"); + LocalVariableDeclaration => { + if count_specific_ancestors!(node, ForStatement, Block) == 0 { + // The initializer, condition, and increment in a for loop are expressions. + // Don't count the variable declaration if in a ForStatement. + // https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html + stats.logical_lines += 1; + } } _ => { check_comment_ends_on_code_line(stats, start); @@ -2082,31 +2078,17 @@ mod tests { ); } - #[test] - fn java_update_expression_lloc() { - check_metrics!( - "int i = 10; - i++;", - "foo.java", - JavaParser, - loc, - [ - (lloc, 2, usize), // The number of statements is 2 - ] - ); - } - #[test] fn java_for_lloc() { check_metrics!( - "for (int i = 0; i < 100; i++) { // + 3 + "for (int i = 0; i < 100; i++) { // + 1 System.out.println(i); // + 1 }", "foo.java", JavaParser, loc, [ - (lloc, 4, usize), // The number of statements is 4 + (lloc, 2, usize), // The number of statements is 2 ] ); } @@ -2197,7 +2179,7 @@ mod tests { check_metrics!( "int max = 10; // +1 - for (int i = 0; i < max; i++) { // +3 + for (int i = 0; i < max; i++) { // +1 if(i % 2 == 0) { continue;} + 2 System.out.println(i); // +1 }", @@ -2205,7 +2187,7 @@ mod tests { JavaParser, loc, [ - (lloc, 7, usize), // The number of statements is 7 + (lloc, 5, usize), // The number of statements is 5 ] ); } @@ -2255,7 +2237,7 @@ mod tests { fn java_expressions_lloc() { check_metrics!( "int x = 10; // +1 local var declaration - x=+90; // +1 expression statement + x=+89; // +1 expression statement int y = x * 2; // +1 local var declaration IntFunction double = (n) -> n*2; // +1 local var declaration int y2 = double(x); // +1 local var declaration @@ -2284,7 +2266,7 @@ mod tests { loc, [ (ploc, 1, usize), // The number of code lines is 1 - (lloc, 4, usize), // The number of statements is 4 + (lloc, 2, usize), // The number of statements is 2 (cloc, 0, usize), // The number of comments is 0 ] ); @@ -2310,7 +2292,7 @@ mod tests { [ (sloc, 11, usize), // The number of lines is 11 (ploc, 4, usize), // The number of code lines is 4 - (lloc, 5, usize), // The number of statements is 5 + (lloc, 3, usize), // The number of statements is 3 (cloc, 6, usize), // The number of comments is 6 (blank, 1, usize) // The number of blank lines is 1 ] From c90d092e21f8312a27e4fa33c4d4d80801fdac44 Mon Sep 17 00:00:00 2001 From: Devon Burriss Date: Wed, 2 Mar 2022 19:44:06 +0100 Subject: [PATCH 16/19] Removes debugging lines --- src/metrics/loc.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/metrics/loc.rs b/src/metrics/loc.rs index dd6922a75..686ac6109 100644 --- a/src/metrics/loc.rs +++ b/src/metrics/loc.rs @@ -810,19 +810,12 @@ impl Loc for CppCode { } } -impl fmt::Display for Java { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{:?}", self) - } -} - impl Loc for JavaCode { fn compute(node: &Node, stats: &mut Stats, is_func_space: bool, is_unit: bool) { use Java::*; let (start, end) = init(node, stats, is_func_space, is_unit); let kind_id: Java = node.object().kind_id().into(); - println!("{}", kind_id); // LLOC in Java is counted for statements only // https://docs.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html match kind_id { @@ -835,7 +828,6 @@ impl Loc for JavaCode { | ReturnStatement | SwitchStatement | ThrowStatement | TryStatement | WhileStatement => { stats.logical_lines += 1; - println!("+1"); } LocalVariableDeclaration => { if count_specific_ancestors!(node, ForStatement, Block) == 0 { From 8bac42f1ec16511a1eafe7e793554efdb2615226 Mon Sep 17 00:00:00 2001 From: Devon Burriss Date: Sat, 19 Mar 2022 08:33:58 +0100 Subject: [PATCH 17/19] Fixes for the recent Stats changes --- src/metrics/loc.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/metrics/loc.rs b/src/metrics/loc.rs index 686ac6109..9bf0ddb59 100644 --- a/src/metrics/loc.rs +++ b/src/metrics/loc.rs @@ -827,19 +827,19 @@ impl Loc for JavaCode { | EnhancedForStatement | ExpressionStatement | ForStatement | IfStatement | ReturnStatement | SwitchStatement | ThrowStatement | TryStatement | WhileStatement => { - stats.logical_lines += 1; + stats.lloc.logical_lines += 1; } LocalVariableDeclaration => { if count_specific_ancestors!(node, ForStatement, Block) == 0 { // The initializer, condition, and increment in a for loop are expressions. // Don't count the variable declaration if in a ForStatement. // https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html - stats.logical_lines += 1; + stats.lloc.logical_lines += 1; } } _ => { check_comment_ends_on_code_line(stats, start); - stats.lines.insert(start); + stats.ploc.lines.insert(start); } } } From c7652930701418d7b279ea0a80615becc37bf82f Mon Sep 17 00:00:00 2001 From: Devon Burriss Date: Sun, 20 Mar 2022 13:22:31 +0100 Subject: [PATCH 18/19] Defines space kinds --- src/getter.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/getter.rs b/src/getter.rs index cbcbe8e1c..e93a729c2 100644 --- a/src/getter.rs +++ b/src/getter.rs @@ -515,6 +515,9 @@ impl Getter for JavaCode { let typ = node.object().kind_id(); match typ.into() { + ClassDeclaration => SpaceKind::Class, + MethodDeclaration | LambdaExpression => SpaceKind::Function, + PackageDeclaration | ModuleDeclaration => SpaceKind::Namespace, Program => SpaceKind::Unit, _ => SpaceKind::Unknown, } From 0a7b404470668336439ee5c7dd87ab060fade897 Mon Sep 17 00:00:00 2001 From: Devon Burriss Date: Tue, 22 Mar 2022 20:56:25 +0100 Subject: [PATCH 19/19] Adds interface space --- src/getter.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/getter.rs b/src/getter.rs index e93a729c2..ec7f95828 100644 --- a/src/getter.rs +++ b/src/getter.rs @@ -515,9 +515,8 @@ impl Getter for JavaCode { let typ = node.object().kind_id(); match typ.into() { - ClassDeclaration => SpaceKind::Class, + InterfaceDeclaration | ClassDeclaration => SpaceKind::Class, MethodDeclaration | LambdaExpression => SpaceKind::Function, - PackageDeclaration | ModuleDeclaration => SpaceKind::Namespace, Program => SpaceKind::Unit, _ => SpaceKind::Unknown, }