Skip to content

Commit aea89c5

Browse files
committed
[refactor] cleanup some dynamic calls in Java to not do lookup
1 parent 0e927fa commit aea89c5

File tree

3 files changed

+70
-76
lines changed

3 files changed

+70
-76
lines changed

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

Lines changed: 66 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -793,117 +793,118 @@ static ASN1ObjectIdentifier toObjectID(final String oid, final boolean silent)
793793
}
794794

795795
@JRubyMethod(name="Boolean", module=true, rest=true)
796-
public static IRubyObject fact_Boolean(IRubyObject self, IRubyObject[] args) {
797-
return callClassNew(self, "Boolean", args);
796+
public static IRubyObject fact_Boolean(ThreadContext context, IRubyObject self, IRubyObject[] args) {
797+
return newInstance(context, self, "Boolean", args);
798798
}
799799

800800
@JRubyMethod(name="Integer", module=true, rest=true)
801-
public static IRubyObject fact_Integer(IRubyObject self, IRubyObject[] args) {
802-
return callClassNew(self, "Integer", args);
801+
public static IRubyObject fact_Integer(ThreadContext context, IRubyObject self, IRubyObject[] args) {
802+
return newInstance(context, self, "Integer", args);
803803
}
804804

805805
@JRubyMethod(name="Enumerated", module=true, rest=true)
806-
public static IRubyObject fact_Enumerated(IRubyObject self, IRubyObject[] args) {
807-
return callClassNew(self, "Enumerated", args);
806+
public static IRubyObject fact_Enumerated(ThreadContext context, IRubyObject self, IRubyObject[] args) {
807+
return newInstance(context, self, "Enumerated", args);
808808
}
809809

810810
@JRubyMethod(name="BitString", module=true, rest=true)
811-
public static IRubyObject fact_BitString(IRubyObject self, IRubyObject[] args) {
812-
return callClassNew(self, "BitString", args);
811+
public static IRubyObject fact_BitString(ThreadContext context, IRubyObject self, IRubyObject[] args) {
812+
return newInstance(context, self, "BitString", args);
813813
}
814814

815815
@JRubyMethod(name="OctetString", module=true, rest=true)
816-
public static IRubyObject fact_OctetString(IRubyObject self, IRubyObject[] args) {
817-
return callClassNew(self, "OctetString", args);
816+
public static IRubyObject fact_OctetString(ThreadContext context, IRubyObject self, IRubyObject[] args) {
817+
return newInstance(context, self, "OctetString", args);
818818
}
819819

820820
@JRubyMethod(name="UTF8String", module=true, rest=true)
821-
public static IRubyObject fact_UTF8String(IRubyObject self, IRubyObject[] args) {
822-
return callClassNew(self, "UTF8String", args);
821+
public static IRubyObject fact_UTF8String(ThreadContext context, IRubyObject self, IRubyObject[] args) {
822+
return newInstance(context, self, "UTF8String", args);
823823
}
824824

825825
@JRubyMethod(name="NumericString", module=true, rest=true)
826-
public static IRubyObject fact_NumericString(IRubyObject self, IRubyObject[] args) {
827-
return callClassNew(self, "NumericString", args);
826+
public static IRubyObject fact_NumericString(ThreadContext context, IRubyObject self, IRubyObject[] args) {
827+
return newInstance(context, self, "NumericString", args);
828828
}
829829

830830
@JRubyMethod(name="PrintableString", module=true, rest=true)
831-
public static IRubyObject fact_PrintableString(IRubyObject self, IRubyObject[] args) {
832-
return callClassNew(self, "PrintableString", args);
831+
public static IRubyObject fact_PrintableString(ThreadContext context, IRubyObject self, IRubyObject[] args) {
832+
return newInstance(context, self, "PrintableString", args);
833833
}
834834

835835
@JRubyMethod(name="T61String", module=true, rest=true)
836-
public static IRubyObject fact_T61String(IRubyObject self, IRubyObject[] args) {
837-
return callClassNew(self, "T61String", args);
836+
public static IRubyObject fact_T61String(ThreadContext context, IRubyObject self, IRubyObject[] args) {
837+
return newInstance(context, self, "T61String", args);
838838
}
839839

840840
@JRubyMethod(name="VideotexString", module=true, rest=true)
841-
public static IRubyObject fact_VideotexString(IRubyObject recv, IRubyObject[] args) {
842-
return ((RubyModule)recv).getClass("VideotexString").callMethod(recv.getRuntime().getCurrentContext(),"new",args);
841+
public static IRubyObject fact_VideotexString(ThreadContext context, IRubyObject self, IRubyObject[] args) {
842+
return newInstance(context, self, "VideotexString", args);
843843
}
844844

845845
@JRubyMethod(name="IA5String", module=true, rest=true)
846-
public static IRubyObject fact_IA5String(IRubyObject recv, IRubyObject[] args) {
847-
return ((RubyModule)recv).getClass("IA5String").callMethod(recv.getRuntime().getCurrentContext(),"new",args);
846+
public static IRubyObject fact_IA5String(ThreadContext context, IRubyObject self, IRubyObject[] args) {
847+
return newInstance(context, self, "IA5String", args);
848848
}
849849

850850
@JRubyMethod(name="GraphicString", module=true, rest=true)
851-
public static IRubyObject fact_GraphicString(IRubyObject recv, IRubyObject[] args) {
852-
return ((RubyModule)recv).getClass("GraphicString").callMethod(recv.getRuntime().getCurrentContext(),"new",args);
851+
public static IRubyObject fact_GraphicString(ThreadContext context, IRubyObject self, IRubyObject[] args) {
852+
return newInstance(context, self, "GraphicString", args);
853853
}
854854

855855
@JRubyMethod(name="ISO64String", module=true, rest=true)
856-
public static IRubyObject fact_ISO64String(IRubyObject recv, IRubyObject[] args) {
857-
return ((RubyModule)recv).getClass("ISO64String").callMethod(recv.getRuntime().getCurrentContext(),"new",args);
856+
public static IRubyObject fact_ISO64String(ThreadContext context, IRubyObject self, IRubyObject[] args) {
857+
return newInstance(context, self, "ISO64String", args);
858858
}
859859

860860
@JRubyMethod(name="GeneralString", module=true, rest=true)
861-
public static IRubyObject fact_GeneralString(IRubyObject self, IRubyObject[] args) {
862-
return callClassNew(self, "GeneralString", args);
861+
public static IRubyObject fact_GeneralString(ThreadContext context, IRubyObject self, IRubyObject[] args) {
862+
return newInstance(context, self, "GeneralString", args);
863863
}
864864

865865
@JRubyMethod(name="UniversalString", module=true, rest=true)
866-
public static IRubyObject fact_UniversalString(IRubyObject self, IRubyObject[] args) {
867-
return callClassNew(self, "UniversalString", args);
866+
public static IRubyObject fact_UniversalString(ThreadContext context, IRubyObject self, IRubyObject[] args) {
867+
return newInstance(context, self, "UniversalString", args);
868868
}
869869

870870
@JRubyMethod(name="BMPString", module=true, rest=true)
871-
public static IRubyObject fact_BMPString(IRubyObject self, IRubyObject[] args) {
872-
return callClassNew(self, "BMPString", args);
871+
public static IRubyObject fact_BMPString(ThreadContext context, IRubyObject self, IRubyObject[] args) {
872+
return newInstance(context, self, "BMPString", args);
873873
}
874874

875875
@JRubyMethod(name="Nul", module=true, rest=true)
876-
public static IRubyObject fact_Null(IRubyObject self, IRubyObject[] args) {
877-
return callClassNew(self, "Null", args);
876+
public static IRubyObject fact_Null(ThreadContext context, IRubyObject self, IRubyObject[] args) {
877+
return newInstance(context, self, "Null", args);
878878
}
879879

880880
@JRubyMethod(name="ObjectId", module=true, rest=true)
881-
public static IRubyObject fact_ObjectId(IRubyObject self, IRubyObject[] args) {
882-
return callClassNew(self, "ObjectId", args);
881+
public static IRubyObject fact_ObjectId(ThreadContext context, IRubyObject self, IRubyObject[] args) {
882+
return newInstance(context, self, "ObjectId", args);
883883
}
884884

885885
@JRubyMethod(name="UTCTime", module=true, rest=true)
886-
public static IRubyObject fact_UTCTime(IRubyObject self, IRubyObject[] args) {
887-
return callClassNew(self, "UTCTime", args);
886+
public static IRubyObject fact_UTCTime(ThreadContext context, IRubyObject self, IRubyObject[] args) {
887+
return newInstance(context, self, "UTCTime", args);
888888
}
889889

890890
@JRubyMethod(name="GeneralizedTime", module=true, rest=true)
891-
public static IRubyObject fact_GeneralizedTime(IRubyObject self, IRubyObject[] args) {
892-
return callClassNew(self, "GeneralizedTime", args);
891+
public static IRubyObject fact_GeneralizedTime(ThreadContext context, IRubyObject self, IRubyObject[] args) {
892+
return newInstance(context, self, "GeneralizedTime", args);
893893
}
894894

895895
@JRubyMethod(name="Sequence", module=true, rest=true)
896-
public static IRubyObject fact_Sequence(IRubyObject self, IRubyObject[] args) {
897-
return callClassNew(self, "Sequence", args);
896+
public static IRubyObject fact_Sequence(ThreadContext context, IRubyObject self, IRubyObject[] args) {
897+
return newInstance(context, self, "Sequence", args);
898898
}
899899

900900
@JRubyMethod(name="Set", module=true, rest=true)
901-
public static IRubyObject fact_Set(IRubyObject self, IRubyObject[] args) {
902-
return callClassNew(self, "Set", args);
901+
public static IRubyObject fact_Set(ThreadContext context, IRubyObject self, IRubyObject[] args) {
902+
return newInstance(context, self, "Set", args);
903903
}
904904

905-
private static IRubyObject callClassNew(final IRubyObject self, final String className, final IRubyObject[] args) {
906-
return ((RubyModule) self).getClass(className).callMethod(self.getRuntime().getCurrentContext(), "new", args);
905+
private static IRubyObject newInstance(final ThreadContext context, final IRubyObject parent,
906+
final String className, final IRubyObject[] args) {
907+
return ((RubyModule) parent).getClass(className).newInstance(context, args, Block.NULL_BLOCK);
907908
}
908909

909910
public static class ObjectId {
@@ -964,17 +965,17 @@ static IRubyObject decodeObject(final ThreadContext context,
964965

965966
if ( obj instanceof ASN1Integer ) {
966967
final BN val = BN.newBN(runtime, ((ASN1Integer) obj).getValue());
967-
return ASN1.getClass("Integer").callMethod(context, "new", val);
968+
return ASN1.getClass("Integer").newInstance(context, val, Block.NULL_BLOCK);
968969
}
969970
if ( obj instanceof DERInteger ) {
970971
final BN val = BN.newBN(runtime, ((DERInteger) obj).getValue());
971-
return ASN1.getClass("Integer").callMethod(context, "new", val);
972+
return ASN1.getClass("Integer").newInstance(context, val, Block.NULL_BLOCK);
972973
}
973974

974975
if ( obj instanceof DERBitString ) {
975976
final DERBitString derObj = (DERBitString) obj;
976977
RubyString str = runtime.newString( new ByteList(derObj.getBytes(), false) );
977-
IRubyObject bitString = ASN1.getClass("BitString").callMethod(context, "new", str);
978+
IRubyObject bitString = ASN1.getClass("BitString").newInstance(context, str, Block.NULL_BLOCK);
978979
bitString.callMethod(context, "unused_bits=", runtime.newFixnum( derObj.getPadBits() ));
979980
return bitString;
980981
}
@@ -1016,7 +1017,7 @@ else if ( obj instanceof DERBMPString ) {
10161017
}
10171018
bytes = ByteList.create(((ASN1String) obj).getString());
10181019
}
1019-
return ASN1.getClass(type).callMethod(context, "new", runtime.newString(bytes));
1020+
return ASN1.getClass(type).newInstance(context, runtime.newString(bytes), Block.NULL_BLOCK);
10201021
}
10211022

10221023
//if ( obj instanceof DEROctetString ) {
@@ -1028,44 +1029,44 @@ else if ( obj instanceof DERBMPString ) {
10281029
final ByteList octets = new ByteList(((ASN1OctetString) obj).getOctets(), false);
10291030
// NOTE: sometimes MRI does include the tag but it really should not ;( !
10301031
//final ByteList octets = new ByteList(((ASN1OctetString) obj).getEncoded(ASN1Encoding.DER), false);
1031-
return ASN1.getClass("OctetString").callMethod(context, "new", runtime.newString(octets));
1032+
return ASN1.getClass("OctetString").newInstance(context, runtime.newString(octets), Block.NULL_BLOCK);
10321033
}
10331034

10341035
if ( obj instanceof ASN1Null ) {
1035-
return ASN1.getClass("Null").callMethod(context,"new", runtime.getNil());
1036+
return ASN1.getClass("Null").newInstance(context, runtime.getNil(), Block.NULL_BLOCK);
10361037
}
10371038
if ( obj instanceof ASN1Boolean ) {
10381039
final boolean val = ((ASN1Boolean) obj).isTrue();
1039-
return ASN1.getClass("Boolean").callMethod(context, "new", runtime.newBoolean(val));
1040+
return ASN1.getClass("Boolean").newInstance(context, runtime.newBoolean(val), Block.NULL_BLOCK);
10401041
}
10411042
// DERBoolean extends ASN1Boolean only since 1.51 (<= 1.50 the other way around)
10421043
if ( obj instanceof DERBoolean ) {
10431044
final boolean val = ((DERBoolean) obj).isTrue();
1044-
return ASN1.getClass("Boolean").callMethod(context, "new", runtime.newBoolean(val));
1045+
return ASN1.getClass("Boolean").newInstance(context, runtime.newBoolean(val), Block.NULL_BLOCK);
10451046
}
10461047

10471048
if ( obj instanceof ASN1UTCTime ) {
10481049
final Date adjustedTime;
10491050
try { adjustedTime = ((ASN1UTCTime) obj).getAdjustedDate(); }
10501051
catch (ParseException e) { throw new IOException(e); }
10511052
final RubyTime time = RubyTime.newTime(runtime, adjustedTime.getTime());
1052-
return ASN1.getClass("UTCTime").callMethod(context,"new", time);
1053+
return ASN1.getClass("UTCTime").newInstance(context, time, Block.NULL_BLOCK);
10531054
}
10541055
// NOTE: keep for BC versions compatibility ... extends ASN1UTCTime (since BC 1.51)
10551056
if ( obj instanceof DERUTCTime ) {
10561057
final Date adjustedTime;
10571058
try { adjustedTime = ((DERUTCTime) obj).getAdjustedDate(); }
10581059
catch (ParseException e) { throw new IOException(e); }
10591060
final RubyTime time = RubyTime.newTime(runtime, adjustedTime.getTime());
1060-
return ASN1.getClass("UTCTime").callMethod(context,"new", time);
1061+
return ASN1.getClass("UTCTime").newInstance(context, time, Block.NULL_BLOCK);
10611062
}
10621063

10631064
if ( obj instanceof ASN1GeneralizedTime ) {
10641065
final Date generalTime;
10651066
try { generalTime = ((ASN1GeneralizedTime) obj).getDate(); }
10661067
catch (ParseException e) { throw new IOException(e); }
10671068
final RubyTime time = RubyTime.newTime(runtime, generalTime.getTime());
1068-
return ASN1.getClass("GeneralizedTime").callMethod(context,"new", time);
1069+
return ASN1.getClass("GeneralizedTime").newInstance(context, time, Block.NULL_BLOCK);
10691070
}
10701071
// NOTE: keep for BC versions compatibility ... extends ASN1GeneralizedTime (since BC 1.51)
10711072
if ( obj instanceof DERGeneralizedTime ) {
@@ -1075,18 +1076,18 @@ else if ( obj instanceof DERBMPString ) {
10751076
}
10761077
catch (ParseException e) { throw new IOException(e); }
10771078
final RubyTime time = RubyTime.newTime(runtime, generalTime.getTime());
1078-
return ASN1.getClass("GeneralizedTime").callMethod(context,"new", time);
1079+
return ASN1.getClass("GeneralizedTime").newInstance(context, time, Block.NULL_BLOCK);
10791080
}
10801081

10811082
if ( obj instanceof ASN1ObjectIdentifier ) {
10821083
final String objId = ((ASN1ObjectIdentifier) obj).getId();
1083-
return ASN1.getClass("ObjectId").callMethod(context, "new", runtime.newString(objId));
1084+
return ASN1.getClass("ObjectId").newInstance(context, runtime.newString(objId), Block.NULL_BLOCK);
10841085
}
10851086
// ASN1ObjectIdentifier extends DERObjectIdentifier < 1.51
10861087
// DERObjectIdentifier extends ASN1ObjectIdentifier = 1.51
10871088
if ( obj instanceof DERObjectIdentifier ) {
10881089
final String objId = ((DERObjectIdentifier) obj).getId();
1089-
return ASN1.getClass("ObjectId").callMethod(context, "new", runtime.newString(objId));
1090+
return ASN1.getClass("ObjectId").newInstance(context, runtime.newString(objId), Block.NULL_BLOCK);
10901091
}
10911092

10921093
if ( obj instanceof ASN1TaggedObject ) {
@@ -1095,9 +1096,7 @@ else if ( obj instanceof DERBMPString ) {
10951096
IRubyObject tag = runtime.newFixnum( taggedObj.getTagNo() );
10961097
IRubyObject tag_class = runtime.newSymbol("CONTEXT_SPECIFIC");
10971098
final RubyArray valArr = runtime.newArray(val);
1098-
return ASN1.getClass("ASN1Data").callMethod(context, "new",
1099-
new IRubyObject[] { valArr, tag, tag_class }
1100-
);
1099+
return ASN1.getClass("ASN1Data").newInstance(context, new IRubyObject[] { valArr, tag, tag_class }, Block.NULL_BLOCK);
11011100
}
11021101

11031102
if ( obj instanceof DERApplicationSpecific ) {
@@ -1107,25 +1106,23 @@ else if ( obj instanceof DERBMPString ) {
11071106
final ASN1Sequence sequence = (ASN1Sequence) appSpecific.getObject(SEQUENCE);
11081107
@SuppressWarnings("unchecked")
11091108
final RubyArray valArr = decodeObjects(context, ASN1, sequence.getObjects());
1110-
return ASN1.getClass("ASN1Data").callMethod(context, "new",
1111-
new IRubyObject[] { valArr, tag, tag_class }
1112-
);
1109+
return ASN1.getClass("ASN1Data").newInstance(context, new IRubyObject[] { valArr, tag, tag_class }, Block.NULL_BLOCK);
11131110
}
11141111

11151112
if ( obj instanceof ASN1Sequence ) {
11161113
@SuppressWarnings("unchecked")
11171114
RubyArray arr = decodeObjects(context, ASN1, ((ASN1Sequence) obj).getObjects());
1118-
return ASN1.getClass("Sequence").callMethod(context, "new", arr);
1115+
return ASN1.getClass("Sequence").newInstance(context, arr, Block.NULL_BLOCK);
11191116
}
11201117
if ( obj instanceof ASN1Set ) {
11211118
@SuppressWarnings("unchecked")
11221119
RubyArray arr = decodeObjects(context, ASN1, ((ASN1Set) obj).getObjects());
1123-
return ASN1.getClass("Set").callMethod(context, "new", arr);
1120+
return ASN1.getClass("Set").newInstance(context, arr, Block.NULL_BLOCK);
11241121
}
11251122

11261123
if ( obj instanceof ASN1Enumerated ) {
11271124
final RubyInteger value = RubyBignum.bignorm(runtime, ((ASN1Enumerated) obj).getValue());
1128-
return ASN1.getClass("Enumerated").callMethod(context, "new", value);
1125+
return ASN1.getClass("Enumerated").newInstance(context, value, Block.NULL_BLOCK);
11291126
}
11301127

11311128
throw new IllegalArgumentException("unable to decode object: " + obj + " (" + ( obj == null ? "" : obj.getClass().getName() ) + ")");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public IRubyObject set_extensions(final IRubyObject extensions) {
172172

173173
@JRubyMethod
174174
public IRubyObject add_extension(final ThreadContext context, final IRubyObject ext) {
175-
return extensions().callMethod(context, "<<", ext);
175+
return extensions().append(ext);
176176
}
177177

178178
@Override

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,8 @@
4343
import org.jruby.RubyTime;
4444
import org.jruby.anno.JRubyMethod;
4545
import org.jruby.exceptions.RaiseException;
46-
import org.jruby.runtime.Arity;
47-
import org.jruby.runtime.ObjectAllocator;
48-
import org.jruby.runtime.ThreadContext;
46+
import org.jruby.runtime.*;
4947
import org.jruby.runtime.builtin.IRubyObject;
50-
import org.jruby.runtime.Visibility;
5148

5249
import org.jruby.ext.openssl.x509store.X509AuxCertificate;
5350
import org.jruby.ext.openssl.x509store.StoreContext;
@@ -183,7 +180,7 @@ public IRubyObject chain(final ThreadContext context) {
183180
try {
184181
for (X509AuxCertificate x509 : chain) {
185182
RubyString encoded = StringHelper.newString(runtime, x509.getEncoded());
186-
result.append( _Certificate.callMethod( context, "new", encoded ) );
183+
result.append( _Certificate.newInstance( context, encoded, Block.NULL_BLOCK ) );
187184
}
188185
}
189186
catch (CertificateEncodingException e) {
@@ -232,7 +229,7 @@ public IRubyObject current_crl(final ThreadContext context) {
232229
final RubyClass _CRL = _CRL(runtime);
233230
try {
234231
final java.security.cert.X509CRL crl = storeContext.getCurrentCRL();
235-
return _CRL.callMethod(context, "new", StringHelper.newString(runtime, crl.getEncoded()));
232+
return _CRL.newInstance(context, StringHelper.newString(runtime, crl.getEncoded()), Block.NULL_BLOCK);
236233
}
237234
catch (CRLException e) {
238235
throw newStoreError(runtime, e.getMessage());

0 commit comments

Comments
 (0)