5

For MVC model classes, my setters look like:

enum BoundProperty {FIELD_NAME, ...}

private Type fieldName;

public setFieldName(Type newValue) {
    Type oldValue = fieldName;
    fieldName = newValue;
    firePropertyChange(BoundProperty.FIELD_NAME, oldValue, newValue);
}

Given a field, can this output be produced from the autogenerated setter? If not is there a way to get this output from a template?

The output should CamelCase the field name to produce the method name, so fieldName generates setFieldName() and Uppercase the field name to produce the property enum.

So fieldName generates FIELD_NAME (or FIELDNAME would work too).

Reddy
  • 8,219
  • 9
  • 50
  • 72
Richard Neish
  • 7,053
  • 4
  • 32
  • 65

3 Answers3

1

I can see this message on "Generate getters/setters" dialog. The format of the getters/setters may be configured on the Code Templates preference page. You can go there (Setter Body under Code section) and modify like below.

Type oldValue = ${field};
${field} = ${param};
firePropertyChange(BoundProperty.FIELD_NAME, oldValue, ${param});

However it is not going to generate BoundProperty though. It need more research to find out if it is possible or not. These links may help

Useful Eclipse Java Code Templates and Getting started with Eclipse code templates

Community
  • 1
  • 1
Reddy
  • 8,219
  • 9
  • 50
  • 72
  • Thanks for the response. I tried this, but it's not enough - Type should be the type of the field, and FIELD_NAME should be the uppercase version of the fieldName. There doesn't seem to be a way to derive these from the variables available in the Code Templates screen? – Richard Neish Jan 17 '12 at 13:39
  • I have just added some links in the answer. May be you can get new variables there? – Reddy Jan 17 '12 at 13:41
  • BTW do you need a way to add new fields in the enum as well? – Reddy Jan 17 '12 at 13:46
1

I think there is not a straightforward way to make this through Eclipse templates, mainly regarding the camelCase/upperCase and generation of enum values. You can check these two questions Is there a way to capitalize the first letter of a value of a variable in Eclipse (Helios) code templates, Programmatically add code templates? to dig into further details.

IMHO, the best way to achieve what you want is to use the Fast Code Eclipse Plugin and write a velocity template for that plugin that generates all the code from the fields.

enum BoundProperty {
#foreach ($field in ${fields})
    ${field.toUpperCase()} #if( $foreach.hasNext ), #end
#end
}

#foreach ($field in ${fields})
    public ${field.type} get${field.name.substring(0,1).toUpperCase()}${field.name.substring(1)}(${field.type} newValue) {
        Type oldValue = fieldName;
        fieldName = newValue;
        firePropertyChange(BoundProperty.${field.name.toUpperCase()}, oldValue, newValue);       
    }
#end

Or change the "getter_setter" template of that plugin.

Community
  • 1
  • 1
jalopaba
  • 7,644
  • 2
  • 42
  • 54
0

The Fast Code Eclipse Plugin looks like an option, but I got an error installing it which I don't have time to chase and the answer offered by user qualidafial on the question Useful Eclipse Java Code Templates does everything I need to do without requiring an Eclipse plugin.

Community
  • 1
  • 1
Richard Neish
  • 7,053
  • 4
  • 32
  • 65