我想动态创建java POJO类
以下是要求:
我有一个方法,它接受参数classname和map。地图键是基本类型,地图值是变量
create Class(String ClassName, HashMap map)
{
}
调用此方法时,应该创建一个具有指定名称的.java文件,并使用传递的hashmap创建POJO并将其存储在某个位置。
你能帮助我吗?
我希望我能清楚地提出我的问题。
感谢。
答案 0 :(得分:0)
假设您要生成Java源代码并将其保存到.java
文件中,您只需迭代地图并输出字段定义和setter / getter(如果需要)。
public String generateJava(String className, Map<String, Object> map) {
StringBuffer b = new StringBuffer("public class ").append(className).append(" {\n");
// first fields
for (Entry<String, Object> field: map.entrySet()) {
String name = field.getKey();
String type = field.getValue().getClass().getName();
b.append("\tprivate ").append(type).append(" ").append(name).append(";\n");
}
// then getters and setters
for (Entry<String, Object> field: map.entrySet()) {
String name = field.getKey();
String type = field.getValue().getClass().getName();
b.append("\n\n");
// getter.
b.append("\tpublic ").append(type).append(" get").append(name.substring(0, 1).toUpperCase()).append(name.substring(1)).append("() {\n\t\t");
b.append("return this.").append(name).append(";\n\t}");
// setter.
b.append("\tpublic void").append(" set").append(name.substring(0, 1).toUpperCase()).append(name.substring(1)).append("(").append(type).append(" ").append(name).append(") {\n\t\t");
b.append("this.").append(name).append(" = ").append(name).append(";\n\t}");
}
return b.append("\n}").toString();
}
答案 1 :(得分:0)
public class MapFirst
{
public static void createClass (
String className, Map <Class<?>, String> fields) throws IOException
{
StringBuilder builder = new StringBuilder ();
String packageName;
int idx = className.lastIndexOf ('.');
if (idx >= 0)
{
packageName = className.substring (0, idx);
className = className.substring (idx + 1);
}
else packageName = null;
if (packageName != null)
builder.append ("package ").append (packageName).append (";\n");
builder.append ("\n");
builder.append ("public class ").append (className).append ("\n");
builder.append ("{\n");
boolean flag = false;
for (Map.Entry <Class <?>, String> field: fields.entrySet ())
{
Class <?> type = field.getKey ();
String name = field.getValue ();
String nameCapitalized = Character.toUpperCase (name.charAt (0)) + name.substring (1);
if (flag) builder.append ("\n");
flag = true;
builder.append (" private ").append (type).append (" ").append (name).append (";\n");
builder.append ("\n");
builder.append (" public ").append (type).append (" get").append (nameCapitalized).append (" ()\n");
builder.append (" {\n");
builder.append (" return ").append (name).append (";\n");
builder.append (" }\n");
builder.append ("\n");
builder.append (" public void set").append (nameCapitalized).append (" (").append (type).append (" ").append (name).append (")\n");
builder.append (" {\n");
builder.append (" this.").append (name).append (" = ").append (name).append (";\n");
builder.append (" }\n");
}
builder.append ("}\n");
File dir = new File (
packageName.replaceAll (
"\\.",
Matcher.quoteReplacement (System.getProperty ("file.separator"))));
dir.mkdirs ();
BufferedWriter writer = new BufferedWriter (
new OutputStreamWriter (
new FileOutputStream (
new File (dir, className + ".java"))));
writer.write (builder.toString ());
writer.close ();
}
public static void main (String [] args) throws Exception
{
Map <Class <?>, String> fields = new HashMap <Class<?>, String> ();
fields.put (int.class, "foo");
fields.put (double.class, "bar");
createClass ("my.pkg.MyClass", fields);
}
}