private boolean hasSetter(Field field) { try { field.getDeclaringClass().getMethod("set" + Reflection.toFirstUpper(field.getName()), field.getType()); return true; } catch (NoSuchMethodException e) { Exceptions.ignore(e); return false; } }
private static Method getterAsIs(@Nonnull Class<?> clazz, @Nonnull String property) throws NoSuchMethodException { try { return clazz.getMethod("is" + toFirstUpper(property)); } catch (NoSuchMethodException ex) { Exceptions.ignore(ex); return clazz.getMethod(property); } }
/** * Returns the setter method according to the java beans specification for a * given property. * * @param clazz the class in which the method should be searched * @param property the name of the property for which the setter is requested * @param fieldType determines the type of the field (which is accepted as parameter of the setter) * @return the <tt>Method</tt> which is used to get the value * @throws IllegalArgumentException if the setter cannot be obtained */ public static Method setter(Class<?> clazz, String property, Class<?> fieldType) { try { return clazz.getMethod("set" + toFirstUpper(property), fieldType); } catch (Exception e) { throw new IllegalArgumentException(Strings.apply("set-Method for Field %s not found: %s", property, e.getMessage()), e); } }
/** * Returns the getter method according to the java beans specification for a * given property. * * @param clazz the class in which the method should be searched * @param property the name of the property for which the getter is requested * @return the <tt>Method</tt> which is used to get the value * @throws IllegalArgumentException if the getter cannot be obtained */ @Nonnull @SuppressWarnings("squid:S1141") @Explain("A nested catch block seems to be the most readable solution here") public static Method getter(@Nonnull Class<?> clazz, @Nonnull String property) { try { try { return clazz.getMethod("get" + toFirstUpper(property)); } catch (NoSuchMethodException e) { Exceptions.ignore(e); return getterAsIs(clazz, property); } } catch (Exception e) { throw new IllegalArgumentException(Strings.apply("get-Method for Field %s not found: %s", property, e.getMessage()), e); } }