lundi 6 avril 2015

How to get first level supper class in freemarker template by java reflection?

suppose that i have a these below classes:



public abstract class A<T> implements Serializable {

}

public class B extends A<Long> {

}

public class C extends B {

}

public class D {
public C c;
}


then, i want to generate code using these below codes and template:



private static void generateJavaCode(Class clazz) {
try {
Configuration cfg = new Configuration();
FileTemplateLoader ftl1 = new FileTemplateLoader(new File("E:/templates/code/"));
cfg.setTemplateLoader(ftl1);

Template template = cfg.getTemplate(tmpl);
Map<String, Object> data = new HashMap<String, Object>();

String modelPackage = clazz.getPackage().getName();

data.put("fields", clazz.getDeclaredFields());

File f = new File(filePath);
String absolutePath = f.getAbsolutePath();
String ffilePath = absolutePath.substring(0, absolutePath.lastIndexOf(File.separator));
new File(ffilePath).mkdirs();
Writer file = new FileWriter(f);
template.process(data, file);
file.flush();
file.close();
}
catch (Exception e) {
}
}


and this is the content of .ftl file:



<#list fields as field>
<#attempt>
field.superclass.superclass
<#recover>
field.superclass
</#attempt>
</#list>


but field.superclass.superclass doesn't work, how can i solve this problem?






Aucun commentaire:

Enregistrer un commentaire