Class: TreeHaver::Backends::Java::Node Private

Inherits:
Object
  • Object
show all
Defined in:
lib/tree_haver/backends/java.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Java backend node wrapper (raw backend node)

This is a raw backend node that wraps a jtreesitter Node object via
JRuby’s Java interop. It provides the minimal interface needed for tree-sitter
operations but is NOT intended for direct use by application code.

== Architecture Note

Unlike pure-Ruby backends (Citrus, Parslet, Prism, Psych) which define Node
classes that inherit from TreeHaver::Base::Node, tree-sitter backends (MRI,
Rust, FFI, Java) define raw wrapper classes that get wrapped by TreeHaver::Node.

The wrapping hierarchy is:
Java::Node (this class) → TreeHaver::Node → Base::Node

When you use TreeHaver::Parser#parse, the returned tree’s nodes are already
wrapped in TreeHaver::Node, which provides the full unified API including:

  • #children - Array of child nodes
  • #text - Extract text from source
  • #first_child, #last_child - Convenience accessors
  • #start_line, #end_line - 1-based line numbers
  • #source_position - Hash with position info
  • #each, #map, etc. - Enumerable methods
  • #to_s, #inspect - String representations

This raw class only implements methods that require direct calls to jtreesitter.
The wrapper adds Ruby-level conveniences.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(impl) ⇒ Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Node.



697
698
699
# File 'lib/tree_haver/backends/java.rb', line 697

def initialize(impl)
  @impl = impl
end

Instance Attribute Details

#implObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



694
695
696
# File 'lib/tree_haver/backends/java.rb', line 694

def impl
  @impl
end

Instance Method Details

#child(index) ⇒ Node?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get a child by index

Parameters:

  • index (Integer)

    the child index

Returns:

  • (Node, nil)

    the child node or nil if index out of bounds



719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
# File 'lib/tree_haver/backends/java.rb', line 719

def child(index)
  # jtreesitter 0.26.0: getChild returns Optional<Node> or throws IndexOutOfBoundsException
  result = @impl.getChild(index)
  return if result.nil?

  # Handle Java Optional
  if result.respond_to?(:isPresent)
    return unless result.isPresent
    java_node = result.get
  else
    # Direct Node return (some jtreesitter versions)
    java_node = result
  end

  Node.new(java_node)
rescue ::Java::JavaLang::IndexOutOfBoundsException
  nil
end

#child_by_field_name(name) ⇒ Node?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get a child by field name

Parameters:

  • name (String)

    the field name

Returns:

  • (Node, nil)

    the child node or nil if not found



742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
# File 'lib/tree_haver/backends/java.rb', line 742

def child_by_field_name(name)
  # jtreesitter 0.26.0: getChildByFieldName returns Optional<Node>
  # However, some versions or scenarios may return null directly
  result = @impl.getChildByFieldName(name)
  return if result.nil?

  # Handle Java Optional
  if result.respond_to?(:isPresent)
    return unless result.isPresent
    java_node = result.get
  else
    # Direct Node return (some jtreesitter versions)
    java_node = result
  end

  Node.new(java_node)
end

#child_countInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the number of children

Returns:

  • (Integer)

    child count



711
712
713
# File 'lib/tree_haver/backends/java.rb', line 711

def child_count
  @impl.childCount
end

#each {|Node| ... } ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Iterate over children

Yields:

  • (Node)

    each child node



764
765
766
767
768
769
# File 'lib/tree_haver/backends/java.rb', line 764

def each
  return enum_for(:each) unless block_given?
  child_count.times do |i|
    yield child(i)
  end
end

#end_byteInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the end byte position

Returns:

  • (Integer)

    end byte



781
782
783
# File 'lib/tree_haver/backends/java.rb', line 781

def end_byte
  @impl.endByte
end

#end_pointHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the end point (row, column)

Returns:

  • (Hash)

    with :row and :column keys



796
797
798
799
# File 'lib/tree_haver/backends/java.rb', line 796

def end_point
  pt = @impl.endPoint
  {row: pt.row, column: pt.column}
end

#has_error?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if this node has an error

Returns:

  • (Boolean)

    true if the node or any descendant has an error



804
805
806
# File 'lib/tree_haver/backends/java.rb', line 804

def has_error?
  @impl.hasError
end

#missing?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if this node is missing

Returns:

  • (Boolean)

    true if this is a MISSING node



811
812
813
# File 'lib/tree_haver/backends/java.rb', line 811

def missing?
  @impl.isMissing
end

#named?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if this is a named node

Returns:

  • (Boolean)

    true if this is a named node



818
819
820
# File 'lib/tree_haver/backends/java.rb', line 818

def named?
  @impl.isNamed
end

#next_siblingNode?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the next sibling node

Returns:

  • (Node, nil)

    the next sibling or nil if none



844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
# File 'lib/tree_haver/backends/java.rb', line 844

def next_sibling
  # jtreesitter 0.26.0: getNextSibling returns Optional<Node>
  result = @impl.getNextSibling
  return if result.nil?

  # Handle Java Optional
  if result.respond_to?(:isPresent)
    return unless result.isPresent
    java_node = result.get
  else
    java_node = result
  end

  Node.new(java_node)
end

#parentNode?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the parent node

Returns:

  • (Node, nil)

    the parent node or nil if this is the root



825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
# File 'lib/tree_haver/backends/java.rb', line 825

def parent
  # jtreesitter 0.26.0: getParent returns Optional<Node>
  result = @impl.getParent
  return if result.nil?

  # Handle Java Optional
  if result.respond_to?(:isPresent)
    return unless result.isPresent
    java_node = result.get
  else
    java_node = result
  end

  Node.new(java_node)
end

#prev_siblingNode?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the previous sibling node

Returns:

  • (Node, nil)

    the previous sibling or nil if none



863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
# File 'lib/tree_haver/backends/java.rb', line 863

def prev_sibling
  # jtreesitter 0.26.0: getPrevSibling returns Optional<Node>
  result = @impl.getPrevSibling
  return if result.nil?

  # Handle Java Optional
  if result.respond_to?(:isPresent)
    return unless result.isPresent
    java_node = result.get
  else
    java_node = result
  end

  Node.new(java_node)
end

#start_byteInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the start byte position

Returns:

  • (Integer)

    start byte



774
775
776
# File 'lib/tree_haver/backends/java.rb', line 774

def start_byte
  @impl.startByte
end

#start_pointHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the start point (row, column)

Returns:

  • (Hash)

    with :row and :column keys



788
789
790
791
# File 'lib/tree_haver/backends/java.rb', line 788

def start_point
  pt = @impl.startPoint
  {row: pt.row, column: pt.column}
end

#textString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the text of this node

Returns:

  • (String)

    the source text



882
883
884
# File 'lib/tree_haver/backends/java.rb', line 882

def text
  @impl.text.to_s
end

#typeString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the type of this node

Returns:

  • (String)

    the node type



704
705
706
# File 'lib/tree_haver/backends/java.rb', line 704

def type
  @impl.type
end