mtedone / podam

PODAM - POjo DAta Mocker
https://mtedone.github.io/podam
MIT License
323 stars 750 forks source link

AttributeMetadata.attributeName and AttributeMetadata.attributeAnnotations return null references when declaring a constructor. #191

Closed VictorAlbertos closed 8 years ago

VictorAlbertos commented 8 years ago

Hi :)

When I extend from AbstractRandomDataProviderStrategy overriding getStringValue the AttributeMetadata instance contains null references in its fields attributeName and attributeAnnotations in the following scenarios:

public class Mock {
  @MockAnnotation private final String s1;

  public Mock(String s1) {
    this.s1 = s1;
  }

  public String getS1() {
    return s1;
  }

}
public class Mock {
  @MockAnnotation private String s1;

  public Mock(String s1) {
    this.s1 = s1;
  }

  public void setS1(String s1) {
    this.s1 = s1;
  }

  public String getS1() {
    return s1;
  }

}

But if I create the previous Mock class without declaring any constructor, the AttributeMetadata.attributeName and AttributeMetadata.attributeAnnotations returns the expected result, that is valid references to the name of the field as long as the annotation.

public class Mock {
  @MockAnnotation private String s1;

  public void setS1(String s1) {
    this.s1 = s1;
  }

  public String getS1() {
    return s1;
  }

}

And I'm using PodamFactory as follows:

public class CustomStrategy extends AbstractRandomDataProviderStrategy {

  @Override public String getStringValue(AttributeMetadata attributeMetadata) {
    return super.getStringValue(attributeMetadata);
  }

}
CustomStrategy customStrategy = new CustomStrategy();
PodamFactory factory = new PodamFactoryImpl(customStrategy);
factory.manufacturePojoWithFullData(Mock.class); 
CustomStrategy customStrategy = new CustomStrategy();
PodamFactory factory = new PodamFactoryImpl(customStrategy);
factory.manufacturePojo(Mock.class);

Ayn help will be appreciate.

Thanks!

daivanov commented 8 years ago

Hi,

The problem here is that in Java it's impossible to connect constructor parameters with properties.

Just consider the following code:

public class Mock {
    @MockAnnotation private final String s1;

    // Here it's impossible to say what is relation between s1 and constructor's parameters
    public Mock(String a1, String a2, String a3) {
        this.s1 = a2 + a3;
    }

    public String getS1() {
        return s1;
    }
}

The only thing you can do is to annotate constructor parameter itself.

Thanks, Daniil