Skip to content

Commit

Permalink
Remove boilerplate from annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
bowbahdoe committed Apr 24, 2024
1 parent a7c97ed commit 3e9c704
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- name: Set up Maven Central Repository
uses: actions/setup-java@v2
with:
java-version: '17'
java-version: '21'
distribution: 'adopt'
server-id: ossrh
server-username: MAVEN_USERNAME
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

A very basic library which will generate POJOs.

Requires Java 17+.
Requires Java 21+.

### Maven

```xml
<dependency>
<groupId>dev.mccue</groupId>
<artifactId>magic-bean</artifactId>
<version>3.3.0</version>
<version>2024.04.24</version>
<scope>provided</scope>
</dependency>
```
Expand All @@ -21,8 +21,8 @@ Requires Java 17+.

```
dependencies {
compileOnly("dev.mccue:magic-bean:3.3.0")
annotationProcessor("dev.mccue:magic-bean:3.3.0")
compileOnly("dev.mccue:magic-bean:2024.04.24")
annotationProcessor("dev.mccue:magic-bean:2024.04.24")
}
```

Expand Down Expand Up @@ -124,9 +124,9 @@ import dev.mccue.magicbean.MagicBean;
import java.util.List;

@MagicBean(
generateAllArgsStaticFactory = true,
generateEqualsAndHashCode = true,
generateToString = true
allArgsStaticFactory = true,
equalsAndHashCode = true,
toString_ = true
)
public final class Example extends ExampleBeanOps {
int x;
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>dev.mccue</groupId>
<artifactId>magic-bean</artifactId>
<version>3.3.0</version>
<version>2024.04.24</version>
<packaging>jar</packaging>

<properties>
Expand Down Expand Up @@ -53,8 +53,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
<source>21</source>
<target>21</target>
</configuration>
<executions>
<execution>
Expand Down
13 changes: 3 additions & 10 deletions src/main/java/dev/mccue/magicbean/MagicBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,20 @@
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
public @interface MagicBean {
/**
* Will require --enable-preview on java 17.
*
* @return Whether to Use a CUPS expression instead of a normal cast.
*/
boolean useTypeSafeCast() default false;

/**
* @return Whether to generate an all args static factory method.
*/
boolean generateAllArgsStaticFactory() default false;
boolean allArgsStaticFactory() default false;

/**
* @return Whether to generate an equals and hash code implementation.
*/
boolean generateEqualsAndHashCode() default false;
boolean equalsAndHashCode() default false;

/**
* @return Whether to generate a basic toString.
*/
boolean generateToString() default false;
boolean toString_() default false;

/**
* @return A class for the generated abstract class to extend. Does not support
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public boolean process(
);

var annotation = typeElement.getAnnotation(MagicBean.class);
if (annotation.generateAllArgsStaticFactory() && !hasValidConstructor) {
if (annotation.allArgsStaticFactory() && !hasValidConstructor) {
messager.printMessage(
Diagnostic.Kind.ERROR,
"Magic beans need to have a non-private zero arg constructor in order for a factory method to be generated.",
Expand Down Expand Up @@ -255,14 +255,9 @@ public boolean process(
}


String selfExpr;
if (typeElement.getAnnotation(MagicBean.class).useTypeSafeCast()) {
selfExpr = "(switch (this) { case %s __ -> __; })".formatted(className);
} else {
selfExpr = "((%s) this)".formatted(className);
}
String selfExpr = "(switch (this) { case %s __ -> __; })".formatted(className);

boolean requiresFinalClass = annotation.generateEqualsAndHashCode();
boolean requiresFinalClass = annotation.equalsAndHashCode();

if (requiresFinalClass && !typeElement.getModifiers().contains(Modifier.FINAL)) {
messager.printMessage(
Expand Down Expand Up @@ -322,7 +317,7 @@ public boolean process(
classDecl.append(packageDecl);
classDecl.append(classDeclStart);

if (annotation.generateAllArgsStaticFactory()) {
if (annotation.allArgsStaticFactory()) {
classDecl.append(staticFactoryMethod(className, fields));
}

Expand All @@ -333,11 +328,11 @@ public boolean process(
));
}

if (annotation.generateEqualsAndHashCode()) {
if (annotation.equalsAndHashCode()) {
classDecl.append(equalsAndHashCodeMethods(selfExpr, className, fields));
}

if (annotation.generateToString()) {
if (annotation.toString()) {
classDecl.append(toStringMethod(selfExpr, className, fields));
}

Expand Down

0 comments on commit 3e9c704

Please sign in to comment.