本文整理匯總了Java中org.apache.commons.beanutils.MethodUtils類的典型用法代碼示例。如果您正苦於以下問題:Java MethodUtils類的具體用法?Java MethodUtils怎麽用?Java MethodUtils使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
MethodUtils類屬於org.apache.commons.beanutils包,在下文中一共展示了MethodUtils類的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: loadOjbRepositoryFiles
import org.apache.commons.beanutils.MethodUtils; //導入依賴的package包/類
/**
* This method is deprecated and won't do anything if the database repository file paths are null or empty.
*
* We use reflection here to avoid having to reference PersistenceService directly since it may or may not be on
* our classpath depending on whether or not KSB is in use.
*/
@Deprecated
protected void loadOjbRepositoryFiles() {
String persistenceServiceOjbName = "persistenceServiceOjb";
if (getDatabaseRepositoryFilePaths() != null) {
for (String repositoryLocation : getDatabaseRepositoryFilePaths()) {
// Need the OJB persistence service because it is the only one ever using the database repository files
if (getPersistenceService() == null) {
setPersistenceService(GlobalResourceLoader.getService(persistenceServiceOjbName));
}
if (persistenceService == null) {
setPersistenceService(applicationContext.getBean(persistenceServiceOjbName));
}
LOG.warn("Loading OJB Configuration in "
+ getNamespaceCode()
+ " module. OJB is deprecated as of Rice 2.4: "
+ repositoryLocation);
try {
MethodUtils.invokeExactMethod(persistenceService, "loadRepositoryDescriptor", repositoryLocation);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
示例5: getProperty
import org.apache.commons.beanutils.MethodUtils; //導入依賴的package包/類
private static <T extends Number> List<T> getProperty(Collection<?> values, String property, T rawValue) {
if (values == null || values.isEmpty()) {
return Collections.<T> emptyList();
}
List<T> result = new ArrayList<T>(values.size());
for (Object value : values) {
try {
String propertyValue = value.toString();
if (property != null) {
propertyValue = BeanUtils.getProperty(value, property);
}
Object key = MethodUtils.invokeExactMethod(rawValue, "valueOf", propertyValue);
result.add((T) key);
} catch (Exception e) {
logger.error(e.getMessage(), e);
continue;
}
}
return result;
}
示例6: getCurrentStatus
import org.apache.commons.beanutils.MethodUtils; //導入依賴的package包/類
/**
* Retrieves the current platform status. The object returned will be created using the current (webapp) classlaoder,
* preventing casting issues.
* @return the current status of the platform, for safe use with the webapp classloader
*/
public PlatformStatus getCurrentStatus() {
Object mgr = getStatusManager();
if (mgr == null) {
LOGGER.debug("Status manager unavailable");
return new PlatformStatus();
} else {
try {
Object status = MethodUtils.invokeExactMethod(mgr, "getCurrentStatus", new Object[0]);
return convertStatusBetweenClassLoaders(status);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw new StatusProxyException("Unable to retrieve status from the manager instance", e);
}
}
}
示例7: 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);
}
}
示例8: 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;
}
示例9: 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;
}
示例10: 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;
}
示例11: 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;
}
示例12: LogbackFactoryAccessor
import org.apache.commons.beanutils.MethodUtils; //導入依賴的package包/類
/**
* Attempts to initialize a Logback logger factory via the given class loader.
*
* @param cl the ClassLoader to use when fetching the factory
*/
public LogbackFactoryAccessor(ClassLoader cl) throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// Get the singleton SLF4J binding, which may or may not be Logback, depending on the
// binding.
Class clazz = cl.loadClass("org.slf4j.impl.StaticLoggerBinder");
Method m1 = MethodUtils.getAccessibleMethod(clazz, "getSingleton", new Class[]{});
Object singleton = m1.invoke(null, null);
Method m = MethodUtils.getAccessibleMethod(clazz, "getLoggerFactory", new Class[]{});
Object loggerFactory = m.invoke(singleton, null);
// Check if the binding is indeed Logback
Class loggerFactoryClass = cl.loadClass("ch.qos.logback.classic.LoggerContext");
if (!loggerFactoryClass.isInstance(loggerFactory)) {
throw new RuntimeException("The singleton SLF4J binding was not Logback");
}
setTarget(loggerFactory);
}
示例13: getLogger
import org.apache.commons.beanutils.MethodUtils; //導入依賴的package包/類
/**
* Returns the Logback logger with a given name.
*
* @return the Logger with the given name
*/
public LogbackLoggerAccessor getLogger(String name) {
try {
Class clazz = getTarget().getClass();
Method m = MethodUtils.getAccessibleMethod(clazz, "getLogger", new Class[] {String.class});
Object logger = m.invoke(getTarget(), new Object[] {name});
if (logger == null) {
throw new NullPointerException(getTarget() + ".getLogger(\"" + name + "\") returned null");
}
LogbackLoggerAccessor accessor = new LogbackLoggerAccessor();
accessor.setTarget(logger);
accessor.setApplication(getApplication());
return accessor;
} catch (Exception e) {
log.error(getTarget() + ".getLogger(\"" + name + "\") failed", e);
}
return null;
}
示例14: getAppenders
import org.apache.commons.beanutils.MethodUtils; //導入依賴的package包/類
/**
* Returns a list of wrappers for all Logback appenders that have an
* associated logger.
*
* @return a list of {@link LogbackAppenderAccessor}s representing all
* appenders that are in use
*/
public List getAppenders() {
List appenders = new ArrayList();
try {
Class clazz = getTarget().getClass();
Method m = MethodUtils.getAccessibleMethod(clazz, "getLoggerList", new Class[]{});
List loggers = (List) m.invoke(getTarget(), null);
Iterator it = loggers.iterator();
while (it.hasNext()) {
LogbackLoggerAccessor accessor = new LogbackLoggerAccessor();
accessor.setTarget(it.next());
accessor.setApplication(getApplication());
appenders.addAll(accessor.getAppenders());
}
} catch (Exception e) {
log.error(getTarget() + ".getLoggerList() failed", e);
}
return appenders;
}
示例15: getRootLogger
import org.apache.commons.beanutils.MethodUtils; //導入依賴的package包/類
public Log4JLoggerAccessor getRootLogger() {
try {
Class clazz = (Class) getTarget();
Method m = MethodUtils.getAccessibleMethod(clazz, "getRootLogger", new Class[]{});
Object logger = m.invoke(null, null);
if (logger == null) {
throw new NullPointerException(getTarget().getClass().getName() + "#getRootLogger() returned null");
}
Log4JLoggerAccessor accessor = new Log4JLoggerAccessor();
accessor.setTarget(logger);
accessor.setApplication(getApplication());
return accessor;
} catch (Exception e) {
log.error(getTarget().getClass().getName() + "#getRootLogger() failed", e);
}
return null;
}