1

I have a fixed length record bean for beanio as follows:

@Record
public class RecordBean {
   @Field(at = 0, length = 3)
   private String field1;

   @Field(at = 3, length = 10)
   private String field2;

   //etc
}

Now I want to use that bean also as a transfer object within my application, and later unmarshal it to the fixedLength record using beanio.

Problem: when I access the fields of the RecordBean, I can never be sure if the field may be null, as I create the object myself:

RecordBean r = new RecordBean();
r.getField1(); //may be null

Question: can I somehow initialize the bean using beanio with default values? Eg an empty string that has the exact length given in the @Field annotation?

I could as a workaround wrap each getter in StringUtils.defaultString(value) but thats much boilerplate code.

membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • No luck here? http://www.beanio.org/2.0/docs/reference/index.html#NullFields – Chetan Kinger Feb 26 '15 at 18:55
  • I know I can control `null` values during `marshalling`. But I want to initialize the fields *before* marshalling as I want to work with the object itself, and not having to perform null checks on each property if I find a way to preinit the fields with their length. – membersound Feb 26 '15 at 18:59
  • Is it absolutely necessary that beanio should do this for you? There is a pretty straightforward way to implement this on your own. – Chetan Kinger Feb 26 '15 at 19:03
  • 1
    I see your edit. That's what I was talking about. I don't see what's wrong in adding StringUtils.defaultString(value) in the getters. It's clean. It's in one place (per field). No need to add null checks all around your client code. The only difference being that instead of having an annotation define the default behaviour, you now have it in your getter. – Chetan Kinger Feb 26 '15 at 19:14
  • OK thanks for assessment. So I will go for this solution. How would you cope with `@Segment` private List list;` getters? Return an empty new arraylist if null? – membersound Feb 26 '15 at 19:17
  • 1
    Yup. You can go with this solution and if someone does answer your quesiton, it shouldn't be a tough job moving to annotations. And yes, returning an empty ArrayList does make sense.. – Chetan Kinger Feb 26 '15 at 19:20

2 Answers2

2

Yes,

According to documentation from beanio for fields :

default : The default value of this field. When unmarshalling a stream, this value is set on the bean object when the field text is null or the empty string. And when marshalling, the default value is used when the property value is null or ignore is set to true (unless disabled). A default value is converted to a Java object using the same type handler configured for the field.

With configuration file :

<field name="myField" type="string" default="default" />

With annotation :

@Field(at = 3, length = 10, default = "defaultValue")
private String field;
Woody
  • 809
  • 8
  • 21
1

Shouldn't it be defaultValue rather than default?

@Field(at = 3, length = 10, defaultValue = "whateverValue")
private String field;
Gibi Abraham
  • 41
  • 1
  • 8