Skip to content

Commit

Permalink
Merge branch 'refs/heads/feat-plugin-test' into feat-data-gui
Browse files Browse the repository at this point in the history
  • Loading branch information
fulminazzo committed Apr 18, 2024
2 parents 5e23444 + c48de9b commit 0b8247c
Show file tree
Hide file tree
Showing 57 changed files with 1,197 additions and 160 deletions.
73 changes: 40 additions & 33 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ plugins {
id 'io.freefair.lombok' version libs.versions.delombok
}

final def TEST_MODULE = 'testing'
final def DEMO_MODULE = 'demo'
final def VERSION_VARIABLE = 'MINECRAFT_VERSION'

group = 'it.angrybear'
version = '4.0'

final def TEST_MODULE = "testing"
final def VERSION_VARIABLE = "MINECRAFT_VERSION"

allprojects {
apply plugin: 'java-library'
apply plugin: 'maven-publish'
Expand All @@ -26,7 +27,7 @@ allprojects {
def obsolete_version = libs.versions.spigot.obsolete.get()

this.ext.getParentFromProject = { project ->
return new HashMap<>(project.getProperties()).get("parent")
return new HashMap<>(project.getProperties()).get('parent')
}

this.ext.getProjectGroupId = {
Expand All @@ -37,7 +38,7 @@ allprojects {
tmp = getParentFromProject(tmp)
}
if (groupId.size() > 0)
groupId = "." + groupId.substring(0, groupId.length() - 1)
groupId = ".${groupId.substring(0, groupId.length() - 1)}"
return "${rootProject.group}${groupId}"
}

Expand All @@ -50,18 +51,18 @@ allprojects {
repositories {
mavenCentral()
maven {
name = "spigotmc-repo"
name = 'spigotmc-repo'
url = 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/'
}
maven { url = 'https://oss.sonatype.org/content/repositories/snapshots' }
maven { url = 'https://oss.sonatype.org/content/repositories/central' }
maven {
name = "JitPack"
url = "https://jitpack.io"
name = 'JitPack'
url = 'https://jitpack.io'
}
maven {
name = "Fulminazzo repository"
url = "https://repo.fulminazzo.it/releases"
name = 'Fulminazzo repository'
url = 'https://repo.fulminazzo.it/releases'
}
}

Expand All @@ -75,49 +76,50 @@ allprojects {

def projectName = project.name

if (projectName.contains("serializer")) api(libs.yamlparser)
if (projectName.contains('serializer')) api(libs.yamlparser)

if (projectName.contains("bukkit")) {
if (projectName.contains('bukkit')) {
compileOnly libs.spigot
testCompileOnly libs.spigot.latest
testRuntimeOnly "org.spigotmc:spigot-api:${latest_version}"

String numVersion = latest_version.substring(2, latest_version.indexOf(".", 2))
def index = numVersion.indexOf("-")
String numVersion = latest_version.substring(2, latest_version.indexOf('.', 2))
def index = numVersion.indexOf('-')
if (index != -1) numVersion = numVersion.substring(0, index)
if (Double.valueOf(numVersion) >= 13) testImplementation libs.jbukkit
else testImplementation libs.jbukkit.legacy
}

if (projectName.contains("legacy")) {
if (projectName.contains('legacy')) {
testCompileOnly libs.spigot.legacy
testRuntimeOnly "org.spigotmc:spigot-api:${legacy_version}"

testImplementation libs.jbukkit.legacy
}

if (projectName.contains("obsolete")) {
if (projectName.contains('obsolete')) {
testCompileOnly libs.spigot.obsolete
testRuntimeOnly "org.spigotmc:spigot-api:${obsolete_version}"

testImplementation libs.jbukkit.legacy
}

if (projectName.contains("-")) {
def name = projectName.substring(0, projectName.indexOf("-"))
def id = projectName.substring(projectName.indexOf("-") + 1)
if (!projectName.contains("base")) api(project(":${name}:${name}-base"))
if (name != "common" && id != "legacy" && id != "obsolete")
if (projectName.contains('-')) {
def name = projectName.substring(0, projectName.indexOf('-'))
def id = projectName.substring(projectName.indexOf('-') + 1)
if (!projectName.contains('base')) api(project(":${name}:${name}-base"))
if (name != 'common' && id != 'legacy' && id != 'obsolete')
api(project(":common:common-${id}"))
}

if (!projectName.contains("-"))
subprojects.findAll { (it.name != TEST_MODULE) } .each {api project(it.path)}

testCompileOnly libs.lombok
testAnnotationProcessor libs.lombok

if (!project.name.equals(TEST_MODULE)) testImplementation project(":" + TEST_MODULE)
if (!project.name.equals(TEST_MODULE)) testImplementation project(":${TEST_MODULE}")

if (project.name.equals(DEMO_MODULE))
rootProject.subprojects.findAll { it.name != TEST_MODULE && it.name != DEMO_MODULE }
.each { implementation project(it.path) }
}

if (project.name.equals(TEST_MODULE)) return
Expand All @@ -126,24 +128,24 @@ allprojects {
def projectName = project.name

if (System.getenv(VERSION_VARIABLE) == null) {
if (projectName.contains("bukkit")) environment VERSION_VARIABLE, latest_version
else if (projectName.contains("legacy")) environment VERSION_VARIABLE, legacy_version
else if (projectName.contains("obsolete")) environment VERSION_VARIABLE, obsolete_version
if (projectName.contains('bukkit')) environment VERSION_VARIABLE, latest_version
else if (projectName.contains('legacy')) environment VERSION_VARIABLE, legacy_version
else if (projectName.contains('obsolete')) environment VERSION_VARIABLE, obsolete_version
}

def env = System.getenv(VERSION_VARIABLE)
if (env != null) println "Testing ${projectName} with Minecraft ${env}"
useJUnitPlatform()
}

tasks.register("testBukkit") {
tasks.register('testBukkit') {
final def minecraftVersion = getMinecraftVersion()
println "Using Minecraft version ${minecraftVersion}"
latest_version = minecraftVersion
legacy_version = minecraftVersion
obsolete_version = minecraftVersion

subprojects.findAll { it.path.contains("bukkit") }.each {dependsOn "${it.path}:test" }
subprojects.findAll { it.path.contains('bukkit') }.each {dependsOn "${it.path}:test" }
}

tasks.register('sourcesJar', Jar) {
Expand Down Expand Up @@ -171,10 +173,10 @@ allprojects {

repositories {
maven {
url "https://repo.fulminazzo.it/releases"
url 'https://repo.fulminazzo.it/releases'
credentials {
username = System.getenv("REPO_USERNAME")
password = System.getenv("REPO_PASSWORD")
username = System.getenv('REPO_USERNAME')
password = System.getenv('REPO_PASSWORD')
}
authentication {
basic(BasicAuthentication)
Expand All @@ -184,6 +186,11 @@ allprojects {
}
}

dependencies {
subprojects.findAll { it.name != TEST_MODULE && it.name != DEMO_MODULE } .each { api project(it.path) }
implementation project(":${DEMO_MODULE}")
}

testCodeCoverageReport {
dependsOn test
reports {
Expand Down
3 changes: 2 additions & 1 deletion common/base/src/main/java/it/angrybear/yagl/ClassEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ private void checkValues() {
if (!this.values.isEmpty()) return;
for (Field field : this.clazz.getDeclaredFields())
if (field.getType().equals(this.clazz))
this.values.put(field.getName().toUpperCase(), ReflectionUtils.get(field, this.clazz));
ReflectionUtils.get(field, this.clazz).ifPresent(o ->
this.values.put(field.getName().toUpperCase(), (T) o));
}
}
}
82 changes: 52 additions & 30 deletions common/base/src/main/java/it/angrybear/yagl/utils/ObjectUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import lombok.NoArgsConstructor;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.*;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -63,37 +66,56 @@ public static <T, O extends T> O copy(final @NotNull T t, @NotNull Class<O> claz
clazz.getCanonicalName(), clazz.getCanonicalName()));
}

Refl<O> object = new Refl<>(clazz, new Object[0]);
for (final Field field : object.getNonStaticFields())
final Refl<O> object = new Refl<>(clazz, new Object[0]);
for (final Field field : object.getNonStaticFields()) {
field.setAccessible(true);
try {
Object obj1 = ReflectionUtils.get(field, t);
if (obj1 instanceof Collection) {
Class<?> tmpClass = obj1.getClass();
// In the case of creation with Arrays.asList()
if (tmpClass.getCanonicalName().equals(Arrays.class.getCanonicalName() + ".ArrayList"))
tmpClass = ArrayList.class;
Class<Collection<Object>> finalClass = (Class<Collection<Object>>) tmpClass;
obj1 = ((Collection<?>) obj1).stream()
.collect(Collectors.toCollection(() -> new Refl<>(finalClass, new Object[0]).getObject()));
} else if (obj1 instanceof Map) {
Map<Object, Object> map = new HashMap<>();
((Map<Object, Object>) obj1).putAll(map);
obj1 = map;
} else if (obj1 != null)
if (obj1.getClass().isArray()) {
Object[] tmp = (Object[]) obj1;
Object[] arr = (Object[]) Array.newInstance(obj1.getClass().getComponentType(), tmp.length);
System.arraycopy(tmp, 0, arr, 0, arr.length);
obj1 = arr;
} else
try {
Method copy = obj1.getClass().getDeclaredMethod("copy");
obj1 = ReflectionUtils.setAccessible(copy).invoke(obj1);
} catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException ignored) {}
object.setFieldObject(field, obj1);
} catch (IllegalArgumentException ignored) {}

ReflectionUtils.get(field, t).map(obj1 -> {
if (obj1 == null) return null;
else if (obj1 instanceof Collection) return copyCollection(obj1);
else if (obj1 instanceof Map) return copyMap(obj1);
else if (obj1.getClass().isArray()) return copyArray(obj1);
else return copyWithMethod(obj1);
}).ifPresent(obj1 -> object.setFieldObject(field, obj1));
} catch (IllegalArgumentException e) {
if (!e.getMessage().contains("Can not set")) throw e;
}
}
return object.getObject();
}

private static @NotNull Object[] copyArray(final @NotNull Object obj1) {
Object[] tmp = (Object[]) obj1;
Object[] arr = (Object[]) Array.newInstance(obj1.getClass().getComponentType(), tmp.length);
System.arraycopy(tmp, 0, arr, 0, arr.length);
return arr;
}

private static @NotNull Map<Object, Object> copyMap(final @NotNull Object obj1) {
Map<Object, Object> map = new HashMap<>();
((Map<Object, Object>) obj1).putAll(map);
return map;
}

private static @NotNull Collection<?> copyCollection(final @NotNull Object obj1) {
Class<?> tmpClass = obj1.getClass();
// In the case of creation with Arrays.asList()
if (tmpClass.getCanonicalName().equals(Arrays.class.getCanonicalName() + ".ArrayList"))
tmpClass = ArrayList.class;
Class<Collection<Object>> finalClass = (Class<Collection<Object>>) tmpClass;
return ((Collection<?>) obj1).stream()
.collect(Collectors.toCollection(() -> new Refl<>(finalClass, new Object[0]).getObject()));
}

private static @NotNull Object copyWithMethod(final @NotNull Object obj1) {
try {
Method copy = obj1.getClass().getDeclaredMethod("copy");
return ReflectionUtils.setAccessible(copy)
.map(m -> m.invoke(obj1))
.orElseGet(obj1);
} catch (NoSuchMethodException e) {
return obj1;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,37 @@ void testCopyOfData() {
assertNotEquals(c1.copiable, c2.copiable);
}

@Test
void testCopyThrowsIllegalArgument() {
Throwable exception = assertThrowsExactly(IllegalArgumentException.class, () ->
ObjectUtils.copy(new GeneralCopyException()));
assertEquals("Everything good", exception.getMessage());
}

private static class GeneralCopy {
List<String> list = Arrays.asList("hello", "world");
String[] array = new String[]{"hello", "world"};
GeneralCopiable copiable = new GeneralCopiable();
}

private static class GeneralCopyException {
GeneralCopiableException copiable = new GeneralCopiableException();
}

private static class GeneralCopiable {

public GeneralCopiable copy() {
return new GeneralCopiable();
}
}

private static class GeneralCopiableException {

public GeneralCopiable copy() {
throw new IllegalArgumentException("Everything good");
}
}

private static class CopyIterableImpl { }

private static class CopyIterable implements Iterable<String> {
Expand Down
40 changes: 40 additions & 0 deletions demo/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
plugins {
id 'groovy'
id 'application'
id 'com.github.johnrengelman.shadow' version '8.1.1'
}

dependencies {
implementation libs.groovy

compileOnly libs.spigot.latest
testImplementation libs.spigot.latest
testImplementation libs.jbukkit
}

shadowJar {
exclude 'META-INF/**'

archiveFileName = "${rootProject.name}-plugin-${project.version}.jar"
mainClassName = "${project.group}.${rootProject.name.toLowerCase()}.${rootProject.name}"
}

jar {
dependsOn processResources
dependsOn shadowJar

archiveFileName = "${project.name}-${project.version}-original.jar"
}

processResources {
def props = [
version: rootProject.version, name: rootProject.name, name_lower: rootProject.name.toLowerCase(),
description: rootProject.description, author: 'Fulminazzo',
group: rootProject.group, module: rootProject.name
]
inputs.properties props
filteringCharset 'UTF-8'
filesMatching('*.yml') {
expand props
}
}
Loading

0 comments on commit 0b8247c

Please sign in to comment.