@Test public void testParamValueInheritance() { class Bean { @Field(required = true) CharSequence field; } Bean parser = new Bean(); Map<String, Param> params = new HashMap<>(); try { String val = "someval"; params.put("field", new Param<String>("field", String.class, val)); AnnotationUtils.assignFieldParams(parser, params); Assert.assertEquals(val, parser.field); } catch (Exception e){ e.printStackTrace(); Assert.fail("Exception not expected, string is assignable to CharSequence"); } try { Date val = new Date(); params.put("field", new Param<Date>("field", Date.class, val)); AnnotationUtils.assignFieldParams(parser, params); Assert.fail("Exception expected, Date is not assignable to CharSequence."); } catch (TikaConfigException e){ } }
public static void assignFieldParams(Object bean, Map<String, Param> params) throws TikaConfigException { Class<?> beanClass = bean.getClass(); if (!PARAM_INFO.containsKey(beanClass)) { synchronized (TikaConfig.class){ if (!PARAM_INFO.containsKey(beanClass)) { List<AccessibleObject> aObjs = collectInfo(beanClass, org.apache.tika.config.Field.class); List<ParamField> fields = new ArrayList<>(aObjs.size()); for (AccessibleObject aObj : aObjs) { fields.add(new ParamField(aObj)); } PARAM_INFO.put(beanClass, fields); } } } List<ParamField> fields = PARAM_INFO.get(beanClass); Set<String> validFieldNames = new HashSet<>(); for (ParamField field : fields) { validFieldNames.add(field.getName()); Param<?> param = params.get(field.getName()); if (param != null){ if (field.getType().isAssignableFrom(param.getType())) { try { field.assignValue(bean, param.getValue()); } catch (Exception e) { throw new TikaConfigException(e.getMessage(), e); } } else { String msg = String.format(Locale.ROOT, "Value '%s' of type '%s' cant be" + " assigned to field '%s' of defined type '%s'", param.getValue(), param.getValue().getClass(), field.getName(), field.getType()); throw new TikaConfigException(msg); } } else if (field.isRequired()){ String msg = String.format(Locale.ROOT, "Param %s is required for %s," + " but it is not given in config.", field.getName(), bean.getClass().getName()); throw new TikaConfigException(msg); } else { LOG.debug("Param not supplied, field is not mandatory"); } } }
AnnotationUtils { public static void assignFieldParams(Object bean, Map<String, Param> params) throws TikaConfigException { Class<?> beanClass = bean.getClass(); if (!PARAM_INFO.containsKey(beanClass)) { synchronized (TikaConfig.class){ if (!PARAM_INFO.containsKey(beanClass)) { List<AccessibleObject> aObjs = collectInfo(beanClass, org.apache.tika.config.Field.class); List<ParamField> fields = new ArrayList<>(aObjs.size()); for (AccessibleObject aObj : aObjs) { fields.add(new ParamField(aObj)); } PARAM_INFO.put(beanClass, fields); } } } List<ParamField> fields = PARAM_INFO.get(beanClass); Set<String> validFieldNames = new HashSet<>(); for (ParamField field : fields) { validFieldNames.add(field.getName()); Param<?> param = params.get(field.getName()); if (param != null){ if (field.getType().isAssignableFrom(param.getType())) { try { field.assignValue(bean, param.getValue()); } catch (Exception e) { throw new TikaConfigException(e.getMessage(), e); } } else { String msg = String.format(Locale.ROOT, "Value '%s' of type '%s' cant be" + " assigned to field '%s' of defined type '%s'", param.getValue(), param.getValue().getClass(), field.getName(), field.getType()); throw new TikaConfigException(msg); } } else if (field.isRequired()){ String msg = String.format(Locale.ROOT, "Param %s is required for %s," + " but it is not given in config.", field.getName(), bean.getClass().getName()); throw new TikaConfigException(msg); } else { LOG.debug("Param not supplied, field is not mandatory"); } } } }
AnnotationUtils { public static void assignFieldParams(Object bean, Map<String, Param> params) throws TikaConfigException { Class<?> beanClass = bean.getClass(); if (!PARAM_INFO.containsKey(beanClass)) { synchronized (TikaConfig.class){ if (!PARAM_INFO.containsKey(beanClass)) { List<AccessibleObject> aObjs = collectInfo(beanClass, org.apache.tika.config.Field.class); List<ParamField> fields = new ArrayList<>(aObjs.size()); for (AccessibleObject aObj : aObjs) { fields.add(new ParamField(aObj)); } PARAM_INFO.put(beanClass, fields); } } } List<ParamField> fields = PARAM_INFO.get(beanClass); Set<String> validFieldNames = new HashSet<>(); for (ParamField field : fields) { validFieldNames.add(field.getName()); Param<?> param = params.get(field.getName()); if (param != null){ if (field.getType().isAssignableFrom(param.getType())) { try { field.assignValue(bean, param.getValue()); } catch (Exception e) { throw new TikaConfigException(e.getMessage(), e); } } else { String msg = String.format(Locale.ROOT, "Value '%s' of type '%s' cant be" + " assigned to field '%s' of defined type '%s'", param.getValue(), param.getValue().getClass(), field.getName(), field.getType()); throw new TikaConfigException(msg); } } else if (field.isRequired()){ String msg = String.format(Locale.ROOT, "Param %s is required for %s," + " but it is not given in config.", field.getName(), bean.getClass().getName()); throw new TikaConfigException(msg); } else { LOG.debug("Param not supplied, field is not mandatory"); } } }  }
AnnotationUtils { public static void assignFieldParams(Object bean, Map<String, Param> params) throws TikaConfigException { Class<?> beanClass = bean.getClass(); if (!PARAM_INFO.containsKey(beanClass)) { synchronized (TikaConfig.class){ if (!PARAM_INFO.containsKey(beanClass)) { List<AccessibleObject> aObjs = collectInfo(beanClass, org.apache.tika.config.Field.class); List<ParamField> fields = new ArrayList<>(aObjs.size()); for (AccessibleObject aObj : aObjs) { fields.add(new ParamField(aObj)); } PARAM_INFO.put(beanClass, fields); } } } List<ParamField> fields = PARAM_INFO.get(beanClass); Set<String> validFieldNames = new HashSet<>(); for (ParamField field : fields) { validFieldNames.add(field.getName()); Param<?> param = params.get(field.getName()); if (param != null){ if (field.getType().isAssignableFrom(param.getType())) { try { field.assignValue(bean, param.getValue()); } catch (Exception e) { throw new TikaConfigException(e.getMessage(), e); } } else { String msg = String.format(Locale.ROOT, "Value '%s' of type '%s' cant be" + " assigned to field '%s' of defined type '%s'", param.getValue(), param.getValue().getClass(), field.getName(), field.getType()); throw new TikaConfigException(msg); } } else if (field.isRequired()){ String msg = String.format(Locale.ROOT, "Param %s is required for %s," + " but it is not given in config.", field.getName(), bean.getClass().getName()); throw new TikaConfigException(msg); } else { LOG.debug("Param not supplied, field is not mandatory"); } } }  static void assignFieldParams(Object bean, Map<String, Param> params); }
AnnotationUtils { public static void assignFieldParams(Object bean, Map<String, Param> params) throws TikaConfigException { Class<?> beanClass = bean.getClass(); if (!PARAM_INFO.containsKey(beanClass)) { synchronized (TikaConfig.class){ if (!PARAM_INFO.containsKey(beanClass)) { List<AccessibleObject> aObjs = collectInfo(beanClass, org.apache.tika.config.Field.class); List<ParamField> fields = new ArrayList<>(aObjs.size()); for (AccessibleObject aObj : aObjs) { fields.add(new ParamField(aObj)); } PARAM_INFO.put(beanClass, fields); } } } List<ParamField> fields = PARAM_INFO.get(beanClass); Set<String> validFieldNames = new HashSet<>(); for (ParamField field : fields) { validFieldNames.add(field.getName()); Param<?> param = params.get(field.getName()); if (param != null){ if (field.getType().isAssignableFrom(param.getType())) { try { field.assignValue(bean, param.getValue()); } catch (Exception e) { throw new TikaConfigException(e.getMessage(), e); } } else { String msg = String.format(Locale.ROOT, "Value '%s' of type '%s' cant be" + " assigned to field '%s' of defined type '%s'", param.getValue(), param.getValue().getClass(), field.getName(), field.getType()); throw new TikaConfigException(msg); } } else if (field.isRequired()){ String msg = String.format(Locale.ROOT, "Param %s is required for %s," + " but it is not given in config.", field.getName(), bean.getClass().getName()); throw new TikaConfigException(msg); } else { LOG.debug("Param not supplied, field is not mandatory"); } } }  static void assignFieldParams(Object bean, Map<String, Param> params);  }