Skip to content

Commit

Permalink
Added full java documentation and inferred nullity.
Browse files Browse the repository at this point in the history
Added EnumYAMLParser for parsing Enum objects.
Added various tests extending to all the project and resources.
Removed ReflectionUtils dependency: now using [jOOR](https://github.com/jOOQ/jOOR).
  • Loading branch information
Fulminazzo committed Jan 3, 2024
1 parent 8314cb8 commit dc83e2b
Show file tree
Hide file tree
Showing 26 changed files with 329 additions and 167 deletions.
30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,34 @@
This project aims to provide a simple way of **accessing data** from **YAML files**.
It uses the following **libraries**:
- [SnakeYAML](https://bitbucket.org/snakeyaml/snakeyaml/src/master/)
- [BearCommands common](https://github.com/Fulminazzo/BearCommands)
- [ReflectionUtils](https://github.com/Fulminazzo/TODO)
- [FulmiCollection](https://github.com/Fulminazzo/FulmiCollection)
- [jOOR](https://github.com/jOOQ/jOOR)

To start using the project, import it with Maven or Gradle:
- **Maven**:
```xml
<repository>
<id>fulminazzo-repo</id>
<url>https://repo.fulminazzo.it/releases</url>
</repository>
```
```xml
<dependency>
<groupId>it.fulminazzo</groupId>
<artifactId>YAMLParser</artifactId>
<version>1.3</version>
</dependency>
```
- **Gradle**:
```groovy
repositories {
maven { url = "https://repo.fulminazzo.it/releases" }
}
dependencies {
implementation 'it.fulminazzo.YAMLParser:1.3'
}
```

| **Table of Contents** |
|---------------------------------------------------------------|
Expand Down
50 changes: 27 additions & 23 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
plugins {
id 'java'

id 'com.github.johnrengelman.shadow' version '8.1.1'
id 'maven-publish'
id 'com.github.johnrengelman.shadow' version '8.1.1'
id 'io.freefair.lombok' version '8.4'
}

group = 'it.fulminazzo'
version = '1.3'

repositories {
mavenCentral()
maven {
url 'https://jitpack.io'
}
mavenLocal()
maven { url = "https://repo.fulminazzo.it/releases" }
}

dependencies {
compileOnly 'org.projectlombok:lombok:1.18.30'
annotationProcessor 'org.projectlombok:lombok:1.18.30'
implementation 'org.jetbrains:annotations:24.1.0'
compileOnly 'org.jetbrains:annotations:24.1.0'

implementation 'org.yaml:snakeyaml:2.2'

//TODO: Fix
/*implementation 'com.github.Fulminazzo:FulmiCollection:1.2'*/
implementation 'org.jooq:joor-java-8:0.9.14'
implementation 'it.fulminazzo:FulmiCollection:1.2'

testImplementation platform('org.junit:junit-bom:5.9.1')
Expand All @@ -47,16 +42,6 @@ shadowJar {
minimize()
}

tasks.register('sourcesJar', Jar) {
from sourceSets.main.allJava
archiveClassifier = 'sources'
}

tasks.register('javadocJar', Jar) {
from javadoc
archiveClassifier = 'javadoc'
}

jar {
manifest {
attributes(
Expand All @@ -69,6 +54,16 @@ jar {
dependsOn(shadowJar)
}

tasks.register('sourcesJar', Jar) {
from sourceSets.main.delombokTask
archiveClassifier = 'sources'
}

tasks.register('javadocJar', Jar) {
from javadoc
archiveClassifier = 'javadoc'
}

publishing {
publications {
maven(MavenPublication) {
Expand All @@ -77,8 +72,17 @@ publishing {
artifact javadocJar
}
}
}

build {
dependsOn(publishToMavenLocal)
repositories {
maven {
url "https://repo.fulminazzo.it/releases"
credentials {
username = System.getenv("REPO_USERNAME")
password = System.getenv("REPO_PASSWORD")
}
authentication {
basic(BasicAuthentication)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* thrown.
*/
public class EmptyArrayException extends YAMLException {

public EmptyArrayException(@NotNull String path, @NotNull String name, @Nullable Object object) {
super(path, name, object, LogMessage.CANNOT_DECIPHER_EMPTY_ARRAY);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package it.fulminazzo.yamlparser.interfaces;

import it.fulminazzo.fulmicollection.utils.EnumUtils;
import it.fulminazzo.reflectionutils.objects.ReflObject;
import it.fulminazzo.reflectionutils.utils.ReflUtil;
import it.fulminazzo.fulmicollection.utils.ReflectionUtils;
import it.fulminazzo.yamlparser.exceptions.yamlexceptions.CannotBeNullException;
import it.fulminazzo.yamlparser.exceptions.yamlexceptions.UnexpectedClassException;
import it.fulminazzo.yamlparser.objects.configurations.ConfigurationSection;
Expand All @@ -11,6 +10,7 @@
import it.fulminazzo.yamlparser.objects.yamlelements.YAMLParser;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.joor.Reflect;

import java.util.*;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -154,7 +154,6 @@ default <O> void set(@NotNull String path, @Nullable O o) {
path = getNameFromPath(path);
}
if (o == null) section.toMap().remove(path);
else if (o instanceof Enum<?>) section.toMap().put(path, ((Enum<?>) o).name());
else {
YAMLParser<O> parser = (YAMLParser<O>) FileConfiguration.getParser(o.getClass());
if (!isPrimitiveOrWrapper(o) && parser != null)
Expand All @@ -164,18 +163,6 @@ default <O> void set(@NotNull String path, @Nullable O o) {
}
}

/**
* Converts the given object using its associated YAML parser (if found).
*
* @param <T> the type of the object
* @param path the path
* @param object the object
* @return the final object
*/
default <T> @Nullable T convertObjectToYAMLObject(@Nullable String path, @Nullable Object object) {
return convertObjectToYAMLObject(path, object, FileConfiguration.getParsers());
}

/**
* Converts the given object using its associated YAML parser (if found).
*
Expand All @@ -195,11 +182,8 @@ default <O> void set(@NotNull String path, @Nullable O o) {
if (objectString.isEmpty()) return (T) new Character((char) 0);
return (T) new Character(objectString.charAt(0));
}
return new ReflObject<>(clazz.getCanonicalName(), false)
.getMethodObject("valueOf", object.toString());
return Reflect.onClass(clazz).call("valueOf", object.toString()).get();
}
if (Float.class.isAssignableFrom(clazz) && object instanceof Double)
object = Float.valueOf(String.valueOf(object));
if (clazz.getCanonicalName().equals("java.util.Arrays.ArrayList") && object instanceof List)
return (T) new ArrayList<>((Collection<?>) object);
}
Expand Down Expand Up @@ -669,12 +653,7 @@ default boolean isByte(@NotNull String path) {
* @return the enum list
*/
default <E extends Enum<E>> @Nullable List<E> getEnumList(@NotNull String path, @NotNull Class<? extends E> eClass) {
List<String> list = getStringList(path);
if (list == null) return null;
return list.stream()
.filter(e -> check(path, e, eClass))
.map(e -> EnumUtils.valueOf(eClass, e))
.collect(Collectors.toList());
return (List<E>) getList(path, eClass);
}

/**
Expand Down Expand Up @@ -912,7 +891,7 @@ default <T> boolean check(@NotNull String name, @Nullable Object object, @NotNul
if (object == null && checkNonNull()) throw new CannotBeNullException(getCurrentPath(), name, name);
if (object == null) return true;
if (clazz.isAssignableFrom(object.getClass())) return true;
if (ReflUtil.getPrimitiveClass(clazz).isAssignableFrom(ReflUtil.getPrimitiveClass(object.getClass()))) return true;
if (ReflectionUtils.getPrimitiveClass(clazz).isAssignableFrom(ReflectionUtils.getPrimitiveClass(object.getClass()))) return true;
throw new UnexpectedClassException(getCurrentPath(), name, object, clazz);
}

Expand Down Expand Up @@ -1029,7 +1008,7 @@ static boolean isPrimitiveOrWrapper(@Nullable Object object) {
*/
static boolean isPrimitiveOrWrapper(@Nullable Class<?> clazz) {
if (clazz == null) return false;
return ReflUtil.isPrimitiveOrWrapper(clazz) || clazz.equals(String.class);
return ReflectionUtils.isPrimitiveOrWrapper(clazz) || clazz.equals(String.class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package it.fulminazzo.yamlparser.objects.configurations;

import it.fulminazzo.fulmicollection.exceptions.GeneralCannotBeNullException;
import it.fulminazzo.reflectionutils.objects.ReflObject;
import it.fulminazzo.reflectionutils.utils.ClassUtils;
import it.fulminazzo.fulmicollection.utils.ClassUtils;
import it.fulminazzo.yamlparser.interfaces.IConfiguration;
import it.fulminazzo.yamlparser.objects.yamlelements.ArrayYAMLParser;
import it.fulminazzo.yamlparser.objects.yamlelements.EnumYAMLParser;
import it.fulminazzo.yamlparser.objects.yamlelements.SerializableYAMLParser;
import it.fulminazzo.yamlparser.objects.yamlelements.YAMLParser;
import it.fulminazzo.yamlparser.utils.FileUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.joor.Reflect;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.representer.Representer;
Expand Down Expand Up @@ -86,7 +87,9 @@ public void save() {
newYaml().dump(IConfiguration.configToGeneralMap(this), writer);
writer.close();
} catch (Exception e) {
throw new RuntimeException(e);
if (e instanceof RuntimeException && e.getCause() instanceof RuntimeException)
throw (RuntimeException) e.getCause();
else throw new RuntimeException(e);
}
}

Expand Down Expand Up @@ -168,8 +171,7 @@ public static void removeParsers(YAMLParser<?> @NotNull ... yamlParsers) {
if (Modifier.isFinal(clazz.getModifiers()) ||
Modifier.isAbstract(clazz.getModifiers()) ||
!Modifier.isPublic(clazz.getModifiers())) continue;
ReflObject<YAMLParser<?>> parserReflObject = new ReflObject<YAMLParser<?>>((Class<YAMLParser<?>>) clazz);
YAMLParser<?> parser = parserReflObject.getObject();
YAMLParser<?> parser = Reflect.onClass(clazz).create().get();
if (parser != null) yamlParsers.add(parser);
} catch (NoSuchMethodException ignored) {}
return yamlParsers;
Expand All @@ -183,11 +185,10 @@ public static void removeParsers(YAMLParser<?> @NotNull ... yamlParsers) {
* @return the parser
*/
@SuppressWarnings("unchecked")
public static <O> YAMLParser<O> getParser(@Nullable Class<O> oClass) {
public static <O, E extends Enum<E>> YAMLParser<O> getParser(@Nullable Class<O> oClass) {
if (oClass == null) return null;
if (oClass.isArray()) return (YAMLParser<O>) getParsers().stream()
.filter(p -> p instanceof ArrayYAMLParser<?>)
.findFirst().orElse(new ArrayYAMLParser<>());
if (oClass.isEnum()) return (YAMLParser<O>) new EnumYAMLParser<E>(oClass);
if (oClass.isArray()) return (YAMLParser<O>) new ArrayYAMLParser<O>();
return (YAMLParser<O>) getParsers().stream()
.filter(p -> p.getOClass().isAssignableFrom(oClass))
.findFirst().orElse(null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package it.fulminazzo.yamlparser.objects.configurations.checkers;

import it.fulminazzo.fulmicollection.objects.Printable;
import it.fulminazzo.reflectionutils.utils.ReflUtil;

import it.fulminazzo.fulmicollection.utils.ReflectionUtils;
import it.fulminazzo.yamlparser.interfaces.IConfiguration;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -50,7 +51,7 @@ else if (obj1 instanceof Collection && obj2 instanceof Collection) {
obj2 = col2.stream().filter(Objects::nonNull).findFirst().orElse(null);
if (obj1 == null) continue;
if (obj2 == null) {
if (ReflUtil.isPrimitive(obj1.getClass()))
if (ReflectionUtils.isPrimitive(obj1.getClass()))
invalidValues.add(new ConfigurationInvalidType(key, obj1.getClass(), null));
} else if (!obj1.getClass().equals(obj2.getClass()))
invalidValues.add(new ConfigurationInvalidType(key, obj1.getClass(), obj2.getClass()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
public class ArrayYAMLParser<T> extends YAMLParser<T[]> {
private final @NotNull ListYAMLParser<T> listYamlParser;

/**
* Instantiates a new Array YAML parser.
*/
public ArrayYAMLParser() {
super((Class<T[]>) ((Class<?>) Object[].class));
this.listYamlParser = new ListYAMLParser<>();
Expand All @@ -36,6 +39,7 @@ public ArrayYAMLParser() {
return (c, s) -> {
if (c == null || s == null) return null;
List<T> tmp = listYamlParser.load(c, s);
if (tmp == null) return null;
T elem = tmp.stream().filter(Objects::nonNull).findAny().orElse(null);
if (elem == null) throw new EmptyArrayException(String.join(".", c.parseSectionPath(s)), c.getNameFromPath(s), tmp);
T[] t = (T[]) Array.newInstance(elem.getClass(), tmp.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import it.fulminazzo.fulmicollection.interfaces.functions.BiFunctionException;
import it.fulminazzo.fulmicollection.interfaces.functions.FunctionException;
import it.fulminazzo.fulmicollection.interfaces.functions.TriConsumer;
import it.fulminazzo.reflectionutils.objects.ReflObject;
import it.fulminazzo.fulmicollection.utils.ReflectionUtils;
import it.fulminazzo.yamlparser.annotations.PreventSaving;
import it.fulminazzo.yamlparser.interfaces.IConfiguration;
import it.fulminazzo.yamlparser.objects.configurations.ConfigurationSection;
import it.fulminazzo.yamlparser.utils.FileUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.joor.Reflect;

import java.lang.reflect.Field;

Expand All @@ -22,7 +23,7 @@ public class CallableYAMLParser<T> extends YAMLParser<T> {
private final FunctionException<ConfigurationSection, T> function;

/**
* Instantiates a new Callable yaml parser.
* Instantiates a new Callable YAML parser.
*
* @param tClass the t class
* @param function the function
Expand All @@ -40,14 +41,12 @@ public CallableYAMLParser(@NotNull Class<T> tClass, FunctionException<Configurat
if (section == null) return null;
T t = function.apply(section);
if (t == null) return null;
ReflObject<T> tReflObject = new ReflObject<>(t);
for (Field field : tReflObject.getFields()) {
// Remove fields used by code coverage from Intellij IDEA.
if (field.getName().equals("__$hits$__")) continue;
Reflect tReflect = Reflect.on(t);
for (Field field : ReflectionUtils.getFields(t)) {
if (field.isAnnotationPresent(PreventSaving.class)) continue;
Object object = section.get(FileUtils.formatStringToYaml(field.getName()), field.getType());
if (object == null) continue;
tReflObject.setField(field.getName(), object);
tReflect.set(field.getName(), object);
}
return t;
};
Expand All @@ -59,12 +58,11 @@ public CallableYAMLParser(@NotNull Class<T> tClass, FunctionException<Configurat
if (c == null || s == null) return;
ConfigurationSection section = c.createSection(s);
if (t == null) return;
ReflObject<T> tReflObject = new ReflObject<>(t);
tReflObject.getFields().forEach(field -> {
// Remove fields used by code coverage from Intellij IDEA.
if (field.getName().equals("__$hits$__")) return;
Reflect tReflect = Reflect.on(t);
ReflectionUtils.getFields(t).forEach(field -> {
if (field.isAnnotationPresent(PreventSaving.class)) return;
section.set(FileUtils.formatStringToYaml(field.getName()), tReflObject.getFieldObject(field.getName()));
String fieldName = field.getName();
section.set(FileUtils.formatStringToYaml(fieldName), tReflect.get(fieldName));
});
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
class CollectionYAMLParser<T, C extends Collection<T>> extends YAMLParser<C> {
protected final @NotNull MapYAMLParser<Integer, T> mapYamlParser;

/**
* Instantiates a new Collection YAML parser.
*
* @param aClass the class
*/
public CollectionYAMLParser(@NotNull Class<C> aClass) {
super(aClass);
this.mapYamlParser = new MapYAMLParser<>(Integer::valueOf, Object::toString);
Expand Down
Loading

0 comments on commit dc83e2b

Please sign in to comment.