とりあえずこんな感じ?
仕様確定
- 名前は、元クラス名-VO(あれば) + FormVO
なんとなく用途が見える人には見えますね
ざっくりと
#* 生成される基本はStringで持つようなVOのtemplate *# public class $source.className { ## 変数定義(privateのもののみ) #foreach($property in $source.properties) /** * ${source.getVarNameOrPropertyName(${property})} */ private ${property.type} ${property.name}; #end //setter/getter #foreach($property in $source.properties) /** * ${source.getVarNameOrPropertyName(${property})}を設定する。 * @param $property.name ${source.getVarNameOrPropertyName(${property})} */ public void ${source.getSetterName(${property})}(${property.type} $property.name) { this.$property.name = $property.name; } /** * ${source.getVarNameOrPropertyName(${property})}を返す。 * @return ${source.getVarNameOrPropertyName(${property})} */ public ${property.type} ${source.getGetterName(${property})}() { return $property.name; } #end }
基本的にはリフレクションばりばりで行く予定なので
こんな感じかな?
package sample.covert; import java.lang.reflect.Field; public class GenSource { private String className; private Field[] properties; public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public Field[] getProperties() { return properties; } public void setProperties(Field[] properties) { this.properties = properties; } /** * @VarNameの指定がある場合にはその値、ない場合は変数名を返す * @param field Field * @return field名 */ public String getVarNameOrPropertyName(Field field) { VarName annotation = field.getAnnotation(VarName.class); if(annotation == null || annotation.value().length() == 0) { return field.getName(); } else { return annotation.value(); } } public String getSetterName(Field field) { String property = field.getName(); StringBuffer buffer = new StringBuffer(property.length() + 3); buffer.append("set"); buffer.append(Character.toUpperCase(property.charAt(0))); buffer.append(property.substring(1)); return buffer.toString(); } public String getGetterName(Field field) { String property = field.getName(); if(field.getType() == Boolean.TYPE || field.getType() == Boolean.class) { StringBuffer buffer = new StringBuffer(property.length() + 2); buffer.append("is"); buffer.append(Character.toUpperCase(property.charAt(0))); buffer.append(property.substring(1)); return buffer.toString(); } StringBuffer buffer = new StringBuffer(property.length() + 3); buffer.append("get"); buffer.append(Character.toUpperCase(property.charAt(0))); buffer.append(property.substring(1)); return buffer.toString(); } }
すでに、POIで・・・なんてのは無視されてる気もしますが・・・そのうちリファクタリングが火を噴くことに・・・
と期待。
とりあえず、@VarNameはこんな感じか?
package sample.covert; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface VarName { public String value(); }
アノテーションはよくわからんのだが、とりあえず言えるのが、
@Retentionの指定をしないとデフォルトがCLASSなので、
リフレクションかましたときに値を取れないんだよね・・・
とりあえずこれを元のソースの変数につけると、
変数名の和名をコメントとしてもってこれると。