X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=inline;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftest%2FTestUtil.java;h=745733f5b76f7051eea8676d09348e2b48b5c1ed;hb=a1fa131a44204b2b42de3b5d467373cd113dab28;hp=8b3160f2cbed45266c874582915ef31fb90ef740;hpb=ffd92ca2374c0b2218e583d02e0bdd24b8c110ae;p=Sone.git
diff --git a/src/test/java/net/pterodactylus/sone/test/TestUtil.java b/src/test/java/net/pterodactylus/sone/test/TestUtil.java
index 8b3160f..745733f 100644
--- a/src/test/java/net/pterodactylus/sone/test/TestUtil.java
+++ b/src/test/java/net/pterodactylus/sone/test/TestUtil.java
@@ -1,17 +1,20 @@
package net.pterodactylus.sone.test;
+import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
+import kotlin.Deprecated;
+import kotlin.ReplaceWith;
+
/**
* Utilities for testing.
- *
- * @author David âBombeâ Roden
*/
public class TestUtil {
+ @Deprecated(message = "It only checks the given class, not its superclasses.", replaceWith = @ReplaceWith(imports = { "net.pterodactylus.sone.test" }, expression = "setField(`object`, fieldName, value)"))
public static void setFinalField(Object object, String fieldName, Object value) {
try {
Field clientCoreField = object.getClass().getField(fieldName);
@@ -20,9 +23,7 @@ public class TestUtil {
modifiersField.setAccessible(true);
modifiersField.setInt(clientCoreField, clientCoreField.getModifiers() & ~Modifier.FINAL);
clientCoreField.set(object, value);
- } catch (NoSuchFieldException e) {
- throw new RuntimeException(e);
- } catch (IllegalAccessException e) {
+ } catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
@@ -32,9 +33,7 @@ public class TestUtil {
Field field = object.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
return (T) field.get(object);
- } catch (NoSuchFieldException e) {
- throw new RuntimeException(e);
- } catch (IllegalAccessException e) {
+ } catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
@@ -44,11 +43,17 @@ public class TestUtil {
Method method = object.getClass().getDeclaredMethod(methodName, new Class[0]);
method.setAccessible(true);
return (T) method.invoke(object);
- } catch (NoSuchMethodException e) {
- throw new RuntimeException(e);
- } catch (InvocationTargetException e) {
+ } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
- } catch (IllegalAccessException e) {
+ }
+ }
+
+ public static T createObject(Class clazz, Class[] parameterTypes, Object... arguments) {
+ try {
+ Constructor constructor = clazz.getDeclaredConstructor(parameterTypes);
+ constructor.setAccessible(true);
+ return constructor.newInstance(arguments);
+ } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}