Example usage for java.beans PropertyDescriptor PropertyDescriptor

List of usage examples for java.beans PropertyDescriptor PropertyDescriptor

Introduction

In this page you can find the example usage for java.beans PropertyDescriptor PropertyDescriptor.

Prototype

PropertyDescriptor(PropertyDescriptor x, PropertyDescriptor y) 

Source Link

Document

Package-private constructor.

Usage

From source file:org.theospi.utils.mvc.impl.SimpleValidator.java

protected void validate(String field, Object obj, Errors errors) {
    if (obj instanceof Map) {
        Map map = (Map) obj;
        if (map.get(field) == null) {
            errors.rejectValue(field, messageCode, messageCode);
        }/*from w w w.j a  va  2  s. co m*/
    } else {
        PropertyDescriptor prop = null;
        try {
            prop = new PropertyDescriptor(field, obj.getClass());

            Object value = prop.getReadMethod().invoke(obj, new Object[] {});
            if (value == null || value.toString().length() == 0) {
                errors.rejectValue(field, messageCode, messageCode);
            }
        } catch (IntrospectionException e) {
            logger.error("", e);
            throw new OspException(e);
        } catch (IllegalAccessException e) {
            logger.error("", e);
            throw new OspException(e);
        } catch (InvocationTargetException e) {
            logger.error("", e);
            throw new OspException(e);
        }
    }
}

From source file:org.jsonschema2pojo.integration.PropertiesIT.java

@Test
@SuppressWarnings("rawtypes")
public void propertiesAreSerializedInCorrectOrder() throws ClassNotFoundException, IntrospectionException,
        InstantiationException, IllegalAccessException, InvocationTargetException {

    ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/properties/orderedProperties.json",
            "com.example");

    Class generatedType = resultsClassLoader.loadClass("com.example.OrderedProperties");
    Object instance = generatedType.newInstance();

    new PropertyDescriptor("type", generatedType).getWriteMethod().invoke(instance, "1");
    new PropertyDescriptor("id", generatedType).getWriteMethod().invoke(instance, "2");
    new PropertyDescriptor("name", generatedType).getWriteMethod().invoke(instance, "3");
    new PropertyDescriptor("hastickets", generatedType).getWriteMethod().invoke(instance, true);
    new PropertyDescriptor("starttime", generatedType).getWriteMethod().invoke(instance, "4");

    String serialized = mapper.valueToTree(instance).toString();

    assertThat("Properties are not in expected order", serialized.indexOf("type"),
            is(lessThan(serialized.indexOf("id"))));
    assertThat("Properties are not in expected order", serialized.indexOf("id"),
            is(lessThan(serialized.indexOf("name"))));
    assertThat("Properties are not in expected order", serialized.indexOf("name"),
            is(lessThan(serialized.indexOf("hastickets"))));
    assertThat("Properties are not in expected order", serialized.indexOf("hastickets"),
            is(lessThan(serialized.indexOf("starttime"))));

}

From source file:org.ivan.service.ExcelExporter.java

public <T extends Object> List<String> getHeadersFromGetMethods(T obj) {

    List<String> fieldNames = getHeaders(obj.getClass());
    PropertyDescriptor propertyDescriptor;//Use for getters and setters from property
    Method currentGetMethod;//Current get method from current property
    List<String> methodNames = new ArrayList<>();
    for (String fieldName : fieldNames) {
        try {//from   ww  w .j a  va2 s  .co m
            propertyDescriptor = new PropertyDescriptor(fieldName, obj.getClass());
            currentGetMethod = propertyDescriptor.getReadMethod();
            //values.add(currentGetMethod.invoke(obj).toString());

            if (currentGetMethod.invoke(obj).toString().contains("=")) {
                methodNames.addAll(getHeadersFromGetMethods(currentGetMethod.invoke(obj)));
            } else {
                methodNames.add(currentGetMethod.getName().replace("get", ""));
            }

            //if(currentGetMethod.invoke(obj).getClass().getSuperclass() != null)
        } catch (IntrospectionException | IllegalAccessException | IllegalArgumentException
                | InvocationTargetException ex) {
            Logger.getLogger(ExcelExporter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return methodNames;

}

From source file:org.mypsycho.swing.app.ResourceManager.java

public ResourceManager(ApplicationContext parent) {

    context = parent;//  w  ww.  ja v  a2s .c o  m

    // type converter
    register(new MenuConverter());
    register(new UiConverter());
    register(new ResourceConverter());
    register(new ActionConverter());
    register(new BorderConverter(getConverter()));
    register(new DerivedFontConverter());

    // collection
    register(new ComponentCollection());
    register(new ActionMapExtension());

    // property
    try {
        register(new ComponentPopupProperty());
        register(new ComponentBoundsProperty());
        register(new WindowIconProperty());
        register(new ComponentProperty());
        register(new MnemonicProperty.Action());
        register(new MnemonicProperty.Button());
        register(new MnemonicProperty.Label());
        register(new MnemonicProperty.Menu());
        register(new TableHeaderProperty());
        register(new TableColumnsProperty());
        // By default, JFrame expose a property 'JMenuBar' not jMenuBar
        // We register a more convenient name (Note: Frame.menuBar is no more accessible)
        register(new DescriptorExtension(JFrame.class, "menuBar",
                new PropertyDescriptor("JMenuBar", JFrame.class)));

        register(ClientComponentProperty.createComponentInstance());
        register(ClientComponentProperty.createWindowInstance());
        register(new DialogPaneProperty());

    } catch (IntrospectionException e) {
        throw new RuntimeException(e);
    }

    register(defaultListener);
}

From source file:com.netflix.hystrix.contrib.javanica.cache.HystrixCacheKeyGenerator.java

private Object getPropertyValue(String name, Object obj) throws HystrixCacheKeyGenerationException {
    try {/*from  www  .ja v a 2 s  . c  o  m*/
        return new PropertyDescriptor(name, obj.getClass()).getReadMethod().invoke(obj);
    } catch (IllegalAccessException e) {
        throw new HystrixCacheKeyGenerationException(e);
    } catch (IntrospectionException e) {
        throw new HystrixCacheKeyGenerationException(e);
    } catch (InvocationTargetException e) {
        throw new HystrixCacheKeyGenerationException(e);
    }
}

From source file:net.sourceforge.vulcan.dto.PluginConfigDto.java

protected final void addProperty(List<PropertyDescriptor> pds, String name, String labelKey,
        String shortDescriptionKey, Locale locale, Map<String, ? extends Object> attrs) {
    try {/*from  w  w w.  j av  a 2s.  co m*/
        final PropertyDescriptor pd = new PropertyDescriptor(name, getClass());
        pd.setDisplayName(applicationContext.getMessage(labelKey, null, locale));
        pd.setShortDescription(applicationContext.getMessage(shortDescriptionKey, null, locale));
        if (attrs != null) {
            for (String key : attrs.keySet()) {
                pd.setValue(key, attrs.get(key));
            }
        }
        pds.add(pd);
    } catch (IntrospectionException e) {
        throw new RuntimeException(e);
    }
}

From source file:de.xwic.sandbox.server.installer.modules.config.ConfigUpgradeScriptHelper.java

/**
 * Generic method to apply property/attribute values from a map to an object.
 * //from   w ww  .  ja  v  a 2s  .c o  m
 * @param area
 * @param args
 * @throws IntrospectionException
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 */
private void applyAttributes(Object bean, Map<String, Object> args) throws Exception {

    for (String propertyName : args.keySet()) {

        Object value = args.get(propertyName);

        PropertyDescriptor pd = new PropertyDescriptor(propertyName, bean.getClass());
        Method mWrite = pd.getWriteMethod();
        if (mWrite == null) {
            throw new RuntimeException(
                    "Property '" + propertyName + "' does not exist or does not have a write method in class '"
                            + bean.getClass().getName() + "'");
        }

        // convert arguments
        Class<?> propertyType = pd.getPropertyType();
        if (propertyType.equals(IPicklistEntry.class)) {

        }
        mWrite.invoke(bean, value);

    }

}

From source file:com.googlecode.jsonschema2pojo.integration.FormatIT.java

@Test
public void formatValueProducesExpectedType() throws NoSuchMethodException, IntrospectionException {

    Method getter = new PropertyDescriptor(propertyName, classWithFormattedProperties).getReadMethod();

    assertThat(getter.getReturnType().getName(), is(this.expectedType.getName()));

}

From source file:org.sakaiproject.metaobj.shared.model.ElementListBeanWrapper.java

protected PropertyDescriptor getPropertyDescriptor(Object key) {
    PropertyDescriptor descriptor = descriptors.get(key);
    if (descriptor == null) {
        try {//from  w w w .ja v  a 2 s .  c  o  m
            descriptor = new PropertyDescriptor((String) key, getWrapper().getClass());
            descriptors.put(key, descriptor);
        } catch (IntrospectionException e) {
            throw new RuntimeException(e);
        }
    }
    return descriptor;
}

From source file:com.webpagebytes.cms.local.WPBLocalDataStoreDao.java

public void setObjectProperty(Object object, String property, Object propertyValue)
        throws WPBSerializerException {
    try {/*from w w  w.  ja va2s . co m*/
        PropertyDescriptor pd = new PropertyDescriptor(property, object.getClass());
        pd.getWriteMethod().invoke(object, propertyValue);
    } catch (Exception e) {
        log.log(Level.SEVERE, "cannot setObjectProperty on " + property, e);
        throw new WPBSerializerException("Cannot set property for object", e);
    }

}