-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from endrealm/better-annotations
Add better annotation support
- Loading branch information
Showing
16 changed files
with
389 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Using the annotations API | ||
First thing for anything to work is register the affected classes. | ||
```java | ||
//service is the implementation of DriveService | ||
ConversionHandler conversion = service.getConversionHandler(); | ||
conversion.registerClasses(GreatEntity.class); | ||
``` | ||
If you forget doing this errors will occur. | ||
## Simple field saving | ||
Now that you have your service you will probably want to start mapping your first classes. As an example we will map the class `GreatEntity` here. | ||
```java | ||
@SaveAll | ||
public class GreatEntity { | ||
|
||
//No-Args constructor is required, when reading values | ||
public GreatEntity() { | ||
this.feet = 2; | ||
} | ||
|
||
public GreatEntity(String entityName, int age, String[] addresses, int feet) { | ||
this.entityName = entityName; | ||
this.age = age; | ||
this.addresses = addresses; | ||
this.feet = feet; | ||
} | ||
|
||
private String entityName; | ||
private int age; | ||
private String[] addresses; | ||
|
||
private int feet; | ||
} | ||
``` | ||
|
||
## Advanced annotation handling | ||
|
||
`@SaveVar(properties)` | scope = Field | ||
properties: | ||
* name: String | default "" || name field will be saved under and loaded from. Leave empty for fieldName- | ||
* aliases: String[] | default {} || A list of aliases this fields value might be saved under. Used when name not found | ||
* optional: Boolean | default true || Is the field optional. If not class cannot be loaded without the value being asigned | ||
> Fields marked with this will be saved and loaded under this value | ||
|
||
`@SaveTable(properties)` | scope = Class | ||
properties: | ||
* value: String | default "" || table name to save under, if query does not force other | ||
> Should be used to save the entity under a specific table. This will pregenerate a table depending on the backend type | ||
`@SaveAll(properties)` | scope = Class | ||
properties: | ||
* ignore: String[] | default {} || field names to ignore | ||
> This will tell the program to save all fields. Note: @SaveVar and @IgnoreVar will overwrite this behaviour | ||
|
||
`@IgnoreVar()` | scope = Field | ||
> Fields marked with this will be ignored when saving and loading | ||
`@WriteOnly()` | scope = Field | ||
> Fields marked with this will only be saved, but not loaded. | ||
`@ReadOnly()` | scope = Field | ||
> Fields marked with this will only be loaded, but not saved. | ||
> **Note:** Having both ReadOnly and WriteOnly on a field will lead to unexpected behaviour and should be replaced by SaveVar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/net/endrealm/realmdrive/annotations/IgnoreVar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package net.endrealm.realmdrive.annotations; | ||
|
||
import net.endrealm.realmdrive.inst.SimpleConversionHandler; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Marks a field to not be saved. | ||
* | ||
* @see SimpleConversionHandler | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.FIELD) | ||
public @interface IgnoreVar { | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/net/endrealm/realmdrive/annotations/ReadOnly.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package net.endrealm.realmdrive.annotations; | ||
|
||
import net.endrealm.realmdrive.inst.SimpleConversionHandler; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Marks a field to only be loaded not saved. | ||
* Do <b>not</b> use {@link WriteOnly} together with this. | ||
* To enable both read and write use {@link SaveVar}. | ||
* | ||
* @see SimpleConversionHandler | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.FIELD) | ||
public @interface ReadOnly { | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/net/endrealm/realmdrive/annotations/SaveAll.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package net.endrealm.realmdrive.annotations; | ||
|
||
import net.endrealm.realmdrive.inst.SimpleConversionHandler; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Marks all fields as to be saved. This can be | ||
* overwritten by {@link SaveVar} and {@link IgnoreVar} | ||
* | ||
* @see SimpleConversionHandler | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
public @interface SaveAll { | ||
/** | ||
* @return field names to be ignored | ||
*/ | ||
String[] ignored() default {}; | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/net/endrealm/realmdrive/annotations/SaveTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package net.endrealm.realmdrive.annotations; | ||
|
||
import net.endrealm.realmdrive.inst.SimpleConversionHandler; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Used to overwrite default table. Table specified in query will overwrite this behaviour. | ||
* | ||
* @see SimpleConversionHandler | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
public @interface SaveTable { | ||
|
||
String tableName() default ""; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/net/endrealm/realmdrive/annotations/WriteOnly.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package net.endrealm.realmdrive.annotations; | ||
|
||
import net.endrealm.realmdrive.inst.SimpleConversionHandler; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Marks a field to only be saved not loaded. | ||
* Do <b>not</b> use {@link ReadOnly} together with this. | ||
* To enable both read and write use {@link SaveVar}. | ||
* | ||
* @see SimpleConversionHandler | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.FIELD) | ||
public @interface WriteOnly { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.