Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 4de1321a9756
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 5744c4d877a6
Choose a head ref
  • 5 commits
  • 10 files changed
  • 1 contributor

Commits on Apr 19, 2016

  1. Copy the full SHA
    d51d63e View commit details
  2. [ji] use ruby_xxx method naming convention for collision methods on j…

    …ava.util.Map
    
    users will be able to do use ruby versions with a simple alias on concrete types
    e.g. `java.util.HashMap.class_eval { alias merge ruby_merge }`
    
    resolves #1249
    kares committed Apr 19, 2016
    Copy the full SHA
    ed55b10 View commit details
  3. [ji] array like first/last on java.util.List with ruby_xxx name (to h…

    …andle potential collision)
    
    e.g. java.util.LinkedList provides getFirst/getLast thus previously behaviour was :
    
    ```
    java.util.ArrayList.new.first # nil
    java.util.LinkedList.new.first # throwing NoSuchElementException
    # will now behave consistent in Ruby-land with : 
    java.util.LinkedList.class_eval { alias last ruby_last; alias first ruby_first }
    ```
    kares committed Apr 19, 2016
    Copy the full SHA
    ebb2295 View commit details
  4. [ji] re-def Enumerable#first on Collection so that it can be aliased …

    …when it conflicts
    
    ... e.g. this would happen on java.util.Deque due having a getFirst method prescribed
    kares committed Apr 19, 2016
    Copy the full SHA
    64bfcde View commit details

Commits on Apr 20, 2016

  1. Copy the full SHA
    5744c4d View commit details
12 changes: 6 additions & 6 deletions core/src/main/java/org/jruby/java/proxies/MapJavaProxy.java
Original file line number Diff line number Diff line change
@@ -638,7 +638,7 @@ public RubyArray keys() {
/** rb_hash_values
*
*/
@JRubyMethod(name = "values")
@JRubyMethod(name = { "values", "ruby_values" }) // collision with java.util.Map#values
public RubyArray rb_values() {
return getOrCreateRubyHashMap().rb_values();
}
@@ -694,8 +694,8 @@ public IRubyObject reject_bang(final ThreadContext context, final Block block) {
/** rb_hash_clear
*
*/
@JRubyMethod(name = "clear")
public RubyHash rb_clear() {
@JRubyMethod(name = { "clear", "ruby_clear" }) // collision with java.util.Map#clear (return type)
public IRubyObject rb_clear() {
return getOrCreateRubyHashMap().rb_clear();
}

@@ -710,15 +710,15 @@ public RubyHash invert(final ThreadContext context) {
/** rb_hash_merge_bang
*
*/
@JRubyMethod(name = {"merge!", "update"}, required = 1)
@JRubyMethod(name = { "merge!", "update" }, required = 1)
public RubyHash merge_bang(final ThreadContext context, final IRubyObject other, final Block block) {
return getOrCreateRubyHashMap().merge_bang(context, other, block);
}

/** rb_hash_merge
*
*/
@JRubyMethod(name = "merge")
@JRubyMethod(name = { "merge", "ruby_merge" }) // collision with java.util.Map#merge on Java 8+
public RubyHash merge(ThreadContext context, IRubyObject other, Block block) {
return getOrCreateRubyHashMap().merge(context, other, block);
}
@@ -734,7 +734,7 @@ public RubyHash initialize_copy(ThreadContext context, IRubyObject other) {
/** rb_hash_replace
*
*/
@JRubyMethod(name = "replace", required = 1)
@JRubyMethod(name = { "replace", "ruby_replace" }, required = 1) // collision with java.util.Map#replace on Java 8+
public RubyHash replace(final ThreadContext context, IRubyObject other) {
return getOrCreateRubyHashMap().replace(context, other);
}
57 changes: 55 additions & 2 deletions core/src/main/java/org/jruby/javasupport/ext/JavaUtil.java
Original file line number Diff line number Diff line change
@@ -137,6 +137,27 @@ public static IRubyObject each_with_index(final ThreadContext context, final IRu
return JavaLang.Iterable.each_with_index(context, self, block);
}

// NOTE: first might conflict with some Java types (e.g. java.util.Deque) thus providing a ruby_ alias
@JRubyMethod(name = { "first", "ruby_first" }) // re-def Enumerable#first
public static IRubyObject first(final ThreadContext context, final IRubyObject self) {
final java.util.Collection coll = unwrapJavaObject(self);
return coll.isEmpty() ? context.nil : convertJavaToUsableRubyObject(context.runtime, coll.iterator().next());
}

@JRubyMethod(name = { "first", "ruby_first" }) // re-def Enumerable#first(n)
public static IRubyObject first(final ThreadContext context, final IRubyObject self, final IRubyObject count) {
final java.util.Collection coll = unwrapJavaObject(self);
int len = count.convertToInteger().getIntValue();
int size = coll.size(); if ( len > size ) len = size;
final Ruby runtime = context.runtime;
if ( len == 0 ) return RubyArray.newEmptyArray(runtime);
final RubyArray arr = RubyArray.newArray(runtime, len);
int i = 0; for ( java.util.Iterator it = coll.iterator(); i < len; i++ ) {
arr.append( convertJavaToUsableRubyObject(runtime, it.next() ) );
}
return arr;
}

@JRubyMethod(name = { "<<" })
public static IRubyObject append(final IRubyObject self, final IRubyObject item) {
java.util.Collection coll = unwrapJavaObject(self);
@@ -299,6 +320,38 @@ public static IRubyObject aset(final ThreadContext context, final IRubyObject se
return val;
}

// NOTE: first conflicts with some Java types e.g. with java.util.LinkedList#getFirst
@JRubyMethod(name = { "first", "ruby_first" }) // re-def Enumerable#first (to skip iterator)
public static IRubyObject first(final ThreadContext context, final IRubyObject self) {
final java.util.List list = unwrapJavaObject(self);
return list.isEmpty() ? context.nil : convertJavaToUsableRubyObject(context.runtime, list.get(0));
}

@JRubyMethod(name = { "first", "ruby_first" }) // #first ext like with array: [1, 2, 3].first(2) == [1, 2]
public static IRubyObject first(final ThreadContext context, final IRubyObject self, final IRubyObject count) {
final java.util.List list = unwrapJavaObject(self);
int len = count.convertToInteger().getIntValue();
int size = list.size(); if ( len > size ) len = size;
return Java.getInstance(context.runtime, list.subList(0, len));
}

// NOTE: first conflicts with some Java types e.g. with java.util.LinkedList#getLast
@JRubyMethod(name = { "last", "ruby_last" }) // like with [].last
public static IRubyObject last(final ThreadContext context, final IRubyObject self) {
final java.util.List list = unwrapJavaObject(self);
return list.isEmpty() ? context.nil : convertJavaToUsableRubyObject(context.runtime, list.get(list.size() - 1));
}

@JRubyMethod(name = { "last", "ruby_last" }) // #last ext like with array: [1, 2, 3].last(2) == [2, 3]
public static IRubyObject last(final ThreadContext context, final IRubyObject self, final IRubyObject count) {
final java.util.List list = unwrapJavaObject(self);
int len = count.convertToInteger().getIntValue();
int size = list.size();
int start = size - len; if ( start < 0 ) start = 0;
int end = start + len; if ( end > size ) end = size;
return Java.getInstance(context.runtime, list.subList(start, end));
}

@JRubyMethod(name = "index", required = 0) // list.index { |val| val > 0 }
public static IRubyObject index(final ThreadContext context, final IRubyObject self, final Block block) {
final Ruby runtime = context.runtime;
@@ -419,8 +472,8 @@ public static RubyArray to_a(final ThreadContext context, final IRubyObject self
return RubyArray.newArrayNoCopy(runtime, array);
}

@JRubyMethod
@SuppressWarnings("unchecked")
@JRubyMethod(name = { "sort", "ruby_sort" }) // name conflict on Java 8, but users can alias if they want
public static IRubyObject sort(final ThreadContext context, final IRubyObject self, final Block block) {
java.util.List dupList = unwrapJavaObject(self.callMethod(context, "dup"));
if ( dupList == unwrapJavaObject(self) ) {
@@ -432,8 +485,8 @@ public static IRubyObject sort(final ThreadContext context, final IRubyObject se
return Java.getInstance(context.runtime, dupList);
}

@JRubyMethod(name = "sort!")
@SuppressWarnings("unchecked")
@JRubyMethod(name = "sort!")
public static IRubyObject sort_bang(final ThreadContext context, final IRubyObject self, final Block block) {
final java.util.List list = unwrapJavaObject(self);
sortImpl(context, list, block);
31 changes: 13 additions & 18 deletions core/src/main/ruby/jruby/java/java_ext/java.io.rb
Original file line number Diff line number Diff line change
@@ -3,31 +3,26 @@
# this file is no longer loaded but is kept to provide doc stubs

class Java::java::io::InputStream
# Convert a stream to a Ruby `IO`
# @option opts [Types] autoclose (nil) sets `IO#autoclose=`
# @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

class Java::java::io::OutputStream
# Convert a stream to a Ruby `IO`
# @option opts [Types] autoclose (nil) sets `IO#autoclose=`
# @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

module Java::java::nio::channels::Channel
# @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
Loading