/*
* This file is part of JCF.
* JCF is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JCF is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with JCF. If not, see <http://www.gnu.org/licenses/>.
*/
package eu.bitfish.jcf.common.util;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import eu.bitfish.jcf.processor.exception.FilterException;
/**
* This class provides utility methods for reflection operations.
*
* @author Michael Sieber
*
*/
public abstract class ReflectionUtil {
/**
* Create the getter name for a member.
*
* @param name
* The name of the member
* @return The getter for the member
*/
public static String createGetter(String name) {
assert (name != null && name.length() > 0);
StringBuffer sb = new StringBuffer(name);
sb.replace(0, 1, name.substring(0, 1).toUpperCase());
return "get" + sb.toString();
}
/**
* Get the string value of a given method.
*
* @param obj
* The object on which the method will be called
* @param method
* The method name which will be called on the object
* @return The object result of the called method
* @throws EvaluationException
* Thrown on errors during method invocation
*/
public static Object getValue(Object obj, String method)
throws FilterException {
assert (obj != null && method != null);
try {
Class<?> clazz = obj.getClass();
Method meth = clazz.getMethod(method, new Class[] {});
return meth.invoke(obj, new Object[] {});
} catch (NoSuchMethodException e) {
throw new FilterException("Method " + method + " does not exist.",
e);
} catch (SecurityException e) {
throw new FilterException("Method " + method
+ " is not accessible.", e);
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
throw new FilterException("Unable to execute the method " + method
+ " on object " + obj, e);
}
}
}