michaelliao/compiler

repository·master·Indexed 18 days ago

https://github.com/michaelliao/compiler

A utility for compiling Java source code in memory using the Java 6 compiler API. It provides the JavaStringCompiler class to compile source strings and load resulting classes on-the-fly without writing files to disk, serving as a lightweight alternative to CGLIB or Javassist for proxy generation.

Tokens
1.4K
Snippets
2
Records
2
Agent score
14%

What's inside compiler

  1. Compile Java source strings and load classes in memory

    master

    You can use JavaStringCompiler to compile Java source code provided as a String and load the resulting classes on-the-fly without writing files to disk. This is useful for generating proxy classes or executing dynamically generated code.

    1. Instantiate JavaStringCompiler.
    2. Call compile(String fileName, String sourceCode) to get a Map<String, byte[]> containing the class names and their corresponding bytecode.
    3. Call loadClass(String className, Map<String, byte[]> results) to load the class into the JVM.
    4. Use reflection (e.g., newInstance()) to create an instance of the loaded 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) {                      "
                + "        super.setDirty(dirty);                                  "
                + "    }                                                          "
                + "    public boolean isDirty() {                                 "
                + "        return this._dirty;                                    "
                + "    }                                                          "
                + "}                                                              ";
    }