Skip to content

Commit 91e1eff

Browse files
committed
BN should unwrap to the underlying java.math.BigInteger
1 parent 5292638 commit 91e1eff

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

src/main/java/org/jruby/ext/openssl/BN.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public IRubyObject to_i() {
280280
}
281281

282282
@JRubyMethod(name = "to_bn")
283-
public IRubyObject to_bn() {
283+
public BN to_bn() {
284284
return this;
285285
}
286286

@@ -852,4 +852,14 @@ public static BigInteger getBigInteger(final IRubyObject arg) {
852852
throw arg.getRuntime().newTypeError("Cannot convert into OpenSSL::BN");
853853
}
854854

855+
@Override
856+
public Object toJava(Class target) {
857+
if ( target.isAssignableFrom(BigInteger.class) || target == Number.class ) return value;
858+
if ( target == Long.class || target == Long.TYPE ) return value.longValue();
859+
if ( target == Integer.class || target == Integer.TYPE ) return value.intValue();
860+
if ( target == Double.class || target == Double.TYPE ) return value.doubleValue();
861+
if ( target == Float.class || target == Float.TYPE ) return value.floatValue();
862+
return super.toJava(target);
863+
}
864+
855865
}

src/test/ruby/test_bn.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,9 @@ def test_to_bn
3838
assert_equal bn, 1234567890123456789012345678901234567890.to_bn
3939
end
4040

41+
def test_to_java
42+
assert_equal java.lang.Integer.new(42), OpenSSL::BN.new('42').to_java(:int)
43+
assert_equal java.math.BigInteger.valueOf(24), OpenSSL::BN.new('24').to_java
44+
end if defined? JRUBY_VERSION
45+
4146
end

0 commit comments

Comments
 (0)