Code, JavaEE, SOA

Dynamic Method Invocation – Java Reflection

This code is the beginning of a basic CRUD pattern implementation, where the POJO (User) has a handler (UserHandler) that processes the required business logic related to the POJO. The basic type is the interface DynamicMethodInvoker that accepts a type T that is the return type for the method invocation. The default implementation of the DynamicMethodInvoker is the abstract class AbstractDynamicMethodInvoker. The handler (UserHandler) will subclass the AbstractDynamicMethodInvoker and implement an interface named CrudProcessor. CrudProcessor accepts two generic types, that is T, representing the POJO and R, representing the return type for the create, update and delete methods. The code follows:

DynamicMethodInvoker

/**
 * DynamicMethodInvoker
 * User: David King
 */
public interface DynamicMethodInvoker<R> {

/**
 * invokeMethod
 *
 * Invoke a method on the given objectInstance with a return type of R
 *
 * @param objectInstance
 * @param methodName
 * @param methodParameterTypes
 * @param methodParameterValues
 * @return
 */
 public R invokeMethod(Object objectInstance,
 String methodName,
 Class[] methodParameterTypes,
 Object[] methodParameterValues);

/**
 * invokeVoidMethod
 *
 * This method invocation does not have a return
 *
 * @param objectInstance
 * @param methodName
 * @param methodParameterTypes
 * @param methodParameterValues
 */
 public void invokeVoidMethod(Object objectInstance,
 String methodName,
 Class[] methodParameterTypes,
 Object[] methodParameterValues);
}

AbstractDynamicMethodInvoker

import metadata.CrudResponse;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * AbstractDynamicMethodInvoker
 * User: David King
 */
public abstract class AbstractDynamicMethodInvoker implements DynamicMethodInvoker<CrudResponse> {

@Override
 public CrudResponse invokeMethod(Object objectInstance, String methodName, Class[] methodParameterTypes, Object[] methodParameterValues) {
 CrudResponse response = new CrudResponse();
 Method methodToInvoke = null;

try {
 methodToInvoke = objectInstance.getClass().getMethod(methodName, methodParameterTypes);
 response = response.getClass().cast(methodToInvoke.invoke(objectInstance, methodParameterValues));

if (response.isErrorCondition()) {
 return response;
 }
 } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
 e.printStackTrace();
 }

return response;
 }

@Override
 public void invokeVoidMethod(Object objectInstance, String methodName, Class[] methodParameterTypes, Object[] methodParameterValues) {
 Method methodToInvoke = null;

try {
 methodToInvoke = objectInstance.getClass().getMethod(methodName, methodParameterTypes);
 methodToInvoke.invoke(objectInstance, methodParameterValues);
 } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
 e.printStackTrace();
 }
 }
}

CrudProcessor


import java.util.Collection;

/**
 * CrudProcessor
 * User: David King
 */
public interface CrudProcessor<T, R> {
 public R create(T t);
 public R update(T t);
 public R delete(T t);
 public T view(String id);
 public Collection<T> viewAll(int limitCount);
}

User


import java.io.Serializable;
import java.util.UUID;

/**
 * User
 * User: David King
 */
public class User implements Serializable {
 private String id;
 private String firstName;
 private String lastName;
 private String email;

public User() {
 setId(UUID.randomUUID().toString());
 }

public User(String id, String firstName, String lastName, String email) {
 this.id = id;
 this.firstName = firstName;
 this.lastName = lastName;
 this.email = email;
 }

public String getId() {
 return id;
 }

public void setId(String id) {
 if (null != id) {
 this.id = id;
 }
 }

public String getFirstName() {
 return firstName;
 }

public void setFirstName(String firstName) {
 this.firstName = firstName;
 }

public String getLastName() {
 return lastName;
 }

public void setLastName(String lastName) {
 this.lastName = lastName;
 }

public String getEmail() {
 return email;
 }

public void setEmail(String email) {
 this.email = email;
 }

@Override
 public boolean equals(Object o) {
 if (this == o) return true;
 if (o == null || getClass() != o.getClass()) return false;

User user = (User) o;

if (!email.equals(user.email)) return false;
 if (!firstName.equals(user.firstName)) return false;
 if (!id.equals(user.id)) return false;
 if (!lastName.equals(user.lastName)) return false;

return true;
 }

@Override
 public int hashCode() {
 int result = id.hashCode();
 result = 31 * result + firstName.hashCode();
 result = 31 * result + lastName.hashCode();
 result = 31 * result + email.hashCode();
 return result;
 }

@Override
 public String toString() {
 return "User{" +
 "id='" + id + '\'' +
 ", firstName='" + firstName + '\'' +
 ", lastName='" + lastName + '\'' +
 ", email='" + email + '\'' +
 '}';
 }
}

UserHandler


&nbsp;

import metadata.CrudProcessor;
import metadata.CrudResponse;
import model.User;

import java.util.Collection;

/**
 * UserHandler
 * User: David King
 */
public class UserHandler extends AbstractDynamicMethodInvoker
 implements CrudProcessor<User, CrudResponse> {

public UserHandler() {}
 @Override
 public CrudResponse create(User user) {
 CrudResponse response = new CrudResponse();
 response.setErrorCondition(Boolean.FALSE);
 response.setResult("Success");
 return response;
 }

@Override
 public CrudResponse update(User user) {
 CrudResponse response = new CrudResponse();
 response.setErrorCondition(Boolean.FALSE);
 response.setResult("Success");
 return response;
 }

@Override
 public CrudResponse delete(User user) {
 CrudResponse response = new CrudResponse();
 response.setErrorCondition(Boolean.FALSE);
 response.setResult("Success");
 return response;
 }

@Override
 public User view(String id) {
 return null;
 }

@Override
 public Collection<User> viewAll(int limitCount) {
 return null;
 }
}

DynamicInvocationTest (Junit)


&nbsp;

import metadata.CrudResponse;
import model.User;
import org.junit.Test;

/**
 * DynamicInvocationTest
 * User: David King
 */
public class DynamicInvocationTest {

@Test
 public void testDynamicMethodInvocation() {
 User user = new User();
 user.setFirstName("David");
 user.setLastName("King");
 user.setEmail("david.king@somehost.com");
 AbstractDynamicMethodInvoker abstractDynamicMethodInvoker = new UserHandler();
 CrudResponse response = abstractDynamicMethodInvoker.invokeMethod(
 abstractDynamicMethodInvoker,
 "create",
 new Class[] { User.class },
 new Object[] {user});
 System.out.printf("TestDynamicMethodInvocation: %s%n", response);
 }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s