From 1d86ffd1a6a611b02a2ed7a8f5d2e2379e139c0d Mon Sep 17 00:00:00 2001 From: n9Mtq4 Date: Wed, 19 Aug 2015 13:35:51 -0400 Subject: [PATCH] added a wrapper for easy ReflectionHelper calls --- .../n9mtq4/reflection/ReflectionWrapper.java | 149 ++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 src/main/java/com/n9mtq4/reflection/ReflectionWrapper.java diff --git a/src/main/java/com/n9mtq4/reflection/ReflectionWrapper.java b/src/main/java/com/n9mtq4/reflection/ReflectionWrapper.java new file mode 100644 index 0000000..392d8e1 --- /dev/null +++ b/src/main/java/com/n9mtq4/reflection/ReflectionWrapper.java @@ -0,0 +1,149 @@ +package com.n9mtq4.reflection; + +import java.io.Serializable; +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +import static com.n9mtq4.reflection.ReflectionHelper.*; + +/** + * A class that when given an object, it will simplify the calls to ReflectionHelper + * by automatically passing the obj param to the ReflectionHelper methods. + * This class takes the static methods from ReflectionHelper and + * implements them in an instance format.
+ * an example:
+ * + * TestObject obj = new TestObject();
+ * //these next two lines are the same.
+ * ReflectionHelper.callVoidMethod("printStuff", obj);
+ * ReflectionWrapper.attachToObject(obj).callMethod("printStuff");
+ *
+ * Created by will on 8/19/15 at 11:59 AM. + * + * @param the type of the object that the wrapper contains + * @see ReflectionHelper + * @since v1.1 + */ +@SuppressWarnings("unused") +public class ReflectionWrapper implements Serializable { + + /** + * Wraps a new instance of an object (E) with the ReflectionWrapper. + * + * @param the type of the object + * @param clazz the class to make a new instance of + * @param classParams an array of classes that the args are + * @param args the args to pass to the class' constructor + * @return the reflection wrapper + */ + public static ReflectionWrapper newInstance(Class clazz, Class[] classParams, Object... args) { + E object = callConstructor(clazz, classParams, args); + return new ReflectionWrapper(object); + } + + /** + * Wraps a new instance of an object (E) with the ReflectionWrapper. + * + * @param the type of the object + * @param clazz the class to make a new instance of + * @param args the args to pass to the class' constructor + * @return the reflection wrapper + */ + public static ReflectionWrapper newInstance(Class clazz, Object... args) { + E object = callConstructor(clazz, args); + return new ReflectionWrapper(object); + } + + /** + * Wraps an existing object with the ReflectionWrapper. + * + * @param the type of the object + * @param object the object + * @return the reflection wrapper + */ + public static ReflectionWrapper attachToObject(E object) { + return new ReflectionWrapper(object); + } + + private E object; + + private ReflectionWrapper(E object) { + this.object = object; + } + + /** + * Gets a member field from the object. + * + * @param fieldName the field name + * @return the field + */ + public R getField(String fieldName) { + return getObject(fieldName, object); + } + + /** + * Gets a member field from the object. + * + * @param field the field + * @return the field + */ + public R getField(Field field) { + return getObject(field, object); + } + + /** + * Calls a member method from the object. + * + * @param what the method returns + * @param name the name of the method + * @param args the args for the method + * @return the return of the method, null if the method is void + */ + public R callMethod(String name, Object... args) { + return callObjectMethod(name, object, args); + } + + /** + * Calls a member method from the object. + * + * @param what the method returns + * @param name the name of the method + * @param classParams the class params + * @param args the args for the method + * @return the return of the method, null if the method is void + */ + public R callMethod(String name, Class[] classParams, Object... args) { + return callObjectMethod(name, object, classParams, args); + } + + /** + * Calls a member method from the object. + * + * @param what the method returns + * @param method the method to call + * @param args the args for the method + * @return the return of the method, null if the method is void + */ + public R callMethod(Method method, Object... args) { + return callObjectMethod(method, object, args); + } + + + /** + * Gets the object that the wrapper contains. + * + * @return the object the wrapper contains + */ + public E getWrappedObject() { + return object; + } + + /** + * @see Object#toString() + * */ + @Override + public String toString() { + return "ReflectionWrapper: " + object.toString(); + } + +}