{"id":17044006,"url":"https://github.com/sbosley/java-traits","last_synced_at":"2025-04-30T08:46:26.180Z","repository":{"id":12700544,"uuid":"15372955","full_name":"sbosley/java-traits","owner":"sbosley","description":"Bringing traits to Java using compile-time code generation","archived":false,"fork":false,"pushed_at":"2016-11-11T11:24:07.000Z","size":3569,"stargazers_count":6,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-15T09:31:41.567Z","etag":null,"topics":[],"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/sbosley.png","metadata":{"files":{"readme":"README.md","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":"2013-12-22T09:10:22.000Z","updated_at":"2023-08-22T09:18:23.000Z","dependencies_parsed_at":"2022-09-23T09:12:22.018Z","dependency_job_id":null,"html_url":"https://github.com/sbosley/java-traits","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sbosley%2Fjava-traits","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sbosley%2Fjava-traits/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sbosley%2Fjava-traits/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sbosley%2Fjava-traits/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sbosley","download_url":"https://codeload.github.com/sbosley/java-traits/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227188205,"owners_count":17744880,"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":[],"created_at":"2024-10-14T09:32:14.178Z","updated_at":"2024-11-29T18:26:19.646Z","avatar_url":"https://github.com/sbosley.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"java-traits\n===========\n\nThe aim of this project is to bring the language feature of [Traits](http://en.wikipedia.org/wiki/Trait_%28computer_programming%29) to Java using compile-time annotation processing.\n\nFor those who want the short explanation, traits are like a combination of a Java interface and an abstract class. They're a useful mechanism for reusing shared logic somewhat akin to multiple inheritance.\n\n## Defining traits\nFor the purposes of this project, traits are defined as abstract classes with an `@Trait` annotation:\n\n```java\n@Trait\npublic abstract class Rectangular {\n    public abstract int getWidth();\n    public abstract int getHeight();\n\n    public int getPerimeter() {\n        return 2 * (getWidth() + getHeight());\n    }\n\n    public int getArea() {\n        return (getWidth() * getHeight());\n    }\n\n    public boolean isSquare() {\n        return (getWidth() == getHeight());\n    }\n}\n```\nA trait implicitly defines an interface of all its public declared methods:\n\n```java\npublic interface IRectangular {\n    public int getWidth();\n    public int getHeight();\n    public int getPerimeter();\n    public int getArea();\n    public boolean isSquare();\n}\n```\n\nThe code generator will take care of generating this interface for you. A class that has the `Rectangular` trait will implement `IRectangular` while guaranteeing that the implementation of the concrete methods `getArea()` etc. will be identical to the implementations declared in `Rectangular`.\n\n## Using traits\n\n### Declaring that a class has traits\nTo declare that one of your classes uses a trait you use the `@HasTraits` annotation:\n\n```java\n@HasTraits(traits={Rectangular.class})\npublic class FootballField extends FootballFieldWithTraits {\n    ...\n}\n```\n\nTake note that if your class uses the `@HasTraits` annotation, it MUST extend from a generated class with the suffix \"WithTraits\", e.g. `FootballFieldWithTraits`. If you don't extend from the generated class with the correct name, none of the traits will be applied. \"But what if I need to subclass some other thing?\" you say? Simply specify your desired superclass in the `@HasTraits` annotation:\n\n```java\n@HasTraits(traits={Rectangular.class}, desiredSuperclass=SportsField.class)\npublic class FootballField extends FootballFieldWithTraits {\n    ...\n}\n```\n\nThe code generator will ensure that `FootballFieldWithTraits` extends `SportsField`. It will also generate the interface definitions implicitly defined by each trait as in the above example and ensure that the generated superclass explicitly implements all the required interfaces. For example, `FootballFieldWithTraits` will look something like this:\n\n```java\npublic abstract class FootballFieldWithTraits extends SportsField implements IRectangular {\n    ....\n}\n```\n\n### Implement details\nThere only thing you are still responsible for is to implement all the abstract methods declared in all the traits your class is using -- the code generator does NOT implement those for you!\n\nA complete class definition using traits would look something like this:\n\n```java\n@HasTraits(traits={Rectangular.class}, desiredSuperclass=SportsField.class)\npublic class FootballField extends FootballFieldWithTraits {\n    public int getWidth() {\n        return 160;\n    }\n    public int getHeight() {\n        return 320;\n    }\n}\n```\n\nThe `FootballField` class is guaranteed to implement the rest of `IRectangular` for you as defined in the trait, and is guaranteed to still be a subclass of `SportsField` -- the code generation takes care of all those details for you.\n\nRemember, you can declare that a class has multiple traits! Just declare them in a comma-separated list, e.g. `@HasTraits(traits={Rectangular.class, Resizeable.class})`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsbosley%2Fjava-traits","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsbosley%2Fjava-traits","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsbosley%2Fjava-traits/lists"}