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; } }
/** * 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); } }
Method m = getter(root.getClass(), pair.getFirst()); try { return evalAccessPath(pair.getSecond(), m.invoke(root)); } catch (IllegalAccessException e) { throw new IllegalArgumentException(Strings.apply("Cannot invoke %s on %s (%s)",
private Object computeEffectiveParameterValue(String fullParameterName) throws SQLException { String accessPath = null; String parameterName = fullParameterName; if (fullParameterName.contains(".")) { accessPath = parameterName.substring(parameterName.indexOf('.') + 1); parameterName = parameterName.substring(0, parameterName.indexOf('.')); } Object paramValue = context.get(parameterName); if (accessPath == null || paramValue == null) { return paramValue; } try { return Reflection.evalAccessPath(accessPath, paramValue); } catch (Exception e) { throw new SQLException(NLS.fmtr("StatementCompiler.cannotEvalAccessPath") .set("name", parameterName) .set("path", accessPath) .set("value", paramValue) .set("query", originalSQL) .format(), e); } }
/** * Returns all fields defined by this class or one of its superclasses. This includes public, protected and * private fields. * * @param clazz the class to collect the fields for * @return a list of all defined fields of the given class and its superclasses (excluding {@code Object}) */ @Nonnull public static List<Field> getAllFields(@Nonnull Class<?> clazz) { DataCollector<Field> collector = new DataCollector<>(); try { walkHierarchy(clazz, value -> collector.addAll(Arrays.asList(value.getDeclaredFields()))); } catch (Exception e) { Exceptions.handle(e); } return collector.getData(); }
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); } }