{"id":19017467,"url":"https://github.com/junkdog/constexpr-java","last_synced_at":"2025-04-23T02:48:52.468Z","repository":{"id":148960239,"uuid":"72383559","full_name":"junkdog/constexpr-java","owner":"junkdog","description":"build-time code-execution for java, a bit like constexpr in C++11","archived":false,"fork":false,"pushed_at":"2016-10-31T19:07:03.000Z","size":34,"stargazers_count":57,"open_issues_count":3,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-23T02:48:46.144Z","etag":null,"topics":["constexpr"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/junkdog.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","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,"publiccode":null,"codemeta":null}},"created_at":"2016-10-31T00:10:14.000Z","updated_at":"2025-04-04T13:01:33.000Z","dependencies_parsed_at":null,"dependency_job_id":"d7ae25ff-a10f-4252-a9e4-bbae2ddbb603","html_url":"https://github.com/junkdog/constexpr-java","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/junkdog%2Fconstexpr-java","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/junkdog%2Fconstexpr-java/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/junkdog%2Fconstexpr-java/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/junkdog%2Fconstexpr-java/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/junkdog","download_url":"https://codeload.github.com/junkdog/constexpr-java/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250360249,"owners_count":21417717,"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":["constexpr"],"created_at":"2024-11-08T19:47:07.015Z","updated_at":"2025-04-23T02:48:52.445Z","avatar_url":"https://github.com/junkdog.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"## `@ConstExpr`\n\nSimulates `constexpr` from C++11, build-time code-execution, from\n[cppreference.com][cppref]: \n\n\u003e The constexpr specifier declares that it is possible\n\u003e to evaluate the value of the function or variable at\n\u003e compile time.\n\n\n#### But Why?\n- embed build-time variables directly into class files\n- naive string obfuscation\n- maven's resource filtering can be clunky for certain use-cases \n\n\n## Usage\n\nThe API is restricted to the `@ConstExpr` annotation. The maven plugin does\nits thing using [ASM](http://asm.ow2.org/).\n\n\n#### On Fields\nFields must satisfy the following prerequisiites to be eligible:\n- `static final` modifiers\n- primitive value or string.\n\nThe final value is written to the transformed class. \n\n\n#### On Methods\nIn contrast to C++1x, `@ConstExpr` on static methods turn them into\nbuild-time only methods. Consequently, do not annotate any methods required\nduring runtime.\n\n\n#### Sample\n\n```java\npublic class Exhibit {\n    @ConstExpr // recording the time at build\n    public static final long timestamp = System.currentTimeMillis();\n    \n    @ConstExpr\n    public static final long seed = generateSeed();\n\n    @ConstExpr // this method will be completely removed\n    private static int generateSeed() {\n        String s = \"hellooooo\";\n        int sum = 0;\n        for (char c : s.toCharArray())\n            sum += c;\n        return new Random(sum).nextInt();\n    }\n}\n```\n\n#### Usage: Maven\n\n```xml\n\u003cdependencies\u003e\n    \u003cdependency\u003e\n        \u003cgroupId\u003enet.onedaybeard.constexpr\u003c/groupId\u003e\n        \u003cartifactId\u003econstexpr-api\u003c/artifactId\u003e\n        \u003cversion\u003e0.1.0\u003c/version\u003e\n    \u003c/dependency\u003e\n\u003c/dependencies\u003e\n\n\u003cbuild\u003e\n    \u003cplugins\u003e\n        \u003cplugin\u003e\n            \u003cgroupId\u003enet.onedaybeard.constexpr\u003c/groupId\u003e\n            \u003cartifactId\u003econstexpr-maven-plugin\u003c/artifactId\u003e\n            \u003cversion\u003e0.1.0\u003c/version\u003e\n            \u003cexecutions\u003e\n                \u003cexecution\u003e\n                    \u003cid\u003etransform\u003c/id\u003e\n                    \u003cgoals\u003e\n                        \u003cgoal\u003econstexpr\u003c/goal\u003e\n                    \u003c/goals\u003e\n                \u003c/execution\u003e\n            \u003c/executions\u003e\n            \u003c!-- default configuration --\u003e\n            \u003cconfiguration\u003e\n                \u003c!-- property: constexpr.skip --\u003e\n                \u003cskip\u003efalse\u003c/skip\u003e\n                \u003c!-- property: constexpr.verbose --\u003e\n                \u003cverbose\u003efalse\u003c/verbose\u003e\n            \u003c/configuration\u003e\n        \u003c/plugin\u003e\n    \u003c/plugins\u003e\n\u003c/build\u003e\n```\n\nRunning the plugin should result in output similar to:\n\n```\n[INFO] --- constexpr-maven-plugin:0.1.0:constexpr (transform) @ map ---\n[INFO] Scanned 499 classes ............................................... 86ms\n[INFO] Transformed 1 classes ............................................. 46ms\n[INFO] \n[INFO] @ConstExpr Log\n[INFO] ------------------------------------------------------------------------\n[INFO] s.f.m.u.BuildProperties ....................................... fields:1\n[INFO] ------------------------------------------------------------------------\n[INFO] \n```\n\n#### The Details\n- scan class files for `@ConstExpr` and validate\n- for each annotated type:\n  - remove annotation\n  - resolve the value of each `@ConstExpr` field via reflection\n  - write the result to the field signature\n  - remove old field initialization bytecode from the static initializer\n  - remove any methods annotated with `@ConstExpr`\n- if static initializer is left empty, remove it completely\n- write transformed `byte[]` to source class file\n  - (entries no longer required by the _constant pool_ are cleared)\n\n#### Bytecode: before and after\n- Bytecode disassembly of [PlainString.java][ps-java], [diff view][diff].\n\n\n [cppref]: http://en.cppreference.com/w/cpp/language/constexpr\n [ps-java]: https://github.com/junkdog/constexpr-java/blob/d4ad613fb6dbc8a9b762af8407f0d9963485ae94/core/src/test/java/net/onedaybeard/constexpr/model/PlainString.java\n [diff]: https://github.com/junkdog/constexpr-java/compare/reference-before...reference-after#diff-f98d296c19afdc978656a8813c42be81\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjunkdog%2Fconstexpr-java","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjunkdog%2Fconstexpr-java","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjunkdog%2Fconstexpr-java/lists"}