X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftest%2FTestUtil.java;h=35e8202b37905d21c59f41833fad15f7c3a1b250;hp=8b3160f2cbed45266c874582915ef31fb90ef740;hb=b9d12d4a287728f062c541d02887c39c24117217;hpb=eb32457356fac09e16ec98394c1b5d48f9dfba84 diff --git a/src/test/java/net/pterodactylus/sone/test/TestUtil.java b/src/test/java/net/pterodactylus/sone/test/TestUtil.java index 8b3160f..35e8202 100644 --- a/src/test/java/net/pterodactylus/sone/test/TestUtil.java +++ b/src/test/java/net/pterodactylus/sone/test/TestUtil.java @@ -1,5 +1,6 @@ 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; @@ -7,8 +8,6 @@ import java.lang.reflect.Modifier; /** * Utilities for testing. - * - * @author David ‘Bombe’ Roden */ public class TestUtil { @@ -20,9 +19,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 +29,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 +39,17 @@ public class TestUtil { Method method = object.getClass().getDeclaredMethod(methodName, new Class[0]); method.setAccessible(true); return (T) method.invoke(object); - } catch (NoSuchMethodException e) { + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); - } catch (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); } }