{"id":36419642,"url":"https://github.com/kasparthommen/java-code-gen","last_synced_at":"2026-01-11T17:05:12.440Z","repository":{"id":57733795,"uuid":"263327467","full_name":"kasparthommen/java-code-gen","owner":"kasparthommen","description":"Annotation-based Java code generation","archived":false,"fork":false,"pushed_at":"2023-05-02T19:50:26.000Z","size":134,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-30T18:06:16.935Z","etag":null,"topics":["annotation","annotation-processor","code-generation","concrete-types","generic","generics","instantiation","java","parametric-polymorphism","primitives","reification","template","template-engine"],"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/kasparthommen.png","metadata":{"files":{"readme":"README.adoc","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-05-12T12:17:15.000Z","updated_at":"2022-05-14T17:27:46.000Z","dependencies_parsed_at":"2022-08-24T11:20:25.227Z","dependency_job_id":null,"html_url":"https://github.com/kasparthommen/java-code-gen","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/kasparthommen/java-code-gen","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kasparthommen%2Fjava-code-gen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kasparthommen%2Fjava-code-gen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kasparthommen%2Fjava-code-gen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kasparthommen%2Fjava-code-gen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kasparthommen","download_url":"https://codeload.github.com/kasparthommen/java-code-gen/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kasparthommen%2Fjava-code-gen/sbom","scorecard":{"id":551213,"data":{"date":"2025-08-11","repo":{"name":"github.com/kasparthommen/java-code-gen","commit":"6b3d94ece744e0c920eb8046b64008df818715ae"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":4.9,"checks":[{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Code-Review","score":0,"reason":"Found 0/30 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Dangerous-Workflow","score":10,"reason":"no dangerous workflow patterns detected","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Pinned-Dependencies","score":0,"reason":"dependency not pinned by hash detected -- score normalized to 0","details":["Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:43: update your workflow using https://app.stepsecurity.io/secureworkflow/kasparthommen/java-code-gen/codeql-analysis.yml/master?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:44: update your workflow using https://app.stepsecurity.io/secureworkflow/kasparthommen/java-code-gen/codeql-analysis.yml/master?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:51: update your workflow using https://app.stepsecurity.io/secureworkflow/kasparthommen/java-code-gen/codeql-analysis.yml/master?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:62: update your workflow using https://app.stepsecurity.io/secureworkflow/kasparthommen/java-code-gen/codeql-analysis.yml/master?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:76: update your workflow using https://app.stepsecurity.io/secureworkflow/kasparthommen/java-code-gen/codeql-analysis.yml/master?enable=pin","Info:   0 out of   5 GitHub-owned GitHubAction dependencies pinned"],"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Token-Permissions","score":9,"reason":"detected GitHub workflow tokens with excessive permissions","details":["Info: jobLevel 'actions' permission set to 'read': .github/workflows/codeql-analysis.yml:28","Info: jobLevel 'contents' permission set to 'read': .github/workflows/codeql-analysis.yml:29","Warn: no topLevel permission defined: .github/workflows/codeql-analysis.yml:1","Info: no jobLevel write permissions found"],"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"SAST","score":10,"reason":"SAST tool detected: CodeQL","details":["Info: SAST configuration detected: CodeQL","Warn: no pull requests merged into dev branch"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: Apache License 2.0: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-20T10:55:41.110Z","repository_id":57733795,"created_at":"2025-08-20T10:55:41.110Z","updated_at":"2025-08-20T10:55:41.110Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28314264,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-11T14:58:17.114Z","status":"ssl_error","status_checked_at":"2026-01-11T14:55:53.580Z","response_time":60,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["annotation","annotation-processor","code-generation","concrete-types","generic","generics","instantiation","java","parametric-polymorphism","primitives","reification","template","template-engine"],"created_at":"2026-01-11T17:05:12.376Z","updated_at":"2026-01-11T17:05:12.435Z","avatar_url":"https://github.com/kasparthommen.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"= Java Code Generator Annotations\n\nimage:https://maven-badges.herokuapp.com/maven-central/io.github.kasparthommen.codegen/java-code-gen/badge.svg[link=\"https://maven-badges.herokuapp.com/maven-central/io.github.kasparthommen.codegen/java-code-gen\"]\n\n:INSTANTIATE:       pass:quotes[link:java-code-gen/src/main/java/com/kt/codegen/Instantiate.java[`@Instantiate`]]\n:DERIVE:            pass:quotes[link:java-code-gen/src/main/java/com/kt/codegen/Derive.java[`@Derive`]]\n:REPLACE:           pass:quotes[link:java-code-gen/src/main/java/com/kt/codegen/Replace.java[`@Replace`]]\n:SOURCE_DIRECTORY:  pass:quotes[link:java-code-gen/src/main/java/com/kt/codegen/SourceDirectory.java[`@SourceDirectory`]]\n:SRC_DIR:           ../main/java/com/kt/codegen/demo\n:GEN_DIR:           ../../target/generated-sources/annotations/com/kt/codegen/demo\n\n\n== TL;DR\n=== What does this library provide?\nThis library provides the following code-generating annotations:\n\n* {INSTANTIATE} generates concrete instantiations of generic classes\n  analogous to C++ templates:\n\n[cols=\"1a\"]\n|===\n|\n[source,java]\n----\n/* you write... */\n@Instantiate(String.class)\nclass MyList\u003cT\u003e {\n    private T[] array;\n    // ...\n}\n----\n|\n[source,java]\n----\n/* ... and you'll get */\nclass MyListString {\n    private String[] array;\n    // ...\n}\n----\n|===\n\n* {DERIVE} generates new classes from existing ones by applying\n  string or regex replacements to the source code:\n\n[cols=\"1a\"]\n|===\n|\n[source,java]\n----\n/* you write... */\n@Derive(name = \"MyFloatList\", replace = @Replace(from = \"double\", to = \"float\"))\nclass MyDoubleList {\n    private double[] array;\n    // ...\n}\n----\n|\n[source,java]\n----\n/* ... and you'll get */\nclass MyFloatList {\n    private float[] array;\n    // ...\n}\n----\n|===\n\n\n=== Why should I use it?\nThe main advantage of this library over generic template engines such as\nlink:https://www.stringtemplate.org/[StringTemplate],\nlink:https://velocity.apache.org/[Velocity]\nor\nlink:https://freemarker.apache.org/[FreeMaker] is:\n\n*Your template is actual code!*\n\nThus, instead of having to write a placeholder-sprinkled, engine-specific\ntemplate file, your \"template\" is a normal Java class (with annotations).\nThe benefits are as follows:\n\n  ** The \"template\" is source code rather than a resource file\n  ** The \"template\" can be unit tested\n  ** The \"template\" enjoys IDE syntax highlighting - no template\n     engine-specific plugins required\n  ** The \"template\" can be auto-formatted, linted and refactored by your IDE\n\n\n== Instantiate generic classes with {INSTANTIATE}\n\n=== Motivation\n\nConsider the following generic class (which, of course, would require a lot more work\nbefore it's a reasonable list implementation):\n[source,java]\n----\npackage com.kt.codegen.demo.list1;\n\nclass MyList\u003cT\u003e {\n    private T[] array;\n\n    MyList(int size) {\n        this.array = (T[]) new Object[size];\n    }\n\n    T get(int index) {\n        return array[index];\n    }\n}\n----\n\nYou can annotate it with {INSTANTIATE} to e.g. create a concrete String instantiation,\nanalogous to C++ templates:\n[source,java]\n----\npackage com.kt.codegen.demo.list2;\n\nimport com.kt.codegen.Instantiate;\n\n@Instantiate(String.class)\nclass MyList\u003cT\u003e {\n    private T[] array;\n\n    MyList(int size) {\n        this.array = (T[]) new Object[size];\n    }\n\n    T get(int index) {\n        return array[index];\n    }\n}\n----\n\nThis will generate the following class:\n[source,java]\n----\n// generated from com.kt.codegen.demo.list2.MyList\npackage com.kt.codegen.demo.list2;\n\nclass MyListString {\n    private String[] array;\n\n    MyListString(int size) {\n        this.array = (String[]) new Object[size];\n    }\n\n    String get(int index) {\n        return array[index];\n    }\n}\n----\n\nNice, but the annotation processor only operates on a source code level and simply\nreplaces occurrences of `T` with `String`. This leads to a guaranteed class cast\nexception in the expression `(String[]) new Object[size]`. Can we fix this? Yes, with custom\nstring replacements, see below.\n\n\n=== Custom String Replacements\nSimply replacing a generic type with a concrete type like we just did doesn't usually\nget us all the way, but fret not, there are custom string replacements:\n[source,java]\n----\npackage com.kt.codegen.demo.list3;\n\nimport com.kt.codegen.Instantiate;\nimport com.kt.codegen.Replace;\n\n@Instantiate(value = String.class,\n             replace = @Replace(from = \"(T[]) new Object[size]\", to = \"new String[size]\"))\nclass MyList\u003cT\u003e {\n    private T[] array;\n\n    MyList(int size) {\n        this.array = (T[]) new Object[size];\n    }\n\n    T get(int index) {\n        return array[index];\n    }\n}\n----\n\nNow the generated string list is safe:\n[source,java]\n----\n// generated from com.kt.codegen.demo.list3.MyList\npackage com.kt.codegen.demo.list3;\n\nclass MyListString {\n    private String[] array;\n\n    MyListString(int size) {\n        this.array = new String[size];\n    }\n\n    String get(int index) {\n        return array[index];\n    }\n}\n----\n\n\n=== Primitives\nHow about adding a primitive version of our list? Simple: just add a `double` instantiation:\n[source,java]\n----\npackage com.kt.codegen.demo.list4;\n\nimport com.kt.codegen.Instantiate;\nimport com.kt.codegen.Replace;\n\n@Instantiate(value = String.class,\n             replace = @Replace(from = \"(T[]) new Object[size]\", to = \"new String[size]\"))\n@Instantiate(value = double.class,\n             replace = @Replace(from = \"(T[]) new Object[size]\", to = \"new double[size]\"))\nclass MyList\u003cT\u003e {\n    private T[] array;\n\n    MyList(int size) {\n        this.array = (T[]) new Object[size];\n    }\n\n    T get(int index) {\n        return array[index];\n    }\n}\n----\n\nThis will additionally geenrate the following class:\n[source,java]\n----\n// generated from com.kt.codegen.demo.list4.MyList\npackage com.kt.codegen.demo.list4;\n\nclass MyListDouble {\n    private double[] array;\n\n    MyListDouble(int size) {\n        this.array = new double[size];\n    }\n\n    double get(int index) {\n        return array[index];\n    }\n}\n----\n\nNote that the class is called `MyListDouble` instead of `MyListdouble` (note the\ndifferent case of the \"d\") to make the two types  explicit in the class name.\n\n\n=== Multiple Type Parameters\nIf your generic class has more than one type parameter then you'll simply have to provide\nthe necessary number of concrete types for each instantiation:\n[source,java]\n----\npackage com.kt.codegen.demo.map;\n\nimport com.kt.codegen.Instantiate;\n\nimport java.time.Instant;\n\n@Instantiate({String.class, Instant.class})  // \u003c-- two concrete types\nclass MyMap\u003cK, V\u003e {                          // \u003c-- two type parameters\n    private K[] keys;\n    private V[] values;\n\n    // ...\n}\n----\n\n\n=== Notes\n* For projects that don't follow the Maven directory layout you can specify the relative\n  source directory with {SOURCE_DIRECTORY} on the source class.\n* If normal string replacement won't cut it, you can set `{REPLACE}.regex` to `true`.\n* You can specify multiple replacements with\n`replace = {@Replace(...), @Replace(...), ...}`.\n* I you prefer prepending the concrete type(s) to the class rather than the default\nappending style (i.e., `StringMyList` rather than `MyListString`) then set\n`{INSTANTIATE}.append` to `false`.\n\n\n== Generate derived classes with {DERIVE}\nSay you are working on a primitive collection library. You have just finished writing\na `double` list implementation:\n[source,java]\n----\npackage com.kt.codegen.demo.double1;\n\npublic class MyDoubleList {\n    private double[] array;\n\n    MyDoubleList(int size) {\n        this.array = new double[size];\n    }\n\n    // ...\n}\n----\n\nNow you have a couple of options to create lists for other primitive types:\n\n. You copy and paste the class a couple of times followed by a search/replace frenzy. This\nis cumbersome, time-consuming, and will eventually lead to implementations drifting\napart because you'll forget to apply that one fix to the `float` implementation.\n\n. You fire up a generic template engine, convert this nice, working, unit-tested,\nsyntax-highlighted, auto-formatted, error-checked class into a template text\nfile that immediately loses all those nice properties, and you start configuring\nthe template engine.\n\n. Or you annotate the class as follows:\n\n[source,java]\n----\npackage com.kt.codegen.demo.double2;\n\nimport com.kt.codegen.Derive;\nimport com.kt.codegen.Replace;\n\n@Derive(name = \"MyFloatList\", replace = @Replace(from = \"\\\\bdouble\\\\b\", to = \"float\", regex = true))\n@Derive(name = \"MyLongList\", replace = @Replace(from = \"\\\\bdouble\\\\b\", to = \"long\", regex = true))\npublic class MyDoubleList {\n    private double[] array;\n\n    MyDoubleList(int size) {\n        this.array = new double[size];\n    }\n\n    // ...\n}\n----\n\nThis will generate two derived classes:\n[source,java]\n----\n// generated from com.kt.codegen.demo.double2.MyDoubleList\npackage com.kt.codegen.demo.double2;\n\npublic class MyFloatList {\n    private float[] array;\n\n    MyFloatList(int size) {\n        this.array = new float[size];\n    }\n\n    // ...\n}\n----\n\nAnd:\n\n[source,java]\n----\n// generated from com.kt.codegen.demo.double2.MyDoubleList\npackage com.kt.codegen.demo.double2;\n\npublic class MyLongList {\n    private long[] array;\n\n    MyLongList(int size) {\n        this.array = new long[size];\n    }\n\n    // ...\n}\n----\n\n=== Notes\n\n* The relative source directory can also be changed using {SOURCE_DIRECTORY}.\n* Custom string replacements can be specified in `{DERIVE}.replace`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkasparthommen%2Fjava-code-gen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkasparthommen%2Fjava-code-gen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkasparthommen%2Fjava-code-gen/lists"}