当前仓库属于关闭状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 1 Fork 0

Jasen / javaCompiler
关闭

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

compiler

Compile Java code in memory using Java 6 compiler API.

Build Status

Why compiler API?

You can use compiler API to compile generated Java source code and load the compiled classes on-the-fly.

For example, generate proxy classes using compiler API instead of CGLIB or Javassist.

How to use this compiler?

Step 1: add maven dependency:

<dependency>
    <groupId>com.itranswarp</groupId>
    <artifactId>compiler</artifactId>
    <version>1.0</version>
</dependency>

Step 2: compile string and load class:

public class Main {

    public static void main(String[] args) {
        JavaStringCompiler compiler = new JavaStringCompiler();
        Map<String, byte[]> results = compiler.compile("UserProxy.java", JAVA_SOURCE_CODE);
        Class<?> clazz = compiler.loadClass("on.the.fly.UserProxy", results);
        // try instance:
        User user = (User) clazz.newInstance();
    }

    static final String JAVA_SOURCE_CODE = "/* a single java source file */   "
            + "package on.the.fly;                                            "
            + "public class UserProxy extends test.User {                     "
            + "    boolean _dirty = false;                                    "
            + "    public void setId(String id) {                             "
            + "        super.setId(id);                                       "
            + "        setDirty(true);                                        "
            + "    }                                                          "
            + "    public void setName(String name) {                         "
            + "        super.setName(name);                                   "
            + "        setDirty(true);                                        "
            + "    }                                                          "
            + "    public void setCreated(long created) {                     "
            + "        super.setCreated(created);                             "
            + "        setDirty(true);                                        "
            + "    }                                                          "
            + "    public void setDirty(boolean dirty) {                      "
            + "        this._dirty = dirty;                                   "
            + "    }                                                          "
            + "    public boolean isDirty() {                                 "
            + "        return this._dirty;                                    "
            + "    }                                                          "
            + "}                                                              ";
}

空文件

简介

暂无描述 展开 收起
Java
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/gitcrazy/javaCompiler.git
git@gitee.com:gitcrazy/javaCompiler.git
gitcrazy
javaCompiler
javaCompiler
master

搜索帮助