12 messages in net.java.dev.jna.usersRe: [jna-users] Generics...
FromSent OnAttachments
Paul LoyJul 2, 2008 5:23 am 
Timothy WallJul 2, 2008 7:13 am 
Timothy WallJul 2, 2008 7:16 am 
Paul LoyJul 2, 2008 7:18 am 
Paul LoyAug 7, 2008 8:26 am 
Timothy WallAug 7, 2008 10:21 am 
Paul LoyAug 14, 2008 3:18 am 
Timothy WallAug 19, 2008 7:29 am 
Paul LoyAug 19, 2008 7:53 am 
Timothy WallAug 19, 2008 8:01 am 
Alex Lam S.L.Aug 19, 2008 10:03 am 
Wayne MeissnerAug 19, 2008 5:32 pm 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [jna-users] Generics...Actions...
From:Timothy Wall (twal@dev.java.net)
Date:Aug 19, 2008 7:29:09 am
List:net.java.dev.jna.users

On Aug 14, 2008, at 6:19 AM, Paul Loy wrote:

Sorry, I was talking about something similar to this:

public class GProperty<T> extends Structure implements ByReference {

public String propertyName; public T propertyData;

... }

Ideally this would be handled by asking NativeMapped for its corresponding native type. While the type info is not available in the class itself, it *is* available from the Field object (getGenericType). Unfortunately, this is a ParameterizedType, so the existing NativeMapped lookup doesn't work.

Is there a way to fix the NativeMapped query so that a ParameterizedType may be used if available?

Extracting the method is probably ok, too, although I'm not sure you need all the suggested arguments. Unfortunately, it encapsulates type and conversion information in a structure containing the type instead of within the type itself, but you're already down that road since the type information is only available in the Field object and not the value or value's class.

protected Class assignNativeMapper(Class type, Object value, Field field, StructField structField) { Class nativeType = type;

if (NativeMapped.class.isAssignableFrom(type)) { NativeMappedConverter tc = NativeMappedConverter.getInstance(type); if (value == null) { value = tc.defaultValue(); setField(structField, value); } nativeType = tc.nativeType(); structField.writeConverter = tc; structField.readConverter = tc; structField.context = new StructureReadContext(this, field); } else if (typeMapper != null) { ToNativeConverter writeConverter = typeMapper.getToNativeConverter(type); FromNativeConverter readConverter = typeMapper.getFromNativeConverter(type); if (writeConverter != null && readConverter != null) { value = writeConverter.toNative(value, new StructureWriteContext(this, structField.field)); nativeType = value != null ? value.getClass() : Pointer.class; structField.writeConverter = writeConverter; structField.readConverter = readConverter; structField.context = new StructureReadContext(this, field); } else if (writeConverter != null || readConverter != null) { String msg = "Structures require bidirectional type conversion for " + type; throw new IllegalArgumentException(msg); } } return nativeType; }

this would then allow me to create a generic superclass of Structure that could handle generics by overriding assignNativeMapper. I would be happy to put that back in to JNA if any one else would find it useful.