Search in sources :

Example 1 with PropertyDefinition

use of org.walkmod.conf.entities.PropertyDefinition in project walkmod-core by walkmod.

the class ConfigurationImpl method getProperties.

private List<PropertyDefinition> getProperties(Object o) {
    List<PropertyDefinition> result = new LinkedList<PropertyDefinition>();
    PropertyDescriptor[] properties = BeanUtils.getPropertyDescriptors(o.getClass());
    if (properties != null) {
        for (PropertyDescriptor pd : properties) {
            if (pd.getWriteMethod() != null) {
                String name = pd.getDisplayName();
                Class<?> clazz = pd.getPropertyType();
                String type = clazz.getSimpleName();
                String value = "";
                if (String.class.isAssignableFrom(clazz) || Number.class.isAssignableFrom(clazz) || clazz.isPrimitive()) {
                    if (pd.getReadMethod() != null) {
                        try {
                            value = pd.getReadMethod().invoke(o).toString();
                        } catch (Exception e) {
                        }
                    } else {
                        Field[] fields = o.getClass().getDeclaredFields();
                        boolean found = false;
                        for (int i = 0; i < fields.length && !found; i++) {
                            if (fields[i].getName().equals(name)) {
                                found = true;
                                fields[i].setAccessible(true);
                                try {
                                    value = fields[i].get(o).toString();
                                } catch (Exception e) {
                                }
                            }
                        }
                    }
                }
                PropertyDefinition item = new PropertyDefinitionImpl(type, name, value);
                result.add(item);
            }
        }
    }
    return result;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) PropertyDefinition(org.walkmod.conf.entities.PropertyDefinition) LinkedList(java.util.LinkedList) WalkModException(org.walkmod.exceptions.WalkModException) Field(java.lang.reflect.Field)

Example 2 with PropertyDefinition

use of org.walkmod.conf.entities.PropertyDefinition in project walkmod-core by walkmod.

the class InspectCommand method execute.

@Override
public void execute() throws Exception {
    if (help) {
        command.usage("inspect");
    } else {
        WalkModFacade facade = new WalkModFacade(OptionsBuilder.options().offline(offline));
        List<BeanDefinition> beans = facade.inspectPlugin(new PluginConfigImpl(pluginId.get(0)));
        List<String> validTypesList = Arrays.asList("String", "JSONObject", "JSONArray");
        if (beans != null) {
            at = new V2_AsciiTable();
            at.addRule();
            at.addRow("TYPE NAME (ID)", "CATEGORY", "PROPERTIES", "DESCRIPTION");
            at.addStrongRule();
            for (BeanDefinition bean : beans) {
                List<PropertyDefinition> properties = bean.getProperties();
                if (properties == null || properties.isEmpty()) {
                    at.addRow(bean.getType(), bean.getCategory(), "", bean.getDescription());
                } else {
                    int i = 0;
                    for (PropertyDefinition pd : properties) {
                        if (validTypesList.contains(pd.getType())) {
                            String label = pd.getName() + ":" + pd.getType();
                            if (pd.getDefaultValue() != null && pd.getDefaultValue().length() != 0) {
                                label = label + " (" + pd.getDefaultValue() + ")";
                            }
                            if (i == 0) {
                                at.addRow(bean.getType(), bean.getCategory(), label, bean.getDescription());
                            } else {
                                at.addRow("", "", label, "");
                            }
                            i++;
                        }
                    }
                }
                at.addRule();
            }
        }
    }
}
Also used : V2_AsciiTable(de.vandermeer.asciitable.v2.V2_AsciiTable) WalkModFacade(org.walkmod.WalkModFacade) BeanDefinition(org.walkmod.conf.entities.BeanDefinition) PluginConfigImpl(org.walkmod.conf.entities.impl.PluginConfigImpl) PropertyDefinition(org.walkmod.conf.entities.PropertyDefinition)

Aggregations

PropertyDefinition (org.walkmod.conf.entities.PropertyDefinition)2 V2_AsciiTable (de.vandermeer.asciitable.v2.V2_AsciiTable)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Field (java.lang.reflect.Field)1 LinkedList (java.util.LinkedList)1 WalkModFacade (org.walkmod.WalkModFacade)1 BeanDefinition (org.walkmod.conf.entities.BeanDefinition)1 PluginConfigImpl (org.walkmod.conf.entities.impl.PluginConfigImpl)1 WalkModException (org.walkmod.exceptions.WalkModException)1