Skip to content

Commit

Permalink
Showing 5 changed files with 849 additions and 268 deletions.
45 changes: 27 additions & 18 deletions core/src/main/ruby/jruby/java/java_ext/java.io.rb
Original file line number Diff line number Diff line change
@@ -2,32 +2,41 @@
# @see org.jruby.javasupport.ext.JavaIo.java
# this file is no longer loaded but is kept to provide doc stubs

# Java *java.io.InputStream* objects are convertible to Ruby `IO`.
# @note Only explicit (or customized) Ruby methods are listed here,
# instances will have all of their Java methods available.
# @see http://docs.oracle.com/javase/8/docs/api/java/io/InputStream.html
class Java::java::io::InputStream
# Convert a Java input stream to a Ruby `IO`.
# @option opts [Types] autoclose changes `IO#autoclose=` if set
# @return [IO]
def to_io(opts = nil)
ruby_io = org.jruby.RubyIO.new(JRuby.runtime, self)
if opts && !opts[:autoclose]
ruby_io.setAutoclose(false)
end
JRuby.dereference(ruby_io)
# stub implemented in org.jruby.javasupport.ext.JavaIo.java
end
end
end if false

# Java *java.io.OutputStream* objects are convertible to Ruby `IO`.
# @note Only explicit (or customized) Ruby methods are listed here,
# instances will have all of their Java methods available.
# @see http://docs.oracle.com/javase/8/docs/api/java/io/OutputStream.html
class Java::java::io::OutputStream
# Convert a Java output stream to a Ruby `IO`.
# @option opts [Types] autoclose changes `IO#autoclose=` if set
# @return [IO]
def to_io(opts = nil)
ruby_io = org.jruby.RubyIO.new(JRuby.runtime, self)
if opts && !opts[:autoclose]
ruby_io.setAutoclose(false)
end
JRuby.dereference(ruby_io)
# stub implemented in org.jruby.javasupport.ext.JavaIo.java
end
end
end if false

# Java channels (*java.nio.channels.Channel*) are convertible to Ruby `IO`.
# @note Only explicit (or customized) Ruby methods are listed here,
# instances will have all of their Java methods available.
# @see http://docs.oracle.com/javase/8/docs/api/java/nio/channels/Channel.html
module Java::java::nio::channels::Channel
# Convert a Java channel to a Ruby `IO`.
# @option opts [Types] autoclose changes `IO#autoclose=` if set
# @return [IO]
def to_io(opts = nil)
ruby_io = org.jruby.RubyIO.new(JRuby.runtime, self)
if opts && !opts[:autoclose]
ruby_io.setAutoclose(false)
end
JRuby.dereference(ruby_io)
# stub implemented in org.jruby.javasupport.ext.JavaIo.java
end
end
end if false
357 changes: 273 additions & 84 deletions core/src/main/ruby/jruby/java/java_ext/java.lang.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# NOTE: these Ruby extensions were moved to native code!
# @see org.jruby.javasupport.ext.JavaLang.java
# @see org.jruby.javasupport.ext.JavaLangReflect.java
# - **org.jruby.javasupport.ext.JavaLang.java**
# - **org.jruby.javasupport.ext.JavaLangReflect.java**
# this file is no longer loaded but is kept to provide doc stubs

# @private internal helper
@@ -32,198 +32,387 @@ def static?
# *java.lang.Runnable* instances allow for a `to_proc` conversion.
# @see http://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html
module Java::java::lang::Runnable
# @return [Proc] calling #run when caled
def to_proc
proc { self.run }
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
# proc { self.run }
end
end
end if false

# A `java.lang.Iterable` will act like a Ruby `Enumerable`.
# @see http://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html
module Java::java::lang::Iterable
include ::Enumerable

def each
iter = iterator
yield(iter.next) while iter.hasNext
# Ruby style `Enumerable#each` iteration for Java iterable types.
# @return [Enumerator] if called without a block to yield to
def each(&block)
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
# iter = iterator
# yield(iter.next) while iter.hasNext
end

def each_with_index
index = 0
iter = iterator
while iter.hasNext
yield(iter.next, index)
index += 1
end
# Ruby style `Enumerable#each_with_index` for Java iterable types.
# @return [Enumerator] if called without a block to yield to
def each_with_index(&block)
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
# index = 0
# iter = iterator
# while iter.hasNext
# yield(iter.next, index)
# index += 1
# end
end
end
end if false

# *java.lang.Comparable* mixes in Ruby's `Comparable` support.
# @see http://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html
module Java::java::lang::Comparable
include ::Comparable

def <=>(a)
return nil if a.nil?
compareTo(a)
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
# return nil if a.nil?
# compareTo(a)
end
end
end if false

# Java's *java.lang.Throwable* (exception/errors) classes resemble Ruby's `Exception`.
# Java's *java.lang.Throwable* (exception/error) classes resemble Ruby's `Exception`.
# @see http://docs.oracle.com/javase/8/docs/api/java/lang/Throwable.html
class Java::java::lang::Throwable

# @return [Array] the mapped stack-trace
def backtrace
stack_trace.map(&:to_s)
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
# stack_trace.map(&:to_s)
end

# @note Noop as Java exceptions can not change trace.
# @note Noop as Java exceptions can not change their stack-trace.
def set_backtrace(trace)
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
end

# Always a non-nil to follow Ruby's {Exception#message} conventions.
# @note getMessage still returns nil, when no message was given for the Java exception!
# @return [String]
def message
getLocalizedMessage || ''
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
# getLocalizedMessage || ''
end

def to_s
message
# message
end

def inspect
to_string
# to_string
end

class << self
alias :old_eqq :===
def ===(rhs)
if (NativeException == rhs.class) && (java_class.assignable_from?(rhs.cause.java_class))
true
else
old_eqq(rhs)
end
end
# Adds case compare against `NativeException` wrapped throwables.
# @example
# begin
# java.lang.Integer.parseInt('gg', 16)
# rescue NativeException => ex
# expect( java.lang.NumberFormatException === ex ).to be true
# end
# @return [true, false]
def self.===(ex)
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
end
end

end if false

# *java.lang.Character* represents an object wrapper for Java's *char* primitive.
# @see http://docs.oracle.com/javase/8/docs/api/java/lang/Character.html
class Java::java::lang::Character
java_alias :isJavaIdentifierStart_char, :isJavaIdentifierStart, [Java::char]
java_alias :isJavaIdentifierPart_char, :isJavaIdentifierPart, [Java::char]

def self.java_identifier_start?(fixnum)
isJavaIdentifierStart_char(fixnum);
# `java.lang.Character.isJavaIdentifierStart(char)`
# @return [true, false]
def self.java_identifier_start?(char)
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
end

def self.java_identifier_part?(fixnum)
isJavaIdentifierPart_char(fixnum);
# `java.lang.Character.isJavaIdentifierPart(char)`
# @return [true, false]
def self.java_identifier_part?(char)
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
end
end

end if false

# *java.lang.Class*
# @note Only explicit (or customized) Ruby methods are listed here,
# Java classes will have all of their Java methods available.
# @see http://docs.oracle.com/javase/8/docs/api/java/lang/Class.html
# @todo likely to get revised!
class Java::java::lang::Class
include ::Comparable
include ::JavaUtilities::ModifierShortcuts
# include ::JavaUtilities::ModifierShortcuts

# @return [Class, Module] the proxy class (or module in case of an interface).
def ruby_class
::JRuby.runtime.java_support.get_proxy_class_from_cache(self)
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
# ::JRuby.runtime.java_support.get_proxy_class_from_cache(self)
end

alias to_s name
# @return [String] the Java class name
def to_s
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
end

# @return [String] `java.lang.Class#toString`
def inspect
"class #{name}"
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
end

def resource_as_string(name)
resource_as_stream(name).to_io.read
# @return [Java::java::io::InputStream]
def resource_as_stream(name)
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
end

alias annotation get_annotation
# @return [String]
def resource_as_string(name)
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
# resource_as_stream(name).to_io.read
end

# @return [true, false]
def annotations?
!annotations.empty?
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
# !annotations.empty?
end

# @return [true, false]
def declared_annotations?
!declared_annotations.empty?
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
# !declared_annotations.empty?
end

alias annotation_present? is_annotation_present

def <=>(other)
return nil unless other.class == java::lang::Class

return 0 if self == other
return 1 if self.is_assignable_from(other)
return -1 if other.is_assignable_from(self)
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
# return nil unless other.class == java::lang::Class
#
# return 0 if self == other
# return +1 if self.is_assignable_from(other)
# return -1 if other.is_assignable_from(self)
end

def java_instance_methods
methods.select {|m| !Modifier.is_static(m.modifiers)}.freeze
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
end

def declared_instance_methods
declared_methods.select {|m| !Modifier.is_static(m.modifiers)}.freeze
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
end

def java_class_methods
methods.select {|m| Modifier.is_static(m.modifiers)}.freeze
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
end

def declared_class_methods
declared_methods.select {|m| Modifier.is_static(m.modifiers)}.freeze
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
end
end

# @return [true, false]
# @since 9.1
def anonymous?
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
end

# @return [true, false]
# @since 9.1
def abstract?
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
end

# @return [true, false]
def public?
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
end

# @return [true, false]
def protected?
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
end

# @return [true, false]
def private?
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
end

# @return [true, false]
def final?
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
end

# @private
def static?
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
end

end if false

# *java.lang.ClassLoader*
# @see http://docs.oracle.com/javase/8/docs/api/java/lang/ClassLoader.html
class Java::java::lang::ClassLoader
alias resource_as_stream get_resource_as_stream
alias resource_as_url get_resource
# @return [Java::java::io::InputStream]
def resource_as_stream(name)
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
end

# @return [String]
def resource_as_string(name)
resource_as_stream(name).to_io.read
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
# resource_as_stream(name).to_io.read
end
end

class Java::java::lang::reflect::AccessibleObject
include ::JavaUtilities::ModifierShortcuts

alias inspect to_s
end
def resource_as_url(name)
# stub implemented in org.jruby.javasupport.ext.JavaLang.java
end
end if false

# @see http://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Constructor.html
class Java::java::lang::reflect::Constructor

def return_type
nil
end

alias argument_types parameter_types
end
def argument_types
# stub implemented in org.jruby.javasupport.ext.JavaLangReflect.java
# parameter_types
end

# @return [true, false]
def public?
# stub implemented in org.jruby.javasupport.ext.JavaLangReflect.java
end

# @return [true, false]
def protected?
# stub implemented in org.jruby.javasupport.ext.JavaLangReflect.java
end

# @return [true, false]
def private?
# stub implemented in org.jruby.javasupport.ext.JavaLangReflect.java
end

# @return [true, false]
def final?
# stub implemented in org.jruby.javasupport.ext.JavaLangReflect.java
end

# @private
def static?
# stub implemented in org.jruby.javasupport.ext.JavaLangReflect.java
end

end if false

# @see http://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html
class Java::java::lang::reflect::Method

def return_type
# stub implemented in org.jruby.javasupport.ext.JavaLangReflect.java
end

def argument_types
# stub implemented in org.jruby.javasupport.ext.JavaLangReflect.java
# parameter_types
end

def invoke_static(*args)
invoke(nil, *args)
# stub implemented in org.jruby.javasupport.ext.JavaLangReflect.java
# invoke(nil, *args)
end

alias argument_types parameter_types
end
# @return [true, false]
# @since 9.1
def abstract?
# stub implemented in org.jruby.javasupport.ext.JavaLangReflect.java
end

# @return [true, false]
def public?
# stub implemented in org.jruby.javasupport.ext.JavaLangReflect.java
end

# @return [true, false]
def protected?
# stub implemented in org.jruby.javasupport.ext.JavaLangReflect.java
end

# @return [true, false]
def private?
# stub implemented in org.jruby.javasupport.ext.JavaLangReflect.java
end

# @return [true, false]
def final?
# stub implemented in org.jruby.javasupport.ext.JavaLangReflect.java
end

# @return [true, false]
def static?
# stub implemented in org.jruby.javasupport.ext.JavaLangReflect.java
end

end if false

# @see http://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html
class Java::java::lang::reflect::Field
alias value_type name
alias value get
alias set_value set

def value_type
# stub implemented in org.jruby.javasupport.ext.JavaLangReflect.java
end

def value(obj)
# stub implemented in org.jruby.javasupport.ext.JavaLangReflect.java
# get(obj)
end

def set_value(obj, value)
# stub implemented in org.jruby.javasupport.ext.JavaLangReflect.java
# set(obj, value)
end

def static_value
get(nil)
# stub implemented in org.jruby.javasupport.ext.JavaLangReflect.java
# get(nil)
end

def set_static_value(val)
set(nil, val)
def set_static_value(value)
# stub implemented in org.jruby.javasupport.ext.JavaLangReflect.java
# set(nil, value)
end
end

# @return [true, false]
def public?
# stub implemented in org.jruby.javasupport.ext.JavaLangReflect.java
end

# @return [true, false]
def protected?
# stub implemented in org.jruby.javasupport.ext.JavaLangReflect.java
end

# @return [true, false]
def private?
# stub implemented in org.jruby.javasupport.ext.JavaLangReflect.java
end

# @return [true, false]
def final?
# stub implemented in org.jruby.javasupport.ext.JavaLangReflect.java
end

# @return [true, false]
def static?
# stub implemented in org.jruby.javasupport.ext.JavaLangReflect.java
end

end if false

Java::byte[].class_eval do
def ubyte_get(index)
@@ -236,4 +425,4 @@ def ubyte_set(index, value)
value -= 256 if value > 127
self[index] = value
end
end
end if false
35 changes: 21 additions & 14 deletions core/src/main/ruby/jruby/java/java_ext/java.net.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
# NOTE: these Ruby extensions were moved to native code!
# @see org.jruby.javasupport.ext.JavaIo.java
# - **org.jruby.javasupport.ext.JavaNet.java**
# this file is no longer loaded but is kept to provide doc stubs

# *java.net.URL* extensions.
# @note Only explicit (or customized) Ruby methods are listed here,
# instances will have all of their Java methods available.
# @see http://docs.oracle.com/javase/8/docs/api/java/net/URL.html
class Java::java::net::URL
def open(*rest, &block)
stream = openStream
io = stream.to_io
if block
begin
block.call(io)
ensure
stream.close
end
else
io
end
# Open the URL stream and yield it as a Ruby `IO`.
# @return [IO] if no block given, otherwise yielded result
def open(&block)
# stub implemented in org.jruby.javasupport.ext.JavaNet.java
# stream = openStream
# io = stream.to_io
# if block
# begin
# block.call(io)
# ensure
# stream.close
# end
# else
# io
# end
end
end
end if false
494 changes: 396 additions & 98 deletions core/src/main/ruby/jruby/java/java_ext/java.util.rb
Original file line number Diff line number Diff line change
@@ -1,74 +1,130 @@
# NOTE: these Ruby extensions were moved to native code!
# @see org.jruby.javasupport.ext.JavaUtil.java
# - **org.jruby.javasupport.ext.JavaUtil.java**
# this file is no longer loaded but is kept to provide doc stubs

# *java.util.Collection* is enhanced (not just) to act like Ruby's `Enumerable`.
# @note Only explicit (or customized) Ruby methods are listed here,
# instances will have all of their Java methods available.
# @see http://docs.oracle.com/javase/8/docs/api/java/util/Collection.html
module Java::java::util::Collection
include ::Enumerable

def each
i = iterator
while i.hasNext
yield i.next
end
# @see Java::java::lang::Iterable#each
def each(&block)
# stub implemented in org.jruby.javasupport.ext.JavaUtil.java
end

# @see Java::java::lang::Iterable#each_with_index
def each_with_index(&block)
# stub implemented in org.jruby.javasupport.ext.JavaUtil.java
end

def <<(a); add(a); self end
# `Enumerable#first`
# @note Might collide with *java.util.Deque#getFirst* in which case you want to alias its ruby_ name
# so that the Ruby version is used e.g. `java.util.ArrayDeque.class_eval { alias first ruby_first }`
# @since 9.1
def first(count = nil)
# stub implemented in org.jruby.javasupport.ext.JavaUtil.java
end
alias ruby_first first

# Pushes (adds) and element into this collection.
# @example
# coll = java.util.ArrayDeque.new
# coll << 111
# @return [Java::java::util::Collection]
def <<(a)
# stub implemented in org.jruby.javasupport.ext.JavaUtil.java
# add(a)
# self
end

# Join this collection with another (adding all elements).
# @example
# coll1 = java.util.ArrayList.new [1, 2]
# coll2 = java.util.LinkedHashSet.new [2, 3]
# coll1 + coll2 # [ 1, 2, 2, 3 ] (java.util.ArrayList)
# coll2 + coll1 # [ 2, 3, 1 ] (java.util.LinkedHashSet)
# @return [Java::java::util::Collection] a new collection
def +(oth)
nw = self.dup
nw.addAll(oth)
nw
# stub implemented in org.jruby.javasupport.ext.JavaUtil.java
# nw = self.dup
# nw.addAll(oth)
# nw
end

# Subtract all elements in the provided collection from this one.
# @example
# coll1= java.util.HashSet.new [2, 3]
# coll2 = java.util.LinkedList.new [1, 2]
# coll1 - coll2 # [ 3 ] (java.util.HashSet)
# @return [Java::java::util::Collection] a new collection
def -(oth)
nw = self.dup
nw.removeAll(oth)
nw
# stub implemented in org.jruby.javasupport.ext.JavaUtil.java
# nw = self.dup
# nw.removeAll(oth)
# nw
end

def length
size
# @return [Integer] the collection size
def size
# stub implemented in org.jruby.javasupport.ext.JavaUtil.java
end
alias length size

# @private Not sure if this makes sense to have.
def join(*args)
to_a.join(*args)
# stub implemented in org.jruby.javasupport.ext.JavaUtil.java
end

# Converts a collection instance to an array.
# @return [Array]
def to_a
# JRUBY-3910: conversion is faster by going directly to java array
# first
toArray.to_a
# stub implemented in org.jruby.javasupport.ext.JavaUtil.java
end

end
# Return a dup-ed collection (if possible).
# @example
# arr = java.util.concurrent.CopyOnWriteArrayList.new ['0']
# arr.dup # ['0'] a new CopyOnWriteArrayList instance
# @return [Java::java::util::Collection] of the same type as target.
# @since 9.1
def dup
# stub implemented in org.jruby.javasupport.ext.JavaUtil.java
end

end if false

# A *java.util.Enumeration* instance might be iterated Ruby style.
# @see http://docs.oracle.com/javase/8/docs/api/java/util/Enumeration.html
module Java::java::util::Enumeration
include ::Enumerable

def each
while has_more_elements
yield next_element
end
# stub implemented in org.jruby.javasupport.ext.JavaUtil.java
# while hasMoreElements
# yield nextElement
# end
end
end
end if false

# A *java.util.Iterator* acts like an `Enumerable`.
# @see http://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
module Java::java::util::Iterator
include ::Enumerable

def each
while has_next
yield self.next
end
# stub implemented in org.jruby.javasupport.ext.JavaUtil.java
# while hasNext
# yield next
# end
end
end
end if false

# Ruby extensions for *java.util.List* instances.
# All of the {@link Java::java::util::Collection} methods are available.
# @note Only explicit (or customized) Ruby methods are listed here,
# instances will have all of their Java methods available.
# @see Java::java::util::Collection
# @see http://docs.oracle.com/javase/8/docs/api/java/util/List.html
module Java::java::util::List
@@ -94,89 +150,331 @@ def compare(o1, o2)
end
private_constant :RubyComparators

def [](ix1, ix2 = nil)
if (ix2)
sub_list(ix1, ix1 + ix2)
elsif (ix1.is_a?(Range))
sub_list(ix1.first, ix1.exclude_end? ? ix1.last : ix1.last + 1)
elsif ix1 < size
get(ix1)
else
nil
end
# Element retrieval (slicing) similar to `Array#[]`.
# @example
# list = java.util.ArrayList.new ['foo', 'bar', 'baz']
# list[0] # 'foo'
# list[-2] # 'bar'
# list[10] # nil
# list[1, 2] # ['bar','baz'] sub-list
# list[0..2] # ['foo','bar'] sub-list
# @return [Java::java::util::List, Object, nil] sub-list, list element or nil
# @note Like with an `Array` will return *nil* for "out-of-bound" indexes.
def [](i1, i2 = nil)
# stub implemented in org.jruby.javasupport.ext.JavaUtil.java
end

def []=(ix,val)
if (ix.is_a?(Range))
ix.each { |i| remove(i) }
add_all(ix.first, val)
elsif size < ix
((ix-size)+1).times { self << nil }
end
set(ix,val)
val
# Set an element or multiple elements like `Array#[]=`.
# @example
# list = java.util.ArrayList.new ['foo', 'bar', 'baz']
# list[-3] = 1 # [1, 'bar', 'baz']
# list[4] = 4 # [1, 'bar', 'baz', nil, 4]
# list[1..2] = 3 # [1, nil, 3, 4]
# @return [Object] set value
def []=(i, val)
# stub implemented in org.jruby.javasupport.ext.JavaUtil.java
end

def index(obj = (no_args = true))
if !no_args
ix = 0
iter = iterator
while (iter.has_next)
return ix if obj == iter.next
ix +=1
end
return nil
elsif block_given?
ix = 0
iter = iterator
while (iter.has_next)
return ix if yield iter.next
ix +=1
end
return nil
else
Enumerator.new(self, :index)
end
# Return an index for the first occurrence of the matched element (similar to `Array#index`).
# @example
# list = java.util.LinkedList.new [ 'foo', 'bar', 'foo' ]
# list.index 'foo' # 0
# list.index 'baz' # nil
# list.index { |e| e.start_with?('ba') } # 1
# @return [Integer, nil, Enumerator]
def index(obj = nil, &block)
# stub implemented in org.jruby.javasupport.ext.JavaUtil.java
end

def rindex(obj = (no_args = true))
if !no_args
i = size
while (i -= 1) >= 0
return i if obj == get(i)

# blocks can modify the list, don't go past bounds
i = size if i > size
end
return nil
elsif block_given?
i = size
while (i -= 1) >= 0
return i if yield get(i)

# blocks can modify the list, don't go past bounds
i = size if i > size
end
return nil
else
Enumerator.new(self, :rindex)
end
# Return an index for the last occurrence of the matched element (similar to `Array#rindex`).
# @example
# list = java.util.LinkedList.new [ 'foo', 'bar', 'foo' ]
# list.rindex 'foo' # 2
# list.rindex 'baz' # nil
# list.rindex { |e| e.start_with?('ba') } # 1
# @return [Integer, nil, Enumerator]
def rindex(obj = nil, &block)
# stub implemented in org.jruby.javasupport.ext.JavaUtil.java
end

# Sort a (mutable) list, returning a new instance of same type.
# @example
# list = java.util.ArrayList.new [ 22, 1, 333 ]
# list.sort # [ 1, 22, 333 ] (java.util.ArrayList) ... only works on Java 7 by default!
# @note Since Java 8 this method collides with the built-in *java.util.List#sort* in which case you want to alias its
# ruby_ name for list types where you want to have the Ruby version available
# e.g. `java.util.ArrayList.class_eval { alias sort ruby_sort }`
# @return [Java::java::util::List] dup-ed list with element sorted
def sort(&block)
comparator = block ? RubyComparators::BlockComparator.new(block) : RubyComparators::SpaceshipComparator.new
list = java::util::ArrayList.new
list.addAll(self)
java::util::Collections.sort(list, comparator)
list
# stub implemented in org.jruby.javasupport.ext.JavaUtil.java
# comparator = block ? RubyComparators::BlockComparator.new(block) : RubyComparators::SpaceshipComparator.new
# list = self.dup
# java::util::Collections.sort(list, comparator)
# list
end
alias ruby_sort sort

# Sort a (mutable) list, in place.
# @example
# list = java.util.Vector.new [ '22', '1', '333' ]
# list.sort! { |a, b| a.length <=> b.length } # [ '1', '22', '333' ]
# @return [Java::java::util::List] self
def sort!(&block)
comparator = block ? RubyComparators::BlockComparator.new(block) : RubyComparators::SpaceshipComparator.new
java::util::Collections.sort(self, comparator)
self
# stub implemented in org.jruby.javasupport.ext.JavaUtil.java
# comparator = block ? RubyComparators::BlockComparator.new(block) : RubyComparators::SpaceshipComparator.new
# java::util::Collections.sort(self, comparator)
# self
end

# @see Java::java::util::Collection#to_a
# @return [Array]
def to_a
# stub implemented in org.jruby.javasupport.ext.JavaUtil.java
end
alias_method :to_ary, :to_a

end
# `Enumerable#first`
# @example
# list = java.util.ArrayList.new [1, 2, 3]
# expect( list.first ).to eq 1
# expect( list.first(2).to_a ).to eq [1, 2]
# list = java.util.LinkedList.new
# expect( list.ruby_first ).to be nil
# @note Might collide with *#getFirst* on some list impls in which case you want to alias its ruby_ name
# so that the Ruby version is used e.g. `java.util.LinkedList.class_eval { alias first ruby_first }`
# @since 9.1
def first(count = nil)
# stub implemented in org.jruby.javasupport.ext.JavaUtil.java
end
alias ruby_first first

# `Array#last`
# @example
# list = java.util.Vector.new [1, 2, 3]
# expect( list.last ).to eq 3
# expect( list.last(2).to_a ).to eq [2, 3]
# @note Might collide with *#getLast* on some list impls in which case you want to alias its ruby_ name
# so that the Ruby version is used e.g. `java.util.LinkedList.class_eval { alias last ruby_last }`
# @since 9.1
def last(count = nil)
# stub implemented in org.jruby.javasupport.ext.JavaUtil.java
end
alias ruby_last last

end if false

# Ruby extensions for *java.util.Map* instances.
# Generally maps behave like Ruby's `Hash` objects.
# @see http://docs.oracle.com/javase/8/docs/api/java/util/Map.html
module Java::java::util::Map

def default(arg = nil)
# stub
end

def default=(value)
# stub
end

def default_proc()
# stub
end

def default_proc=(proc)
# stub
end

alias size length

def empty?
# stub
end

# @return [Array]
def to_a
# stub
end

# @return [Proc]
def to_proc
# stub
end

# @return [Hash]
def to_h
# stub
end
alias to_hash to_h

def [](key)
# stub
end

def []=(key, value)
# stub
end
alias store []

def fetch(key, default = nil, &block)
# stub
end

def key?(key)
# stub
end
alias has_key? key?
alias include? key?
alias member? key?

def value?(value)
# stub
end
alias has_value? value?

def each(&block)
# stub
end
alias each_pair each

def each_key(&block)
# stub
end

def each_value(&block)
# stub
end

def ==(other)
# stub
end

def <(other)
# stub
end

def <=(other)
# stub
end

def >(other)
# stub
end

def >=(other)
# stub
end

def select(&block)
# stub
end

def select!(&block)
# stub
end

def keep_if(&block)
# stub
end

def sort(&block)
# stub
end

def delete(key, &block)
# stub
end

def delete_if(&block)
# stub
end

def reject(&block)
# stub
end

def reject!(&block)
# stub
end

def invert
# stub
end

def key(value)
# stub
end

def keys
# stub
end

def values
# stub
end
alias ruby_values values

def values_at(*args)
# stub
end

def fetch_values(*args)
# stub
end

def clear
# stub
end
alias ruby_clear clear

# `Hash#merge`
# @example
# map = java.util.HashMap.new({ 1 => '1', 2 => '2' })
# map.merge 1 => 'one' # { 1 => "one", 2 => "2" } (java.util.HashMap)
# @note Since Java 8 this method collides with the built-in *java.util.Map#merge* in which case you want to alias its
# ruby_ name for map types where you want to have the Ruby version available
# e.g. `java.util.HashMap.class_eval { alias merge ruby_merge }`
# @return [Java::java::util::Map] merged map instance
def merge(other, &block)
# stub
end
alias ruby_merge merge

def merge!(other, &block)
# stub
end

# `Hash#replace`
# @example
# map1 = java.util.Hashtable.new({ 1 => '1', 2 => '2' })
# map2 = java.util.LinkedHashMap.new; map2[1] = 'one'
# map1.replace map2 # { 1 => "one" } (java.util.Hashtable)
# @note Since Java 8 this method collides with the built-in *java.util.Map#replace* in which case you want to alias its
# ruby_ name for map types where you want to have the Ruby version available
# e.g. `java.util.Hashtable.class_eval { alias replace ruby_replace }`
# @return [Java::java::util::Map] replaced map instance
def replace(other)
# stub
end
alias ruby_replace replace

def flatten(level = nil)
# stub
end

def assoc(obj)
# stub
end

def rassoc(obj)
# stub
end

def any?(&block)
# stub
end

def dig(*args)
# stub
end

end if false
186 changes: 132 additions & 54 deletions core/src/main/ruby/jruby/java/java_ext/java.util.regex.rb
Original file line number Diff line number Diff line change
@@ -2,86 +2,164 @@
# @see org.jruby.javasupport.ext.JavaUtilRegex.java
# this file is no longer loaded but is kept to provide doc stubs

# *java.util.regex.Pattern* enhanced to act similar to Ruby's `Regexp`.
# @note Only explicit (or customized) Ruby methods are listed, instances will have all of their Java methods available.
# @see http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html
class Java::java::util::regex::Pattern
# Matches this pattern against provided string.
# @return [Integer, nil] start (index) of the match if any
def =~(str)
m = self.matcher(str)
m.find ? m.start : nil
# stub implemented in org.jruby.javasupport.ext.JavaUtilRegex.java
# m = matcher(str)
# m.find ? m.start : nil
end


# Case equality for Java patterns.
# @return [true, false]
def ===(str)
self.matcher(str).find
# stub implemented in org.jruby.javasupport.ext.JavaUtilRegex.java
# matcher(str).find
end

# Returns a `Matcher` object describing the match against the string (or nil if there was no match).
# @example
# pattern = java.util.regex.Pattern.compile('[a-f]')
# matcher = pattern.match('abcdef') # java.util.regex.Matcher[pattern=[a-f] region=0,6 lastmatch=a]
# @return [Java::java::util::regex::Matcher, nil]
def match(str)
m = self.matcher(str)
m.str = str
m.find ? m : nil
# stub implemented in org.jruby.javasupport.ext.JavaUtilRegex.java
# m = matcher(str)
# m.find ? m : nil
end

# Returns the value of the case-insensitive flag.
# @return [true, false]
# @since 9.1
def casefold?
# stub implemented in org.jruby.javasupport.ext.JavaUtilRegex.java
end
end
end if false

# *java.util.regex.Matcher* represents a Java regex `Pattern` match, customized to quack like Ruby's `MatchData`.
# @note Only explicit (or customized) Ruby methods are listed, instances will have all of their Java methods available.
# @see http://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html
class Java::java::util::regex::Matcher
# @private
attr_accessor :str

#attr_accessor :str

# @return [Java::java::util::regex::Pattern]
# @since 9.1
def regexp
# stub implemented in org.jruby.javasupport.ext.JavaUtilRegex.java
end

def string
# stub implemented in org.jruby.javasupport.ext.JavaUtilRegex.java
end

# Returns an array of captures.
# pattern = java.util.regex.Pattern.compile("(.)(.)(\\d+)(\\d)")
# pattern.match('THX1138.').captures # ['H', 'X', '113', '8']
# @return [Array]
# @see #to_a
def captures
g = self.group_count
capt = []
count.times do |i|
capt << group(i+1)
end
capt
end

# stub implemented in org.jruby.javasupport.ext.JavaUtilRegex.java
end

# Matcher acts like an array and its capture elements might be accessed.
# @example
# pattern = java.util.regex.Pattern.compile("(.)(.)(\\d+)(\\d)")
# matcher = pattern.match('THX1138.')
# expect( m[0] ).to eq 'HX1138'
# expect( m[1, 2] ).to eq ['H', 'X']
# expect( m[1..3] ).to eq ['H', 'X', '113']
# @return [Array]
# @see #to_a
def [](*args)
self.to_a[*args]
# stub implemented in org.jruby.javasupport.ext.JavaUtilRegex.java
end

def begin(ix)
self.start(ix)
# Returns the array of matches.
# @example
# pattern = java.util.regex.Pattern.compile("(.)(.)(\\d+)(\\d)")
# pattern.match('THX1138.').captures # ['HX1138', 'H', 'X', '113', '8']
# @return [Array]
def to_a
# stub implemented in org.jruby.javasupport.ext.JavaUtilRegex.java
end
def end(ix)
self.java_send(:end, [Java::int], ix)

def values_at(*args)
# stub implemented in org.jruby.javasupport.ext.JavaUtilRegex.java
end

def to_a
arr = []
self.group_count.times do |gg|
if self.start(gg) == -1
arr << nil
else
arr << self.group(gg)
end
end
arr
end


# Returns the number of elements in the match array.
# @example
# pattern = java.util.regex.Pattern.compile("(.)(.)(\\d+)(\\d)")
# pattern.match('THX1138.').size # 5
# @return [Integer]
def size
self.group_count
# stub implemented in org.jruby.javasupport.ext.JavaUtilRegex.java
end
alias length size

def values_at(*args)
to_a.values_at(*args)

# Returns the offset of the start of the n-th element of the match array in the string.
# @example
# pattern = java.util.regex.Pattern.compile("(.)(.)(\\d+)(\\d)")
# matcher = pattern.match('THX1138.')
# expect( matcher.begin(0) ).to eq 1
# expect( matcher.begin(2) ).to eq 2
# @param n can be a string or symbol to reference a named capture
# @return [Integer]
# @see #offset
# @see #end
# @note Named captures referencing is not available on Java 7.
def begin(n)
# stub implemented in org.jruby.javasupport.ext.JavaUtilRegex.java
end

def select
yield self.to_a
# Returns the offset of the character immediately following the end of the n-th element of the match array in the string.
# @example
# pattern = java.util.regex.Pattern.compile("(.)(.)(\\d+)(\\d)")
# matcher = pattern.match('THX1138.')
# expect( matcher.begin(0) ).to eq 7
# expect( matcher.begin(2) ).to eq 3
# @param n can be a string or symbol to reference a named capture
# @return [Integer]
# @see #offset
# @see #begin
# @note Named captures referencing is not available on Java 7.
def end(n)
# stub implemented in org.jruby.javasupport.ext.JavaUtilRegex.java
end

def offset(ix)
[self.start(ix), self.end(ix)]

# Returns the offset of the character immediately following the end of the n-th element of the match array in the string.
# @example
# pattern = java.util.regex.Pattern.compile("(.)(.)(\\d+)(\\d)")
# matcher = pattern.match('THX1138.')
# expect( m.offset(0) ).to eq [1, 7]
# expect( m.offset(4) ).to eq [6, 7]
#
# pattern = java.util.regex.Pattern.compile("(?<foo>.)(.)(?<bar>.)")
# matcher = pattern.match('hoge')
# expect( m.offset(:bar) ).to eq [2, 3]
# @param n can be a string or symbol to reference a named capture
# @return [Array]
# @see #begin
# @see #end
def offset(n)
# stub implemented in org.jruby.javasupport.ext.JavaUtilRegex.java
end

# Returns the portion of the original string before the current match.
# @return [String]
def pre_match
self.str[0..(self.start(0))]
# stub implemented in org.jruby.javasupport.ext.JavaUtilRegex.java
end


# Returns the portion of the original string after the current match.
# @return [String]
def post_match
self.str[(self.end(0))..-1]
# stub implemented in org.jruby.javasupport.ext.JavaUtilRegex.java
end

def string
group(0)
end
end

end if false

0 comments on commit 5f147cc

Please sign in to comment.