Esempio n. 1
0
 /*
  * returns the declared fields for the class and its superclass.
  */
 private static List<Field> getAllFields(Component component) {
   List<Field> fields = new ArrayList<Field>();
   fields.addAll(Arrays.asList(component.getClass().getDeclaredFields()));
   // check also the fields of the super class if exists
   if (component.getClass().getSuperclass() != null) {
     fields.addAll(Arrays.asList(component.getClass().getSuperclass().getDeclaredFields()));
   }
   return fields;
 }
Esempio n. 2
0
 /**
  * @param component The component to analyse.
  * @return All config options of the component with their respective value.
  */
 public static Map<ConfigOption, Object> getConfigOptionValues(Component component) {
   Map<ConfigOption, Object> optionValues = new HashMap<ConfigOption, Object>();
   List<Field> fields = getAllFields(component);
   for (Field field : fields) {
     ConfigOption option = field.getAnnotation(ConfigOption.class);
     if (option != null) {
       try {
         // we invoke the public getter instead of accessing a private field (may cause problem
         // with SecurityManagers)
         // use Spring BeanUtils TODO: might be unnecessarily slow because we already have the
         // field?
         Object value =
             BeanUtils.getPropertyDescriptor(component.getClass(), field.getName())
                 .getReadMethod()
                 .invoke(component);
         optionValues.put(option, value);
       } catch (IllegalArgumentException e1) {
         e1.printStackTrace();
       } catch (IllegalAccessException e1) {
         e1.printStackTrace();
       } catch (BeansException e) {
         e.printStackTrace();
       } catch (InvocationTargetException e) {
         e.printStackTrace();
       }
     }
   }
   return optionValues;
 }
Esempio n. 3
0
 /**
  * Configures the given component by setting the value for the appropriate config option.
  *
  * @param component the component to be configured
  * @param configName the name of the config option
  * @param configValue the value of the config option
  */
 @Deprecated
 public static <T> void configure(Component component, String configName, T configValue) {
   List<Field> fields = getAllFields(component);
   for (Field f : fields) {
     ConfigOption option = f.getAnnotation(ConfigOption.class);
     if (option != null) {
       if (option.name().equals(configName)) {
         try {
           PropertyEditor editor = PropertyEditor.class.newInstance();
           editor.setAsText(configValue.toString());
           Method method =
               component
                   .getClass()
                   .getMethod(
                       "set"
                           + Character.toUpperCase(f.getName().charAt(0))
                           + f.getName().substring(1),
                       getClassForObject(editor.getValue()));
           method.invoke(component, editor.getValue());
         } catch (IllegalArgumentException e) {
           e.printStackTrace();
         } catch (InstantiationException e) {
           e.printStackTrace();
         } catch (IllegalAccessException e) {
           e.printStackTrace();
         } catch (InvocationTargetException e) {
           e.printStackTrace();
         } catch (SecurityException e) {
           e.printStackTrace();
         } catch (NoSuchMethodException e) {
           e.printStackTrace();
         }
       }
     }
   }
 }
Esempio n. 4
0
 /**
  * Returns all config options for the given component.
  *
  * @param component
  * @return
  */
 public static List<ConfigOption> getConfigOptions(Component component) {
   return getConfigOptions(component.getClass());
 }