Skip to content

Commit 5559273

Browse files
committedMar 27, 2018
Back off part of JavaUtil opto.
The original logic could handle Object[] with many different types while my new logic used the base type of the array. This is only appropriate for specifically-typed, homogeneous arrays, so I have narrowed the optimization to only String[].
1 parent 0cdd2f9 commit 5559273

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed
 

Diff for: ‎core/src/main/java/org/jruby/RubyDir.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ private static RubyArray entriesCommon(ThreadContext context, String path, Encod
311311
FileResource directory = JRubyFile.createResource(context, path);
312312
String[] files = getEntries(context, directory, adjustedPath);
313313

314-
return RubyArray.newArrayMayCopy(runtime, JavaUtil.convertJavaArrayToRuby(runtime, files, new JavaUtil.StringConverter() {
314+
return RubyArray.newArrayMayCopy(runtime, JavaUtil.convertStringArrayToRuby(runtime, files, new JavaUtil.StringConverter() {
315315
public IRubyObject convert(Ruby runtime, Object object) {
316316
if (object == null) return runtime.getNil();
317317
return RubyString.newString(runtime, (String)object, encoding);

Diff for: ‎core/src/main/java/org/jruby/javasupport/JavaUtil.java

+16-23
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,25 @@ public class JavaUtil {
122122
}
123123

124124
public static IRubyObject[] convertJavaArrayToRuby(final Ruby runtime, final Object[] objects) {
125-
JavaConverter converter = getJavaConverter(objects.getClass().getComponentType());
126-
127-
return convertJavaArrayToRuby(runtime, objects, converter);
128-
}
129-
130-
public static IRubyObject[] convertJavaArrayToRuby(final Ruby runtime, final Object[] objects, JavaConverter converter) {
131125
if ( objects == null || objects.length == 0 ) return IRubyObject.NULL_ARRAY;
132126

127+
if (objects instanceof String[]) return convertStringArrayToRuby(runtime, (String[]) objects, JAVA_STRING_CONVERTER);
128+
133129
IRubyObject[] rubyObjects = new IRubyObject[objects.length];
134130

135131
for (int i = 0; i < objects.length; i++) {
136-
rubyObjects[i] = converter.get(runtime, objects, i);
132+
rubyObjects[i] = convertJavaToUsableRubyObject(runtime, objects[i]);
133+
}
134+
return rubyObjects;
135+
}
136+
137+
public static IRubyObject[] convertStringArrayToRuby(final Ruby runtime, final String[] strings, StringConverter converter) {
138+
if ( strings == null || strings.length == 0 ) return IRubyObject.NULL_ARRAY;
139+
140+
IRubyObject[] rubyObjects = new IRubyObject[strings.length];
141+
142+
for (int i = 0; i < strings.length; i++) {
143+
rubyObjects[i] = convertJavaToUsableRubyObjectWithConverter(runtime, strings[i], converter);
137144
}
138145
return rubyObjects;
139146
}
@@ -857,23 +864,9 @@ public void set(Ruby runtime, Object array, int i, IRubyObject value) {
857864
}
858865
}
859866

860-
public static class InternalStringConverter extends StringConverter {
861-
public InternalStringConverter() {
862-
super();
863-
}
864-
865-
@Override
866-
public IRubyObject convert(Ruby runtime, Object object) {
867-
if (object == null) return runtime.getNil();
868-
return RubyString.newInternalFromJavaExternal(runtime, (String) object);
869-
}
870-
}
871-
872-
public static final JavaConverter JAVA_STRING_CONVERTER = new StringConverter();
873-
874-
public static final JavaConverter JAVA_INTERNAL_STRING_CONVERTER = new InternalStringConverter();
867+
public static final StringConverter JAVA_STRING_CONVERTER = new StringConverter();
875868

876-
public static final JavaConverter JAVA_CHARSEQUENCE_CONVERTER = new JavaConverter(String.class) {
869+
public static final JavaConverter JAVA_CHARSEQUENCE_CONVERTER = new JavaConverter(CharSequence.class) {
877870
public IRubyObject convert(Ruby runtime, Object object) {
878871
if (object == null) return runtime.getNil();
879872
return RubyString.newUnicodeString(runtime, (CharSequence)object);

0 commit comments

Comments
 (0)
Please sign in to comment.