Today i was using normally "Generate Getters and Setters" utility provided by eclipse.
But had a little thought how the code generation would have happened and hence gave it a try.
I know this is still basic way of handling this and a better and clear way is yet to be achieved by me.
Hence i require few suggestions of the way we can enhance or improve this code.
Though eclipse gives a warning and generates setter methods for final variables i thought not to generate and rather give a error message.
What i am wondering is whether eclipse more or else does the same way or not?
package automaticGettesSetters;
import java.lang.reflect.Field;
public class Demo {
private static String myname="prashant jalasutram";
private String collegue="kasi subramanium";
private String myplace="UK";
private final Integer myPhone=10011;
private String anotherCollegue="Gatta Venu";
private String wifeName="Sneha Jalasutram";
void getProperties() {
try {
getPropertiesForField();
}catch(IllegalAccessException iae) {
iae.printStackTrace();
}catch(InstantiationException ie) {
ie.printStackTrace();
}
}
public static void main(String[] args) {
new Demo().getProperties();
}
void getPropertiesForField() throws IllegalAccessException,InstantiationException {
Class<?> clazz=Demo.class;
Field[] fieldArr=clazz.getDeclaredFields();
for (Field field : fieldArr) {
String fieldName=field.getName();
String fieldNameGenericString=field.toGenericString();
Class<?> typeClazz=field.getType();
String fieldTypeName=typeClazz.getSimpleName();
String modifiers=getModifiers(fieldNameGenericString);
getGetterMethod(modifiers,fieldTypeName,fieldName);
if(fieldNameGenericString.indexOf("final")<0)
getSetterMethod(modifiers,fieldTypeName,fieldName);
else
System.out.println("Setters are not supported for Final variables");
}
}
public String getModifiers(String fieldNameGenericString) {
StringBuffer modifiers=new StringBuffer("public ");
if(fieldNameGenericString.indexOf("static") >=0)
modifiers.append("static ");
if(fieldNameGenericString.indexOf("synchronized") >=0)
modifiers.append(" synchronized ");
return modifiers.toString();
}
public void getGetterMethod(String modifiers,String fieldTypeName,String fieldName) {
StringBuffer getterMethod=new StringBuffer();
String fieldNameWithFirstLetterUpperCase=fieldName.substring(0,1).toUpperCase().concat(fieldName.substring(1));
getterMethod.append(modifiers + fieldTypeName + " get" + fieldNameWithFirstLetterUpperCase + "() {");
getterMethod.append("\n");
getterMethod.append("return " + fieldName + ";");
getterMethod.append("\n");
getterMethod.append(" }");
System.out.println(getterMethod.toString());
}
public void getSetterMethod(String modifiers,String fieldTypeName,String fieldName) {
StringBuffer setterMethod=new StringBuffer();
String fieldNameWithFirstLetterUpperCase=fieldName.substring(0,1).toUpperCase().concat(fieldName.substring(1));
setterMethod.append(modifiers + "void " + "set" + fieldNameWithFirstLetterUpperCase + "(");
setterMethod.append(fieldTypeName + " " + fieldName + ") {");
setterMethod.append("\n");
setterMethod.append("this." + fieldName + "=" + fieldName + ";");
setterMethod.append("\n");
setterMethod.append("}");
System.out.println(setterMethod.toString());
}
}
Monday, 29 October 2007
Subscribe to:
Post Comments (Atom)
2 comments:
Can you reformat the code for better readability (indentation, syntax high lightning)?
thank you, it was useful .
Sumathi
Post a Comment