/** * 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; }
@SuppressWarnings("unchecked") public static Collection<Class<? extends Storable>> getStorableEntities() { Set<Class<? extends Storable>> entities = new HashSet<>(); ReflectionHelper.getAnnotatedClasses("com.hortonworks", StorableEntity.class).forEach(clazz -> { if (Storable.class.isAssignableFrom(clazz)) { entities.add((Class<? extends Storable>) clazz); } }); return entities; }
/** * 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; }
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; } }
private RedisCodec getRedisCodec() { final String codec = cacheConfig.getCacheEntry().getCodec(); if (codec != null) { return new Utf8StringCodec(); } else { try { return ReflectionHelper.newInstance(codec); } catch (Exception e) { throw new RuntimeException("Exception occurred creating codec", e); } } }
/** * Default implementation that will read all the instance variable names and invoke setter. * * Same as the toMap() method you should override this method when a field's defined type is not a primitive. * * @param map the map * @return the storable */ public Storable fromMap(Map<String, Object> map) { for(Map.Entry<String, Object> entry: map.entrySet()) { try { if(entry.getValue() != null) { ReflectionHelper.invokeSetter(entry.getKey(), this, entry.getValue()); } } catch (NoSuchMethodException|InvocationTargetException|IllegalAccessException e) { throw new StorageException(e); } } return this; }
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; } }
private RedisCodec getRedisCodec() { final String codec = cacheConfig.getCacheEntry().getCodec(); if (codec != null) { return new Utf8StringCodec(); } else { try { return ReflectionHelper.newInstance(codec); } catch (Exception e) { throw new RuntimeException("Exception occurred creating codec", e); } } }
/** * Default implementation that will read all the instance variable names and invoke setter. * * Same as the toMap() method you should override this method when a field's defined type is not a primitive. * * @param map the map * @return the storable */ public Storable fromMap(Map<String, Object> map) { for(Map.Entry<String, Object> entry: map.entrySet()) { try { if(entry.getValue() != null) { ReflectionHelper.invokeSetter(entry.getKey(), this, entry.getValue()); } } catch (NoSuchMethodException|InvocationTargetException|IllegalAccessException e) { throw new StorageException(e); } } return this; }
/** * 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 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; }
/** * 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; }
@SuppressWarnings("unchecked") public static Collection<Class<? extends Storable>> getStorableEntities() { Set<Class<? extends Storable>> entities = new HashSet<>(); ReflectionHelper.getAnnotatedClasses("com.hortonworks", StorableEntity.class).forEach(clazz -> { if (Storable.class.isAssignableFrom(clazz)) { entities.add((Class<? extends Storable>) clazz); } }); return entities; }
/** * 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; }
/** * 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; }
/** * 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); }
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 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); }
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)); }
try { for (OrderByField orderByField : orderByFields) { Comparable value1 = ReflectionHelper.invokeGetter(orderByField.getFieldName(), storable1); Comparable value2 = ReflectionHelper.invokeGetter(orderByField.getFieldName(), storable2); int compareTo;