MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / extract_field

Method extract_field

codegraph-kernel/src/java.rs:715–793  ·  view source on GitHub ↗

extractField — each declarator becomes a field/constant node.

(&mut self, node: Node<'t>)

Source from the content-addressed store, hash-verified

713
714 /// extractField — each declarator becomes a field/constant node.
715 fn extract_field(&mut self, node: Node<'t>) {
716 let docstring = preceding_docstring(node, self.src);
717 let visibility = self.visibility_of(node);
718 let is_static = Some(self.is_static(node));
719 let field_kind: &'static str = if self.is_const(node) { "constant" } else { "field" };
720
721 let declarators: Vec<Node> = (0..node.named_child_count())
722 .filter_map(|i| node.named_child(i))
723 .filter(|c| c.kind() == "variable_declarator")
724 .collect();
725
726 if !declarators.is_empty() {
727 let type_node = (0..node.named_child_count())
728 .filter_map(|i| node.named_child(i))
729 .find(|c| {
730 !matches!(
731 c.kind(),
732 "modifiers" | "modifier" | "variable_declarator" | "variable_declaration"
733 | "marker_annotation" | "annotation"
734 )
735 });
736 let type_text = type_node.map(|t| self.text(t).to_string());
737
738 for decl in declarators {
739 let name_node = decl.child_by_field_name("name").or_else(|| {
740 (0..decl.named_child_count())
741 .filter_map(|i| decl.named_child(i))
742 .find(|c| c.kind() == "identifier")
743 });
744 let Some(name_node) = name_node else { continue };
745 let name = self.text(name_node).to_string();
746 let signature = match &type_text {
747 Some(t) => format!("{t} {name}"),
748 None => name.clone(),
749 };
750 let row = self.create_node(
751 field_kind,
752 &name,
753 decl,
754 Extra {
755 docstring: docstring.clone(),
756 signature: Some(signature),
757 visibility,
758 is_static,
759 ..Extra::default()
760 },
761 );
762 if let Some(row) = row {
763 self.extract_decorators_for(node, row);
764 self.extract_type_annotations(node, row);
765 // Walk the initializer ATTRIBUTED to the declared field
766 // (#693, the Go fix): the dispatcher only fn-ref-scans this
767 // subtree, so a lambda / method reference / anonymous class
768 // in `private final Runnable r = () -> target();` emitted no
769 // call edge at all.
770 if let Some(value) = decl.child_by_field_name("value") {
771 self.stack.push(Scope { row, kind: field_kind, name: name.clone() });
772 self.visit_function_body(value);

Callers 1

visit_nodeMethod · 0.45

Calls 12

preceding_docstringFunction · 0.85
mapMethod · 0.80
pushMethod · 0.80
textMethod · 0.65
visibility_ofMethod · 0.45
is_staticMethod · 0.45
is_constMethod · 0.45
collectMethod · 0.45
create_nodeMethod · 0.45
visit_function_bodyMethod · 0.45

Tested by

no test coverage detected