@@ -100,42 +100,41 @@ public static PythonLikeObject wrapJavaObject(Object object, Map<Object, PythonL
100
100
return PythonString .valueOf (string );
101
101
}
102
102
103
- if (object instanceof Iterator iterator ) {
103
+ if (object instanceof Iterator <?> iterator ) {
104
104
return new DelegatePythonIterator <>(iterator );
105
105
}
106
106
107
- if (object instanceof List list ) {
108
- PythonLikeList out = new PythonLikeList ();
107
+ if (object instanceof List <?> list ) {
108
+ PythonLikeList <?> out = new PythonLikeList <> ();
109
109
createdObjectMap .put (object , out );
110
110
for (Object item : list ) {
111
111
out .add (wrapJavaObject (item ));
112
112
}
113
113
return out ;
114
114
}
115
115
116
- if (object instanceof Set set ) {
117
- PythonLikeSet out = new PythonLikeSet ();
116
+ if (object instanceof Set <?> set ) {
117
+ PythonLikeSet <?> out = new PythonLikeSet <> ();
118
118
createdObjectMap .put (object , out );
119
119
for (Object item : set ) {
120
120
out .add (wrapJavaObject (item ));
121
121
}
122
122
return out ;
123
123
}
124
124
125
- if (object instanceof Map map ) {
126
- PythonLikeDict out = new PythonLikeDict ();
125
+ if (object instanceof Map <?, ?> map ) {
126
+ PythonLikeDict <?, ?> out = new PythonLikeDict <> ();
127
127
createdObjectMap .put (object , out );
128
- Set < Map . Entry <?, ?>> entrySet = map .entrySet ();
129
- for (Map . Entry <?, ?> entry : entrySet ) {
128
+ var entrySet = map .entrySet ();
129
+ for (var entry : entrySet ) {
130
130
out .put (wrapJavaObject (entry .getKey ()), wrapJavaObject (entry .getValue ()));
131
131
}
132
132
return out ;
133
133
}
134
134
135
- if (object instanceof Class maybeFunctionClass ) {
136
- if (Set .of (maybeFunctionClass .getInterfaces ()).contains (PythonLikeFunction .class )) {
137
- return new PythonCode ((Class <? extends PythonLikeFunction >) maybeFunctionClass );
138
- }
135
+ if (object instanceof Class <?> maybeFunctionClass &&
136
+ Set .of (maybeFunctionClass .getInterfaces ()).contains (PythonLikeFunction .class )) {
137
+ return new PythonCode ((Class <? extends PythonLikeFunction >) maybeFunctionClass );
139
138
}
140
139
141
140
if (object instanceof OpaquePythonReference opaquePythonReference ) {
@@ -263,8 +262,7 @@ public static <T> T convertPythonObjectToJavaType(Class<? extends T> type, Pytho
263
262
return null ;
264
263
}
265
264
266
- if (object instanceof JavaObjectWrapper ) {
267
- JavaObjectWrapper wrappedObject = (JavaObjectWrapper ) object ;
265
+ if (object instanceof JavaObjectWrapper wrappedObject ) {
268
266
Object javaObject = wrappedObject .getWrappedObject ();
269
267
if (!type .isAssignableFrom (javaObject .getClass ())) {
270
268
throw new TypeError ("Cannot convert from (" + getPythonLikeType (javaObject .getClass ()) + ") to ("
@@ -275,11 +273,10 @@ public static <T> T convertPythonObjectToJavaType(Class<? extends T> type, Pytho
275
273
276
274
if (type .equals (byte .class ) || type .equals (short .class ) || type .equals (int .class ) || type .equals (long .class ) ||
277
275
type .equals (float .class ) || type .equals (double .class ) || Number .class .isAssignableFrom (type )) {
278
- if (!(object instanceof PythonNumber )) {
276
+ if (!(object instanceof PythonNumber pythonNumber )) {
279
277
throw new TypeError ("Cannot convert from (" + getPythonLikeType (object .getClass ()) + ") to ("
280
278
+ getPythonLikeType (type ) + ")." );
281
279
}
282
- PythonNumber pythonNumber = (PythonNumber ) object ;
283
280
Number value = pythonNumber .getValue ();
284
281
285
282
if (type .equals (BigInteger .class ) || type .equals (BigDecimal .class )) {
@@ -312,11 +309,10 @@ public static <T> T convertPythonObjectToJavaType(Class<? extends T> type, Pytho
312
309
}
313
310
314
311
if (type .equals (boolean .class ) || type .equals (Boolean .class )) {
315
- if (!(object instanceof PythonBoolean )) {
312
+ if (!(object instanceof PythonBoolean pythonBoolean )) {
316
313
throw new TypeError ("Cannot convert from (" + getPythonLikeType (object .getClass ()) + ") to ("
317
314
+ getPythonLikeType (type ) + ")." );
318
315
}
319
- PythonBoolean pythonBoolean = (PythonBoolean ) object ;
320
316
return (T ) (Boolean ) pythonBoolean .getBooleanValue ();
321
317
}
322
318
@@ -344,6 +340,53 @@ public static void loadName(MethodVisitor methodVisitor, String name) {
344
340
false );
345
341
}
346
342
343
+ private record ReturnValueOpDescriptor (
344
+ String wrapperClassName ,
345
+ String methodName ,
346
+ String methodDescriptor ,
347
+ int opcode ,
348
+ boolean noConversionNeeded ) {
349
+ public static ReturnValueOpDescriptor noConversion () {
350
+ return new ReturnValueOpDescriptor ("" , "" , "" ,
351
+ Opcodes .ARETURN , true );
352
+ }
353
+
354
+ public static ReturnValueOpDescriptor forNumeric (String methodName ,
355
+ String methodDescriptor ,
356
+ int opcode ) {
357
+ return new ReturnValueOpDescriptor (Type .getInternalName (Number .class ), methodName , methodDescriptor , opcode ,
358
+ false );
359
+ }
360
+ }
361
+
362
+ private static final Map <Type , ReturnValueOpDescriptor > numericReturnValueOpDescriptorMap = Map .of (
363
+ Type .BYTE_TYPE , ReturnValueOpDescriptor .forNumeric (
364
+ "byteValue" ,
365
+ Type .getMethodDescriptor (Type .BYTE_TYPE ),
366
+ Opcodes .IRETURN ),
367
+ Type .SHORT_TYPE , ReturnValueOpDescriptor .forNumeric (
368
+ "shortValue" ,
369
+ Type .getMethodDescriptor (Type .SHORT_TYPE ),
370
+ Opcodes .IRETURN ),
371
+ Type .INT_TYPE , ReturnValueOpDescriptor .forNumeric (
372
+ "intValue" ,
373
+ Type .getMethodDescriptor (Type .INT_TYPE ),
374
+ Opcodes .IRETURN ),
375
+ Type .LONG_TYPE , ReturnValueOpDescriptor .forNumeric (
376
+ "longValue" ,
377
+ Type .getMethodDescriptor (Type .LONG_TYPE ),
378
+ Opcodes .LRETURN ),
379
+ Type .FLOAT_TYPE , ReturnValueOpDescriptor .forNumeric (
380
+ "floatValue" ,
381
+ Type .getMethodDescriptor (Type .FLOAT_TYPE ),
382
+ Opcodes .FRETURN ),
383
+ Type .DOUBLE_TYPE , ReturnValueOpDescriptor .forNumeric (
384
+ "doubleValue" ,
385
+ Type .getMethodDescriptor (Type .DOUBLE_TYPE ),
386
+ Opcodes .DRETURN ),
387
+ Type .getType (BigInteger .class ), ReturnValueOpDescriptor .noConversion (),
388
+ Type .getType (BigDecimal .class ), ReturnValueOpDescriptor .noConversion ());
389
+
347
390
/**
348
391
* If {@code method} return type is not void, convert TOS into its Java equivalent and return it.
349
392
* If {@code method} return type is void, immediately return.
@@ -353,77 +396,36 @@ public static void loadName(MethodVisitor methodVisitor, String name) {
353
396
public static void returnValue (MethodVisitor methodVisitor , MethodDescriptor method , StackMetadata stackMetadata ) {
354
397
Type returnAsmType = method .getReturnType ();
355
398
399
+ if (Type .CHAR_TYPE .equals (returnAsmType )) {
400
+ throw new IllegalStateException ("Unhandled case for primitive type (char)." );
401
+ }
402
+
356
403
if (Type .VOID_TYPE .equals (returnAsmType )) {
357
404
methodVisitor .visitInsn (Opcodes .RETURN );
358
405
return ;
359
406
}
360
407
361
- if (Type .BYTE_TYPE .equals (returnAsmType ) ||
362
- Type .CHAR_TYPE .equals (returnAsmType ) ||
363
- Type .SHORT_TYPE .equals (returnAsmType ) ||
364
- Type .INT_TYPE .equals (returnAsmType ) ||
365
- Type .LONG_TYPE .equals (returnAsmType ) ||
366
- Type .FLOAT_TYPE .equals (returnAsmType ) ||
367
- Type .DOUBLE_TYPE .equals (returnAsmType ) ||
368
- Type .getType (BigInteger .class ).equals (returnAsmType ) ||
369
- Type .getType (BigDecimal .class ).equals (returnAsmType )) {
408
+ if (numericReturnValueOpDescriptorMap .containsKey (returnAsmType )) {
409
+ var returnValueOpDescriptor = numericReturnValueOpDescriptorMap .get (returnAsmType );
370
410
methodVisitor .visitTypeInsn (Opcodes .CHECKCAST , Type .getInternalName (PythonNumber .class ));
371
411
methodVisitor .visitMethodInsn (Opcodes .INVOKEINTERFACE ,
372
412
Type .getInternalName (PythonNumber .class ),
373
413
"getValue" ,
374
414
Type .getMethodDescriptor (Type .getType (Number .class )),
375
415
true );
376
416
377
- if (Type .getType (BigInteger .class ).equals (returnAsmType ) ||
378
- Type .getType (BigDecimal .class ).equals (returnAsmType )) {
417
+ if (returnValueOpDescriptor .noConversionNeeded ) {
379
418
methodVisitor .visitTypeInsn (Opcodes .CHECKCAST , returnAsmType .getInternalName ());
380
419
methodVisitor .visitInsn (Opcodes .ARETURN );
381
420
return ;
382
421
}
383
422
384
- String wrapperClassName = null ;
385
- String methodName = null ;
386
- String methodDescriptor = null ;
387
- int returnOpcode = 0 ;
388
-
389
- if (Type .BYTE_TYPE .equals (returnAsmType )) {
390
- wrapperClassName = Type .getInternalName (Number .class );
391
- methodName = "byteValue" ;
392
- methodDescriptor = Type .getMethodDescriptor (Type .BYTE_TYPE );
393
- returnOpcode = Opcodes .IRETURN ;
394
- } else if (Type .CHAR_TYPE .equals (returnAsmType )) {
395
- throw new IllegalStateException ("Unhandled case for primitive type (char)." );
396
- // returnOpcode = Opcodes.IRETURN;
397
- } else if (Type .SHORT_TYPE .equals (returnAsmType )) {
398
- wrapperClassName = Type .getInternalName (Number .class );
399
- methodName = "shortValue" ;
400
- methodDescriptor = Type .getMethodDescriptor (Type .SHORT_TYPE );
401
- returnOpcode = Opcodes .IRETURN ;
402
- } else if (Type .INT_TYPE .equals (returnAsmType )) {
403
- wrapperClassName = Type .getInternalName (Number .class );
404
- methodName = "intValue" ;
405
- methodDescriptor = Type .getMethodDescriptor (Type .INT_TYPE );
406
- returnOpcode = Opcodes .IRETURN ;
407
- } else if (Type .FLOAT_TYPE .equals (returnAsmType )) {
408
- wrapperClassName = Type .getInternalName (Number .class );
409
- methodName = "floatValue" ;
410
- methodDescriptor = Type .getMethodDescriptor (Type .FLOAT_TYPE );
411
- returnOpcode = Opcodes .FRETURN ;
412
- } else if (Type .LONG_TYPE .equals (returnAsmType )) {
413
- wrapperClassName = Type .getInternalName (Number .class );
414
- methodName = "longValue" ;
415
- methodDescriptor = Type .getMethodDescriptor (Type .LONG_TYPE );
416
- returnOpcode = Opcodes .LRETURN ;
417
- } else if (Type .DOUBLE_TYPE .equals (returnAsmType )) {
418
- wrapperClassName = Type .getInternalName (Number .class );
419
- methodName = "doubleValue" ;
420
- methodDescriptor = Type .getMethodDescriptor (Type .DOUBLE_TYPE );
421
- returnOpcode = Opcodes .DRETURN ;
422
- }
423
423
methodVisitor .visitMethodInsn (Opcodes .INVOKEVIRTUAL ,
424
- wrapperClassName , methodName , methodDescriptor ,
424
+ returnValueOpDescriptor .wrapperClassName ,
425
+ returnValueOpDescriptor .methodName ,
426
+ returnValueOpDescriptor .methodDescriptor ,
425
427
false );
426
- methodVisitor .visitInsn (returnOpcode );
428
+ methodVisitor .visitInsn (returnValueOpDescriptor . opcode );
427
429
return ;
428
430
}
429
431
0 commit comments