DWR

Inherited fields with @RemoteProperty are undefined

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Normal Normal
  • Resolution: Fixed
  • Affects Version/s: 2.0.rc5, 2.0, 2.0.1, 2.0.2, 2.0.3, 2.0.4, 2.0.5, 2.0.6, 2.0.7, 2.0.8, 2.0.9, 2.0.10, 3.0.M1, 3.0.RC1, 3.0.RC2
  • Fix Version/s: 2.0.11, 3.0.RC3
  • Component/s: Core
  • Documentation Required:
    No
  • Description:
    Hide
    Hello,

    We are using DWR 2.0.5 with annotations.

    We have an abstract class A annotated with @DataTransferObject
    and a class B which extends A also annotated with @DataTransferObject.


    The @RemoteProperty is set on the attributes of both classes.
    (Our modelization tool forces us to put this annotation on properties instead of methods).

    However, when we try to access the properties of the parent class from the javascript, the attributes from the parent class A
    are undefined.
    Looking at the AnnotationsConfigurator, we see that the fields are taken from:

     fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
     fields.addAll(Arrays.asList(clazz.getFields()));

    not from the superclass.

    So, we tried to hack this in order to take the fields of the super class:

    {code}
                Class parentClass = clazz.getSuperclass();
                while(parentClass != null && Object.class != parentClass){
                    log.info("Adding field for super class " + clazz.getSuperclass().getName());
                 fields.addAll(Arrays.asList(clazz.getSuperclass().getFields()));
                 fields.addAll(Arrays.asList(clazz.getSuperclass().getDeclaredFields()));
                 parentClass = parentClass.getSuperclass();
                }
                
    {code}

    and it works.


    Is there another proper way to configure DWR annotations with inheritance without doing a hack like this?

    Thank you,
    Gilles
    Show
    Hello, We are using DWR 2.0.5 with annotations. We have an abstract class A annotated with @DataTransferObject and a class B which extends A also annotated with @DataTransferObject. The @RemoteProperty is set on the attributes of both classes. (Our modelization tool forces us to put this annotation on properties instead of methods). However, when we try to access the properties of the parent class from the javascript, the attributes from the parent class A are undefined. Looking at the AnnotationsConfigurator, we see that the fields are taken from:  fields.addAll(Arrays.asList(clazz.getDeclaredFields()));  fields.addAll(Arrays.asList(clazz.getFields())); not from the superclass. So, we tried to hack this in order to take the fields of the super class: {code}             Class parentClass = clazz.getSuperclass();             while(parentClass != null && Object.class != parentClass){                 log.info("Adding field for super class " + clazz.getSuperclass().getName());              fields.addAll(Arrays.asList(clazz.getSuperclass().getFields()));              fields.addAll(Arrays.asList(clazz.getSuperclass().getDeclaredFields()));              parentClass = parentClass.getSuperclass();             }              {code} and it works. Is there another proper way to configure DWR annotations with inheritance without doing a hack like this? Thank you, Gilles
  1. AnnotationsConfigurator.java
    (13 kB)
    Gilles Robert
    29/Mar/12 6:15 AM

Activity

Hide
David Marginian added a comment - 29/Mar/12 6:48 AM

Thanks Gilles. We will not be using the change you made as it only accounts for one level of inheritance but I know how to fix it pretty easily. Please watch this issue so you can help me test the change when it gets checked in.

Show
David Marginian added a comment - 29/Mar/12 6:48 AM Thanks Gilles. We will not be using the change you made as it only accounts for one level of inheritance but I know how to fix it pretty easily. Please watch this issue so you can help me test the change when it gets checked in.
Hide
David Marginian added a comment - 29/Mar/12 6:02 PM

Resolved. Will be in the 2.0.11 release and in 3.0 RC3. I have added test cases to trunk only (not the 2.x branch). Gilles, if you could help test this further that would be great. It is out on our Bamboo CI:

http://ci.directwebremoting.org/bamboo/browse/DWR20-ALL-88/artifact

Show
David Marginian added a comment - 29/Mar/12 6:02 PM Resolved. Will be in the 2.0.11 release and in 3.0 RC3. I have added test cases to trunk only (not the 2.x branch). Gilles, if you could help test this further that would be great. It is out on our Bamboo CI: http://ci.directwebremoting.org/bamboo/browse/DWR20-ALL-88/artifact
Hide
Gilles Robert added a comment - 30/Mar/12 12:05 AM

Hi David,

It's OK.
Just 2 points:

  • Can you add && superClazz != Enum.class in your while ?
  • Is it still necessary to use superClazz.getDeclaredFields() ?

Thx

Show
Gilles Robert added a comment - 30/Mar/12 12:05 AM Hi David, It's OK. Just 2 points:
  • Can you add && superClazz != Enum.class in your while ?
  • Is it still necessary to use superClazz.getDeclaredFields() ?
Thx
Hide
David Marginian added a comment - 30/Mar/12 5:21 AM

1) I am not sure doing this is correct, let me think about it.
2) Yes, actually I think you have it mixed up. getFields is not necessary but getDeclaredFields certainly is. getFields only returns public fields.

Show
David Marginian added a comment - 30/Mar/12 5:21 AM 1) I am not sure doing this is correct, let me think about it. 2) Yes, actually I think you have it mixed up. getFields is not necessary but getDeclaredFields certainly is. getFields only returns public fields.
Hide
David Marginian added a comment - 31/Mar/12 10:18 PM - edited

Gilles, you don't mention it in your description but a key part of this is that the field in the abstract (super class) has to be private in order to see this - if it is public everything works fine.

You should look at the Java Doc for the reflection APIs we are using and you will see why calling getDeclaredFields is necessary.

I don't think your request for the Enum change is valid.

I am considering this issue resolved.

Show
David Marginian added a comment - 31/Mar/12 10:18 PM - edited Gilles, you don't mention it in your description but a key part of this is that the field in the abstract (super class) has to be private in order to see this - if it is public everything works fine. You should look at the Java Doc for the reflection APIs we are using and you will see why calling getDeclaredFields is necessary. I don't think your request for the Enum change is valid. I am considering this issue resolved.

People

Dates

  • Created:
    29/Mar/12 6:15 AM
    Updated:
    31/Mar/12 10:59 PM
    Resolved:
    31/Mar/12 10:25 PM