Skip to content

Instantly share code, notes, and snippets.

@daicoden
Created September 16, 2013 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daicoden/6586144 to your computer and use it in GitHub Desktop.
Save daicoden/6586144 to your computer and use it in GitHub Desktop.
Patch that would be helpful to ethers if it gets into jruby 1.7.5.
JRuby 1.5 cleanup
# COMITTED UPSTREAM - But leaving here in case we need refernce.
# There was some implementation changed, and I didn't have tests to catch everything
# this is here until we can get it commited upstream
module JRuby
class JavaSignature
# FIXME: Can make this accept whole list too if that is actual contract
# FIXME: Can be literals too
def as_java_type(string)
type = primitive? string
return type if type
# If annotation makes it in strip @ before we try and match it.
if string.is_a?(String)
string = string[1..-1] if string.start_with? '@'
end
eval make_class_jiable(string)
end
def make_class_jiable(string)
if string.is_a?(Java::OrgJrubyAstJava_signature::ReferenceTypeNode)
# Basically, if a package segment is capitalized, it's assumed to be a class in the package name.
name = string.getFullyTypedName()
klass_names,package_parts = name.split(".").partition{|n| n =~ /^[A-Z]/ }
"#{package_parts.join(".")}.#{klass_names.join("::")}"
elsif string.is_a?(Java::OrgJruby::RubyClass)
return string.class.to_s
else
new_list = []
string.split(/\./).inject(false) do |last_cap, segment|
if segment =~ /[A-Z]/
if last_cap
new_list << "::" + segment
else
new_list << "." + segment
end
last_cap = true
else
new_list << "." + segment
last_cap = false
end
end
"Java::#{new_list.join("")[1..-1]}"
end
end
end
end
class Class
def add_field_signature(name, type)
self_r = JRuby.reference0(self)
signature = JRuby::JavaSignature.new(nil, nil)
java_return_type = signature.as_java_type(type)
self_r.add_field_signature(name, java_return_type.java_class)
end
end
@daicoden
Copy link
Author

@daicoden
Copy link
Author

JavaSignature may actually be committed upstream, I can't figure out what my comment is saying.

add_field_signature is definitely busted, it only works for primitives, this patch works for objects as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment