diff --git a/TinyProtocol/src/main/java/com/comphenix/tinyprotocol/ConstructorInvokerHelper.java b/TinyProtocol/src/main/java/com/comphenix/tinyprotocol/ConstructorInvokerHelper.java new file mode 100644 index 000000000..3cdb05d44 --- /dev/null +++ b/TinyProtocol/src/main/java/com/comphenix/tinyprotocol/ConstructorInvokerHelper.java @@ -0,0 +1,19 @@ +package com.comphenix.tinyprotocol; + +import java.lang.reflect.Constructor; + +public class ConstructorInvokerHelper { + private final Constructor constructor; + + public ConstructorInvokerHelper(String className, Class... params) { + this.constructor = Reflection.getConstructor(Reflection.getClass(className), params); + } + + public Object invoke(Object... arguments) { + try { + return constructor.newInstance(arguments); + } catch (Exception e) { + throw new RuntimeException("Cannot invoke constructor " + constructor, e); + } + } +} diff --git a/TinyProtocol/src/main/java/com/comphenix/tinyprotocol/FieldAccessorHelper.java b/TinyProtocol/src/main/java/com/comphenix/tinyprotocol/FieldAccessorHelper.java new file mode 100644 index 000000000..9c7956f15 --- /dev/null +++ b/TinyProtocol/src/main/java/com/comphenix/tinyprotocol/FieldAccessorHelper.java @@ -0,0 +1,34 @@ +package com.comphenix.tinyprotocol; + +import java.lang.reflect.Field; + +public class FieldAccessorHelper implements Reflection.FieldAccessor { + private final Field field; + + public FieldAccessorHelper(Class target, String name, Class fieldType) { + this.field = Reflection.getField(target, name, fieldType, 0); + } + + @Override + public T get(Object target) { + try { + return (T) field.get(target); + } catch (IllegalAccessException e) { + throw new RuntimeException("Cannot access reflection.", e); + } + } + + @Override + public void set(Object target, T value) { + try { + field.set(target, value); + } catch (IllegalAccessException e) { + throw new RuntimeException("Cannot access reflection.", e); + } + } + + @Override + public boolean hasField(Object target) { + return field.getDeclaringClass().isAssignableFrom(target.getClass()); + } +} diff --git a/TinyProtocol/src/main/java/com/comphenix/tinyprotocol/MethodInvokerHelper.java b/TinyProtocol/src/main/java/com/comphenix/tinyprotocol/MethodInvokerHelper.java new file mode 100644 index 000000000..8a0cc4399 --- /dev/null +++ b/TinyProtocol/src/main/java/com/comphenix/tinyprotocol/MethodInvokerHelper.java @@ -0,0 +1,19 @@ +package com.comphenix.tinyprotocol; + +import java.lang.reflect.Method; + +public class MethodInvokerHelper { + private final Method method; + + public MethodInvokerHelper(String className, String methodName, Class... params) { + this.method = Reflection.getTypedMethod(Reflection.getClass(className), methodName, null, params); + } + + public Object invoke(Object target, Object... arguments) { + try { + return method.invoke(target, arguments); + } catch (Exception e) { + throw new RuntimeException("Cannot invoke method " + method, e); + } + } +}