Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.comphenix.tinyprotocol;

import java.lang.reflect.Field;

public class FieldAccessorHelper<T> implements Reflection.FieldAccessor<T> {
private final Field field;

public FieldAccessorHelper(Class<?> target, String name, Class<T> 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());
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}