本文整理匯總了Java中org.apache.commons.beanutils.MethodUtils.invokeMethod方法的典型用法代碼示例。如果您正苦於以下問題:Java MethodUtils.invokeMethod方法的具體用法?Java MethodUtils.invokeMethod怎麽用?Java MethodUtils.invokeMethod使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.beanutils.MethodUtils
的用法示例。
在下文中一共展示了MethodUtils.invokeMethod方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: toMap
import org.apache.commons.beanutils.MethodUtils; //導入方法依賴的package包/類
/**
* 將一個JavaBean對象的所有屬性封裝在Map中並返回,
* 注意隻能將擁有Getter方法的屬性才能取出並封裝到Map中
*
* @param bean 需要轉換的Bean對象
* @return 返回一個Map , 失敗返回null
*/
public static <T extends Object> Map<String, Object> toMap(T bean) {
Map<String, Object> map = null;
try {
map = PropertyUtils.describe(bean);
map.remove("class");
StringBuilder methodName = new StringBuilder("get");
for (String property : map.keySet()) {
methodName.delete(3, methodName.length()).append(property.substring(0, 1).toUpperCase())
.append(property.substring(1));
Object o = MethodUtils.invokeMethod(bean, methodName.toString(), null);
map.put(property, o);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return map;
}
示例2: execute
import org.apache.commons.beanutils.MethodUtils; //導入方法依賴的package包/類
public void execute(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/xml;charset=utf8");
PrintWriter pw = response.getWriter();
String beanId = (String) request.getParameter("beanId");
String methodName = (String) request.getParameter("methodName");
String parameter = (String) request.getParameter("parameter");
if (StringUtils.isNotEmpty(beanId) && StringUtils.isNotEmpty(methodName)) {
try {
Object object = DoradoContext.getAttachedWebApplicationContext().getBean(beanId);
Object[] arguments = null;
if (StringUtils.isNotEmpty(parameter)) {
arguments = (Object[]) SerializationUtils.deserialize(new Base64(true).decode(parameter));
}
MethodUtils.invokeMethod(object, methodName, arguments);
log.info("invoke refreshCache method [" + beanId + "#" + methodName + "] success");
pw.print("ok");
} catch (Exception e) {
log.error("invoke refreshCache method [" + beanId + "#" + methodName + "] error," + e.getMessage());
pw.print("error");
}
}
}
示例3: executeMethod
import org.apache.commons.beanutils.MethodUtils; //導入方法依賴的package包/類
private static Object executeMethod(Object o, String methodName) {
Object newObject = null;
try {
if (o instanceof RepositoryItem) {
RepositoryItem repositoryItem = (RepositoryItem) o;
if (repositoryItem.getItemDescriptor().hasProperty(methodName)) {
newObject = repositoryItem.getPropertyValue(methodName);
}
} else if (o instanceof Map) {
Map map = (Map) o;
newObject = map.get(methodName);
} else {
methodName = "get" + upperCaseFirstLetter(methodName);
newObject = MethodUtils.invokeMethod(o, methodName, null);
}
} catch (Exception e) {
// dont care
}
return newObject;
}
示例4: createDir
import org.apache.commons.beanutils.MethodUtils; //導入方法依賴的package包/類
@Override
public Vfs.Dir createDir(URL url) {
try {
File tmpFile = File.createTempFile("vfs-jndi-bundle", ".jar");
// we need to load this resource from the server context using the server ClassLoader
// we use reflections to call the service exposed by the server
BundleContext bundleContext = FrameworkUtil.getBundle(JndiUrlType.class).getBundleContext();
Object jndiService = OSGiServiceUtils.findService(bundleContext, JNDI_SERVICE);
// copy to the resource to the temp file
MethodUtils.invokeMethod(jndiService, WRITE_TO_FILE_METHOD,
new Object[] {url.toString(), tmpFile.getAbsolutePath()});
return new TmpDir(tmpFile);
} catch (IOException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
throw new IllegalArgumentException("Unable to create mvn url for " + url, e);
}
}
示例5: getHandlers
import org.apache.commons.beanutils.MethodUtils; //導入方法依賴的package包/類
public List getHandlers() {
List handlerAccessors = new ArrayList();
try {
Object handlers[] = (Object[]) MethodUtils.invokeMethod(getTarget(), "getHandlers", null);
for (int h = 0; h < handlers.length; h++) {
Object handler = handlers[h];
Jdk14HandlerAccessor handlerAccessor = wrapHandler(handler, h);
if (handlerAccessor != null) {
handlerAccessors.add(handlerAccessor);
}
}
} catch (Exception e) {
log.error(getTarget().getClass().getName() + "#handlers inaccessible", e);
}
return handlerAccessors;
}
示例6: getLevel
import org.apache.commons.beanutils.MethodUtils; //導入方法依賴的package包/類
public String getLevel() {
try {
Object level = null;
Object target = getTarget();
while (level == null && target != null) {
level = getLevelInternal(target);
target = MethodUtils.invokeMethod(target, "getParent", null);
}
if (level == null && isJuliRoot()) {
return "INFO";
} else {
return (String) MethodUtils.invokeMethod(level, "getName", null);
}
} catch (Exception e) {
log.error(getTarget().getClass().getName() + "#getLevel() failed", e);
}
return null;
}
示例7: getAppenders
import org.apache.commons.beanutils.MethodUtils; //導入方法依賴的package包/類
/**
* Returns all appenders of this logger.
*
* @return a list of {@link LogbackAppenderAccessor}s
*/
public List getAppenders() {
List appenders = new ArrayList();
try {
Iterator it = (Iterator) MethodUtils.invokeMethod(getTarget(), "iteratorForAppenders", null);
while (it.hasNext()) {
Object appender = it.next();
List siftedAppenders = getSiftedAppenders(appender);
if (siftedAppenders != null) {
for (int i = 0; i < siftedAppenders.size(); i++) {
Object siftedAppender = siftedAppenders.get(i);
wrapAndAddAppender(siftedAppender, appenders);
}
} else {
wrapAndAddAppender(appender, appenders);
}
}
} catch (Exception e) {
log.error(getTarget().getClass().getName() + "#getAppenders() failed", e);
}
return appenders;
}
示例8: getAppender
import org.apache.commons.beanutils.MethodUtils; //導入方法依賴的package包/類
/**
* Returns the appender of this logger with the given name.
*
* @param name the name of the appender to return
* @return the appender with the given name, or null if no such
* appender exists for this logger
*/
public LogbackAppenderAccessor getAppender(String name) {
try {
Object appender = MethodUtils.invokeMethod(getTarget(), "getAppender", name);
if (appender == null) {
List appenders = getAppenders();
for (int i = 0; i < appenders.size(); i++) {
LogbackAppenderAccessor wrappedAppender = (LogbackAppenderAccessor) appenders.get(i);
if (wrappedAppender.getIndex().equals(name)) {
return wrappedAppender;
}
}
}
return wrapAppender(appender);
} catch (Exception e) {
log.error(getTarget().getClass().getName() + "#getAppender() failed", e);
}
return null;
}
示例9: provide
import org.apache.commons.beanutils.MethodUtils; //導入方法依賴的package包/類
@Override
public Object provide(Object source, Object currentValue) {
ExecutionYear executionYear;
try {
executionYear = (ExecutionYear) MethodUtils.invokeMethod(source, "getExecutionYear", null);
} catch (Exception e) {
throw new RuntimeException(e);
}
List<ExecutionSemester> periods = new ArrayList<ExecutionSemester>();
if (executionYear != null) {
periods.addAll(executionYear.getExecutionPeriodsSet());
}
return periods;
}
示例10: invokeFactoryMethod
import org.apache.commons.beanutils.MethodUtils; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
protected T invokeFactoryMethod(Object factory)
{
try
{
return (T) MethodUtils.invokeMethod(factory, getMethodName(), getArgs());
}
catch( Exception e )
{
throw new RuntimeException(e);
}
}
示例11: replaceField
import org.apache.commons.beanutils.MethodUtils; //導入方法依賴的package包/類
public static void replaceField(Customer current, Customer to) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Field[] fields = Customer.class.getFields();
for (Field field : fields) {
if (!"id".equalsIgnoreCase(field.getName())) {
String getter = "get" + StringUtils.capitalize(field.getName());
String setter = "set" + StringUtils.capitalize(field.getName());
Object value = MethodUtils.invokeMethod(to, getter, ArrayUtils.EMPTY_OBJECT_ARRAY);
if (value != null) {
MethodUtils.invokeMethod(current, setter, new Object[]{value});
}
}
}
}
示例12: invokeMethod
import org.apache.commons.beanutils.MethodUtils; //導入方法依賴的package包/類
/**
* Invoca un metodo sobre un objeto
* @param obj El objeto sobre el que se invoca el metodo (si es null se intenta llamar a un m�todo est�tico)
* @param methodName nombre del metodo a invocar
* @param argsTypes tipos de los argumentos a invocar
* @param argsValues valores de los argumentos
* @return El objeto devuelto tras la invocacion del metodo
* @throws ReflectionException si ocurre algun error
*/
@SuppressWarnings("unchecked")
public static <T> T invokeMethod(final Object obj,
final String methodName,final Class<?>[] argsTypes,final Object[] argsValues) {
if (obj == null || methodName == null) throw new IllegalArgumentException(Throwables.message("Cannot invoke {} method on null object instance",
(methodName != null ? methodName : "null"),
(obj != null ? obj.getClass() : "null")));
try {
return (T)MethodUtils.invokeMethod(obj,methodName,argsValues,argsTypes);
} catch (Throwable th) {
throw ReflectionException.of(th);
}
}
示例13: moveToTrash
import org.apache.commons.beanutils.MethodUtils; //導入方法依賴的package包/類
@Override
@Transactional
public void moveToTrash(Object instance, Long entityVersion) {
Class<?> trashClass = HistoryTrashClassHelper.getClass(instance, EntityType.TRASH,
getBundleContext());
if (null != trashClass) {
LOGGER.debug("Moving {} to trash", instance);
// create and save a trash instance
LOGGER.debug("Creating trash instance for: {}", instance);
Object trash = create(trashClass, instance, null);
LOGGER.debug("Created trash instance for: {}", instance);
try {
MethodUtils.invokeMethod(trash, "setSchemaVersion", entityVersion);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
LOGGER.error("Failed to set schema version of the trash instance.");
}
PersistenceManager manager = getPersistenceManagerFactory().getPersistenceManager();
manager.makePersistent(trash);
} else {
throw new IllegalStateException(
"Not found the trash class for " + instance.getClass().getName()
);
}
}
示例14: getLogger
import org.apache.commons.beanutils.MethodUtils; //導入方法依賴的package包/類
public Jdk14LoggerAccessor getLogger(String name) {
try {
Object logger = MethodUtils.invokeMethod(getTarget(), "getLogger", name);
if (logger == null) {
throw new NullPointerException(getTarget().getClass().getName() + "#getLogger(\"" + name + "\") returned null");
}
Jdk14LoggerAccessor accessor = new Jdk14LoggerAccessor();
accessor.setTarget(logger);
accessor.setApplication(getApplication());
return accessor;
} catch (Exception e) {
log.error(getTarget().getClass().getName() + "#getLogger(\"" + name + "\") failed", e);
}
return null;
}
示例15: setLevel
import org.apache.commons.beanutils.MethodUtils; //導入方法依賴的package包/類
public void setLevel(String newLevelStr) {
try {
Object level = MethodUtils.invokeMethod(getTarget(), "getLevel", null);
Object newLevel = MethodUtils.invokeMethod(level, "parse", newLevelStr);
MethodUtils.invokeMethod(getTarget(), "setLevel", newLevel);
} catch (Exception e) {
log.error(getTarget().getClass().getName() + "#setLevel(\"" + newLevelStr + "\") failed", e);
}
}