Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java LoC metric #694

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/getter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,4 +509,16 @@ 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() {
InterfaceDeclaration | ClassDeclaration => SpaceKind::Class,
MethodDeclaration | LambdaExpression => SpaceKind::Function,
Program => SpaceKind::Unit,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My definition of space is: something which can have its own metrics.
So for example, you can have a class A with an inner class B each of them could be a space: this way we'll have some metrics for both of them:

  • B has its own metrics
  • A has its own metrics merged with the B ones
    And at the end in the final output you'll have the granularity the user wants.
    So not having more spaces here means that we don't have any granularity.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. I have taken a shot at it based on the other implementations and that explanation. Other than that not having Program => SpaceKind::Unit causes the line count to be off (javascript and typescript have this match too), I am uncertain how to actually test this in the context of LoC?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@calixteman I think this covers what can contain other code in Java. As I said in the above comment, I am uncertain how to test this in the context of LoC.

ClassDeclaration => SpaceKind::Class,
MethodDeclaration | LambdaExpression => SpaceKind::Function,
PackageDeclaration | ModuleDeclaration => SpaceKind::Namespace,
Program => SpaceKind::Unit

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to see space in actions you could just run cargo run -p rust-code-analysis-cli -- -m -p foo.java and have a look on the output, with foo.java:

class Outer {
  String x;
 
  class Inner {
    String y;
    int bar() {
       return 12;
    }
  }
}

If you return unknown for any kind of space, you'll get some unknown in the output.
Maybe the "off by 1" issue is related to:
https://github.com/mozilla/rust-code-analysis/blob/master/src/metrics/loc.rs#L40

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah cool! Makes complete sense. And yes that line of code was what led me to the fix of Unit for Program for the "off by 1" issue. It was in November last year when I started on this (and learning Rust) so the memories are a little hazy.

Adding a lambda expression and a package to the java file didn't seem to add anything to the structure. Not sure if I should leave them in, conceptually it seems correct to me as is. The code on this comment is not showing as outdated but I do think the kind is working as you wanted. See image:

image

_ => SpaceKind::Unknown,
}
}
}
Loading