jruby

Calling Java method doesn't work after upgrading jruby 9.2.11 to 9.3.0.0


import "com.emeter.energyip.#{@interface}.MessageHeader" // This is my java class_name
header = MessageHeader::Factory.new_instance
element = 'create'
print "Value :: #{MessageHeader::Verb.respond_to?(element.to_s.upcase)}"

It is printing value as false instead of true after I upgraded from jRuby 9.2.11 to 9.3.0.0

What has changed in 9.3.0.0 ?


Solution

  • You don't provide enough details to reproduce your issue (in particular, the Java classes you're using don't seem to be publicly available, so I can't check what methods they actually have), but I have a hunch anyway.

    In particular, I bet that MessageHeader::Verb is a Java interface, and that CREATE is a constant defined in that interface. In JRuby 9.2, such Java interface constants (unintentionally) had getter methods created for them, so that you could access them as e.g. MessageHeader::Verb.CREATE. However, this behavior was deprecated in JRuby 9.2.8.0 (and you would've been warned about it if you were running your code in verbose mode, which alas few people do) and removed in JRuby 9.3.

    The intended and supported way of accessing such Java constants is the same as for Ruby class or module constants, e.g. MessageHeader::Verb::CREATE with :: instead of .. Also, you can check for their existence with const_defined? instead of respond_to?.


    Ps. You seem to be a bit late with your upgrade. The JRuby 9.3 branch is currently being ramped down, with a planned end-of-life date at the end of 2023, i.e. just over a week from now. While occasional security updates to JRuby 9.3 may still be released, you really should be moving to JRuby 9.4 by now.

    Also, while it can make sense to upgrade incrementally one major version at a time (i.e. 9.2 → 9.3 → 9.4), I'd generally recommend going straight to the latest minor version in each major branch (i.e. currently 9.3.13.0 and 9.4.5.0). In particular, the 9.x.0.0 versions of JRuby are kind of notorious for often being released with major bugs that get fixed in subsequent versions. IME at least it's much easier to fix incompatibilities in your own code if you don't have to also work around weird bugs in JRuby itself at the same time.