-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ConfigName annotation to specify the path to a value in config
- Loading branch information
Showing
13 changed files
with
311 additions
and
141 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
name: RedLib | ||
main: redempt.redlib.RedLib | ||
version: 2021-12-21 20:47 | ||
version: 2021-12-22 04:41 | ||
author: Redempt | ||
api-version: 1.13 | ||
load: STARTUP |
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,103 @@ | ||
package redempt.redlib.config; | ||
|
||
import redempt.redlib.config.annotations.ConfigName; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
/** | ||
* Wraps a Field and stores the name which should be used to store its value in config | ||
* @author Redempt | ||
*/ | ||
public class ConfigField { | ||
|
||
private Field field; | ||
private String name; | ||
|
||
/** | ||
* Constructs a ConfigField from a field | ||
* @param field The Field | ||
*/ | ||
public ConfigField(Field field) { | ||
this.field = field; | ||
ConfigName annotation = field.getAnnotation(ConfigName.class); | ||
name = annotation == null ? field.getName() : annotation.value(); | ||
} | ||
|
||
/** | ||
* @return The wrapped Field | ||
*/ | ||
public Field getField() { | ||
return field; | ||
} | ||
|
||
/** | ||
* Attemps to set the value of the field for the target object to the value | ||
* @param target The target object | ||
* @param value The value | ||
*/ | ||
public void set(Object target, Object value) { | ||
try { | ||
field.set(target, value); | ||
} catch (IllegalAccessException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
* Attemps to set the field in a static context to the given value | ||
* @param value The value | ||
*/ | ||
public void set(Object value) { | ||
set(null, value); | ||
} | ||
|
||
/** | ||
* Attempts to get the field's value for a given object | ||
* @param target The target object to get the value from | ||
* @return The value | ||
*/ | ||
public Object get(Object target) { | ||
try { | ||
return field.get(target); | ||
} catch (IllegalAccessException e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Attemps to get the value of the field in a static context | ||
* @return The value | ||
*/ | ||
public Object get() { | ||
return get(null); | ||
} | ||
|
||
/** | ||
* @return The name for the field that should be used to store config values | ||
*/ | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
/** | ||
* Sets the name of this ConfigField | ||
* @param name The name to set | ||
*/ | ||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public int hashCode() { | ||
return field.hashCode(); | ||
} | ||
|
||
public boolean equals(Object o) { | ||
if (!(o instanceof ConfigField)) { | ||
return false; | ||
} | ||
ConfigField cf = (ConfigField) o; | ||
return cf.field.equals(field); | ||
} | ||
|
||
} |
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
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
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,16 @@ | ||
package redempt.redlib.config.annotations; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({ElementType.FIELD, ElementType.PARAMETER}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface ConfigName { | ||
|
||
String value(); | ||
|
||
} |
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
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
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
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
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.