概述
Created by KdMobiB on 2017/7/11.
*/
public class Utils {
File file;//java文件的生成目录
String rootName;
/**
开始解析Json数据
@param jsonStr
@return
*/
@SuppressWarnings("unchecked")
public HashMap getRootFromJson(String rootClass, String jsonStr) {
LinkedTreeMap a = new Gson().fromJson(jsonStr, LinkedTreeMap.class);
return getKidFromJson(rootClass, a, true);
}
public HashMap getListRootFromJson(String rootClass, String jsonStr) {
LinkedTreeMap a = new Gson().fromJson(jsonStr, LinkedTreeMap.class);
startChildArrayClass(rootClass);
return getKidFromJson(rootClass, a, false);
}
/**
WRITE THE KIDCLASS
@param a
@return
*/
public HashMap getKidFromJson(String childClass, LinkedTreeMap a, boolean disable) {
if ((!rootName.equals(childClass)) && disable) {
startChildClass(childClass);
}
HashMap maps = new HashMap();
Set> entry = a.entrySet();
Iterator> iterator = entry.iterator();
while (iterator.hasNext()) {
LinkedTreeMap.Entry stringObjectEntry = iterator.next();
String sk = stringObjectEntry.getKey();
Object sv = stringObjectEntry.getValue();
if (sv instanceof LinkedTreeMap) {
//如果TreeMap类型,代表数据还有分支,继续遍历下去
getKidFromJson(sk, (LinkedTreeMap) sv, true);
stopChildClass();
} else if (sv instanceof ArrayList) {
//如果是List类型,列表内的数据需要重新创建一个类
getListRootFromJson(sk, new Gson().toJson(((ArrayList) sv).get(0)));
stopChildClass();
} else {
//当判断不是数组类型或者列表类型,直接将key写入当前的类
sb.append("t" + sv.getClass().getSimpleName() + " " + sk + ";tn");
addGetAndSet(sb, sk, sv.getClass().getSimpleName());
}
maps.put(sk, sv);
}
return maps;
}
StringBuilder sb = new StringBuilder("rn");
/**
开始
@param root
*/
public void startFileWrite(String root) {
rootName = root;
file = new File(getClass().getResource("").getFile() + "/" + root + ".java");
sb.append(getClass().getPackage() + ";" + "rn");
sb.append("public class " + root + "{" + "rn");
}
/**
结束
*/
public void finishFileWrite() {
try {
FileWriter fw = new FileWriter(file);
sb.append("rn");
sb.append("}");
fw.write(sb.toString());
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void startChildClass(String childName) {
String childClass = childName + "Class";
sb.append("public " + childClass + " " + childName + ";rn");
sb.append("class " + childClass);
sb.append("{" + "rn");
}
public void startChildArrayClass(String childName) {
addLibsImport("import java.util.ArrayList;");
String childClass = childName + "Class";
sb.append("public ArrayList " + childName + ";rn");
sb.append("class " + childClass);
sb.append("{" + "rn");
}
public void stopChildClass() {
sb.append("rn");
sb.append("}");
}
/**
编写Java文件
@param maps
*/
public void writeInJava(HashMap maps) {
Iterator> iterator = maps.entrySet().iterator();
while (iterator.hasNext()) {
LinkedTreeMap.Entry entry = iterator.next();
if (entry.getValue() instanceof LinkedTreeMap) return;
String key = entry.getKey();
String FieldType = entry.getValue().getClass().getSimpleName();
sb.append("rnt");
sb.append("public " + FieldType + " " + key + ";");
sb.append("rnt");
addGetAndSet(sb, key, FieldType);
}
}
public void addGetAndSet(StringBuilder sb, String name, String type) {
sb.append("rnt");
sb.append("public void " + "set" + name);
sb.append("(" + type + " " + name + "1" + ")");
sb.append("{");
sb.append("rntt");
sb.append("this." + name + "=" + name + "1" + ";");
sb.append("rnt");
sb.append("}");
sb.append("rnt");
sb.append("public " + type + " " + "get" + name);
sb.append("(" + ")");
sb.append("{");
sb.append("rntt");
sb.append("return " + name + ";");
sb.append("rnt");
sb.append("}");
sb.append("rn");
}
/**
编译生成的Java文件
*/
public void complie2Class() {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileMgr = compiler.getStandardFileManager(null, null, null);
Iterable units = fileMgr.getJavaFileObjects(file);
JavaCompiler.CompilationTask t = compiler.getTask(null, fileMgr, null, null, null, units);
t.call();
try {
fileMgr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
通过Url加载生成Class文件
@param className
*/
public void loadClass(String className) {
String fileUrl = "file:/" + file.getParent();
System.out.println(fileUrl);
try {
URL[] urls = new URL[]{new URL(fileUrl)};
URLClassLoader ul = new URLClassLoader(urls, ClassLoader.getSystemClassLoader());
Class c = ul.loadClass(getClass().getPackage().getName() + "." + className);
System.out.println(c.newInstance().getClass().getName());
Object o = c.newInstance();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
/**
添加第三方导包
*/
public void addLibsImport(String lib) {
String regex = getClass().getPackage() + ";" + "rn";
if (!sb.toString().contains(lib)) {
sb.insert(sb.indexOf(regex) + regex.length(), "rn" + lib + "rn");
}
}
}
最后
以上就是冷傲钥匙为你收集整理的java动态生成java文件_【Java】动态生成Java代码并编译class文件加载的全部内容,希望文章能够帮你解决java动态生成java文件_【Java】动态生成Java代码并编译class文件加载所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复