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: 4e7b0209376e
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: b8698e1a76f8
Choose a head ref
  • 8 commits
  • 15 files changed
  • 1 contributor

Commits on Apr 11, 2016

  1. Copy the full SHA
    8b3c644 View commit details
  2. Copy the full SHA
    5040de3 View commit details
  3. Copy the full SHA
    97eafe9 View commit details
  4. Copy the full SHA
    0650ec5 View commit details
  5. Copy the full SHA
    05c8d18 View commit details
  6. Copy the full SHA
    0fbb495 View commit details
  7. [ji] improve *.rb files documentation for yard tool

    ... mostly adding Java:: prefix since otherwise generated names look weird
    kares committed Apr 11, 2016
    Copy the full SHA
    a6be0c1 View commit details
  8. [ji] remove java.lang.Throwable#to_str and note on why message does n…

    …ot return nil
    
    + added some documentation on Java backtrace/message
    + also added some exception type diversion in the spec
    
    GH-3789
    kares committed Apr 11, 2016
    Copy the full SHA
    b8698e1 View commit details
25 changes: 11 additions & 14 deletions core/src/main/java/org/jruby/Main.java
Original file line number Diff line number Diff line change
@@ -316,36 +316,34 @@ public void run() {
}
}

private Status handleUnsupportedClassVersion(UnsupportedClassVersionError ucve) {
private Status handleUnsupportedClassVersion(UnsupportedClassVersionError ex) {
config.getError().println("Error: Some library (perhaps JRuby) was built with a later JVM version.");
config.getError().println("Please use libraries built with the version you intend to use or an earlier one.");
if (config.isVerbose()) {
config.getError().println("Exception trace follows:");
ucve.printStackTrace();
ex.printStackTrace(config.getError());
} else {
config.getError().println("Specify -w for full UnsupportedClassVersionError stack trace");
config.getError().println("Specify -w for full " + ex + " stack trace");
}
return new Status(1);
}

/**
* Print a nicer stack size error since Rubyists aren't used to seeing this.
*/
private Status handleStackOverflow(StackOverflowError soe) {
private Status handleStackOverflow(StackOverflowError ex) {
String memoryMax = getRuntimeFlagValue("-Xss");

if (memoryMax != null) {
config.getError().println("Error: Your application used more stack memory than the safety cap of " + memoryMax + ".");
config.getError().println("Error: Your application used more stack memory than the safety cap of " + memoryMax + '.');
} else {
config.getError().println("Error: Your application used more stack memory than the default safety cap.");
}
config.getError().println("Specify -J-Xss####k to increase it (#### = cap size in KB).");

if (config.isVerbose()) {
config.getError().println("Exception trace follows:");
soe.printStackTrace(config.getError());
ex.printStackTrace(config.getError());
} else {
config.getError().println("Specify -w for full StackOverflowError stack trace");
config.getError().println("Specify -w for full " + ex + " stack trace");
}

return new Status(1);
@@ -354,10 +352,10 @@ private Status handleStackOverflow(StackOverflowError soe) {
/**
* Print a nicer memory error since Rubyists aren't used to seeing this.
*/
private Status handleOutOfMemory(OutOfMemoryError oome) {
private Status handleOutOfMemory(OutOfMemoryError ex) {
System.gc(); // try to clean up a bit of space, hopefully, so we can report this error

String oomeMessage = oome.getMessage();
String oomeMessage = ex.getMessage();
boolean heapError = false;

if (oomeMessage != null) {
@@ -387,10 +385,9 @@ private Status handleOutOfMemory(OutOfMemoryError oome) {
}

if (config.isVerbose()) {
config.getError().println("Exception trace follows:");
oome.printStackTrace(config.getError());
ex.printStackTrace(config.getError());
} else {
config.getError().println("Specify -w for full OutOfMemoryError stack trace");
config.getError().println("Specify -w for full " + ex + " stack trace");
}

return new Status(1);
13 changes: 7 additions & 6 deletions core/src/main/java/org/jruby/RubyModule.java
Original file line number Diff line number Diff line change
@@ -1199,7 +1199,7 @@ public void removeMethod(ThreadContext context, String name) {

testFrozen("class/module");

if(name.equals("object_id") || name.equals("__send__") || name.equals("initialize")) {
if (name.equals("object_id") || name.equals("__send__") || name.equals("initialize")) {
runtime.getWarnings().warn(ID.UNDEFINING_BAD, "removing `" + name + "' may cause serious problems");
}

@@ -2243,12 +2243,8 @@ public void addWriteAttribute(ThreadContext context, String name) {
/** rb_mod_attr
*
*/
public IRubyObject attr(ThreadContext context, IRubyObject[] args) {
return attr19(context, args);
}

@JRubyMethod(name = "attr", rest = true, visibility = PRIVATE, reads = VISIBILITY)
public IRubyObject attr19(ThreadContext context, IRubyObject[] args) {
public IRubyObject attr(ThreadContext context, IRubyObject[] args) {
Ruby runtime = context.runtime;

if (args.length == 2 && (args[1] == runtime.getTrue() || args[1] == runtime.getFalse())) {
@@ -2260,6 +2256,11 @@ public IRubyObject attr19(ThreadContext context, IRubyObject[] args) {
return attr_reader(context, args);
}

@Deprecated
public IRubyObject attr19(ThreadContext context, IRubyObject[] args) {
return attr(context, args);
}

@Deprecated
public IRubyObject attr_reader(IRubyObject[] args) {
return attr_reader(getRuntime().getCurrentContext(), args);
Original file line number Diff line number Diff line change
@@ -52,19 +52,23 @@ public abstract class Initializer {
private static final Map<String, String> SCALA_OPERATORS;

// TODO: other reserved names?
private static final Map<String, AssignedName> RESERVED_NAMES = new HashMap<String, AssignedName>();
static {
private static Map<String, AssignedName> newReservedNamesMap(final int size) {
HashMap<String, AssignedName> RESERVED_NAMES = new HashMap<>(size + 4, 1);
RESERVED_NAMES.put("__id__", new AssignedName("__id__", Priority.RESERVED));
RESERVED_NAMES.put("__send__", new AssignedName("__send__", Priority.RESERVED));
// JRUBY-5132: java.awt.Component.instance_of?() expects 2 args
RESERVED_NAMES.put("instance_of?", new AssignedName("instance_of?", Priority.RESERVED));
return RESERVED_NAMES;
}
protected static final Map<String, AssignedName> STATIC_RESERVED_NAMES = new HashMap<String, AssignedName>(RESERVED_NAMES);

protected static final Map<String, AssignedName> STATIC_RESERVED_NAMES;
static {
STATIC_RESERVED_NAMES = newReservedNamesMap(1);
STATIC_RESERVED_NAMES.put("new", new AssignedName("new", Priority.RESERVED));
}
protected static final Map<String, AssignedName> INSTANCE_RESERVED_NAMES = new HashMap<String, AssignedName>(RESERVED_NAMES);
protected static final Map<String, AssignedName> INSTANCE_RESERVED_NAMES;
static {
INSTANCE_RESERVED_NAMES = newReservedNamesMap(2);
// only possible for "getClass" to be an instance method in Java
INSTANCE_RESERVED_NAMES.put("class", new AssignedName("class", Priority.RESERVED));
// "initialize" has meaning only for an instance (as opposed to a class)
Original file line number Diff line number Diff line change
@@ -190,7 +190,11 @@ private static boolean isFilteredClass(final String className) {
case "ir" : return true;
case "internal" : return true; // e.g. org.jruby.internal.runtime.methods.DynamicMethod.call
case "java" : return true; // e.g. org.jruby.java.invokers.InstanceMethodInvoker.call
case "javasupport" : return true;
// NOTE: if filtering javasupport is added back consider keeping some of the internals as they
// help identify issues and probably makes sense to NOT be filtered, namely:
// - (most if not all) classes in the package such as Java, JavaPackage, JavaUtil
// - sub-packages such as util, binding - maybe only filter the "proxy" sub-package?
//case "javasupport" : return true;
case "parser" : return true;
case "platform" : return true;
case "runtime" : return true; // e.g. org.jruby.runtime.callsite.CachingCallSite.cacheAndCall
3 changes: 2 additions & 1 deletion core/src/main/ruby/jruby/java/core_ext/kernel.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen-literal-string: true

# Create convenience methods for top-level java packages so we do not need to prefix
# with 'Java::'. We undef these methods within Package in case we run into 'com.foo.com'.
# with Java::com. We undef these methods within Package in case we run into 'com.foo.com'.
module Kernel
def java
JavaUtilities.get_package_module_dot_format('java')
10 changes: 6 additions & 4 deletions core/src/main/ruby/jruby/java/core_ext/object.rb
Original file line number Diff line number Diff line change
@@ -3,13 +3,14 @@ class Object
# using either its base name or by using a name returned from an optional block,
# passing all specified classes in turn and providing the block package name
# and base class name.
# @deprecated use {Object#java_import}
def include_class(include_class, &block)
warn "#{__method__} is deprecated. Use java_import."
java_import(include_class, &block)
end
# TODO: this can go away now, but people may be using it
def java_kind_of?(other)

# @deprecated
def java_kind_of?(other) # TODO: this can go away now, but people may be using it
return true if self.kind_of?(other)
return false unless self.respond_to?(:java_class) && other.respond_to?(:java_class) &&
other.kind_of?(Module) && !self.kind_of?(Module)
@@ -22,6 +23,7 @@ def java_kind_of?(other)
# java_import java.lang.System, java.lang.Thread
# java_import [java.lang.System, java.lang.Thread]
#
# @!visibility public
def java_import(*import_classes)
import_classes = import_classes.each_with_object([]) do |classes, flattened|
if classes.is_a?(Array)
@@ -96,9 +98,9 @@ def java_import(*import_classes)
import_class
end
end

private :java_import

# @private
def handle_different_imports(*args, &block)
if args.first.respond_to?(:java_class)
java_import(*args, &block)
6 changes: 3 additions & 3 deletions core/src/main/ruby/jruby/java/java_ext/java.io.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class java::io::InputStream
class Java::java::io::InputStream
def to_io(opts = nil)
ruby_io = org.jruby.RubyIO.new(JRuby.runtime, self)
if opts && !opts[:autoclose]
@@ -8,7 +8,7 @@ def to_io(opts = nil)
end
end

class java::io::OutputStream
class Java::java::io::OutputStream
def to_io(opts = nil)
ruby_io = org.jruby.RubyIO.new(JRuby.runtime, self)
if opts && !opts[:autoclose]
@@ -18,7 +18,7 @@ def to_io(opts = nil)
end
end

module java::nio::channels::Channel
module Java::java::nio::channels::Channel
def to_io(opts = nil)
ruby_io = org.jruby.RubyIO.new(JRuby.runtime, self)
if opts && !opts[:autoclose]
180 changes: 94 additions & 86 deletions core/src/main/ruby/jruby/java/java_ext/java.lang.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
lang = java::lang
# @private internal helper
module JavaUtilities::ModifierShortcuts
# @private
Modifier = java.lang.reflect.Modifier

def public?
Modifier.is_public(modifiers)
end

def protected?
Modifier.is_protected(modifiers)
end

def private?
Modifier.is_private(modifiers)
end

def final?
Modifier.is_final(modifiers)
end

def static?
Modifier.is_static(modifiers)
end
end

module lang::Runnable
# *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
def to_proc
proc { self.run }
end
end

module lang::Iterable
include Enumerable
# 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
@@ -24,37 +52,40 @@ def each_with_index
end
end

module lang::Comparable
include Comparable
# *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)
end
end

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

def backtrace
stack_trace.map(&:to_s)
end

# @note Noop as Java exceptions can not change trace.
def set_backtrace(trace)
# ignored; Java exceptions can't be set to Ruby trace
trace
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
msg = getLocalizedMessage
msg ? msg : ""
getLocalizedMessage || ''
end

def to_s
message
end

def to_str
to_s
end

def inspect
to_string
end
@@ -71,127 +102,119 @@ def ===(rhs)
end
end

module JavaUtilities::ModifierShortcuts
java_import java.lang.reflect.Modifier

def public?
Modifier.is_public(modifiers)
end

def protected?
Modifier.is_protected(modifiers)
end

def private?
Modifier.is_private(modifiers)
end

def final?
Modifier.is_final(modifiers)
end

def static?
Modifier.is_static(modifiers)
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);
end
end

class lang::ClassLoader
alias resource_as_stream get_resource_as_stream
alias resource_as_url get_resource

def resource_as_string(name)
resource_as_stream(name).to_io.read
def self.java_identifier_part?(fixnum)
isJavaIdentifierPart_char(fixnum);
end
end

class lang::Class
include Comparable
include JavaUtilities::ModifierShortcuts
# *java.lang.Class*
# @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

def ruby_class
JRuby.runtime.java_support.get_proxy_class_from_cache(self)
::JRuby.runtime.java_support.get_proxy_class_from_cache(self)
end

alias to_s name

def inspect
"class #{name}"
end

def resource_as_string(name)
resource_as_stream(name).to_io.read
end

alias annotation get_annotation

def annotations?
!annotations.empty?
end

def declared_annotations?
!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)
end

def java_instance_methods
methods.select {|m| !Modifier.is_static(m.modifiers)}.freeze
end

def declared_instance_methods
declared_methods.select {|m| !Modifier.is_static(m.modifiers)}.freeze
end

def java_class_methods
methods.select {|m| Modifier.is_static(m.modifiers)}.freeze
end

def declared_class_methods
declared_methods.select {|m| Modifier.is_static(m.modifiers)}.freeze
end
end

reflect = lang::reflect
# *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

def resource_as_string(name)
resource_as_stream(name).to_io.read
end
end

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

class reflect::AccessibleObject
include JavaUtilities::ModifierShortcuts

alias inspect to_s
end

class reflect::Constructor
class Java::java::lang::reflect::Constructor
def return_type
nil
end

alias argument_types parameter_types
end

class reflect::Method
class Java::java::lang::reflect::Method
def invoke_static(*args)
invoke(nil, *args)
end

alias argument_types parameter_types
end

class reflect::Field
class Java::java::lang::reflect::Field
alias value_type name
alias value get
alias set_value set

def static_value
get(nil)
end

def set_static_value(val)
set(nil, val)
end
@@ -209,18 +232,3 @@ def ubyte_set(index, value)
self[index] = value
end
end

class lang::Character
java_alias :isJavaIdentifierStart_char, :isJavaIdentifierStart, [Java::char]
java_alias :isJavaIdentifierPart_char, :isJavaIdentifierPart, [Java::char]

class << self
def java_identifier_start?(fixnum)
isJavaIdentifierStart_char(fixnum);
end

def java_identifier_part?(fixnum)
isJavaIdentifierPart_char(fixnum);
end
end
end
2 changes: 1 addition & 1 deletion core/src/main/ruby/jruby/java/java_ext/java.net.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class java::net::URL
class Java::java::net::URL
def open(*rest, &block)
stream = openStream
io = stream.to_io
39 changes: 29 additions & 10 deletions core/src/main/ruby/jruby/java/java_ext/java.util.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
# TODO java.util.Comparator support?
module java::util::Collection
include Enumerable

# *java.util.Collection* is enhanced (not just) to act like Ruby's `Enumerable`.
# @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
end
def <<(a); add(a); self; end

def <<(a); add(a); self end

def +(oth)
nw = self.dup
nw.addAll(oth)
nw
end

def -(oth)
nw = self.dup
nw.removeAll(oth)
nw
end

def length
size
end

def join(*args)
to_a.join(*args)
end

def to_a
# JRUBY-3910: conversion is faster by going directly to java array
# first
@@ -32,8 +42,10 @@ def to_a
alias_method :to_ary, :to_a
end

module java::util::Enumeration
include Enumerable
# 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
@@ -42,8 +54,10 @@ def each
end
end

module java::util::Iterator
include Enumerable
# 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
@@ -52,7 +66,11 @@ def each
end
end

module java::util::List
# Ruby extensions for *java.util.List* instances.
# @see Java::java::util::Collection
# @see http://docs.oracle.com/javase/8/docs/api/java/util/List.html
module Java::java::util::List
# @private
module RubyComparators
class BlockComparator
include java::util::Comparator
@@ -65,14 +83,14 @@ def compare(o1, o2)
@block.call(o1, o2)
end
end

class SpaceshipComparator
include java::util::Comparator
def compare(o1, o2)
o1 <=> o2
end
end
end
private_constant :RubyComparators

def [](ix1, ix2 = nil)
if (ix2)
@@ -156,4 +174,5 @@ def sort!(&block)
java::util::Collections.sort(self, comparator)
self
end
end

end
4 changes: 2 additions & 2 deletions core/src/main/ruby/jruby/java/java_ext/java.util.regex.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class java::util::regex::Pattern
class Java::java::util::regex::Pattern
def =~(str)
m = self.matcher(str)
m.find ? m.start : nil
@@ -15,7 +15,7 @@ def match(str)
end
end

class java::util::regex::Matcher
class Java::java::util::regex::Matcher
attr_accessor :str

def captures
1 change: 1 addition & 0 deletions core/src/main/ruby/jruby/java/java_utilities.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# @private
module JavaUtilities
def self.extend_proxy(java_class_name, &block)
java_class = JavaUtilities.get_proxy_class(java_class_name)
15 changes: 3 additions & 12 deletions spec/java_integration/addons/throwable_spec.rb
Original file line number Diff line number Diff line change
@@ -10,31 +10,22 @@
end

it "implements backtrace= as a no-op" do
ex = java.lang.Exception.new
ex = java.lang.IllegalStateException.new
backtrace = ex.backtrace
ex.set_backtrace ['blah']
expect(ex.backtrace).to eq backtrace
end

it "implements to_s as message" do
ex = java.lang.Exception.new
expect(ex.to_s).to eq ""
expect(ex.to_s).to eq ''
expect(ex.to_s).to eq ex.message

ex = java.lang.Exception.new('hello')
ex = java.lang.RuntimeException.new('hello')
expect(ex.to_s).to eq 'hello'
expect(ex.to_s).to eq ex.message
end

it "implements to_str to call to_s" do
ex = java.lang.Exception.new
def ex.to_s
'hello'
end

expect(ex.to_str).to eq 'hello'
end

it "implements inspect as toString" do
ex = java.lang.Exception.new('hello')
expect(ex.inspect).to eq "java.lang.Exception: hello"
15 changes: 15 additions & 0 deletions test/jruby/test_higher_javasupport.rb
Original file line number Diff line number Diff line change
@@ -1245,6 +1245,21 @@ def test_that_classes_beginning_with_small_letter_can_be_referenced
assert ! org.jruby.test.smallLetterClass.is_a?(Java::JavaPackage)
end

Module.send :remove_method, :attr

module SmallLetter
java_import 'org.jruby.test.smallLetterClass$ClassWithAttr'
end

def test_inner_class_starting_with_small_letter
assert SmallLetter::ClassWithAttr
assert SmallLetter::ClassWithAttr.java_class
assert_equal 42, SmallLetter::ClassWithAttr.id.value
# we need to undef Module#attr :
# Module.send :remove_method, :attr
assert_equal 42, SmallLetter::ClassWithAttr.attr.value
end

# JRUBY-1076
def test_package_module_aliased_methods
assert java.lang.respond_to?(:__constants__)
8 changes: 8 additions & 0 deletions test/org/jruby/test/smallLetterClass.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
package org.jruby.test;

public class smallLetterClass {
public static class ClassWithAttr {
public static final class attr {
public static final int value = 42;
}
public static final class id {
public static final int value = 42;
}
}
}