Skip to content

Enum.java:238:in `valueOf': java.lang.IllegalArgumentException: No enum constant jnr.constants.platform.AddressFamily.AF_AF_INET #1618

@francis

Description

@francis

For jruby 1.7.11 running either on my iMac or Windows box, I get an illegal constant trying to lookup 'AF_INET' when calling Socket.new. Looking at the code, on line 540 of SocketUtils.java -

addressFamily = AddressFamily.valueOf("AF_" + domainString);

you have:

    addressFamily = AddressFamily.valueOf("AF_" + domainString);

Apparently, that extra AF_ is not needed if it already starts with AF_?

Here is a small text program that shows this problem:
jruby-1.7.11:~ $ cat bug_af_af_inet.rb

require 'socket'
addrinfo = Socket.getaddrinfo('localhost', 22)
family = addrinfo[0][0] # this is the string 'AF_INET' or 'AF_INET6'
socket = Socket.new(family, Socket::SOCK_STREAM, 0)
socket.close

jruby-1.7.11:~ $ ruby bug_af_af_inet.rb
Enum.java:238:in valueOf': java.lang.IllegalArgumentException: No enum constant jnr.constants.platform.AddressFamily.AF_AF_INET from AddressFamily.java:4:invalueOf'
from SocketUtils.java:540:in addressFamilyFromArg' from RubySocket.java:418:ininitDomain'
from RubySocket.java:337:in initFieldsFromArgs' from RubySocket.java:188:ininitialize19'
from RubySocket$INVOKER$i$initialize19.gen:-1:in call' from JavaMethod.java:1056:incall'
from JavaMethod.java:677:in call' from CachingCallSite.java:286:incacheAndCall'
from CachingCallSite.java:81:in callBlock' from CachingCallSite.java:85:incall'
from RubyClass.java:825:in newInstance' from RubyIO.java:852:innewInstance'
from RubyIO$INVOKER$s$0$0$newInstance.gen:-1:in call' from DynamicMethod.java:226:incall'
from DynamicMethod.java:222:in call' from MethodHandle.java:636:ininvokeWithArguments'
from InvocationLinker.java:191:in invocationFallback' from bug_af_af_inet.rb:5:infile'
from bug_af_af_inet.rb:-1:in load' from Ruby.java:811:inrunScript'
from Ruby.java:804:in runScript' from Ruby.java:673:inrunNormally'
from Ruby.java:522:in runFromMain' from Main.java:395:indoRunFromMain'
from Main.java:290:in internalRun' from Main.java:217:inrun'
from Main.java:197:in `main'

Switching back to MRI, you don't see this behavior:
jruby-1.7.11:~ $ rvm use ruby
ruby-2.1.1:~ $ ruby bug_af_af_inet.rb
ruby-2.1.1:~ $

A workaround is to map the constant yourself:

def get_address_family(addrinfo)
  # jruby handles this wrong, so map it by hand
  family = addrinfo[0][0]
  family = Socket::AF_INET if family == 'AF_INET'
  family = Socket::AF_INET6 if family == 'AF_INET6'
  family
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions