Skip to content

Commit

Permalink
added a wrapper for easy ReflectionHelper calls
Browse files Browse the repository at this point in the history
  • Loading branch information
n9Mtq4 committed Aug 19, 2015
1 parent 091955a commit 1d86ffd
Showing 1 changed file with 149 additions and 0 deletions.
149 changes: 149 additions & 0 deletions src/main/java/com/n9mtq4/reflection/ReflectionWrapper.java
Original file line number Diff line number Diff line change
@@ -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.<br>
* an example:<br>
* <code>
* TestObject obj = new TestObject();<br>
* //these next two lines are the same.<br>
* ReflectionHelper.callVoidMethod("printStuff", obj);<br>
* ReflectionWrapper.attachToObject(obj).callMethod("printStuff");<br>
* </code>
* Created by will on 8/19/15 at 11:59 AM.
*
* @param <E> the type of the object that the wrapper contains
* @see ReflectionHelper
* @since v1.1
*/
@SuppressWarnings("unused")
public class ReflectionWrapper<E> implements Serializable {

/**
* Wraps a new instance of an object (E) with the ReflectionWrapper.
*
* @param <E> 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 <E> ReflectionWrapper<E> newInstance(Class clazz, Class[] classParams, Object... args) {
E object = callConstructor(clazz, classParams, args);
return new ReflectionWrapper<E>(object);
}

/**
* Wraps a new instance of an object (E) with the ReflectionWrapper.
*
* @param <E> 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 <E> ReflectionWrapper<E> newInstance(Class clazz, Object... args) {
E object = callConstructor(clazz, args);
return new ReflectionWrapper<E>(object);
}

/**
* Wraps an existing object with the ReflectionWrapper.
*
* @param <E> the type of the object
* @param object the object
* @return the reflection wrapper
*/
public static <E> ReflectionWrapper<E> attachToObject(E object) {
return new ReflectionWrapper<E>(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> R getField(String fieldName) {
return getObject(fieldName, object);
}

/**
* Gets a member field from the object.
*
* @param field the field
* @return the field
*/
public <R> R getField(Field field) {
return getObject(field, object);
}

/**
* Calls a member method from the object.
*
* @param <R> 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> R callMethod(String name, Object... args) {
return callObjectMethod(name, object, args);
}

/**
* Calls a member method from the object.
*
* @param <R> 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> R callMethod(String name, Class[] classParams, Object... args) {
return callObjectMethod(name, object, classParams, args);
}

/**
* Calls a member method from the object.
*
* @param <R> 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> 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();
}

}

0 comments on commit 1d86ffd

Please sign in to comment.