我想动态创建 java POJO 类
以下是要求:
我有一个方法,它接受参数 classname 和 map 。映射键将是原始类型,映射值将是变量
create Class(String ClassName, HashMap map)
{
}
调用此方法时,它应该创建一个具有指定名称的 .java 文件,并使用传递的 hashmap 创建 POJO 并将其存储在某个位置。
你们能帮我解决这个问题吗?
我希望我能清楚地提出我的问题。
谢谢。
假设您要生成 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();
}
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);
}
}