
正文
文字生成java代码 java 文字生成图片
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
如何将字符串转换为java代码
Java实现字符串转换成可执行代码
使用commons的jexl可实现将字符串变成可执行代码的功能文字生成java代码,我写文字生成java代码了一个类来封装这个功能文字生成java代码:
import java.util.Map;
import org.apache.commons.jexl2.Expression;
import org.apache.commons.jexl2.JexlContext;
import org.apache.commons.jexl2.JexlEngine;
import org.apache.commons.jexl2.MapContext;
/**
* 动态加载方法
*
*/
public class DyMethodUtil {
public static Object invokeMethod(String jexlExp,MapString,Object map){
JexlEngine jexl=new JexlEngine();
Expression e = jexl.createExpression(jexlExp);
JexlContext jc = new MapContext();
for(String key:map.keySet()){
jc.set(key, map.get(key));
}
if(null==e.evaluate(jc)){
return "";
}
return e.evaluate(jc);
}
}
调用
MapString,Object map=new HashMapString,Object();
map.put("testService",testService);
map.put("person",person);
String expression="testService.save(person)";
DyMethodUtil.invokeMethod(expression,map);
相关问答
Q1: java字符串如何解析成能运行的java代码?
java字符串如何解析成运行的java代码
有些情况下,不得不动态运行Java代码,以便提供更加灵活的方式,以下代码可参考(在JDK 1.5+平台上运行通过):
public static void main(String[] args) {
int i = 10;
String code = "System.out.println(\"Hello World!\"+(13+2*5/3));";
code += "for(int i=0;i" + i + ";i++){";
code += " System.out.println(Math.pow(i,2));";
code += "}";
try {
run(code);
} catch (Exception e) {
e.printStackTrace();
}
}
private synchronized static File compile(String code) throws Exception {
File file = File.createTempFile("JavaRuntime", ".java", new File(System.getProperty("user.dir")));
file.deleteOnExit();
// 获得类名
String classname = getBaseFileName(file);
// 将代码输出到文件
PrintWriter out = new PrintWriter(new FileOutputStream(file));
out.println(getClassCode(code, classname));
out.close();
// 编译生成的java文件
String[] cpargs = new String[] { "-d",
System.getProperty("user.dir") + "\\WebRoot\\WEB-INF\\classes",
file.getName() };
int status = Main.compile(cpargs);
if (status != 0) {
throw new Exception("语法错误!");
}
return file;
}
private static synchronized void run(String code) throws Exception {
String classname = getBaseFileName(compile(code));
new File(System.getProperty("user.dir")
+ "\\WebRoot\\WEB-INF\\classes\\" + classname + ".class")
.deleteOnExit();
try {
Class cls = Class.forName(classname);
Method main = cls.getMethod("method", null);
main.invoke(cls, null);
} catch (Exception se) {
se.printStackTrace();
}
}
private static String getClassCode(String code, String className) {
StringBuffer text = new StringBuffer();
text.append("public class " + className + "{\n");
text.append(" public static void method(){\n");
text.append(" " + code + "\n");
text.append(" }\n");
text.append("}");
return text.toString();
}
private static String getBaseFileName(File file) {
String fileName = file.getName();
int index = fileName.indexOf(".");
String result = "";
if (index != -1) {
result = fileName.substring(0, index);
} else {
result = fileName;
}
return result;
}
Q2: Java随机生成字符串
代码如下,供参考:
import java.util.Random;
public class Main {
public static void main(String[] args) {
char[] chs = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
String str = new Main().getStr(chs);
System.out.println(str);
}
public String getStr(char[] chs) {
String str = "";
Random random = new Random();
for (int i = 0; i 4; i++) {
// 这种写法易于扩展,chs内容改了不用修改代码
str += chs[random.nextInt(chs.length)];
}
str += random.nextInt(10);
return str;
}
}
文字生成java代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java 文字生成图片、文字生成java代码的信息别忘了在本站进行查找喔。







