Strict long type casting
Ty Young
youngty1997 at gmail.com
Tue Apr 7 01:54:05 UTC 2020
On 4/6/20 8:12 PM, Maurizio Cimadamore wrote:
> Hi Ty,
> I'd need to have a look at the code of MemoryNumber/MemoryLong to
> understand what's going on.
The abstract class can be found here:
https://github.com/BlueGoliath/Crosspoint/blob/master/src/main/java/org/goliath/crosspoint/abstracts/NativeNumber.java
implementation:
https://github.com/BlueGoliath/Crosspoint/blob/master/src/main/java/org/goliath/crosspoint/numbers/NativeLong.java
(classes have just been renamed. Same code)
Basic usage:
NativeLong test = new NativeLong();
test.setValue(Long.MAX_VALUE);
System.out.println(test.byteValue());
Which fails with:
Caused by: java.lang.invoke.WrongMethodTypeException: cannot convert
MethodHandle(VarHandle,MemoryAddress)long to (VarHandle,MemoryAddress)int
at
java.base/java.lang.invoke.MethodHandle.asTypeUncached(MethodHandle.java:881)
at
java.base/java.lang.invoke.MethodHandle.asType(MethodHandle.java:866)
at
java.base/java.lang.invoke.VarHandleGuards.guard_L_I(VarHandleGuards.java:106)
at
org.goliath.crosspoint/org.goliath.crosspoint.abstracts.NativeNumber.intValue(NativeNumber.java:46)
at java.base/java.lang.Number.byteValue(Number.java:104)
...but only with long.
>
> Thanks
> Maurizio
>
> On 07/04/2020 01:49, Ty Young wrote:
>> Hi all,
>>
>>
>> I'm trying to create a way of reading primitive number based struct
>> values(since VarHandle.get is polymorphic) by combining Java's Number
>> abstract class with my abstraction like so:
>>
>>
>> public abstract class MemoryNumber<E extends Number> extends Number
>> implements MemoryValue<E>
>>
>>
>> Which is then extended by classes like "MemoryLong".
>>
>>
>> The problem here is that with long(and only long) the type casting is
>> strict and fails. Attempting to call byteValue() fails on intValue().
>> This doesn't, for whatever reason, happen with normal on-heap long
>> values nor does it happen with an MemoryInt implementation either.
>>
>>
>> I tried looking in the source code of Long.class and I couldn't find
>> anything that I was doing differently besides using Panama, so I'm a
>> bit lost here. Does anyone know what's going on?
>>
More information about the panama-dev
mailing list