Example usage for java.beans PropertyDescriptor setValue

List of usage examples for java.beans PropertyDescriptor setValue

Introduction

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

Prototype

public void setValue(String attributeName, Object value) 

Source Link

Document

Associate a named attribute with this feature.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    BeanInfo bi = Introspector.getBeanInfo(BeanToXmlTransient.class);
    PropertyDescriptor[] pds = bi.getPropertyDescriptors();
    for (int i = 0; i < pds.length; i++) {
        PropertyDescriptor propertyDescriptor = pds[i];
        if (propertyDescriptor.getName().equals("itemQuantities")) {
            propertyDescriptor.setValue("transient", Boolean.TRUE);
        }/* www  .  ja  va 2  s.c  o  m*/
    }
    BeanToXmlTransient bean = new BeanToXmlTransient();
    bean.setId(new Long(1));
    bean.setItemName("Item");
    bean.setItemColour("Red");
    bean.setItemQuantities(new Integer(100));

    XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("BeanTransient.xml")));

    encoder.writeObject(bean);
    encoder.close();

}

From source file:Main.java

public static void makeTransient(Class<?> beanClass, String... pdNames) {
    try {/*from  w  ww .  ja  va 2  s  .c o  m*/
        BeanInfo info = Introspector.getBeanInfo(beanClass);
        PropertyDescriptor[] descs = info.getPropertyDescriptors();
        if (descs == null) {
            throw new RuntimeException("Cannot access property descriptor for class " + beanClass);
        }
        Map<String, PropertyDescriptor> mapping = new HashMap<String, PropertyDescriptor>();
        for (PropertyDescriptor desc : descs) {
            mapping.put(desc.getName(), desc);
        }
        for (String pdName : pdNames) {
            PropertyDescriptor desc = mapping.get(pdName);
            if (desc == null) {
                throw new RuntimeException("Property " + pdName + " does not exist in " + beanClass);
            }
            desc.setValue("transient", Boolean.TRUE);
        }
    } catch (IntrospectionException ie) {
        throw new RuntimeException(ie);
    }
}

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   ww  w.  ja v a2 s .  c  o  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:com.twinsoft.convertigo.beans.core.MySimpleBeanInfo.java

protected PropertyDescriptor getPropertyDescriptor(String name) throws IntrospectionException {
    checkAdditionalProperties();/*w w w .  j a va  2  s .  co m*/
    for (int i = 0; i < properties.length; i++) {
        PropertyDescriptor property = properties[i];
        if (name.equals(property.getName())) {
            PropertyDescriptor clone = new PropertyDescriptor(name, property.getReadMethod(),
                    property.getWriteMethod());
            clone.setDisplayName(property.getDisplayName());
            clone.setShortDescription(property.getShortDescription());
            clone.setPropertyEditorClass(property.getPropertyEditorClass());
            clone.setBound(property.isBound());
            clone.setConstrained(property.isConstrained());
            clone.setExpert(property.isExpert());
            clone.setHidden(property.isHidden());
            clone.setPreferred(property.isPreferred());
            for (String attributeName : Collections.list(property.attributeNames())) {
                clone.setValue(attributeName, property.getValue(attributeName));
            }
            return properties[i] = clone;
        }
    }
    return null;
}

From source file:org.apache.jmeter.protocol.jdbc.autoCSV_JDBCconfig.DataSourceElementBeanInfo.java

public DataSourceElementBeanInfo() {
    super(DataSourceElement.class);

    createPropertyGroup("varName", new String[] { "dataSource" });

    createPropertyGroup("pool", new String[] { "poolMax", "timeout", "trimInterval" });

    createPropertyGroup("keep-alive", new String[] { "keepAlive", "connectionAge", "checkQuery" });

    createPropertyGroup("database",
            new String[] { "dbUrl", "dbName", "tableName", "driver", "username", "password" });

    PropertyDescriptor p = property("dataSource");
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, "");
    p = property("poolMax");
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, "10");
    p = property("timeout");
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, "10000");
    p = property("trimInterval");
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, "60000");
    //        p = property("autocommit");
    //        p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    //        p.setValue(DEFAULT, Boolean.TRUE);
    //        p = property("transactionIsolation");
    //        p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    //        p.setValue(DEFAULT, "DEFAULT");
    //        p.setValue(NOT_EXPRESSION, Boolean.TRUE);
    //        Set<String> modesSet = TRANSACTION_ISOLATION_MAP.keySet();
    //        String[] modes = modesSet.toArray(new String[modesSet.size()]);
    //        p.setValue(TAGS, modes);
    p = property("keepAlive");
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, Boolean.TRUE);
    p = property("connectionAge");
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, "5000");
    p = property("checkQuery");
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, "Select 1");
    p = property("dbUrl");
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, "");

    p = property("dbName");
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, "");
    p = property("tableName");
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, "");

    p = property("driver");
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, "");
    p = property("username");
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, "");
    p = property("password", TypeEditor.PasswordEditor);
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, "");
}

From source file:org.apache.jmeter.protocol.jdbc.config.DataSourceElementBeanInfo.java

public DataSourceElementBeanInfo() {
    super(DataSourceElement.class);

    createPropertyGroup("varName", new String[] { "dataSource" });

    createPropertyGroup("pool",
            new String[] { "poolMax", "timeout", "trimInterval", "autocommit", "transactionIsolation" });

    createPropertyGroup("keep-alive", new String[] { "keepAlive", "connectionAge", "checkQuery" });

    createPropertyGroup("database", new String[] { "dbUrl", "driver", "username", "password" });

    PropertyDescriptor p = property("dataSource");
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, "");
    p = property("poolMax");
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, "10");
    p = property("timeout");
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, "10000");
    p = property("trimInterval");
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, "60000");
    p = property("autocommit");
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, Boolean.TRUE);
    p = property("transactionIsolation");
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, "DEFAULT");
    p.setValue(NOT_EXPRESSION, Boolean.TRUE);
    Set<String> modesSet = TRANSACTION_ISOLATION_MAP.keySet();
    String[] modes = modesSet.toArray(new String[modesSet.size()]);
    p.setValue(TAGS, modes);/*  ww  w.ja va  2 s  . com*/
    p = property("keepAlive");
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, Boolean.TRUE);
    p = property("connectionAge");
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, "5000");
    p = property("checkQuery");
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, "Select 1");
    p = property("dbUrl");
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, "");
    p = property("driver");
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, "");
    p = property("username");
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, "");
    p = property("password", TypeEditor.PasswordEditor);
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, "");
}

From source file:org.apache.jmeter.util.ScriptingBeanInfoSupport.java

protected ScriptingBeanInfoSupport(Class<? extends TestBean> beanClass, String[] languageTags,
        ResourceBundle rb) {/*from  ww w.jav a2  s. com*/
    super(beanClass);
    PropertyDescriptor p;

    p = property("scriptLanguage"); // $NON-NLS-1$
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, ""); // $NON-NLS-1$
    if (rb != null) {
        p.setValue(RESOURCE_BUNDLE, rb);
    }
    p.setValue(TAGS, languageTags);

    createPropertyGroup("scriptingLanguage", // $NON-NLS-1$
            new String[] { "scriptLanguage" }); // $NON-NLS-1$

    p = property("parameters"); // $NON-NLS-1$
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, ""); // $NON-NLS-1$

    createPropertyGroup("parameterGroup", // $NON-NLS-1$
            new String[] { "parameters" }); // $NON-NLS-1$

    p = property("filename"); // $NON-NLS-1$
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, ""); // $NON-NLS-1$
    p.setPropertyEditorClass(FileEditor.class);

    createPropertyGroup("filenameGroup", // $NON-NLS-1$
            new String[] { "filename" }); // $NON-NLS-1$

    /*
     * If we are creating a JSR223 element, add the cache key property.
     * 
     * Note that this cannot be done in the JSR223BeanInfoSupport class
     * because that causes problems with the group; its properties are
     * not always set up before they are needed. This cause various
     * issues with the GUI:
     * - wrong field attributes (should not allow null)
     * - sometimes GUI is completely mangled
     * - field appears at start rather than at end.
     * - the following warning is logged:
     * jmeter.testbeans.gui.GenericTestBeanCustomizer: 
     * org.apache.jmeter.util.JSR223TestElement#cacheKey does not appear to have been configured
     * 
     * Adding the group here solves these issues, and it's also
     * possible to add the key just before the script panel
     * to which it relates.
     * 
     * It's not yet clear why this should be, but it looks as though
     * createPropertyGroup does not work properly if it is called from
     * any subclasses of this class.
     * 
     */
    if (JSR223TestElement.class.isAssignableFrom(beanClass)) {
        p = property("cacheKey"); // $NON-NLS-1$
        p.setValue(NOT_UNDEFINED, Boolean.TRUE);
        p.setValue(DEFAULT, ""); // $NON-NLS-1$
        p.setPropertyEditorClass(JSR223ScriptCacheCheckboxEditor.class);

        createPropertyGroup("cacheKey_group", // $NON-NLS-1$
                new String[] { "cacheKey" }); // $NON-NLS-1$
    }

    p = property("script"); // $NON-NLS-1$
    p.setValue(NOT_UNDEFINED, Boolean.TRUE);
    p.setValue(DEFAULT, ""); // $NON-NLS-1$
    p.setPropertyEditorClass(TextAreaEditor.class);

    createPropertyGroup("scripting", // $NON-NLS-1$
            new String[] { "script" }); // $NON-NLS-1$

}

From source file:org.apache.openjpa.lib.conf.ConfigurationImpl.java

/**
 * Create a property descriptor for the given value.
 *///  ww w  . j  a v a 2 s.c o m
private PropertyDescriptor getPropertyDescriptor(Value val) throws IntrospectionException {
    String prop = val.getProperty();
    prop = prop.substring(prop.lastIndexOf('.') + 1);

    // set up property descriptor
    PropertyDescriptor pd;
    try {
        pd = new PropertyDescriptor(Introspector.decapitalize(prop), getClass());
    } catch (IntrospectionException ie) {
        // if there aren't any methods for this value(i.e., if it's a
        // dynamically-added value), then an IntrospectionException will
        // be thrown. Try to create a PD with no read or write methods.
        pd = new PropertyDescriptor(Introspector.decapitalize(prop), (Method) null, (Method) null);
    }
    pd.setDisplayName(findLocalized(prop + "-name", true, val.getScope()));
    pd.setShortDescription(findLocalized(prop + "-desc", true, val.getScope()));
    pd.setExpert("true".equals(findLocalized(prop + "-expert", false, val.getScope())));

    try {
        pd.setReadMethod(getClass().getMethod("get" + StringUtils.capitalize(prop), (Class[]) null));
        pd.setWriteMethod(getClass().getMethod("set" + StringUtils.capitalize(prop),
                new Class[] { pd.getReadMethod().getReturnType() }));
    } catch (Throwable t) {
        // if an error occurs, it might be because the value is a
        // dynamic property.
    }

    String type = findLocalized(prop + "-type", true, val.getScope());
    if (type != null)
        pd.setValue(ATTRIBUTE_TYPE, type);

    String cat = findLocalized(prop + "-cat", false, val.getScope());
    if (cat != null)
        pd.setValue(ATTRIBUTE_CATEGORY, cat);

    pd.setValue(ATTRIBUTE_XML, toXMLName(prop));

    String order = findLocalized(prop + "-displayorder", false, val.getScope());
    if (order != null)
        pd.setValue(ATTRIBUTE_ORDER, order);

    // collect allowed values from alias keys, listed values, and
    // interface implementors
    Collection<String> allowed = new TreeSet<String>();
    List<String> aliases = Collections.emptyList();
    if (val.getAliases() != null) {
        aliases = Arrays.asList(val.getAliases());
        for (int i = 0; i < aliases.size(); i += 2)
            allowed.add(aliases.get(i));
    }
    String[] vals = Strings.split(findLocalized(prop + "-values", false, val.getScope()), ",", 0);
    for (int i = 0; i < vals.length; i++)
        if (!aliases.contains(vals[i]))
            allowed.add(vals[i]);
    try {
        Class<?> intf = Class.forName(findLocalized(prop + "-interface", true, val.getScope()), false,
                getClass().getClassLoader());
        pd.setValue(ATTRIBUTE_INTERFACE, intf.getName());
        String[] impls = Services.getImplementors(intf);
        for (int i = 0; i < impls.length; i++)
            if (!aliases.contains(impls[i]))
                allowed.add(impls[i]);
    } catch (Throwable t) {
    }
    if (!allowed.isEmpty())
        pd.setValue(ATTRIBUTE_ALLOWED_VALUES, (String[]) allowed.toArray(new String[allowed.size()]));

    return pd;
}

From source file:org.apache.tiles.evaluator.el.TilesContextELResolver.java

/**
 * Collects bean infos from a class and filling a list.
 *
 * @param clazz The class to be inspected.
 * @param list The list to fill.//from   w w w  .  j a  v  a 2  s.  co  m
 * @param properties The properties set to be filled.
 * @since 2.1.0
 */
protected void collectBeanInfo(Class<?> clazz, List<FeatureDescriptor> list, Set<String> properties) {
    BeanInfo info = null;
    try {
        info = Introspector.getBeanInfo(clazz);
    } catch (Exception ex) {
        if (log.isDebugEnabled()) {
            log.debug("Cannot inspect class " + clazz, ex);
        }
    }
    if (info == null) {
        return;
    }
    for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
        pd.setValue("type", pd.getPropertyType());
        pd.setValue("resolvableAtDesignTime", Boolean.TRUE);
        list.add(pd);
        properties.add(pd.getName());
    }
}