for (String property : properties.properties()) { Method g = properties.getter(property, null); if (g == null) { continue; Method s = properties.setter(property, null); if (s == null) { continue;
/** Accessor for the class to property info cache. */ static ClassProperties classProperties(Class clazz) { // SoftValueHashMap is thread safe, no need to synch ClassProperties properties = classPropertiesCache.get(clazz); if (properties == null) { properties = new ClassProperties(clazz); classPropertiesCache.put(clazz, properties); } return properties; }
public ClassProperties(Class clazz) { methods = Multimaps.newListMultimap( new TreeMap<>(String.CASE_INSENSITIVE_ORDER), () -> new ArrayList<>()); getters = Multimaps.newListMultimap( new TreeMap<>(String.CASE_INSENSITIVE_ORDER), () -> new ArrayList<>()); setters = Multimaps.newListMultimap( new TreeMap<>(String.CASE_INSENSITIVE_ORDER), () -> new ArrayList<>()); for (Method method : clazz.getMethods()) { final String name = method.getName(); methods.put(name, method); final Class<?>[] params = method.getParameterTypes(); if ((name.startsWith("get") || name.startsWith("is") || COMMON_DERIVED_PROPERTIES.contains(name)) && params.length == 0) { getters.put(gp(method), method); } else if (name.startsWith("set") && params.length == 1) { setters.put(name.substring(3), method); } } // avoid keeping lots of useless empty arrays in memory for // the long term, use just one if (methods.size() == 0) methods = EMPTY; if (getters.size() == 0) getters = EMPTY; if (setters.size() == 0) setters = EMPTY; }
/** * Looks up a setter method by property name. * * <p>setter("foo",Integer) --> void setFoo(Integer); * * @param property The property. * @param type The type of the property. * @return The setter for the property, or null if it does not exist. */ public Method setter(String property, Class type) { Collection<Method> methods = setters.get(property); for (Method setter : methods) { if (type == null) { return setter; } else { Class target = setter.getParameterTypes()[0]; if (target.isAssignableFrom(type) || (target.isPrimitive() && type == wrapper(target)) || (type.isPrimitive() && target == wrapper(type))) { return setter; } } } // could not be found, try again with a more lax match String lax = lax(property); if (!lax.equals(property)) { return setter(lax, type); } return null; }
protected void log(Object object, int level, StringBuffer log) { ClassProperties props = OwsUtils.getClassProperties(object.getClass()); for (String prop : props.properties()) { if ("class".equalsIgnoreCase(prop)) continue; Object value = OwsUtils.get(object, prop); log.append("\n"); for (int i = 0; i < level; i++) log.append("\t"); log.append(prop).append(" = ").append(value); } } }
ClassProperties classProps = OwsUtils.getClassProperties(obj.getClass()); return classProps .properties() .stream() .map( new Property( p, classProps.getter(p, null).getReturnType(), () -> { Object value;
/** * Returns a getter method for a property of java bean. * * @param clazz The type of the bean. * @param property The property name. * @param type The type of the property, may be null. * @return The setter method, or <code>null</code> if not found. */ public static Method getter(Class clazz, String property, Class type) { return classProperties(clazz).getter(property, type); }
ClassProperties cp = new ClassProperties(String.class); ClassProperties cp2 = new ClassProperties(cp.getClass());
Method setter(String propertyName, Class type) { Method s = null; try { s = proxyObject.getClass().getMethod("set" + propertyName, type); } catch (NoSuchMethodException e) { s = cp().setter(propertyName, type); } return s; }
for (String p : cp.properties()) { if ("Class".equals(p)) continue; Object value;
Method getter(String propertyName) { Method g = null; try { g = proxyObject.getClass().getMethod("get" + propertyName, null); } catch (NoSuchMethodException e1) { // could be boolean try { g = proxyObject.getClass().getMethod("is" + propertyName, null); } catch (NoSuchMethodException e2) { } } if (g == null) { g = cp().getter(propertyName, null); } return g; }
/** * Returns a setter method for a property of java bean. * * <p>The <tt>type</tt> parameter may be <code>null</code> to indicate the the setter for the * property should be returned regardless of the type. If not null it will be used to filter the * returned method. * * @param clazz The type of the bean. * @param property The property name. * @param type The type of the property, may be <code>null</code>. * @return The setter method, or <code>null</code> if not found. */ public static Method setter(Class clazz, String property, Class type) { return classProperties(clazz).setter(property, type); }
for (String p : properties.properties()) { Method getter = properties.getter(p, null); if (getter == null) { continue; // should not really happen Method setter = properties.setter(p, type);
for ( String p : cp.properties() ) { if ( "Class".equals( p ) ) continue; Object value = OwsUtils.get(object, p);
for (String propertyName : properties.properties()) { try {