Class AutoConfig
java.lang.Object
io.github.thecsdev.tcdcommons.api.config.AutoConfig
- All Implemented Interfaces:
ACJsonHandler<com.google.gson.JsonObject>
- Direct Known Subclasses:
TCDCommonsConfig
-
Field Summary
Modifier and TypeFieldDescriptionprotected static final String
final String
The name of theAutoConfig
file.final Path
The final path of theAutoConfig
file.protected static final com.google.gson.Gson
TheGson
used byAutoConfig
to serialize and deserialize properties. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected final boolean
deserializeProperty
(Field property, com.google.gson.JsonElement jEl) Deserializes aJsonElement
to a given property (field) that belongs to thisAutoConfig
.Returns all valid property fields for this object.final String
getPropertyName
(Field property) Checks if a given propertyField
has theSerializedAs
annotation, and returns the name of theField
based on that.final void
loadFromFile
(boolean log) Loads thisAutoConfig
from it's file.final void
loadFromFileOrCrash
(boolean log) Same asloadFromFile(boolean)
, but any raisedIOException
s will not be handled.boolean
loadFromJson
(com.google.gson.JsonObject json) Loads the data from a givenJsonElement
to this object, and then returns true if loading was successful.final void
saveToFile
(boolean log) Saves thisAutoConfig
to it's file.final void
saveToFileOrCrash
(boolean log) Same assaveToFile(boolean)
, but any raisedIOException
s will not be handled.com.google.gson.JsonObject
Saves the data from this object into a newJsonElement
, and then returns theJsonElement
.protected final @Nullable com.google.gson.JsonElement
serializeProperty
(Field property) final boolean
tryLoadFromFile
(boolean log) Same asloadFromFile(boolean)
, but returns false if anIOException
is raised.final boolean
trySaveToFile
(boolean log) Same assaveToFile(boolean)
, but returns false if anIOException
is raised.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.github.thecsdev.tcdcommons.api.config.ACJsonHandler
loadFromJsonElement
-
Field Details
-
FILE_EXTENSION
- See Also:
-
GSON
protected static final transient com.google.gson.Gson GSONTheGson
used byAutoConfig
to serialize and deserialize properties. -
fileName
The name of theAutoConfig
file. -
filePath
The final path of theAutoConfig
file. This is wheresaveToFile(boolean)
will save to, and whereloadFromFile(boolean)
will load from.
-
-
Constructor Details
-
AutoConfig
Constructs anAutoConfig
instance.- Parameters:
name
- The name of the config file.- Throws:
InvalidPathException
- If the fileName is invalid.
-
-
Method Details
-
getPropertyFields
Returns all valid property fields for this object. Both declared fields as well as inherited fields are included. Property fields must be public non-static primitive types orACJsonHandler
s. -
serializeProperty
- Parameters:
property
- The propertyField
from thisAutoConfig
.- Returns:
- null if serialization fails or if the given
Field
type is unsupported.
-
deserializeProperty
Deserializes aJsonElement
to a given property (field) that belongs to thisAutoConfig
.- Parameters:
property
- The propertyField
from thisAutoConfig
.jEl
- TheJsonElement
to deserialize.- Returns:
- true if nothing went wrong while deserializing
the given property
Field
.
-
getPropertyName
Checks if a given propertyField
has theSerializedAs
annotation, and returns the name of theField
based on that.- Parameters:
property
- The propertyField
from thisAutoConfig
.- Returns:
Field.getName()
if the field doesn't have theSerializedAs
annotation.
-
saveToJson
public com.google.gson.JsonObject saveToJson()Description copied from interface:ACJsonHandler
Saves the data from this object into a newJsonElement
, and then returns theJsonElement
.- Specified by:
saveToJson
in interfaceACJsonHandler<com.google.gson.JsonObject>
-
loadFromJson
public boolean loadFromJson(com.google.gson.JsonObject json) Description copied from interface:ACJsonHandler
Loads the data from a givenJsonElement
to this object, and then returns true if loading was successful.- Specified by:
loadFromJson
in interfaceACJsonHandler<com.google.gson.JsonObject>
- Parameters:
json
- TheJsonElement
to load the data from.
-
saveToFile
Saves thisAutoConfig
to it's file.- Parameters:
log
- Would you like to log this operation to the console?- Throws:
IOException
- If an I/O error occurs writing to or creating the file.SecurityException
- SeeSecurityManager.checkWrite(String)
.
-
saveToFileOrCrash
public final void saveToFileOrCrash(boolean log) Same assaveToFile(boolean)
, but any raisedIOException
s will not be handled.- Parameters:
log
- Would you like to log this operation to the console?- See Also:
-
trySaveToFile
public final boolean trySaveToFile(boolean log) Same assaveToFile(boolean)
, but returns false if anIOException
is raised.- Parameters:
log
- Would you like to log this operation to the console?- See Also:
-
loadFromFile
Loads thisAutoConfig
from it's file.- Parameters:
log
- Would you like to log this operation to the console?- Throws:
IOException
- If an I/O error occurs reading from the file.
-
loadFromFileOrCrash
public final void loadFromFileOrCrash(boolean log) Same asloadFromFile(boolean)
, but any raisedIOException
s will not be handled.- Parameters:
log
- Would you like to log this operation to the console?- See Also:
-
tryLoadFromFile
public final boolean tryLoadFromFile(boolean log) Same asloadFromFile(boolean)
, but returns false if anIOException
is raised.- Parameters:
log
- Would you like to log this operation to the console?- See Also:
-