List of usage examples for java.lang.reflect Method getReturnType
public Class<?> getReturnType()
From source file:bdi4jade.util.ReflectionUtils.java
private static boolean isGetter(Method method) { if (!method.getName().startsWith(GETTER_PREFIX)) return false; if (method.getParameterTypes().length != 0) return false; if (void.class.equals(method.getReturnType())) return false; return true;//w w w. j av a 2 s .c om }
From source file:bdi4jade.util.ReflectionUtils.java
private static boolean isSetter(Method method) { if (!method.getName().startsWith(SETTER_PREFIX)) return false; if (method.getParameterTypes().length != 1) return false; if (!void.class.equals(method.getReturnType())) return false; return true;//from w ww. j av a 2 s.c om }
From source file:edu.umn.msi.tropix.proteomics.parameters.ParameterUtils.java
/** * This is the (mostly) inverse of {@link #setParametersFromMap(Map, Object)}. Given a parameter object that is bean where the parameters are the * attributes with a simple type (i.e. Double, Integer, Float, or Boolean), a map of the attribute names to values (as strings) is * created./*from w ww . ja v a 2s .c o m*/ * * @param parameters * Parameter object to pull keys and values from. * @return */ @SuppressWarnings("unchecked") public static void setMapFromParameters(final Object parameters, final Map parameterMap) { final List<Class> simpleTypes = java.util.Arrays.asList(new Class[] { Double.class, Integer.class, Float.class, Boolean.class, Long.class, String.class, Short.class, double.class, int.class, float.class, boolean.class, long.class, short.class }); for (final Method method : parameters.getClass().getMethods()) { String methodName = method.getName(); if (method.getParameterTypes().length != 0 || !(methodName.startsWith("get") || methodName.startsWith("is")) || !simpleTypes.contains(method.getReturnType())) { continue; } String attributeName; if (methodName.startsWith("get")) { attributeName = methodName.substring(3, 4).toLowerCase() + methodName.substring(4); } else { // is method attributeName = methodName.substring(2, 3).toLowerCase() + methodName.substring(3); } Object result; try { result = method.invoke(parameters); } catch (final Exception e) { logger.info(e); throw new IllegalArgumentException( "Failed to invoke get method on bean, should not happen with simple beans.", e); } if (result != null) { parameterMap.put(attributeName, result.toString()); } // else null value found in setMapFromParameters, not adding it to the map" } }
From source file:it.sample.parser.util.CommonsUtil.java
/** * metodo di utilita' per controllare che un metodo sia un getter * /*from ww w .j a v a 2s .com*/ * @param method * @return true se il metodo e' un getter, false altrimenti */ private static boolean isGetter(Method method) { if (!method.getName().startsWith("get")) return false; if (method.getParameterTypes().length != 0) return false; if (void.class.equals(method.getReturnType())) return false; return true; }
From source file:com.hihframework.core.utils.BeanUtils.java
public static void bean2Bean(Object src, Object dest, String... excludes) { if (src == null || dest == null || src == dest) return;//from w w w . java 2 s . co m Method[] methods = src.getClass().getDeclaredMethods(); for (Method m : methods) { String name = m.getName(); if (!Modifier.isPublic(m.getModifiers())) continue; if (!name.startsWith("get") && !name.startsWith("is")) continue; if (Collection.class.isAssignableFrom(m.getReturnType())) continue; boolean exc = false; for (String exclude : excludes) { if (name.equalsIgnoreCase(exclude)) { exc = true; break; } } if (exc) continue; int position = name.startsWith("get") ? 3 : 2; String method = name.substring(position); try { Object val = m.invoke(src); if (val == null) continue; Method targetFun = dest.getClass().getMethod("set" + method, m.getReturnType()); targetFun.invoke(dest, val); } catch (Exception e) { log.error(e); } } }
From source file:com.github.cherimojava.data.mongo.entity.EntityUtils.java
/** * returns the setter method of the given getter method or throws an exception if no such method exists * * @param m getter method for which a matching setter method shall be found * @return setter method belonging to the given getter method * @throws java.lang.IllegalArgumentException if the given getter Method has no setter method *///from w w w . j a v a2 s . c o m static Method getSetterFromGetter(Method m) { try { return m.getDeclaringClass() .getMethod(m.getName().startsWith("get") ? m.getName().replaceFirst("g", "s") : m.getName().replaceFirst("is", "set"), m.getReturnType()); } catch (NoSuchMethodException e) { throw new IllegalArgumentException(format("Method %s has no corresponding setter method", m.getName())); } }
From source file:com.baasbox.configuration.PropertiesConfigurationHelper.java
public static String dumpConfiguration(String section) { Class en = CONFIGURATION_SECTIONS.get(section); try {// w w w . j a v a2 s . co m StringBuilder sb = new StringBuilder(); String enumDescription = ""; Method getEnumDescription = en.getMethod("getEnumDescription"); if (getEnumDescription != null && getEnumDescription.getReturnType() == String.class && Modifier.isStatic(getEnumDescription.getModifiers())) enumDescription = (String) getEnumDescription.invoke(null); sb.append(enumDescription); sb.append("\n"); sb.append(section.toUpperCase()); sb.append("\n"); String lastSection = ""; EnumSet values = EnumSet.allOf(en); for (Object v : values) { String key = (String) ((Method) v.getClass().getMethod("getKey")).invoke(v); Object value = ((Method) en.getMethod("getValue")).invoke(v); String subsection = key.substring(0, key.indexOf('.')); if (!lastSection.equals(subsection)) { sb.append(" - "); sb.append(subsection.toUpperCase()); sb.append("\n"); lastSection = subsection; } sb.append(" + "); sb.append(key); sb.append(" = "); sb.append(value); sb.append("\n"); } return sb.toString(); } catch (Exception e) { BaasBoxLogger.error("Cannot generate a json for " + en.getSimpleName() + " Enum. Is it an Enum that implements the IProperties interface?", e); } return ""; }
From source file:com.netflix.hystrix.contrib.javanica.cache.CacheInvocationContextFactory.java
private static MethodExecutionAction createCacheKeyAction(String method, MetaHolder metaHolder) { MethodExecutionAction cacheKeyAction = null; if (StringUtils.isNotBlank(method)) { Method cacheKeyMethod = getDeclaredMethod(metaHolder.getObj().getClass(), method, metaHolder.getMethod().getParameterTypes()); if (cacheKeyMethod == null) { throw new HystrixCachingException("method with name '" + method + "' doesn't exist in class '" + metaHolder.getObj().getClass() + "'"); }//ww w .j a v a 2 s. c o m if (!cacheKeyMethod.getReturnType().equals(String.class)) { throw new HystrixCachingException("return type of cacheKey method must be String. Method: '" + method + "', Class: '" + metaHolder.getObj().getClass() + "'"); } MetaHolder cMetaHolder = MetaHolder.builder().obj(metaHolder.getObj()).method(cacheKeyMethod) .args(metaHolder.getArgs()).build(); cacheKeyAction = new MethodExecutionAction(cMetaHolder.getObj(), cacheKeyMethod, cMetaHolder.getArgs(), cMetaHolder); } return cacheKeyAction; }
From source file:com.amalto.core.metadata.ClassRepository.java
private static Method[] getMethods(Class clazz) { // TMDM-5851: Work around several issues in introspection (getDeclaredMethods() and getMethods() may return // inherited methods if returned type is a sub class of super class method). Map<String, Class<?>> superClassMethods = new HashMap<String, Class<?>>(); if (clazz.getSuperclass() != null) { for (Method method : clazz.getSuperclass().getMethods()) { superClassMethods.put(method.getName(), method.getReturnType()); }/* w w w .j a v a2s .co m*/ } Map<String, Method> methods = new HashMap<String, Method>(); for (Method method : clazz.getDeclaredMethods()) { if (!(superClassMethods.containsKey(method.getName()) && superClassMethods.get(method.getName()).equals(method.getReturnType()))) { methods.put(method.getName(), method); } } Method[] allMethods = clazz.getMethods(); for (Method method : allMethods) { if (!methods.containsKey(method.getName())) { methods.put(method.getName(), method); } } Method[] classMethods = methods.values().toArray(new Method[methods.size()]); // TMDM-5483: getMethods() does not always return methods in same order: sort them to ensure fixed order. Arrays.sort(classMethods, new Comparator<Method>() { @Override public int compare(Method method1, Method method2) { return method1.getName().compareTo(method2.getName()); } }); return classMethods; }
From source file:jp.terasoluna.fw.web.struts.actions.FileDownloadUtil.java
/** * uEU_E??[h?B//from w w w . j a v a 2 s.c o m * * @param result _E??[hf?[^?CX^X?B * @param request NGXg?B * @param response X|X?B */ @SuppressWarnings("unchecked") public static void download(Object result, HttpServletRequest request, HttpServletResponse response) { List<AbstractDownloadObject> downloadList = new ArrayList<AbstractDownloadObject>(); if (result instanceof AbstractDownloadObject) { downloadList.add((AbstractDownloadObject) result); } else { BeanWrapper wrapper = new BeanWrapperImpl(result); PropertyDescriptor[] propertyDescriptors = wrapper.getPropertyDescriptors(); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { Method readMethod = propertyDescriptor.getReadMethod(); if (readMethod == null) { continue; } Class type = readMethod.getReturnType(); if (AbstractDownloadObject.class.isAssignableFrom(type)) { downloadList .add((AbstractDownloadObject) wrapper.getPropertyValue(propertyDescriptor.getName())); } } } if (downloadList.isEmpty()) { return; } // _E??[hIuWFNg???O if (downloadList.size() != 1) { throw new SystemException(new IllegalStateException("Too many AbstractDownloadObject properties."), TOO_MANY_DOWNLOAD_ERROR); } try { download(downloadList.get(0), request, response, true); } catch (SocketException e) { if (log.isDebugEnabled()) { log.debug(e.getMessage(), e); } } catch (IOException e) { if (log.isErrorEnabled()) { log.error("IOException has occurred while downloading", e); } } }