Skip to content

Commit fc62c54

Browse files
authored
Optimize boxPrimitiveArray method (#136)
1 parent 5efedf1 commit fc62c54

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed

src/main/java/com/btk5h/skriptmirror/util/JavaUtil.java

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,50 @@ public static String toGenericString(Member o) {
111111
*/
112112
public static Object boxPrimitiveArray(Object obj) {
113113
Class<?> componentType = obj.getClass().getComponentType();
114-
if (componentType != null && componentType.isPrimitive()) {
115-
int length = Array.getLength(obj);
116-
Object[] boxedArray = newArray(WRAPPER_CLASSES.get(componentType), length);
117-
118-
for (int i = 0; i < length; i++) {
119-
boxedArray[i] = Array.get(obj, i);
120-
}
121-
122-
obj = boxedArray;
114+
if (componentType == null || !componentType.isPrimitive()) {
115+
return obj;
116+
}
117+
int length = Array.getLength(obj);
118+
if (componentType == int.class) {
119+
int[] source = (int[]) obj;
120+
Integer[] target = new Integer[length];
121+
for (int i = 0; i < length; i++) target[i] = source[i];
122+
return target;
123+
} else if (componentType == long.class) {
124+
long[] source = (long[]) obj;
125+
Long[] target = new Long[length];
126+
for (int i = 0; i < length; i++) target[i] = source[i];
127+
return target;
128+
} else if (componentType == double.class) {
129+
double[] source = (double[]) obj;
130+
Double[] target = new Double[length];
131+
for (int i = 0; i < length; i++) target[i] = source[i];
132+
return target;
133+
} else if (componentType == float.class) {
134+
float[] source = (float[]) obj;
135+
Float[] target = new Float[length];
136+
for (int i = 0; i < length; i++) target[i] = source[i];
137+
return target;
138+
} else if (componentType == boolean.class) {
139+
boolean[] source = (boolean[]) obj;
140+
Boolean[] target = new Boolean[length];
141+
for (int i = 0; i < length; i++) target[i] = source[i];
142+
return target;
143+
} else if (componentType == byte.class) {
144+
byte[] source = (byte[]) obj;
145+
Byte[] target = new Byte[length];
146+
for (int i = 0; i < length; i++) target[i] = source[i];
147+
return target;
148+
} else if (componentType == char.class) {
149+
char[] source = (char[]) obj;
150+
Character[] target = new Character[length];
151+
for (int i = 0; i < length; i++) target[i] = source[i];
152+
return target;
153+
} else if (componentType == short.class) {
154+
short[] source = (short[]) obj;
155+
Short[] target = new Short[length];
156+
for (int i = 0; i < length; i++) target[i] = source[i];
157+
return target;
123158
}
124159
return obj;
125160
}

0 commit comments

Comments
 (0)