Skip to content

Commit cc0004a

Browse files
committed
review ASN1's package private methods - avoid using getSymLookup / getOIDLookup
1 parent aca089c commit cc0004a

File tree

5 files changed

+37
-30
lines changed

5 files changed

+37
-30
lines changed

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,11 @@ static ASN1ObjectIdentifier sym2Oid(final Ruby runtime, final String name) {
442442
return getOIDLookup(runtime).get(name);
443443
}
444444

445-
static Map<String, ASN1ObjectIdentifier> getOIDLookup(final Ruby runtime) {
445+
private static Map<String, ASN1ObjectIdentifier> getOIDLookup(final Ruby runtime) {
446446
return symToOid(runtime);
447447
}
448448

449-
static Map<ASN1ObjectIdentifier, String> getSymLookup(final Ruby runtime) {
449+
private static Map<ASN1ObjectIdentifier, String> getSymLookup(final Ruby runtime) {
450450
return oidToSym(runtime);
451451
}
452452

@@ -483,8 +483,8 @@ static Map<ASN1ObjectIdentifier, String> getSymLookup(final Ruby runtime) {
483483
{"CHARACTER_STRING", null, null },
484484
{"BMPSTRING", org.bouncycastle.asn1.DERBMPString.class, "BMPString" }};
485485

486-
private final static Map<Class, Integer> CLASS_TO_ID = new HashMap<Class, Integer>();
487-
private final static Map<String, Integer> RUBYNAME_TO_ID = new HashMap<String, Integer>();
486+
private final static Map<Class<?>, Integer> CLASS_TO_ID = new HashMap<Class<?>, Integer>(24);
487+
private final static Map<String, Integer> RUBYNAME_TO_ID = new HashMap<String, Integer>(24);
488488

489489
static {
490490
for ( int i = 0; i < ASN1_INFO.length; i++ ) {
@@ -498,7 +498,7 @@ static Map<ASN1ObjectIdentifier, String> getSymLookup(final Ruby runtime) {
498498
}
499499
}
500500

501-
static int idForClass(Class type) {
501+
static int idForJava(Class<?> type) {
502502
Integer v = null;
503503
while ( type != Object.class && v == null ) {
504504
v = CLASS_TO_ID.get(type);
@@ -507,11 +507,19 @@ static int idForClass(Class type) {
507507
return v == null ? -1 : v.intValue();
508508
}
509509

510-
static int idForRubyName(String name) {
510+
static int idForJava(final Object obj) {
511+
return idForJava( obj.getClass() );
512+
}
513+
514+
private static int idForRuby(final String name) {
511515
Integer v = RUBYNAME_TO_ID.get(name);
512516
return v == null ? -1 : v.intValue();
513517
}
514518

519+
static int idForRuby(final RubyClass metaClass) {
520+
return idForRuby( metaClass.getRealClass().getBaseName() );
521+
}
522+
515523
static Class<? extends ASN1Encodable> classForId(int id) {
516524
@SuppressWarnings("unchecked")
517525
Class<? extends ASN1Encodable> result = (Class<? extends ASN1Encodable>)(ASN1_INFO[id][1]);
@@ -779,7 +787,7 @@ public static IRubyObject oid(final ThreadContext context, final IRubyObject sel
779787
private static IRubyObject decodeObject(final ThreadContext context, final RubyModule _ASN1, final Object obj)
780788
throws IOException, IllegalArgumentException {
781789

782-
int ix = idForClass(obj.getClass());
790+
int ix = idForJava(obj.getClass());
783791
final String className = ix == -1 ? null : (String) ( ASN1_INFO[ix][2] );
784792

785793
if ( className != null ) {
@@ -987,7 +995,7 @@ public IRubyObject to_der(final ThreadContext context) {
987995
}
988996

989997
protected IRubyObject defaultTag() {
990-
int i = idForRubyName(getMetaClass().getRealClass().getBaseName());
998+
int i = idForRuby( getMetaClass() );
991999
if(i != -1) {
9921000
return getRuntime().newFixnum(i);
9931001
} else {
@@ -1098,8 +1106,8 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
10981106
tag_class = runtime.newSymbol("UNIVERSAL");
10991107
}
11001108
if ( "ObjectId".equals( getMetaClass().getRealClass().getBaseName() ) ) {
1101-
String v = getSymLookup(runtime).get( getObjectIdentifier(runtime, value.toString()) );
1102-
if ( v != null ) value = runtime.newString(v);
1109+
String name = oid2Sym( runtime, getObjectIdentifier(runtime, value.toString()) );
1110+
if ( name != null ) value = runtime.newString(name);
11031111
}
11041112

11051113
this.callMethod(context, "tag=", tag);
@@ -1111,7 +1119,7 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
11111119

11121120
@Override
11131121
ASN1Encodable toASN1(final ThreadContext context) {
1114-
final int tag = idForRubyName(getMetaClass().getRealClass().getBaseName());
1122+
final int tag = idForRuby( getMetaClass() );
11151123
@SuppressWarnings("unchecked")
11161124
Class<? extends ASN1Encodable> impl = (Class<? extends ASN1Encodable>) ASN1_INFO[tag][1];
11171125

@@ -1258,7 +1266,7 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
12581266

12591267
@Override
12601268
ASN1Encodable toASN1(final ThreadContext context) {
1261-
final int id = idForRubyName(getMetaClass().getRealClass().getBaseName());
1269+
final int id = idForRuby( getMetaClass() );
12621270
if ( id != -1 ) {
12631271
final ASN1EncodableVector vec = new ASN1EncodableVector();
12641272
final RubyArray value = value(context);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public IRubyObject sign(final IRubyObject key, final IRubyObject digest) {
210210
final String digAlg = ((Digest) digest).getShortAlgorithm();
211211
final String symKey = keyAlg.toLowerCase() + '-' + digAlg.toLowerCase();
212212
try {
213-
final ASN1ObjectIdentifier alg = ASN1.getOIDLookup(getRuntime()).get( symKey );
213+
final ASN1ObjectIdentifier alg = ASN1.sym2Oid( getRuntime(), symKey );
214214
final PublicKey publicKey = ( (PKey) this.public_key ).getPublicKey();
215215
final String challengeStr = challenge.toString();
216216
final NetscapeCertRequest cert;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ private static IRubyObject newX509Name(final ThreadContext context, X500Name nam
182182
if ( tv.getValue() instanceof ASN1String ) {
183183
val = ( (ASN1String) tv.getValue() ).getString();
184184
}
185-
RubyFixnum typef = runtime.newFixnum( ASN1.idForClass(tv.getValue().getClass()) ); //TODO correct?
186-
newName.addEntry(oid, val, typef);
185+
RubyFixnum type = runtime.newFixnum( ASN1.idForJava( tv.getValue() ) ); //TODO correct?
186+
newName.addEntry(oid, val, type);
187187
}
188188
}
189189

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -594,9 +594,9 @@ else if (args.length > 1) {
594594

595595
@JRubyMethod
596596
public IRubyObject oid(final ThreadContext context) {
597-
String val = ASN1.getSymLookup(context.runtime).get(oid);
598-
if ( val == null ) val = oid.toString();
599-
return context.runtime.newString(val);
597+
String name = ASN1.oid2Sym(context.runtime, oid);
598+
if ( name == null ) name = oid.toString();
599+
return context.runtime.newString(name);
600600
}
601601

602602
@JRubyMethod(name="oid=")

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ private void fromRDNElement(final RDN rdn) {
177177
} else {
178178
values.add(null); //TODO really?
179179
}
180-
types.add(getRuntime().newFixnum(ASN1.idForClass(tv.getValue().getClass())));
180+
types.add(getRuntime().newFixnum( ASN1.idForJava(tv.getValue())) );
181181
}
182182
}
183183

@@ -196,7 +196,7 @@ private void fromASN1Sequence(Object element) {
196196
} else {
197197
values.add(null);
198198
}
199-
types.add(getRuntime().newFixnum(ASN1.idForClass(typeAndValue.getObjectAt(1).getClass())));
199+
types.add(getRuntime().newFixnum( ASN1.idForJava(typeAndValue.getObjectAt(1))) );
200200
}
201201

202202
@JRubyMethod(visibility = Visibility.PRIVATE)
@@ -322,6 +322,7 @@ public IRubyObject add_entry(final ThreadContext context,
322322

323323
@JRubyMethod(name = "to_s", rest = true)
324324
public IRubyObject to_s(IRubyObject[] args) {
325+
final Ruby runtime = getRuntime();
325326
/*
326327
Should follow parameters like this:
327328
if 0 (COMPAT):
@@ -357,42 +358,40 @@ public IRubyObject to_s(IRubyObject[] args) {
357358
valuesIter = values.iterator();
358359
}
359360

360-
final Map<ASN1ObjectIdentifier, String> lookup = ASN1.getSymLookup(getRuntime());
361361
final StringBuilder str = new StringBuilder(); String sep = "";
362362
while( oidsIter.hasNext() ) {
363363
final ASN1ObjectIdentifier oid = oidsIter.next();
364-
String outOid = lookup.get(oid);
365-
if ( outOid == null ) outOid = oid.toString();
364+
String name = ASN1.oid2Sym(runtime, oid);
365+
if ( name == null ) name = oid.toString();
366366
final String value = valuesIter.next();
367367

368368
if ( flag == RFC2253 ) {
369-
str.append(sep).append(outOid).append('=').append(value);
369+
str.append(sep).append(name).append('=').append(value);
370370
sep = ",";
371371
} else {
372-
str.append('/').append(outOid).append('=').append(value);
372+
str.append('/').append(name).append('=').append(value);
373373
}
374374
}
375375

376-
return getRuntime().newString( str.toString() );
376+
return runtime.newString( str.toString() );
377377
}
378378

379379
@Override
380380
@JRubyMethod
381381
public RubyArray to_a() {
382382
final Ruby runtime = getRuntime();
383383
final RubyArray entries = runtime.newArray( oids.size() );
384-
final Map<ASN1ObjectIdentifier, String> lookup = ASN1.getSymLookup(getRuntime());
385384
final Iterator<ASN1ObjectIdentifier> oidsIter = oids.iterator();
386385
final Iterator<String> valuesIter = values.iterator();
387386
final Iterator<RubyInteger> typesIter = types.iterator();
388387
while ( oidsIter.hasNext() ) {
389388
ASN1ObjectIdentifier oid = oidsIter.next();
390-
String outOid = lookup.get(oid);
391-
if ( outOid == null ) outOid = "UNDEF";
389+
String name = ASN1.oid2Sym(runtime, oid);
390+
if ( name == null ) name = "UNDEF";
392391
final String value = valuesIter.next();
393392
final IRubyObject type = typesIter.next();
394393
final IRubyObject[] entry = new IRubyObject[] {
395-
runtime.newString(outOid), runtime.newString(value), type
394+
runtime.newString(name), runtime.newString(value), type
396395
};
397396
entries.append( runtime.newArrayNoCopy(entry) );
398397
}

0 commit comments

Comments
 (0)