/** * Given a class, this method returns a map of names of all the instance (non static) fields to type. * if the class has any super class it also includes those fields. * @param clazz , not null * @return */ public static Map<String, Class> getFieldNamesToTypes(Class clazz) { Field[] declaredFields = clazz.getDeclaredFields(); Map<String, Class> instanceVariableNamesToTypes = new HashMap<>(); for(Field field : declaredFields) { if(!Modifier.isStatic(field.getModifiers())) { LOG.trace("clazz {} has field {} with type {}", clazz.getName(), field.getName(), field.getType().getName()); instanceVariableNamesToTypes.put(field.getName(), field.getType()); } else { LOG.trace("clazz {} has field {} with type {}, which is static so ignoring", clazz.getName(), field.getName(), field.getType().getName()); } } if(!clazz.getSuperclass().equals(Object.class)) { instanceVariableNamesToTypes.putAll(getFieldNamesToTypes(clazz.getSuperclass())); } return instanceVariableNamesToTypes; }
/** * Given a class, this method returns a map of names of all the instance (non static) fields to type. * if the class has any super class it also includes those fields. * @param clazz , not null * @return */ public static Map<String, Class> getFieldNamesToTypes(Class clazz) { Field[] declaredFields = clazz.getDeclaredFields(); Map<String, Class> instanceVariableNamesToTypes = new HashMap<>(); for(Field field : declaredFields) { if(!Modifier.isStatic(field.getModifiers())) { LOG.trace("clazz {} has field {} with type {}", clazz.getName(), field.getName(), field.getType().getName()); instanceVariableNamesToTypes.put(field.getName(), field.getType()); } else { LOG.trace("clazz {} has field {} with type {}, which is static so ignoring", clazz.getName(), field.getName(), field.getType().getName()); } } if(!clazz.getSuperclass().equals(Object.class)) { instanceVariableNamesToTypes.putAll(getFieldNamesToTypes(clazz.getSuperclass())); } return instanceVariableNamesToTypes; }
/** * Given a class, this method returns a map of names of all the instance (non static) fields -> type. * if the class has any super class it also includes those fields. * * @param clazz , not null * @return */ public static Map<String, Class> getFieldNamesToTypes(Class clazz) { Field[] declaredFields = clazz.getDeclaredFields(); Map<String, Class> instanceVariableNamesToTypes = new HashMap<>(); for (Field field : declaredFields) { if (!Modifier.isStatic(field.getModifiers())) { LOG.trace("clazz {} has field {} with type {}", clazz.getName(), field.getName(), field.getType().getName()); instanceVariableNamesToTypes.put(field.getName(), field.getType()); } else { LOG.trace("clazz {} has field {} with type {}, which is static so ignoring", clazz.getName(), field.getName(), field.getType().getName()); } } if (!clazz.getSuperclass().equals(Object.class)) { instanceVariableNamesToTypes.putAll(getFieldNamesToTypes(clazz.getSuperclass())); } return instanceVariableNamesToTypes; } }
/** * Default implementation that will generate schema by reading all the field names in the class and use its * define type to convert to the Schema type. * * @return the schema */ @JsonIgnore public Schema getSchema() { Map<String, Class> fieldNamesToTypes = ReflectionHelper.getFieldNamesToTypes(this.getClass()); List<Schema.Field> fields = new ArrayList<>(); for(Map.Entry<String, Class> entry : fieldNamesToTypes.entrySet()) { try { getField(entry.getKey(), entry.getValue()).ifPresent(field -> { fields.add(field); LOG.trace("getSchema: Adding {}", field); }); } catch (NoSuchFieldException|NoSuchMethodException|InvocationTargetException|IllegalAccessException|ParserException e) { throw new StorageException(e); } } return Schema.of(fields); }
/** * Default implementation that will generate schema by reading all the field names in the class and use its * define type to convert to the Schema type. * * @return the schema */ @JsonIgnore public Schema getSchema() { Map<String, Class> fieldNamesToTypes = ReflectionHelper.getFieldNamesToTypes(this.getClass()); List<Schema.Field> fields = new ArrayList<>(); for(Map.Entry<String, Class> entry : fieldNamesToTypes.entrySet()) { try { getField(entry.getKey(), entry.getValue()).ifPresent(field -> { fields.add(field); LOG.trace("getSchema: Adding {}", field); }); } catch (NoSuchFieldException|NoSuchMethodException|InvocationTargetException|IllegalAccessException|ParserException e) { throw new StorageException(e); } } return Schema.of(fields); }
/** * 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; }