List of usage examples for java.lang.reflect Method getReturnType
public Class<?> getReturnType()
From source file:arena.utils.ServletUtils.java
/** * Do a reflective call to the getContextPath method on the context object, since we don't want * to require the 2.5 servlet API, but we want to call it if it's available. *///from ww w. jav a 2s . c o m public static String getContextPath(ServletContext context) { String contextPath = null; try { Method methContextPath = context.getClass().getMethod("getContextPath", new Class[0]); if (methContextPath.getReturnType().equals(String.class)) { contextPath = (String) methContextPath.invoke(context, new Object[0]); } } catch (Throwable err) { } return contextPath; }
From source file:Main.java
public static Map<String, Object> getProperties(Object object, boolean includeSuperClasses, boolean deepCopy) { HashMap<String, Object> map = new HashMap<String, Object>(); if (object == null) { return map; }// w w w. j av a2s.c o m Class<?> objectClass = object.getClass(); Method[] methods = includeSuperClasses ? objectClass.getMethods() : objectClass.getDeclaredMethods(); for (Method method : methods) { if (method.getParameterTypes().length == 0 && !method.getReturnType().equals(Void.TYPE)) { String methodName = method.getName(); String propertyName = ""; if (methodName.startsWith("get")) { propertyName = methodName.substring(3); } else if (methodName.startsWith("is")) { propertyName = methodName.substring(2); } if (propertyName.length() > 0 && Character.isUpperCase(propertyName.charAt(0))) { propertyName = Character.toLowerCase(propertyName.charAt(0)) + propertyName.substring(1); Object value = null; try { value = method.invoke(object); } catch (Exception e) { } Object value2 = value; if (!deepCopy) { map.put(propertyName, value); } else { if (isSimpleObject(value)) { map.put(propertyName, value); } else if (value instanceof Map) { Map<String, Object> submap = new HashMap<String, Object>(); for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) { submap.put(String.valueOf(entry.getKey()), convertObject(entry.getValue(), includeSuperClasses)); } map.put(propertyName, submap); } else if (value instanceof Iterable) { List<Object> sublist = new ArrayList<Object>(); for (Object v : (Iterable<?>) object) { sublist.add(convertObject(v, includeSuperClasses)); } map.put(propertyName, sublist); } else if (value.getClass().isArray()) { List<Object> sublist = new ArrayList<Object>(); int length = Array.getLength(value); for (int i = 0; i < length; i++) { sublist.add(convertObject(Array.get(value, i), includeSuperClasses)); } map.put(propertyName, sublist); } else { map.put(propertyName, getProperties(value, includeSuperClasses, deepCopy)); } } } } } return map; }
From source file:com.helpinput.propertyeditors.PropertyEditorRegister.java
private static boolean addProperty(Class<? extends PropertyEditor> propertyEditorType, Property propertyAnn, Map<Method, Object> setMethodAndValues) { final String methodName = Commons.getSetterName(propertyAnn.name()); if (!Utils.hasLength(methodName)) return false; Method method = Utils.findMethod(propertyEditorType, methodName); if (method == null || !method.getReturnType().equals(Void.TYPE)) return false; Class<?>[] parameterTypes = method.getParameterTypes(); if (parameterTypes == null || parameterTypes.length != 1) return false; try {/*from ww w . j ava2s .c o m*/ Class<?> parameterType = parameterTypes[0]; Object realValue = ConvertUtils.convert(propertyAnn.value(), parameterType); setMethodAndValues.put(method, realValue); } catch (Exception e) { e.printStackTrace(); return false; } return true; }
From source file:Main.java
/** * get the method start with 'get' or 'is'. *//*from ww w. java 2s. c o m*/ public static Method getGetter(Object bean, String property) { Map<String, Method> cache = GETTER_CACHE.get(bean.getClass()); if (cache == null) { cache = new ConcurrentHashMap<>(); for (Method method : bean.getClass().getMethods()) { if (Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers()) && !void.class.equals(method.getReturnType()) && method.getParameterTypes().length == 0) { String name = method.getName(); if (name.length() > 3 && name.startsWith("get")) { cache.put(name.substring(3, 4).toLowerCase() + name.substring(4), method); } else if (name.length() > 2 && name.startsWith("is")) { cache.put(name.substring(2, 3).toLowerCase() + name.substring(3), method); } } } Map<String, Method> old = GETTER_CACHE.putIfAbsent(bean.getClass(), cache); if (old != null) { cache = old; } } return cache.get(property); }
From source file:Main.java
/** * Convenience method for obtaining most non-null human readable properties * of a JComponent. Array properties are not included. * <P>/*from w ww.ja va 2 s. co m*/ * Implementation note: The returned value is a HashMap. This is subject to * change, so callers should code against the interface Map. * * @param component * the component whose proerties are to be determined * @return the class and value of the properties */ public static Map<Object, Object> getProperties(final JComponent component) { final Map<Object, Object> retVal = new HashMap<Object, Object>(); final Class<?> clazz = component.getClass(); final Method[] methods = clazz.getMethods(); Object value = null; for (final Method method : methods) { if (method.getName().matches("^(is|get).*") && method.getParameterTypes().length == 0) { try { final Class returnType = method.getReturnType(); if (returnType != void.class && !returnType.getName().startsWith("[") && !setExclude.contains(method.getName())) { final String key = method.getName(); value = method.invoke(component); if (value != null && !(value instanceof Component)) { retVal.put(key, value); } } // ignore exceptions that arise if the property could not be // accessed } catch (final IllegalAccessException ex) { } catch (final IllegalArgumentException ex) { } catch (final InvocationTargetException ex) { } } } return retVal; }
From source file:Main.java
/** * Convenience method for obtaining most non-null human readable properties * of a JComponent. Array properties are not included. * <P>/*from w w w .jav a2s . c om*/ * Implementation note: The returned value is a HashMap. This is subject * to change, so callers should code against the interface Map. * * @param component the component whose proerties are to be determined * @return the class and value of the properties */ public static Map<Object, Object> getProperties(JComponent component) { Map<Object, Object> retVal = new HashMap<>(); Class<?> clazz = component.getClass(); Method[] methods = clazz.getMethods(); Object value = null; for (Method method : methods) { if (method.getName().matches("^(is|get).*") && method.getParameterTypes().length == 0) { try { Class<?> returnType = method.getReturnType(); if (returnType != void.class && !returnType.getName().startsWith("[") && !setExclude.contains(method.getName())) { String key = method.getName(); value = method.invoke(component); if (value != null && !(value instanceof Component)) { retVal.put(key, value); } } // ignore exceptions that arise if the property could not be accessed } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { } } } return retVal; }
From source file:com.github.cherimojava.data.mongo.entity.EntityUtils.java
/** * checks if the given method has a return type which is assignable from the declaring class * * @return true if the given method return type is assignable from the declaring class, false otherwise *///w w w. j a v a2 s .c o m static boolean isAssignableFromClass(Method m) { return m.getReturnType().isAssignableFrom(m.getDeclaringClass()); }
From source file:com.agileapes.couteau.context.spring.event.impl.GenericTranslationScheme.java
static boolean isGetter(Method method) { return Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers()) && method.getParameterTypes().length == 0 && !method.getReturnType().equals(void.class) && (method.getName().matches("get[A-Z].*") || (method.getName().matches("is[A-Z].*") && method.getReturnType().equals(boolean.class))); }
From source file:ch.ifocusit.plantuml.utils.ClassUtils.java
public static Set<Class> getConcernedTypes(Method method) { Set<Class> classes = new HashSet<>(); // manage returned types classes.add(method.getReturnType()); classes.addAll(getGenericTypes(method)); // manage parameters types for (Parameter parameter : method.getParameters()) { classes.addAll(getConcernedTypes(parameter)); }//from w ww. j a v a2 s .co m return classes; }
From source file:com.arpnetworking.jackson.BuilderDeserializer.java
static boolean isSetterMethod(final Class<?> builderClass, final Method method) { return method.getName().startsWith(SETTER_PREFIX) && builderClass.equals(method.getReturnType()) && !method.isVarArgs() && method.getParameterTypes().length == 1; }