Details

    • Type: Bug Bug
    • Status: Resolved Resolved
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: JRuby 1.6.5
    • Fix Version/s: JRuby 1.7.0.pre2
    • Component/s: Java Integration
    • Labels:
      None
    • Environment:
      Linux 2.6.32-35-generic #78-Ubuntu SMP Tue Oct 11 16:11:24 UTC 2011 x86_64 GNU/Linux
    • Number of attachments :
      0

      Description

      For a Java method that accepts a java.lang.Number.

      public static void testMethod(Number number)

      Calling that method with a Ruby Fixnum literal will not work with the following error:

      testMethod(100)

      TypeError: could not coerce Fixnum to class java.lang.Number

        Activity

        Hide
        Hiro Asari added a comment -

        Can you try testMethod(100.to_java)? See https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby.

        Show
        Hiro Asari added a comment - Can you try testMethod(100.to_java) ? See https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby .
        Hide
        John added a comment -

        That works, and so does saying:

        testMethod(Integer.new(100))

        But I would expect this to be something that works without having to do these kinds of workarounds. The guide says Ruby type 1 (a Fixnum) is coerced into a Java Long, which is a java.lang.Number. Seems confusing that it wouldn't work then.

        Show
        John added a comment - That works, and so does saying: testMethod(Integer.new(100)) But I would expect this to be something that works without having to do these kinds of workarounds. The guide says Ruby type 1 (a Fixnum) is coerced into a Java Long, which is a java.lang.Number. Seems confusing that it wouldn't work then.
        Hide
        Alex Tambellini added a comment -

        +1, I just ran into this as well. Would be awesome if any ruby Numeric would be able to be coerced into any java.lang.Number.

        Show
        Alex Tambellini added a comment - +1, I just ran into this as well. Would be awesome if any ruby Numeric would be able to be coerced into any java.lang.Number.
        Hide
        Charles Oliver Nutter added a comment -

        Agreed. Will investigate.

        Show
        Charles Oliver Nutter added a comment - Agreed. Will investigate.
        Hide
        Charles Oliver Nutter added a comment -

        Softball!

        commit 14be2612f7066a3e54e0b2d56a4e5a0b4a7c35f6
        Author: Charles Oliver Nutter <headius@headius.com>
        Date:   Mon Jul 30 10:58:15 2012 -0500
        
            Fix JRUBY-6395: Coerce Fixnum to Number
            
            Simple enough fix: added Number (and Serializable) to the map of
            types to converters in JavaUtil. Because our Numeric types were
            already reporting that their "natural" classes were Number
            subclasses, they were being selected for Number-receiving methods.
            But the coercion logic did not know about Number, so it failed.
        
        :100644 100644 4f3d22d... b4fd217... M	spec/java_integration/fixtures/CoreTypeMethods.java
        :100644 100644 a1d2c33... 462db94... M	spec/java_integration/types/coercion_spec.rb
        :100644 100644 d0d29fe... 6433f16... M	src/org/jruby/javasupport/JavaUtil.java
        
        Show
        Charles Oliver Nutter added a comment - Softball! commit 14be2612f7066a3e54e0b2d56a4e5a0b4a7c35f6 Author: Charles Oliver Nutter <headius@headius.com> Date: Mon Jul 30 10:58:15 2012 -0500 Fix JRUBY-6395: Coerce Fixnum to Number Simple enough fix: added Number (and Serializable) to the map of types to converters in JavaUtil. Because our Numeric types were already reporting that their "natural" classes were Number subclasses, they were being selected for Number-receiving methods. But the coercion logic did not know about Number, so it failed. :100644 100644 4f3d22d... b4fd217... M spec/java_integration/fixtures/CoreTypeMethods.java :100644 100644 a1d2c33... 462db94... M spec/java_integration/types/coercion_spec.rb :100644 100644 d0d29fe... 6433f16... M src/org/jruby/javasupport/JavaUtil.java

          People

          • Assignee:
            Charles Oliver Nutter
            Reporter:
            John
          • Votes:
            2 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: