@Test public void testInjectionStaticField() { Map<String, String> config = new HashMap<>(2); config.put("name", "haha"); FieldUtils.injectionStaticField(config, FieldUtilsTest.class, String::toString); assertTrue("haha".equals(FieldUtilsTest.name)); }
public static <T> void injectionStaticField(Map<String, T> templates, Class<?> target, Function<T, String> fun) { for (Field field : target.getFields()) { TplAttr attr = field.getAnnotation(TplAttr.class); if (attr != null) { if (!Modifier.isStatic(field.getModifiers())) { throw new ServerBootstrapException(target.getName() + " 的 " + field.getName() + " 不是静态属性，无法注入."); } T template = templates.get(attr.name()); if (template == null) { if (attr.required()) { throw new ServerBootstrapException(target.getName() + " 的 " + field.getName() + " 为必选参数."); } } else { Converter<?> converter = ConvertManager.getInstance().getConverter(field.getType()); try { FieldUtils.writeField(null, field, converter.convert(field, fun.apply(template))); } catch (Exception e) { throw new ConvertException(target.getName() + " >> " + field.getName() + " >> " + fun.apply(template) + "-->" + converter.buildErrorMsg(), e); } } } } }
FieldUtils { public static <T> void injectionStaticField(Map<String, T> templates, Class<?> target, Function<T, String> fun) { for (Field field : target.getFields()) { TplAttr attr = field.getAnnotation(TplAttr.class); if (attr != null) { if (!Modifier.isStatic(field.getModifiers())) { throw new ServerBootstrapException(target.getName() + " 的 " + field.getName() + " 不是静态属性，无法注入."); } T template = templates.get(attr.name()); if (template == null) { if (attr.required()) { throw new ServerBootstrapException(target.getName() + " 的 " + field.getName() + " 为必选参数."); } } else { Converter<?> converter = ConvertManager.getInstance().getConverter(field.getType()); try { FieldUtils.writeField(null, field, converter.convert(field, fun.apply(template))); } catch (Exception e) { throw new ConvertException(target.getName() + " >> " + field.getName() + " >> " + fun.apply(template) + "-->" + converter.buildErrorMsg(), e); } } } } } }
FieldUtils { public static <T> void injectionStaticField(Map<String, T> templates, Class<?> target, Function<T, String> fun) { for (Field field : target.getFields()) { TplAttr attr = field.getAnnotation(TplAttr.class); if (attr != null) { if (!Modifier.isStatic(field.getModifiers())) { throw new ServerBootstrapException(target.getName() + " 的 " + field.getName() + " 不是静态属性，无法注入."); } T template = templates.get(attr.name()); if (template == null) { if (attr.required()) { throw new ServerBootstrapException(target.getName() + " 的 " + field.getName() + " 为必选参数."); } } else { Converter<?> converter = ConvertManager.getInstance().getConverter(field.getType()); try { FieldUtils.writeField(null, field, converter.convert(field, fun.apply(template))); } catch (Exception e) { throw new ConvertException(target.getName() + " >> " + field.getName() + " >> " + fun.apply(template) + "-->" + converter.buildErrorMsg(), e); } } } } }  }
FieldUtils { public static <T> void injectionStaticField(Map<String, T> templates, Class<?> target, Function<T, String> fun) { for (Field field : target.getFields()) { TplAttr attr = field.getAnnotation(TplAttr.class); if (attr != null) { if (!Modifier.isStatic(field.getModifiers())) { throw new ServerBootstrapException(target.getName() + " 的 " + field.getName() + " 不是静态属性，无法注入."); } T template = templates.get(attr.name()); if (template == null) { if (attr.required()) { throw new ServerBootstrapException(target.getName() + " 的 " + field.getName() + " 为必选参数."); } } else { Converter<?> converter = ConvertManager.getInstance().getConverter(field.getType()); try { FieldUtils.writeField(null, field, converter.convert(field, fun.apply(template))); } catch (Exception e) { throw new ConvertException(target.getName() + " >> " + field.getName() + " >> " + fun.apply(template) + "-->" + converter.buildErrorMsg(), e); } } } } }  static void writeField(final Object target, final Field field, final Object value); static void writeField(final Object target, final String fieldName, final Object value); static Object readField(final Object target, final Field field); static Field getField(final Class<?> klass, String fieldName); static List<Field> getAllField(final Class<?> klass); static String genGetMethodName(Field field); static String genSetMethodName(Field field); static Field[] scanAllField(final Class<?> klass, List<Class<? extends Annotation>> annotations); static void injectionStaticField(Map<String, T> templates, Class<?> target, Function<T, String> fun); }
FieldUtils { public static <T> void injectionStaticField(Map<String, T> templates, Class<?> target, Function<T, String> fun) { for (Field field : target.getFields()) { TplAttr attr = field.getAnnotation(TplAttr.class); if (attr != null) { if (!Modifier.isStatic(field.getModifiers())) { throw new ServerBootstrapException(target.getName() + " 的 " + field.getName() + " 不是静态属性，无法注入."); } T template = templates.get(attr.name()); if (template == null) { if (attr.required()) { throw new ServerBootstrapException(target.getName() + " 的 " + field.getName() + " 为必选参数."); } } else { Converter<?> converter = ConvertManager.getInstance().getConverter(field.getType()); try { FieldUtils.writeField(null, field, converter.convert(field, fun.apply(template))); } catch (Exception e) { throw new ConvertException(target.getName() + " >> " + field.getName() + " >> " + fun.apply(template) + "-->" + converter.buildErrorMsg(), e); } } } } }  static void writeField(final Object target, final Field field, final Object value); static void writeField(final Object target, final String fieldName, final Object value); static Object readField(final Object target, final Field field); static Field getField(final Class<?> klass, String fieldName); static List<Field> getAllField(final Class<?> klass); static String genGetMethodName(Field field); static String genSetMethodName(Field field); static Field[] scanAllField(final Class<?> klass, List<Class<? extends Annotation>> annotations); static void injectionStaticField(Map<String, T> templates, Class<?> target, Function<T, String> fun);  }