List of usage examples for org.apache.commons.lang.reflect MethodUtils invokeMethod
public static Object invokeMethod(Object object, String methodName, Object[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
Invoke a named method whose parameter type matches the object type.
This method delegates the method search to #getMatchingAccessibleMethod(Class,String,Class[]) .
This method supports calls to methods taking primitive parameters via passing in wrapping classes.
From source file:com.ocs.dynamo.utils.ClassUtils.java
/** * @param obj// w w w . j a v a 2 s.co m * @param fieldName * @param value */ public static void setFieldValue(Object obj, String fieldName, Object value) { try { int p = fieldName.indexOf("."); if (p >= 0) { String firstProperty = fieldName.substring(0, p); Object first = MethodUtils.invokeMethod(obj, GET + StringUtils.capitalize(firstProperty), new Object[] {}); if (first != null) { setFieldValue(first, fieldName.substring(p + 1), value); } } else { MethodUtils.invokeMethod(obj, SET + StringUtils.capitalize(fieldName), new Object[] { value }); } } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { LOG.error(e.getMessage(), e); throw new OCSRuntimeException(e.getMessage(), e); } }
From source file:com.googlecode.psiprobe.AbstractTomcatContainer.java
protected JspCompilationContext createJspCompilationContext(String name, boolean isErrPage, Options opt, ServletContext sctx, JspRuntimeContext jrctx, ClassLoader cl) { JspCompilationContext jcctx = new JspCompilationContext(name, false, opt, sctx, null, jrctx); if (cl != null && cl instanceof URLClassLoader) { try {/*from w w w. ja v a 2 s . co m*/ jcctx.setClassLoader((URLClassLoader) cl); } catch (NoSuchMethodError err) { //JBoss Web 2.1 has a different method signature for setClassLoader(). try { MethodUtils.invokeMethod(jcctx, "setClassLoader", cl); } catch (NoSuchMethodException ex) { throw new RuntimeException(ex); } catch (IllegalAccessException ex) { throw new RuntimeException(ex); } catch (InvocationTargetException ex) { throw new RuntimeException(ex); } } } return jcctx; }
From source file:com.haulmont.cuba.gui.xml.layout.loaders.AbstractComponentLoader.java
protected String loadShortcutFromFQNConfig(String shortcut) { if (shortcut.contains("#")) { String[] splittedShortcut = shortcut.split("#"); if (splittedShortcut.length != 2) { String message = "An error occurred while loading shortcut: incorrect format of shortcut."; throw new GuiDevelopmentException(message, context.getFullFrameId()); }// w ww . j av a 2s. co m String fqnConfigName = splittedShortcut[0].substring(2); String methodName = splittedShortcut[1].substring(0, splittedShortcut[1].length() - 1); //noinspection unchecked Class<Config> configClass = (Class<Config>) scripting.loadClass(fqnConfigName); if (configClass != null) { Config config = configuration.getConfig(configClass); try { String shortcutValue = (String) MethodUtils.invokeMethod(config, methodName, null); if (StringUtils.isNotEmpty(shortcutValue)) { return shortcutValue; } } catch (NoSuchMethodException e) { String message = String.format( "An error occurred while loading shortcut: " + "can't find method \"%s\" in \"%s\"", methodName, fqnConfigName); throw new GuiDevelopmentException(message, context.getFullFrameId()); } catch (IllegalAccessException | InvocationTargetException e) { String message = String.format( "An error occurred while loading shortcut: " + "can't invoke method \"%s\" in \"%s\"", methodName, fqnConfigName); throw new GuiDevelopmentException(message, context.getFullFrameId()); } } else { String message = String.format( "An error occurred while loading shortcut: " + "can't find config interface \"%s\"", fqnConfigName); throw new GuiDevelopmentException(message, context.getFullFrameId()); } } return null; }
From source file:org.apache.niolex.commons.reflect.MethodUtil.java
/** * <p>Invoke a named method whose parameter type matches the object type.</p> * * <p>This method delegates directly to {@link MethodUtils#invokeMethod(Object, String, Object[])}.</p> * * <p>This method supports calls to methods taking primitive parameters * via passing in wrapping classes. So, for example, a <code>Boolean</code> object * would match a <code>boolean</code> primitive.</p> * * <b>This method only work for public/protected/package access methods.</b> * * @param object invoke method on this object * @param methodName get method with this name * @param args use these arguments// w ww . j a v a2 s. co m * @return The value returned by the invoked method * * @throws NoSuchMethodException if there is no such accessible method * @throws InvocationTargetException wraps an exception thrown by the method invoked * @throws IllegalAccessException if the requested method is not accessible via reflection * * @see #invokeMethod(Object, String, Object...) */ public static Object invokePublicMethod(Object object, String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { return MethodUtils.invokeMethod(object, methodName, args); }
From source file:org.bigmouth.nvwa.utils.url.URLDecoder.java
/** * URL???//from w w w. j a v a2 s .c o m * * @param <T> * @param url * ?URL? * <ul> * <li>http://www.big-mouth.cn/nvwa-utils/xml/decoder?id=123&json_data=bbb</li> * <li>/nvwa-utils/xml/decoder?id=123&json_data=bbb</li> * <li>id=123&json_data=bbb</li> * </li> * </ul> * @param cls * @return * @throws Exception */ public static <T> T decode(String url, Class<T> cls) throws Exception { if (StringUtils.isBlank(url)) return null; T t = cls.newInstance(); String string = null; if (StringUtils.contains(url, "?")) { string = StringUtils.substringAfter(url, "?"); } else { int start = StringUtils.indexOf(url, "?") + 1; string = StringUtils.substring(url, start); } Map<String, Object> attrs = Maps.newHashMap(); String[] kvs = StringUtils.split(string, "&"); for (String kv : kvs) { String[] entry = StringUtils.split(kv, "="); if (entry.length <= 1) { continue; } else if (entry.length == 2) { attrs.put(entry[0], entry[1]); } else { List<String> s = Lists.newArrayList(entry); s.remove(0); attrs.put(entry[0], StringUtils.join(s.toArray(new String[0]), "=")); } } Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { // if the field name is 'appId' String fieldName = field.getName(); String paramName = fieldName; if (field.isAnnotationPresent(Argument.class)) { paramName = field.getAnnotation(Argument.class).name(); } // select appId node Object current = attrs.get(paramName); if (null == current) { // select appid node current = attrs.get(paramName.toLowerCase()); } if (null == current) { // select APPID node current = attrs.get(paramName.toUpperCase()); } if (null == current) { // select app_id node String nodename = StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(paramName), "_") .toLowerCase(); current = attrs.get(nodename); } if (null == current) { // select APP_ID node String nodename = StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(paramName), "_") .toUpperCase(); current = attrs.get(nodename); } if (null != current) { String invokeName = StringUtils.join(new String[] { "set", StringUtils.capitalize(fieldName) }); try { MethodUtils.invokeMethod(t, invokeName, current); } catch (NoSuchMethodException e) { LOGGER.warn("NoSuchMethod-" + invokeName); } catch (IllegalAccessException e) { LOGGER.warn("IllegalAccess-" + invokeName); } catch (InvocationTargetException e) { LOGGER.warn("InvocationTarget-" + invokeName); } } } return t; }
From source file:org.bigmouth.nvwa.utils.url.URLEncoder.java
public static String encode(Object obj, String jointChar, boolean sort, Comparator<String> comparator) { StringBuilder rst = new StringBuilder(); List<String> list = Lists.newArrayList(); Field[] fields = obj.getClass().getDeclaredFields(); for (Field field : fields) { String fieldName = field.getName(); String paramName = fieldName; if (field.isAnnotationPresent(Argument.class)) { paramName = field.getAnnotation(Argument.class).name(); }/*from w ww . j a v a2 s .com*/ String invokeName = StringUtils.join(new String[] { "get", StringUtils.capitalize(fieldName) }); try { Object value = MethodUtils.invokeMethod(obj, invokeName, new Object[0]); if (null != value) list.add(StringUtils.join(new String[] { paramName, String.valueOf(value) }, "=")); } catch (NoSuchMethodException e) { LOGGER.warn("NoSuchMethod-" + invokeName); } catch (IllegalAccessException e) { LOGGER.warn("IllegalAccess-" + invokeName); } catch (InvocationTargetException e) { LOGGER.warn("InvocationTarget-" + invokeName); } } if (sort) Collections.sort(list, (null != comparator) ? comparator : String.CASE_INSENSITIVE_ORDER); for (String item : list) { rst.append(item).append(jointChar); } return StringUtils.removeEnd(rst.toString(), jointChar); }
From source file:org.bigmouth.nvwa.utils.xml.Dom4jDecoder.java
/** * <p>XML???</p>//from w w w . j av a 2 s . c om * ? * appId? * ?XMLappId?appid?APPID?app_id?APP_ID * @param <T> * @param xml * @param xpath * @param cls * @return */ public static <T> T decode(String xml, String xpath, Class<T> cls) throws Exception { if (StringUtils.isBlank(xml)) return null; T t = cls.newInstance(); Document doc = DocumentHelper.parseText(xml); Node itemNode = doc.selectSingleNode(xpath); Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { // if the field name is 'appId' String name = field.getName(); String nodename = name; if (field.isAnnotationPresent(Argument.class)) { nodename = field.getAnnotation(Argument.class).name(); } // select appId node Node current = itemNode.selectSingleNode(nodename); if (null == current) { // select appid node current = itemNode.selectSingleNode(nodename.toLowerCase()); } if (null == current) { // select APPID node current = itemNode.selectSingleNode(nodename.toUpperCase()); } if (null == current) { // select app_id node nodename = StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(nodename), "_").toLowerCase(); current = itemNode.selectSingleNode(nodename); } if (null == current) { // select APP_ID node nodename = StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(nodename), "_").toUpperCase(); current = itemNode.selectSingleNode(nodename); } if (null != current) { String invokeName = StringUtils.join(new String[] { "set", StringUtils.capitalize(name) }); try { MethodUtils.invokeMethod(t, invokeName, current.getText()); } catch (NoSuchMethodException e) { LOGGER.warn("NoSuchMethod-" + invokeName); } catch (IllegalAccessException e) { LOGGER.warn("IllegalAccess-" + invokeName); } catch (InvocationTargetException e) { LOGGER.warn("InvocationTarget-" + invokeName); } } } return t; }
From source file:org.bigmouth.nvwa.utils.xml.Dom4jEncoder.java
private static void addElement(String itemNodeName, Element parent, Object obj) { Class<?> cls = obj.getClass(); Field[] fields = cls.getDeclaredFields(); Element item = (StringUtils.isNotBlank(itemNodeName)) ? parent.addElement(itemNodeName) : parent; for (Field field : fields) { String name = field.getName(); String nodename = StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(name), "_").toLowerCase(); if (field.isAnnotationPresent(Argument.class)) { nodename = field.getAnnotation(Argument.class).name(); }//from w ww . ja v a 2s . c o m Element node = item.addElement(nodename); String invokeName = StringUtils.join(new String[] { "get", StringUtils.capitalize(name) }); try { Object value = MethodUtils.invokeMethod(obj, invokeName, new Object[0]); if (null != value) { if (value instanceof String || value instanceof Double || value instanceof Float || value instanceof Long || value instanceof Integer || value instanceof Short || value instanceof Byte || value instanceof Boolean) { node.setText(value.toString()); } else { addElement(null, node, value); } } } catch (NoSuchMethodException e) { LOGGER.warn("NoSuchMethod-" + invokeName); } catch (IllegalAccessException e) { LOGGER.warn("IllegalAccess-" + invokeName); } catch (InvocationTargetException e) { LOGGER.warn("InvocationTarget-" + invokeName); } } }
From source file:org.diffkit.diff.conf.DKApplication.java
@SuppressWarnings("unchecked") private static void loadDropinJars() { Logger userLog = DKRuntime.getInstance().getUserLog(); File dropinDir = DKRuntime.getInstance().getDropinDir(); if ((dropinDir == null) || !dropinDir.isDirectory()) { userLog.info("no dropin dir"); return;/*w ww .j a v a 2 s .c o m*/ } userLog.info("dropin dir->{}", dropinDir); Collection<File> jarFiles = FileUtils.listFiles(dropinDir, new String[] { "jar" }, false); if (CollectionUtils.isEmpty(jarFiles)) { userLog.info("no jar files in dropin dir"); return; } Logger systemLog = getSystemLog(); Object jarClassLoader = DKApplication.class.getClassLoader(); systemLog.debug("jarClassLoader->{}", jarClassLoader); try { MethodUtils.invokeMethod(jarClassLoader, "prependJarFiles", new Object[] { new ArrayList<File>(jarFiles) }); // jarClassLoader.prependJarFiles(new ArrayList<File>(jarFiles)); userLog.info("loaded dropin jars->{}", jarFiles); systemLog.debug("loaded dropin jars->{}", jarFiles); } catch (Exception e_) { systemLog.error(null, e_); } }
From source file:org.lunifera.runtime.common.state.impl.GlobalDataState.java
@Override public void register(Object key, Object object) { checkDisposed();// w w w. jav a 2s . c o m if (contains(key)) { // already contained return; } super.register(key, object); if (object != null) { try { MethodUtils.invokeMethod(object, "addPropertyChangeListener", this); } catch (SecurityException e) { LOGGER.warn( "Observer for dirtyState handling could not be added for " + object.getClass().getName()); } catch (IllegalAccessException e) { LOGGER.warn( "Observer for dirtyState handling could not be added for " + object.getClass().getName()); } catch (IllegalArgumentException e) { LOGGER.warn( "Observer for dirtyState handling could not be added for " + object.getClass().getName()); } catch (InvocationTargetException e) { LOGGER.warn( "Observer for dirtyState handling could not be added for " + object.getClass().getName()); } catch (NoSuchMethodException e) { LOGGER.warn( "Observer for dirtyState handling could not be added for " + object.getClass().getName()); } } }