List of usage examples for java.lang.reflect Method getReturnType
public Class<?> getReturnType()
From source file:it.sample.parser.util.CommonsUtil.java
/** * Metodo di utilita' per controllare che un'istanza di una classe abbia almeno un campo valorizzato * /*from w w w.jav a 2s . c o m*/ * @param instance * @return true se l'istanza ha almeno un campo valorizzato, false altrimenti */ public static <T> boolean isFilled(T instance) { boolean isFilled = false; Method[] methods = instance != null ? instance.getClass().getMethods() : new Method[] {}; for (Method method : methods) { if (isGetter(method)) { Class<?> returnType = method.getReturnType(); Object obj = null; try { obj = method.invoke(instance, new Object[] {}); if (obj != null && (returnType.getSimpleName().equals("String") && obj.toString().length() > 0)) { isFilled = true; break; } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } return isFilled; }
From source file:unquietcode.tools.beanmachine.AnnotationUtils.java
/** * Retrieve the given annotation's attributes as a Map. * @param annotation the annotation to retrieve the attributes for * @param classValuesAsString whether to turn Class references into Strings (for compatibility with * {@link org.springframework.core.type.AnnotationMetadata} or to preserve them as Class references * @return the Map of annotation attributes, with attribute names as keys and * corresponding attribute values as values *//*from w ww . jav a2 s . co m*/ public static Map<String, Object> getAnnotationAttributes(Annotation annotation, boolean classValuesAsString) { Map<String, Object> attrs = new HashMap<String, Object>(); Method[] methods = annotation.annotationType().getDeclaredMethods(); for (Method method : methods) { if (method.getParameterTypes().length == 0 && method.getReturnType() != void.class) { try { Object value = method.invoke(annotation); if (classValuesAsString) { if (value instanceof Class) { value = ((Class) value).getName(); } else if (value instanceof Class[]) { Class[] clazzArray = (Class[]) value; String[] newValue = new String[clazzArray.length]; for (int i = 0; i < clazzArray.length; i++) { newValue[i] = clazzArray[i].getName(); } value = newValue; } } attrs.put(method.getName(), value); } catch (Exception ex) { throw new IllegalStateException("Could not obtain annotation attribute values", ex); } } } return attrs; }
From source file:com.diversityarrays.kdxplore.stats.StatsUtil.java
public static Class<?> getValueClass(SimpleStatistics.StatName statName) { Method method = METHOD_BY_STATNAME.get(statName); return method.getReturnType(); }
From source file:org.zht.framework.util.ZBeanUtil.java
private static void copy(Object source, Object target, Boolean ignorNull, Class<?> editable, String... ignoreProperties) throws BeansException { Assert.notNull(source, "Source must not be null"); Assert.notNull(target, "Target must not be null"); Class<?> actualEditable = target.getClass(); if (editable != null) { if (!editable.isInstance(target)) { throw new IllegalArgumentException("Target class [" + target.getClass().getName() + "] not assignable to Editable class [" + editable.getName() + "]"); }//from w ww .j av a 2 s .c o m actualEditable = editable; } PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null); for (PropertyDescriptor targetPd : targetPds) { Method writeMethod = targetPd.getWriteMethod(); if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) { PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName()); if (sourcePd != null) { Method readMethod = sourcePd.getReadMethod(); if (readMethod != null && ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) { try { if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } Object value = readMethod.invoke(source); if (ignorNull != null && ignorNull) { if (value != null && (!"[]".equals(value.toString()))) {// ? if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, value); } } else { if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, value); } } catch (Throwable ex) { throw new FatalBeanException( "Could not copy property '" + targetPd.getName() + "' from source to target", ex); } } } } } }
From source file:cat.albirar.framework.dynabean.impl.DynaBeanImplementationUtils.java
/** * Comprova que el mtode representa una propietat correcta. Un propietat correcta s: * <ul>/*from www.j av a 2 s. c o m*/ * <li>Un {@link #isSetter(String) mtode set} amb arguments i sense retorn</li> * <li>Un {@link #isGetter(String) mtode get} sense arguments i amb retorn diferent de 'void'.</li> * <li>Un {@link #isGetterBoolean(String) mtode 'is'} sense arguments i amb retorn 'boolean'.</li> * </ul> * * @param method El mtode * @return true si s correcta i false en cas contrari */ public static final boolean isCorrectProperty(Method method) { if (isGetter(method.getName())) { if (isGetterBoolean(method.getName())) { return (method.getParameterTypes().length == 0 && method.getReturnType().equals(boolean.class)); } return (!method.getReturnType().equals(void.class) && method.getParameterTypes().length == 0); } else { return (method.getParameterTypes().length == 1 && method.getReturnType().equals(void.class)); } }
From source file:com.msopentech.odatajclient.plugin.Utility.java
public static Map.Entry<String, String> getDocumentation(final Object obj) { final String summary; final String description; try {/*from ww w . j a v a 2 s. co m*/ final Method method = obj.getClass().getMethod("getDocumentation"); if (method == null || method.getReturnType() != Documentation.class) { throw new Exception("Documentation not found"); } final Documentation doc = (Documentation) method.invoke(obj); summary = doc.getSummary() == null ? "" : doc.getSummary(); description = doc.getLongDescription() == null ? "" : doc.getLongDescription(); return new AbstractMap.SimpleEntry<String, String>(summary, description); } catch (Exception e) { return null; } }
From source file:org.synyx.hades.util.ClassUtils.java
/** * Asserts the given {@link Method}'s return type to be one of the given * types./*from w ww . jav a 2 s . c o m*/ * * @param method * @param types */ public static void assertReturnType(Method method, Class<?>... types) { if (!Arrays.asList(types).contains(method.getReturnType())) { throw new IllegalStateException( "Method has to have one of the following return types! " + Arrays.toString(types)); } }
From source file:kelly.util.BeanUtils.java
/** * Copy the property values of the given source bean into the given target bean. * <p>Note: The source and target classes do not have to match or even be derived * from each other, as long as the properties match. Any bean properties that the * source bean exposes but the target bean does not will silently be ignored. * @param source the source bean/*from w ww .j a va 2s . c o m*/ * @param target the target bean * @param editable the class (or interface) to restrict property setting to * @param ignoreProperties array of property names to ignore * @throws BeansException if the copying failed * @see BeanWrapper */ private static void copyProperties(Object source, Object target, Class<?> editable, String... ignoreProperties) { Validate.notNull(source, "Source must not be null"); Validate.notNull(target, "Target must not be null"); Class<?> actualEditable = target.getClass(); if (editable != null) { if (!editable.isInstance(target)) { throw new IllegalArgumentException("Target class [" + target.getClass().getName() + "] not assignable to Editable class [" + editable.getName() + "]"); } actualEditable = editable; } PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); List<String> ignoreList = (ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null; for (PropertyDescriptor targetPd : targetPds) { Method writeMethod = targetPd.getWriteMethod(); if (writeMethod != null && (ignoreProperties == null || (!ignoreList.contains(targetPd.getName())))) { PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName()); if (sourcePd != null) { Method readMethod = sourcePd.getReadMethod(); if (readMethod != null && writeMethod.getParameterTypes()[0].isAssignableFrom(readMethod.getReturnType())) { try { if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } Object value = readMethod.invoke(source); if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, value); } catch (Throwable ex) { throw new BeanException( "Could not copy property '" + targetPd.getName() + "' from source to target", ex); } } } } } }
From source file:dk.netdesign.common.osgi.config.Attribute.java
private static Class getMethodReturnType(Method classMethod) throws InvalidMethodException { Class methodReturnType = classMethod.getReturnType(); if (classMethod.isAnnotationPresent(Property.class)) { Property methodProperty = classMethod.getAnnotation(Property.class); if (List.class.isAssignableFrom(methodReturnType) && methodProperty.type() == void.class) { throw new InvalidMethodException("Could not create handler for method " + classMethod.getName() + ". Lists must be accompanied by a returnType"); }//w ww . j a v a 2s . c o m } return methodReturnType; }
From source file:ch.digitalfondue.npjt.QueryType.java
private static boolean isReturnOptional(Method method) { try {// w ww. j a v a 2 s.co m return optionalAvailable && method.getReturnType().isAssignableFrom(Class.forName("java.util.Optional")); } catch (ClassNotFoundException e) { return false; } }