public static List<Pair<Field, Object>> getAnnotatedFieldValues(Storable storable, Class<? extends Annotation> clazz) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { List<Pair<Field, Object>> res = new ArrayList<>(); for (Field field : storable.getClass().getDeclaredFields()) { if (field.getAnnotation(clazz) != null) { Object val = ReflectionHelper.invokeGetter(field.getName(), storable); if (val != null) { res.add(Pair.of(field, val)); } } } return res; } }
public static List<Pair<Field, Object>> getAnnotatedFieldValues(Storable storable, Class<? extends Annotation> clazz) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { List<Pair<Field, Object>> res = new ArrayList<>(); for (Field field : storable.getClass().getDeclaredFields()) { if (field.getAnnotation(clazz) != null) { Object val = ReflectionHelper.invokeGetter(field.getName(), storable); if (val != null) { res.add(Pair.of(field, val)); } } } return res; } }
/** * Uses reflection to query the field or the method. Assumes * a public getXXX method is available to get the field value. */ private boolean matches(Storable val, List<QueryParam> queryParams, Class<?> clazz) { Object fieldValue; boolean res = true; for (QueryParam qp : queryParams) { try { fieldValue = ReflectionHelper.invokeGetter(qp.name, val); if (!fieldValue.toString().equals(qp.value)) { return false; } } catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException e) { LOG.error("FAILED to invoke getter for query param {} , is your param name correct?", qp.getName(), e); return false; } } return res; }
/** * Uses reflection to query the field or the method. Assumes * a public getXXX method is available to get the field value. */ private boolean matches(Storable val, List<QueryParam> queryParams, Class<?> clazz) { Object fieldValue; boolean res = true; for (QueryParam qp : queryParams) { try { fieldValue = ReflectionHelper.invokeGetter(qp.name, val); if (!fieldValue.toString().equals(qp.value)) { return false; } } catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException e) { LOG.error("FAILED to invoke getter for query param {} , is your param name correct?", qp.getName(), e); return false; } } return res; }
private Optional<Schema.Field> getField(String name, Class<?> clazz) throws NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, ParserException { Field field = this.getClass().getDeclaredField(name); if (field.getAnnotation(SchemaIgnore.class) != null) { LOG.debug("Ignoring field {}", field); return Optional.empty(); } Object val = ReflectionHelper.invokeGetter(name, this); Schema.Type type; if (val != null) { type = Schema.fromJavaType(val); } else { type = Schema.fromJavaType(clazz); } return Optional.of(new Schema.Field(name, type)); }
private Optional<Schema.Field> getField(String name, Class<?> clazz) throws NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, ParserException { Field field = this.getClass().getDeclaredField(name); if (field.getAnnotation(SchemaIgnore.class) != null) { LOG.debug("Ignoring field {}", field); return Optional.empty(); } Object val = ReflectionHelper.invokeGetter(name, this); Schema.Type type; if (val != null) { type = Schema.fromJavaType(val); } else { type = Schema.fromJavaType(clazz); } return Optional.of(new Schema.Field(name, type)); }
/** * Default implementation that will read all the instance variable names using API and * get the value by calling getter method (POJO) convention on it. * * Sometimes for JDBC to work we need an extra layer of transformation , for example see the implementation * in {@code DataSource} which defines a field of type @{code Type} which is enum and not a primitive type as expected * by the JDBC layer, you can call this method and override the fields that needs transformation. * * @return the map */ public Map<String, Object> toMap() { Set<String> instanceVariableNames = ReflectionHelper.getFieldNamesToTypes(this.getClass()).keySet(); Map<String, Object> fieldToVal = new HashMap<>(); for(String fieldName : instanceVariableNames) { try { Object val = ReflectionHelper.invokeGetter(fieldName, this); fieldToVal.put(fieldName, val); if(LOG.isTraceEnabled()) { LOG.trace("toMap: Adding fieldName {} = {} ", fieldName, val); } } catch (NoSuchMethodException|InvocationTargetException|IllegalAccessException e) { throw new StorageException(e); } } return fieldToVal; }
/** * Default implementation that will read all the instance variable names using API and * get the value by calling getter method (POJO) convention on it. * * Sometimes for JDBC to work we need an extra layer of transformation , for example see the implementation * in {@code DataSource} which defines a field of type @{code Type} which is enum and not a primitive type as expected * by the JDBC layer, you can call this method and override the fields that needs transformation. * * @return the map */ public Map<String, Object> toMap() { Set<String> instanceVariableNames = ReflectionHelper.getFieldNamesToTypes(this.getClass()).keySet(); Map<String, Object> fieldToVal = new HashMap<>(); for(String fieldName : instanceVariableNames) { try { Object val = ReflectionHelper.invokeGetter(fieldName, this); fieldToVal.put(fieldName, val); if(LOG.isTraceEnabled()) { LOG.trace("toMap: Adding fieldName {} = {} ", fieldName, val); } } catch (NoSuchMethodException|InvocationTargetException|IllegalAccessException e) { throw new StorageException(e); } } return fieldToVal; }
try { for (OrderByField orderByField : orderByFields) { Comparable value1 = ReflectionHelper.invokeGetter(orderByField.getFieldName(), storable1); Comparable value2 = ReflectionHelper.invokeGetter(orderByField.getFieldName(), storable2); int compareTo;
try { for (OrderByField orderByField : orderByFields) { Comparable value1 = ReflectionHelper.invokeGetter(orderByField.getFieldName(), storable1); Comparable value2 = ReflectionHelper.invokeGetter(orderByField.getFieldName(), storable2); int compareTo;