List of usage examples for org.apache.commons.lang3.reflect MethodUtils invokeMethod
public static Object invokeMethod(final Object object, final String methodName) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
Invokes a named method without parameters.
This method delegates the method search to #getMatchingAccessibleMethod(Class,String,Class[]) .
This is a convenient wrapper for #invokeMethod(Object object,String methodName,Object[] args,Class[] parameterTypes) .
From source file:com.thinkbiganalytics.servicemonitor.rest.client.cdh.ClouderaRootResourceManager.java
/** * Will attempt to get configured api version. If that fails it will fall back to API v1. *//* w w w . j a v a 2 s. c om*/ static ClouderaRootResource getRootResource(ApiRootResource apiRootResource) { String version = apiRootResource.getCurrentVersion(); Integer numericVersion = new Integer(StringUtils.substringAfter(version, "v")); RootResourceV1 rootResource = null; Integer maxVersion = 10; if (numericVersion > maxVersion) { numericVersion = maxVersion; } try { rootResource = (RootResourceV1) MethodUtils.invokeMethod(apiRootResource, "getRootV" + numericVersion); } catch (Exception ignored) { } if (rootResource == null) { LOG.info("Unable to get RootResource for version {}, returning version 1", numericVersion); rootResource = apiRootResource.getRootV1(); } return new DefaultClouderaRootResource(rootResource); }
From source file:com.thoughtworks.go.javasysmon.wrapper.Java9CompatibleCurrentProcess.java
@Override public void infanticide() { LOG.debug("Using java 9 compatible infanticide"); try {/*www . j a v a 2s . c o m*/ MethodUtils.invokeMethod(currentProcess(), "descendants"); Stream<Object> processHandles = (Stream<Object>) MethodUtils.invokeMethod(currentProcess(), "descendants"); processHandles.forEach(processHandle -> { ProcessHandleReflectionDelegate ph = new ProcessHandleReflectionDelegate(processHandle); LOG.debug("Attempting to destroy process {}", ph); if (ph.isAlive()) { ph.destroy(); LOG.debug("Destroyed process {}", ph); } }); } catch (Exception e) { LOG.error("Unable to infanticide", e); } }
From source file:com.thoughtworks.go.javasysmon.wrapper.Java9CompatibleCurrentProcess.java
private static <T> T invokeMethod(Object o, String methodName) { try {/* w w w .ja va2s .c o m*/ return (T) MethodUtils.invokeMethod(o, methodName); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:cn.afterturn.easypoi.excel.html.css.impl.BorderCssConverImpl.java
@Override public void convertToExcel(Cell cell, CellStyle cellStyle, CellStyleEntity style) { if (style == null || style.getBorder() == null) { return;/*from w w w . j av a 2s. co m*/ } CellStyleBorderEntity border = style.getBorder(); for (String pos : new String[] { TOP, RIGHT, BOTTOM, LEFT }) { String posName = StringUtils.capitalize(pos.toLowerCase()); // color String colorAttr = null; try { colorAttr = (String) MethodUtils.invokeMethod(border, "getBorder" + posName + "Color"); } catch (Exception e) { log.error("Set Border Style Error Caused.", e); } if (StringUtils.isNotEmpty(colorAttr)) { if (cell instanceof HSSFCell) { HSSFColor poiColor = PoiCssUtils.parseColor((HSSFWorkbook) cell.getSheet().getWorkbook(), colorAttr); if (poiColor != null) { try { MethodUtils.invokeMethod(cellStyle, "set" + posName + "BorderColor", poiColor.getIndex()); } catch (Exception e) { log.error("Set Border Color Error Caused.", e); } } } if (cell instanceof XSSFCell) { XSSFColor poiColor = PoiCssUtils.parseColor(colorAttr); if (poiColor != null) { try { MethodUtils.invokeMethod(cellStyle, "set" + posName + "BorderColor", poiColor); } catch (Exception e) { log.error("Set Border Color Error Caused.", e); } } } } // width int width = 0; try { String widthStr = (String) MethodUtils.invokeMethod(border, "getBorder" + posName + "Width"); if (PoiCssUtils.isNum(widthStr)) { width = Integer.parseInt(widthStr); } } catch (Exception e) { log.error("Set Border Style Error Caused.", e); } String styleValue = null; try { styleValue = (String) MethodUtils.invokeMethod(border, "getBorder" + posName + "Style"); } catch (Exception e) { log.error("Set Border Style Error Caused.", e); } BorderStyle shortValue = BorderStyle.NONE; // empty or solid if (StringUtils.isBlank(styleValue) || "solid".equals(styleValue)) { if (width > 2) { shortValue = BorderStyle.THICK; } else if (width > 1) { shortValue = BorderStyle.MEDIUM; } else { shortValue = BorderStyle.THIN; } } else if (ArrayUtils.contains(new String[] { NONE, HIDDEN }, styleValue)) { shortValue = BorderStyle.NONE; } else if (DOUBLE.equals(styleValue)) { shortValue = BorderStyle.DOUBLE; } else if (DOTTED.equals(styleValue)) { shortValue = BorderStyle.DOTTED; } else if (DASHED.equals(styleValue)) { if (width > 1) { shortValue = BorderStyle.MEDIUM_DASHED; } else { shortValue = BorderStyle.DASHED; } } // border style if (shortValue != BorderStyle.NONE) { try { MethodUtils.invokeMethod(cellStyle, "setBorder" + posName, shortValue); } catch (Exception e) { log.error("Set Border Style Error Caused.", e); } } } }
From source file:com.opensymphony.xwork2.util.ProxyUtil.java
/** * Determine the ultimate target class of the given spring bean instance, traversing * not only a top-level spring proxy but any number of nested spring proxies as well — * as long as possible without side effects, that is, just for singleton targets. * @param candidate the instance to check (might be a spring AOP proxy) * @return the ultimate target class (or the plain class of the given * object as fallback; never {@code null}) *//*from w w w. jav a 2 s .c o m*/ private static Class<?> springUltimateTargetClass(Object candidate) { Object current = candidate; Class<?> result = null; while (null != current && implementsInterface(current.getClass(), SPRING_TARGETCLASSAWARE_CLASS_NAME)) { try { result = (Class<?>) MethodUtils.invokeMethod(current, "getTargetClass"); } catch (Throwable ignored) { } current = getSingletonTarget(current); } if (result == null) { Class<?> clazz = candidate.getClass(); result = (isCglibProxyClass(clazz) ? clazz.getSuperclass() : candidate.getClass()); } return result; }
From source file:com.anrisoftware.globalpom.reflection.annotations.AnnotationAccessImpl.java
@SuppressWarnings("unchecked") private <T> T asType(String name, Annotation a) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { return (T) MethodUtils.invokeMethod(a, name); }
From source file:com.opensymphony.xwork2.util.ProxyUtil.java
/** * Obtain the singleton target object behind the given spring proxy, if any. * @param candidate the (potential) spring proxy to check * @return the singleton target object, or {@code null} in any other case * (not a spring proxy, not an existing singleton target) *///from w w w . j a v a 2s . co m private static Object getSingletonTarget(Object candidate) { try { if (implementsInterface(candidate.getClass(), SPRING_ADVISED_CLASS_NAME)) { Object targetSource = MethodUtils.invokeMethod(candidate, "getTargetSource"); if (implementsInterface(targetSource.getClass(), SPRING_SINGLETONTARGETSOURCE_CLASS_NAME)) { return MethodUtils.invokeMethod(targetSource, "getTarget"); } } } catch (Throwable ignored) { } return null; }
From source file:com.offbynull.coroutines.instrumenter.generators.GenericGeneratorsTest.java
@Test public void mustConstructAndCall() throws Exception { // Augment signature methodNode.desc = Type.getMethodDescriptor(Type.getType(String.class), new Type[] {}); // Initialize variable table VariableTable varTable = new VariableTable(classNode, methodNode); Variable sbVar = varTable.acquireExtra(StringBuilder.class); Variable retVar = varTable.acquireExtra(String.class); // Update method logic /**// www .j a v a 2 s . co m * return new StringBuilder().append("hi!").toString() */ methodNode.instructions = merge(construct(StringBuilder.class.getConstructor()), saveVar(sbVar), call(StringBuilder.class.getMethod("append", String.class), loadVar(sbVar), loadStringConst("hi!")), call(StringBuilder.class.getMethod("toString"), loadVar(sbVar)), saveVar(retVar), returnValue(Type.getType(String.class), loadVar(retVar))); // Write to JAR file + load up in classloader -- then execute tests try (URLClassLoader cl = createJarAndLoad(classNode)) { Object obj = cl.loadClass(STUB_CLASSNAME).newInstance(); assertEquals("hi!", MethodUtils.invokeMethod(obj, STUB_METHOD_NAME)); } }
From source file:hoot.services.controllers.osm.OSMTestUtils.java
/** * Gets a total tag count for a specified element type belonging to a * specific map// w w w .j av a2 s .c om * * @param mapId * ID of the map for which to retrieve the tag count * @param elementType * element type for which to retrieve the tag count * @return a tag count * @throws Exception */ static long getTagCountForElementType(long mapId, ElementType elementType) throws Exception { Element prototype = ElementFactory.create(mapId, elementType); List<?> records = createQuery(mapId).select(prototype.getElementTable()).from(prototype.getElementTable()) .fetch(); long tagCount = 0; for (Object record : records) { Object tags = MethodUtils.invokeMethod(record, "getTags"); if (tags != null) { tagCount += PostgresUtils.postgresObjToHStore(tags).size(); } } return tagCount; }
From source file:org.apache.servicecomb.it.junit.ITJUnitUtils.java
private static void initClasses(Class<?>[] classes) throws Throwable { for (Class<?> cls : classes) { for (Field field : FieldUtils.getAllFieldsList(cls)) { if (Consumers.class.isAssignableFrom(field.getType()) || GateRestTemplate.class.isAssignableFrom(field.getType()) || ITSCBRestTemplate.class.isAssignableFrom(field.getType())) { Object target = FieldUtils.readStaticField(field, true); MethodUtils.invokeMethod(target, "init"); }//from w ww.j a v a2s . com } } }