Skip to content

Commit a85c385

Browse files
committed
Initial halstead impl
1 parent bcb3ca8 commit a85c385

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

src/getter.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,4 +521,59 @@ impl Getter for JavaCode {
521521
_ => SpaceKind::Unknown,
522522
}
523523
}
524+
525+
// fn get_func_space_name<'a>(node: &Node, code: &'a [u8]) -> Option<&'a str> {
526+
// if let Some(name) = node.object().child_by_field_name("name") {
527+
// let code = &code[name.start_byte()..name.end_byte()];
528+
// std::str::from_utf8(code).ok()
529+
// } else {
530+
// Some("<anonymous>")
531+
// }
532+
// }
533+
534+
fn get_op_type(node: &Node) -> HalsteadType {
535+
use Java::*;
536+
537+
// keywords, operators, literals: https://docs.oracle.com/javase/specs/jls/se18/html/jls-3.html#jls-3.12
538+
// https://www.geeksforgeeks.org/software-engineering-halsteads-software-metrics/?msclkid=5e181114abef11ecbb03527e95a34828
539+
//
540+
// comments not considered
541+
// Operator: function calls?
542+
// Operator: control flow
543+
// Operator: keywords
544+
// Operator: brackets and comma and terminators
545+
// Operator: operators
546+
// Operator: InstanceOf
547+
// Operator: .
548+
// Operand: struct name
549+
// Operand: vars and const
550+
551+
let typ = node.object().kind_id();
552+
match typ.into() {
553+
// LPAREN | COMMA | STAR | GTGT | COLON | SEMI | Return | Break | Continue | If | Else
554+
// | Switch | Case | Default | For | While | New | Try | Catch | Throw | Throws
555+
// | Throws2 | EQ | EQEQ | AMP | AMPAMP | AMPEQ | LT | LTLT | LTEQ | LTLTEQ | BANGEQ
556+
// | GTEQ | GTGTEQ | GTGTGT | GTGTGTEQ | PLUSEQ | BANG | STAREQ | SLASHEQ | PERCENTEQ
557+
// | CARET | CARETEQ | LBRACE | RBRACE | QMARK | Instanceof
558+
// Operator: function calls?
559+
MethodInvocation
560+
// Operator: control flow
561+
| If | Else | Switch | Case | Try | Catch | Throw | Throws | Throws2 | For | While | Continue | Break | Do | Finally
562+
// Operator: keywords
563+
| New | Return | Default | Abstract | Assert | Instanceof | Extends | Final | Implements | Transient | Synchronized | Super | This
564+
// Operator: brackets and comma and terminators (separators)
565+
| SEMI | COMMA | COLONCOLON | LBRACE | LBRACK | LPAREN | RBRACE | RBRACK | RPAREN | DOTDOTDOT | DOT
566+
// Operator: operators
567+
| EQ | LT | GT | BANG | TILDE | QMARK | COLON // no grammar for lambda operator ->
568+
| EQEQ | LTEQ | GTEQ | BANGEQ | AMPAMP | PIPEPIPE | PLUSPLUS | DASHDASH
569+
| PLUS | DASH | STAR | SLASH | AMP | PIPE | CARET | PERCENT| LTLT | GTGT | GTGTGT
570+
| PLUSEQ | DASHEQ | STAREQ | SLASHEQ | AMPEQ | PIPEEQ | CARETEQ | PERCENTEQ | LTLTEQ | GTGTEQ | GTGTGTEQ
571+
=> HalsteadType::Operator,
572+
// variables, constants
573+
Identifier | Literal => HalsteadType::Operand,
574+
_ => HalsteadType::Unknown,
575+
}
576+
}
577+
578+
get_operator!(Java);
524579
}

src/metrics/halstead.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,14 @@ impl Halstead for CppCode {
325325
}
326326
}
327327

328+
impl Halstead for JavaCode {
329+
fn compute<'a>(node: &Node<'a>, code: &'a [u8], halstead_maps: &mut HalsteadMaps<'a>) {
330+
compute_halstead::<Self>(node, code, halstead_maps);
331+
}
332+
}
333+
328334
impl Halstead for PreprocCode {}
329335
impl Halstead for CcommentCode {}
330-
impl Halstead for JavaCode {}
331336

332337
#[cfg(test)]
333338
mod tests {
@@ -521,4 +526,25 @@ mod tests {
521526
]
522527
);
523528
}
529+
530+
#[test]
531+
fn java_operators_and_operands() {
532+
check_metrics!(
533+
"public static void main(String args[]) {
534+
int a, b, c, avg;
535+
a = 5; b = 5; c = 5;
536+
avg = (a + b + c) / 3;
537+
MessageFormat.format(\"{0}\", avg);
538+
}",
539+
"foo.java",
540+
JavaParser,
541+
halstead,
542+
[
543+
(u_operators, 10, usize), // function, (), {}, var, =, +, /, ,, ., ;
544+
// (operators, 24, usize),
545+
// (u_operands, 11, usize), // main, a, b, c, avg, 3, 5, console.log, console, log, "{}"
546+
// (operands, 21, usize)
547+
]
548+
);
549+
}
524550
}

0 commit comments

Comments
 (0)