Skip to content

Commit aa1db88

Browse files
committed
Arity-split Point#initialize
1 parent b32a4fa commit aa1db88

File tree

1 file changed

+49
-26
lines changed

1 file changed

+49
-26
lines changed

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

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -931,44 +931,53 @@ private static RaiseException newError(final Ruby runtime, final String message)
931931
return Utils.newError(runtime, Error, message);
932932
}
933933

934-
@JRubyMethod(rest = true, visibility = Visibility.PRIVATE)
935-
public IRubyObject initialize(final ThreadContext context, final IRubyObject[] args) {
936-
final Ruby runtime = context.runtime;
934+
@JRubyMethod(visibility = Visibility.PRIVATE)
935+
public IRubyObject initialize(final ThreadContext context, final IRubyObject groupOrPoint) {
936+
getPointAndGroup(context, groupOrPoint);
937937

938-
final int argc = Arity.checkArgumentCount(runtime, args, 1, 2);
939-
final IRubyObject arg = args[0];
938+
return this;
939+
}
940940

941-
if ( arg instanceof Point ) {
942-
this.group = ((Point) arg).group;
943-
this.point = ((Point) arg).point;
941+
@JRubyMethod(visibility = Visibility.PRIVATE)
942+
public IRubyObject initialize(final ThreadContext context, final IRubyObject groupOrPoint, final IRubyObject bn) {
943+
if (getPointAndGroup(context, groupOrPoint)) {
944944
return this;
945945
}
946946

947-
if ( arg instanceof Group ) {
948-
this.group = (Group) arg;
947+
final byte[] encoded;
948+
if (bn instanceof BN) {
949+
encoded = ((BN) bn).getValue().abs().toByteArray();
949950
} else {
950-
throw runtime.newTypeError(arg, _EC(runtime).getClass("Group"));
951+
encoded = bn.convertToString().getBytes();
951952
}
952-
953-
if ( argc == 2 ) { // (group, bn)
954-
final byte[] encoded;
955-
if (args[1] instanceof BN) {
956-
encoded = ((BN) args[1]).getValue().abs().toByteArray();
957-
} else {
958-
encoded = args[1].convertToString().getBytes();
959-
}
960-
try {
961-
this.point = ECPointUtil.decodePoint(group.getCurve(), encoded);
962-
}
963-
catch (IllegalArgumentException ex) {
964-
// MRI: OpenSSL::PKey::EC::Point::Error: invalid encoding
965-
throw newError(context.runtime, ex.getMessage());
966-
}
953+
try {
954+
this.point = ECPointUtil.decodePoint(group.getCurve(), encoded);
955+
}
956+
catch (IllegalArgumentException ex) {
957+
// MRI: OpenSSL::PKey::EC::Point::Error: invalid encoding
958+
throw newError(context.runtime, ex.getMessage());
967959
}
968960

969961
return this;
970962
}
971963

964+
private boolean getPointAndGroup(ThreadContext context, IRubyObject groupOrPoint) {
965+
final Ruby runtime = context.runtime;
966+
967+
if ( groupOrPoint instanceof Point) {
968+
this.group = ((Point) groupOrPoint).group;
969+
this.point = ((Point) groupOrPoint).point;
970+
return true;
971+
}
972+
973+
if ( groupOrPoint instanceof Group) {
974+
this.group = (Group) groupOrPoint;
975+
} else {
976+
throw runtime.newTypeError(groupOrPoint, _EC(runtime).getClass("Group"));
977+
}
978+
return false;
979+
}
980+
972981
@Override
973982
@JRubyMethod(name = { "==", "eql?" })
974983
public IRubyObject op_equal(final ThreadContext context, final IRubyObject obj) {
@@ -1059,6 +1068,20 @@ public IRubyObject inspect() {
10591068
return ObjectSupport.inspect(this, (List) Collections.singletonList(entry));
10601069
}
10611070

1071+
@Deprecated
1072+
public IRubyObject initialize(final ThreadContext context, final IRubyObject[] args) {
1073+
final int argc = Arity.checkArgumentCount(context.runtime, args, 1, 2);
1074+
1075+
switch (argc) {
1076+
case 1:
1077+
return initialize(context, args[0]);
1078+
case 2:
1079+
return initialize(context, args[0], args[1]);
1080+
default:
1081+
throw context.runtime.newArgumentError(args.length, 1);
1082+
}
1083+
}
1084+
10621085
}
10631086

10641087
static byte[] encode(final ECPublicKey pubKey) {

0 commit comments

Comments
 (0)