JAVA中动态编译的简单使用

一、引用库

pom文件中申明如下:

    <dependencies><!-- https://mvnrepository.com/artifact/junit/junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/com.github.henryhuang/dynamiccompiler --><dependency><groupId>com.github.henryhuang</groupId><artifactId>dynamiccompiler</artifactId><version>0.1.0</version></dependency></dependencies>

 

二、测试代码

import com.github.henryhuang.dynamiccompiler.ClassGenerator;
import org.junit.Test;import java.lang.reflect.Method;public class HelloTest {@Testpublic void test() {ClassGenerator builder = new ClassGenerator(".");try {Class<?> testclass = builder.generate("TestClass", "" +"public class TestClass{" +"   public static String execute(String[] args){" +"       System.out.println(\"Test!\"); " +"       return args[0];" +"   }" +"}");Method method = testclass.getMethod("execute", String[].class);for (int i = 0; i < 100; i++) {String[] params = {"宋柱柱" + i, "def"};Object result = method.invoke(null, (Object) params);System.out.println("result:" + result);}} catch (Exception ex) {ex.printStackTrace();}}
}

 

三、运行结果

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390647.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

程序员实用小程序_我从阅读《实用程序员》中学到了什么

程序员实用小程序In short: old but gold.简而言之&#xff1a;古老而又黄金。 Published in 1999, The Pragmatic Programmer is a book about how to become a Pragmatic Programmer. Which really means a ‘Good Programmer’. 《实用程序员》于1999年出版&#xff0c;是一…

leetcode 5786. 可移除字符的最大数目(二分法)

题目 给你两个字符串 s 和 p &#xff0c;其中 p 是 s 的一个 子序列 。同时&#xff0c;给你一个元素 互不相同 且下标 从 0 开始 计数的整数数组 removable &#xff0c;该数组是 s 中下标的一个子集&#xff08;s 的下标也 从 0 开始 计数&#xff09;。 请你找出一个整数…

如何使用Picterra的地理空间平台分析卫星图像

From April-May 2020, Sentinel-Hub had organized the third edition of their custom script competition. The competition was organized in collaboration with the Copernicus EU Earth Observation programme, the European Space Agency and AI4EO consortium.从2020年…

df -l查看本地文件系统

df -l, --locallimit listing to local file systems 转载于:https://www.cnblogs.com/jonathanyue/p/9301222.html

在Packet Tracer中路由器静态路由配置

实验目标&#xff1a;<1>掌握静态路由的配置方法和技巧<2>掌握通过静态路由方式实现网络的连通性<3>熟悉广域网线缆的链接方式技术原理&#xff1a;<1>路由器属于网络层设备&#xff0c;能够根据IP包头的信息&#xff0c;选择一条最佳路径&#xff0c;…

python示例_带有示例的Python功能指南

python示例Python函数简介 (Introduction to Functions in Python) A function allows you to define a reusable block of code that can be executed many times within your program.函数允许您定义一个可重用的代码块&#xff0c;该代码块可以在程序中多次执行。 Function…

leetcode 852. 山脉数组的峰顶索引(二分查找)

题目 符合下列属性的数组 arr 称为 山脉数组 &#xff1a; arr.length > 3 存在 i&#xff08;0 < i < arr.length - 1&#xff09;使得&#xff1a; arr[0] < arr[1] < … arr[i-1] < arr[i] arr[i] > arr[i1] > … > arr[arr.length - 1] 给你由…

hopper_如何利用卫星收集的遥感数据轻松对蚱hopper中的站点进行建模

hopper建筑学与数据科学 (Architectonics and Data Science) Understanding the site and topography are crucial first step of any architectural project. Site modelling can become very daunting, expensive, or just cumbersome, often having to use various software…

Git 仓库代码迁移步骤记录

迁移远程仓库 // 克隆旧仓库镜像 git clone --mirror [oldRepoUrl]// 添加新仓库地址 cd the_repo git remote add [remoteName] [newRepoUrl]// 推到新的远程库 git push -f --tags [remoteName] refs/heads/*:refs/heads/* 复制代码中括号中的名称需根据自己项目需求替换 更新…

TensorFlow MNIST 入门 代码

其实就是按照TensorFlow中文教程的内容一步步跟着敲的代码。 不过在运行代码的时候遇到代码中加载不到MNIST数据资源&#xff0c;似乎是被墙了&#xff08;(⊙﹏⊙)b&#xff09; 于是自己手动下载了数据包&#xff0c;放到 MNIST_data/ 文件夹下&#xff0c;代码就能正常运转了…

JavaScript中的基本表单验证

In the past, form validation would occur on the server, after a person had already entered in all of their information and pressed the submit button. 过去&#xff0c;表单验证会在一个人已经输入了所有信息并按下“提交”按钮之后在服务器上进行。 If the informa…

leetcode 877. 石子游戏(dp)

题目 亚历克斯和李用几堆石子在做游戏。偶数堆石子排成一行&#xff0c;每堆都有正整数颗石子 piles[i] 。 游戏以谁手中的石子最多来决出胜负。石子的总数是奇数&#xff0c;所以没有平局。 亚历克斯和李轮流进行&#xff0c;亚历克斯先开始。 每回合&#xff0c;玩家从行的…

es6的Map()构造函数

普通的object对象是键值对的集合&#xff0c;但对于它的键却有着严苛的要求&#xff0c;必须是字符串&#xff0c;这给我们平时带来很多的不方便 Map函数类似于对象&#xff0c;但它是一个更加完美的简直对集合&#xff0c;键可以是任意类型 set()方法可以向map实例对象中添加一…

mac里打开隐藏的 library文件夹

打开Finder&#xff0c;单击【前往】&#xff0c;此时只有按住【option】键&#xff0c;就能出现“资源库”的选项。 或者键入 ~/Library 进入 转载于:https://www.cnblogs.com/laolinghunWbfullstack/p/8888124.html

华为开源构建工具_为什么我构建了用于大数据测试和质量控制的开源工具

华为开源构建工具I’ve developed an open-source data testing and a quality tool called data-flare. It aims to help data engineers and data scientists assure the data quality of large datasets using Spark. In this post I’ll share why I wrote this tool, why …

字号与磅值对应关系_终极版式指南:磅值,大写与小写,Em和En破折号等

字号与磅值对应关系Typography is a field that deals with the written word and how letters and characters are presented.印刷术是处理文字以及字母和字符的显示方式的领域。 The same letters can be styled in different ways to convey different emotions. And there…

leetcode 65. 有效数字(正则表达式)

题目 有效数字&#xff08;按顺序&#xff09;可以分成以下几个部分&#xff1a; 一个 小数 或者 整数 &#xff08;可选&#xff09;一个 ‘e’ 或 ‘E’ &#xff0c;后面跟着一个 整数 小数&#xff08;按顺序&#xff09;可以分成以下几个部分&#xff1a; &#xff08;…

Swift中的闭包例子

常见的实现&#xff0c; 要熟悉了解&#xff0c; 至于闭包逃逸&#xff0c; 自动闭包这些内容&#xff0c; 可以以后用到时再学吧。 let names ["Chris", "Alex", "Eva", "Barry", "Daniella"]func backward(_ s1: String,…

如何判断自己的编程水平

有的朋友说&#xff1a;当一段时间后的你&#xff0c;再重新看回以前写的代码&#xff0c;会觉得很渣&#xff0c;就证明你有学到新东西了。转载于:https://www.cnblogs.com/viplued/p/7943405.html

数据科学项目_完整的数据科学组合项目

数据科学项目In this article, I would like to showcase what might be my simplest data science project ever.在本文中&#xff0c;我想展示一下有史以来最简单的数据科学项目 。 I have spent hours training a much more complex models in the past, and struggled to …