我通過將數據類型和字段名稱傳遞給我的方法來動態創建java類文件。現在我想動態地(從控制檯)傳遞相同的值,我該怎麼做。在不使用反射API的情況下在JAVA中創建動態類?
下面是我的代碼:
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"); // I wnat to pass these values from console
fields.put(char.class, "me");
fields.put(int.class, "i");
createClass("my.pkg.MyClass1", fields);
}
}
現在我要動態傳遞這些價值我該怎麼辦。
我可以問你爲什麼這樣做?就像我討厭他們一樣,在這裏'Map'可能是更好的選擇。 –