{"id":13694773,"url":"https://github.com/edefazio/varcode","last_synced_at":"2025-05-03T04:30:54.636Z","repository":{"id":217159799,"uuid":"67149267","full_name":"edefazio/varcode","owner":"edefazio","description":"Generate, compile and run .java source dynamically at runtime","archived":false,"fork":false,"pushed_at":"2019-04-23T01:09:03.000Z","size":2148,"stargazers_count":11,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-12T21:39:12.211Z","etag":null,"topics":["api","code","generator","java-source","metaprogramming","runtime"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/edefazio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2016-09-01T16:44:30.000Z","updated_at":"2022-12-21T02:47:11.000Z","dependencies_parsed_at":null,"dependency_job_id":"4cfc48b6-f3e9-417b-a39c-b7baf60ca33e","html_url":"https://github.com/edefazio/varcode","commit_stats":null,"previous_names":["edefazio/varcode"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edefazio%2Fvarcode","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edefazio%2Fvarcode/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edefazio%2Fvarcode/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edefazio%2Fvarcode/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/edefazio","download_url":"https://codeload.github.com/edefazio/varcode/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252144468,"owners_count":21701418,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","code","generator","java-source","metaprogramming","runtime"],"created_at":"2024-08-02T17:01:41.694Z","updated_at":"2025-05-03T04:30:51.975Z","avatar_url":"https://github.com/edefazio.png","language":"Java","funding_links":[],"categories":["Java"],"sub_categories":[],"readme":"\u003cimg src=\"https://github.com/edefazio/varcode/blob/master/varcode_greenOnWhite.png?raw=true\" width=\"60\"/\u003e\ncombines a **code generator** and **ad-hoc tools** to **compile, load, and run .java source code at runtime**.  \n\n## generate / run / export .java code at runtime ##\n```java\n//1 generate a model\n_class _model = _class.of( \"package mymodel;\", \n    \"public class Model\" )\n    .imports( UUID.class )\n    .method( \"public String createId()\",\n        \"return UUID.randomUUID().toString();\" );\n// 2) create instance        \nObject dynamicModel = _model.instance();        \n\n// 3) call a method on the dynamic instance\nString id1 = (String)Java.call( dynamicModel, \"createId\" ); \n\n// 4) export .java \u0026 .class files:\n//    export \"C:\\MyApp\\src\\main\\java\\mymodel\\Model.java\"\nExport.dir( \"C:\\\\MyApp\\\\src\\\\main\\\\java\\\\\").toFile( _model );\n//    export \"C:\\MyApp\\traget\\classes\\mymodel\\Model.class\"\nExport.dir( \"C:\\\\MyApp\\\\target\\\\classes\\\\\").toFile( dynamicModel.getClass() );\n```\n\n...```Export( \"C:\\\\MyApp\\\\src\\\\main\\\\java\\\\\").toFile( _model );``` will create the file ```C:\\\\myapp\\\\src\\\\main\\\\java\\\\mymodel\\\\Model.java``` containing:\n```java\npackage mymodel;\nimport java.util.UUID;\n\npublic class Model\n{\n    public String createId(  )\n    {\n        return UUID.randomUUID().toString();\n    }\n}\n```\n\n## metaprogramming : read in existing code, modify it \u0026 run it ##\nvarcode makes **metaprogramming** easy. **load a  ```( _class, _enum, _interface, _annotationType )```** from an existing class, modify it at the source level, then compile \u0026 use it at runtime (no restarting required). \n\n```java\n// 1. build the _class model from the .java source of the Class\n_class _c = Java._classFrom( OriginalClass.class ); \n\n// 2. modify the model\n_c.setName(\"Tailored\");// change the class Name\n_c.field(\"private static final int ID = 100;\"); // add field\n\n// get and modify the \"toString\" method\n_c.getMethod(\"toString\").body( \"return getClass().getSimpleName() + ID;\")\n\n// 3. compile, instantiate and use it\nObject tailored = _c.instance(); // create a new instance of \"Tailored\"\nSystem.out.println( tailored );  //prints \"Tailored100\"\n```\n\n## construct classes step by step ##\nclasses for ( [\\_class](https://gist.github.com/edefazio/b491989cd6ef72ad7ea2bc0005895c81), [\\_interface](https://gist.github.com/edefazio/adbbd9cd500617d3202b2a2a3c7ebf68), [\\_enum](https://gist.github.com/edefazio/0e566868ab5f134720cfde6db24b9b11), [\\_annotationType](https://gist.github.com/edefazio/f1bed02ff66524149c215311c6d6f356) ) can be built in a single compound statement or incrementally using simple mutator methods. \n```java \n_class _c = _class.of( \"package ex.mutable;\",\n    _imports.of( Serializable.class ),\n    \"@Deprecated\",     \n    \"public class MyMutableModel implements Serializable\" );\n    \n_method _m = _method.of( \"/** create a random number */\", \n    \"@Generated\",\n    \"public static final double random()\", \n    \"return Math.random();\" );\n    \n_c.method( _m );   //add the \"random\" method to the _class  \n    \n_field _f = _field.of( \"/** field javadoc */\", \n    \"public static int ID = 100;\"); \n    \n_c.add( _f ); //add the \"ID\" field to the _class\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedefazio%2Fvarcode","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fedefazio%2Fvarcode","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedefazio%2Fvarcode/lists"}