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:Paul Loy (kete@gmail.com)
Date:Aug 7, 2008 8:26:26 am
List:net.java.dev.jna.users

Hi Timothy,

Except... because a 'vanilla' instance of the nativemapped class is created by Structure (well by NativeMappedConverter.getInstance(type)). This means that we do not have the generic type applied so it tries to map to Object rather than Integer (for example if GProperty<Integer> were used). I guess I don't understand why when the data type extends NativeMapped does it then create a new instance? Why not use the instance we now have?

Or, could we add another interface that Structure could check for. Maybe a GenericType interface so Structure.calculateSize could know there is a special case here.

I really don't want to have to create X Property classes for each type. especially when I think it's so close to being able to accept them anyway.

Thanks,

Paul.

On Wed, Jul 2, 2008 at 3:17 PM, Timothy Wall <twal@dev.java.net> wrote:

Have you considered making GProperty implement NativeMapped? Structure will calculate based on whatever native type your custom object indicates.

On Jul 2, 2008, at 8:23 AM, Paul Loy wrote:

Hi All,

I'm currently implementing a JNA wrapper around GTK. On GObjects we can set and get properties. These properties can be different types (Boolean, Integer, GObjects, etc). So the mapped methods look like:

g_object_set(GObject on, GPropertyByValue...property); g_object_get(GObject from, GPropertyByReference...propertyPointer);

where:

public class GPropertyByReference<T> extends GProperty<T> implements ByValue {

public String propertyName; public Pointer propertyData;

... }

public class GPropertyByValue<T> extends GProperty<T> {

public String propertyName; public T propertyData;

... }

I wanted to make GPropertyByValue and GPropertyByReference generic. I have managed it with GPropertyByReference because I can do the 'marshalling' back from the Pointer on a getValue() call as I know the type when I initially create the property (i.e. I have a contructor that takes the type). But I get stuck with GPropertyByValue.

When JNA tries to marshall this, it gets the type of the propertyData field as Object which it doesn't know what to do with.

The problem, as I see it, is that calculateSize is doing a little too much so I can't, for example, inject the type in anywhere without completely losing calculateSize. As I don't want to re-invent-the-wheel I was wondering if re-factoring the calculateSize out a little would be possible. I guess a getFieldType() method would do the trick, although I was even thinking that a seperate method for creating the structFields, which could then be overloaded, would be useful?

Other than this, is there another way to use generics in this way?

PS. I know I've called GPropertyByReference 'ByReference' when in Structure terms it's 'ByValue', but it's to indicate that the data contained within is ByReference (or Pointer).