List of usage examples for org.apache.commons.beanutils PropertyUtils setProperty
public static void setProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
Set the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.
For more details see PropertyUtilsBean
.
From source file:io.neocdtv.eclipselink.entitygraph.CopyPartialEntities.java
private void handleSimpleNode(final AttributeNodeImpl node, final Object copyFrom, final Object copyTo) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { final String attributeName = node.getAttributeName(); final Object property = PropertyUtils.getProperty(copyFrom, attributeName); PropertyUtils.setProperty(copyTo, attributeName, property); }
From source file:com.utest.webservice.builders.Builder.java
public Ti toInfo(final To object, final UriBuilder ub, Object... uriBuilderArgs) throws Exception { Ti result;/*from w w w . j a v a 2s. c om*/ final Constructor<Ti> constr = infoClass.getConstructor(new Class[] {}); if (constr == null) { throw new IllegalArgumentException("No default constructor found for " + infoClass.getName()); } result = constr.newInstance(new Object[] {}); ConvertUtilsBean cub = new ConvertUtilsBean(); // do not throw exceptions on null values cub.register(false, true, 0); BeanUtilsBean bub = new BeanUtilsBean(cub); bub.copyProperties(result, object); // PropertyUtils.copyProperties(result, object); if (object instanceof LocalizedEntity) { LocalizedEntity localizedEntity = (LocalizedEntity) object; LocaleDescriptable localDescriptable = localizedEntity.getLocale(Locale.DEFAULT_LOCALE); // PropertyUtils.copyProperties(result, localDescriptable); bub.copyProperties(result, localDescriptable); } Map<?, ?> resultProperties = PropertyUtils.describe(result); // don't return password field if (resultProperties.containsKey("password")) { PropertyUtils.setProperty(result, "password", null); } // populateLocators(result, ub); // if (result instanceof BaseInfo) { populateIdentityAndTimeline((BaseInfo) result, object, ub, uriBuilderArgs); } // special handling for descendands of Builder populateExtendedProperties(result, object, ub, uriBuilderArgs); return result; }
From source file:com.expressui.core.view.tomanyrelationship.ToManyRelationship.java
/** * Sets references inside the given value to the parent. * * @param value value in which to set reference *///from w w w . j a v a 2 s .c o m public void setReferenceToParent(T value) { try { BeanPropertyType beanPropertyType = BeanPropertyType.getBeanPropertyType(getType(), getParentPropertyId()); Assert.PROGRAMMING.isTrue(!beanPropertyType.isCollectionType(), "Parent property id (" + getType() + "." + getParentPropertyId() + ") must not be a collection type"); PropertyUtils.setProperty(value, getParentPropertyId(), getEntityQuery().getParent()); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } }
From source file:jetbrains.buildServer.commitPublisher.github.api.impl.HttpClientWrapperImpl.java
public HttpClientWrapperImpl() throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException { final String serverVersion = ServerVersionHolder.getVersion().getDisplayVersion(); final HttpParams ps = new BasicHttpParams(); DefaultHttpClient.setDefaultHttpParams(ps); final int timeout = TeamCityProperties.getInteger("teamcity.github.http.timeout", 300 * 1000); HttpConnectionParams.setConnectionTimeout(ps, timeout); HttpConnectionParams.setSoTimeout(ps, timeout); HttpProtocolParams.setUserAgent(ps, "JetBrains TeamCity " + serverVersion); final SchemeRegistry schemaRegistry = SchemeRegistryFactory.createDefault(); final SSLSocketFactory sslSocketFactory = new SSLSocketFactory(new TrustStrategy() { public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return !TeamCityProperties.getBoolean("teamcity.github.verify.ssl.certificate"); }/* w w w . j av a 2 s . co m*/ }) { @Override public Socket connectSocket(int connectTimeout, Socket socket, HttpHost host, InetSocketAddress remoteAddress, InetSocketAddress localAddress, HttpContext context) throws IOException { if (socket instanceof SSLSocket) { try { PropertyUtils.setProperty(socket, "host", host.getHostName()); } catch (Exception ex) { LOG.warn(String.format( "A host name is not passed to SSL connection for the purpose of supporting SNI due to the following exception: %s", ex.toString())); } } return super.connectSocket(connectTimeout, socket, host, remoteAddress, localAddress, context); } }; schemaRegistry.register(new Scheme("https", 443, sslSocketFactory)); final DefaultHttpClient httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager(schemaRegistry), ps); setupProxy(httpclient); httpclient.setRoutePlanner(new ProxySelectorRoutePlanner( httpclient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault())); httpclient.addRequestInterceptor(new RequestAcceptEncoding()); httpclient.addResponseInterceptor(new ResponseContentEncoding()); httpclient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(3, true)); myClient = httpclient; }
From source file:com.discovery.darchrow.bean.PropertyUtil.java
/** * {@link PropertyUtils#setProperty(Object, String, Object)} ?(<b>??</b>). * /*w ww. j ava 2 s . c o m*/ * <pre> * BeanUtils.setProperty(pt1, "x", "9"); // 9String * PropertyUtils.setProperty(pt1, "x", 9); // int * // BeanUtilsPropertyUtils,?int?? * </pre> * * * <pre> * {@code * getPropertysetProperty,?2?JavaBean????. * Company c = new Company(); * c.setName("Simple"); * * Simple????? * //Simple * LOGGER.debug(BeanUtils.getProperty(c, "name")); * * Map???key?? * //Map * LOGGER.debug(BeanUtils.getProperty(c, "address (A2)")); * HashMap am = new HashMap(); * am.put("1","234-222-1222211"); * am.put("2","021-086-1232323"); * BeanUtils.setProperty(c,"telephone",am); * LOGGER.debug(BeanUtils.getProperty(c, "telephone (2)")); * * Indexed??[]??ArrayList???. * //index * LOGGER.debug(BeanUtils.getProperty(c, "otherInfo[2]")); * BeanUtils.setProperty(c, "product[1]", "NOTES SERVER"); * LOGGER.debug(BeanUtils.getProperty(c, "product[1]")); * * 3???? * //nest * LOGGER.debug(BeanUtils.getProperty(c, "employee[1].name")); * } * </pre> * * @param bean * Bean whose property is to be modified * @param name * Possibly indexed and/or nested name of the property to be modified * @param value * Value to which this property is to be set * @see org.apache.commons.beanutils.BeanUtils#setProperty(Object, String, Object) * @see org.apache.commons.beanutils.PropertyUtils#setProperty(Object, String, Object) * @see com.baozun.nebulaplus.bean.BeanUtil#setProperty(Object, String, Object) */ public static void setProperty(Object bean, String name, Object value) { try { //Set the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions. // PropertyUtilsBeanUtils,????? PropertyUtils.setProperty(bean, name, value); } catch (Exception e) { LOGGER.error(e.getClass().getName(), e); throw new BeanUtilException(e); } }
From source file:com.reachlocal.grails.plugins.cassandra.utils.OrmHelper.java
public static void safeSetProperty(Object object, String name, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { if (PropertyUtils.getPropertyType(object, name) != null) { PropertyUtils.setProperty(object, name, value); }//from ww w .jav a 2 s .c om }
From source file:com.mycollab.module.crm.ui.components.RelatedEditItemField.java
public RelatedEditItemField(Object bean) { this.bean = bean; relatedItemComboBox = new RelatedItemComboBox(); itemField = new TextField(); itemField.setEnabled(true);//w ww .j av a2 s .c o m browseBtn = new MButton("", clickEvent -> { String type = (String) relatedItemComboBox.getValue(); if (CrmTypeConstants.ACCOUNT.equals(type)) { AccountSelectionWindow accountWindow = new AccountSelectionWindow(RelatedEditItemField.this); UI.getCurrent().addWindow(accountWindow); accountWindow.show(); } else if (CrmTypeConstants.CAMPAIGN.equals(type)) { CampaignSelectionWindow campaignWindow = new CampaignSelectionWindow(RelatedEditItemField.this); UI.getCurrent().addWindow(campaignWindow); campaignWindow.show(); } else if (CrmTypeConstants.CONTACT.equals(type)) { ContactSelectionWindow contactWindow = new ContactSelectionWindow(RelatedEditItemField.this); UI.getCurrent().addWindow(contactWindow); contactWindow.show(); } else if (CrmTypeConstants.LEAD.equals(type)) { LeadSelectionWindow leadWindow = new LeadSelectionWindow(RelatedEditItemField.this); UI.getCurrent().addWindow(leadWindow); leadWindow.show(); } else if (CrmTypeConstants.OPPORTUNITY.equals(type)) { OpportunitySelectionWindow opportunityWindow = new OpportunitySelectionWindow( RelatedEditItemField.this); UI.getCurrent().addWindow(opportunityWindow); opportunityWindow.show(); } else if (CrmTypeConstants.CASE.equals(type)) { CaseSelectionWindow caseWindow = new CaseSelectionWindow(RelatedEditItemField.this); UI.getCurrent().addWindow(caseWindow); caseWindow.show(); } else { relatedItemComboBox.focus(); } }).withIcon(FontAwesome.ELLIPSIS_H).withStyleName(WebThemes.BUTTON_OPTION, WebThemes.BUTTON_SMALL_PADDING); clearBtn = new MButton("", clickEvent -> { try { PropertyUtils.setProperty(bean, "typeid", null); PropertyUtils.setProperty(bean, "type", null); relatedItemComboBox.setValue(null); itemField.setValue(""); } catch (Exception e) { LOG.error("Error while saving type", e); } }).withIcon(FontAwesome.TRASH_O).withStyleName(WebThemes.BUTTON_OPTION, WebThemes.BUTTON_SMALL_PADDING); }
From source file:io.neocdtv.eclipselink.entitygraph.CopyPartialEntities.java
private void copyDefaultAttributes(final Object copyFrom, final Object copyTo) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { final Object id = PropertyUtils.getProperty(copyFrom, ID); PropertyUtils.setProperty(copyTo, ID, id); final Object version = PropertyUtils.getProperty(copyFrom, VERSION); PropertyUtils.setProperty(copyTo, VERSION, version); }
From source file:net.sf.ij_plugins.util.DialogUtil.java
/** * Utility to automatically create ImageJ's GenericDialog for editing bean properties. It uses BeanInfo to extract * display names for each field. If a fields type is not supported irs name will be displayed with a tag * "[Unsupported type: class_name]"./* w w w . jav a 2s .c om*/ * * @param bean * @return <code>true</code> if user closed bean dialog using OK button, <code>false</code> otherwise. */ static public boolean showGenericDialog(final Object bean, final String title) { final BeanInfo beanInfo; try { beanInfo = Introspector.getBeanInfo(bean.getClass()); } catch (IntrospectionException e) { throw new IJPluginsRuntimeException("Error extracting bean info.", e); } final GenericDialog genericDialog = new GenericDialog(title); // Create generic dialog fields for each bean's property final PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); try { for (int i = 0; i < propertyDescriptors.length; i++) { final PropertyDescriptor pd = propertyDescriptors[i]; final Class type = pd.getPropertyType(); if (type.equals(Class.class) && "class".equals(pd.getName())) { continue; } final Object o = PropertyUtils.getSimpleProperty(bean, pd.getName()); if (type.equals(Boolean.TYPE)) { boolean value = ((Boolean) o).booleanValue(); genericDialog.addCheckbox(pd.getDisplayName(), value); } else if (type.equals(Integer.TYPE)) { int value = ((Integer) o).intValue(); genericDialog.addNumericField(pd.getDisplayName(), value, 0); } else if (type.equals(Float.TYPE)) { double value = ((Float) o).doubleValue(); genericDialog.addNumericField(pd.getDisplayName(), value, 6, 10, ""); } else if (type.equals(Double.TYPE)) { double value = ((Double) o).doubleValue(); genericDialog.addNumericField(pd.getDisplayName(), value, 6, 10, ""); } else { genericDialog.addMessage(pd.getDisplayName() + "[Unsupported type: " + type + "]"); } } } catch (IllegalAccessException e) { throw new IJPluginsRuntimeException(e); } catch (InvocationTargetException e) { throw new IJPluginsRuntimeException(e); } catch (NoSuchMethodException e) { throw new IJPluginsRuntimeException(e); } // final Panel helpPanel = new Panel(); // helpPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 0)); // final Button helpButton = new Button("Help"); //// helpButton.addActionListener(this); // helpPanel.add(helpButton); // genericDialog.addPanel(helpPanel, GridBagConstraints.EAST, new Insets(15,0,0,0)); // Show modal dialog genericDialog.showDialog(); if (genericDialog.wasCanceled()) { return false; } // Read fields from generic dialog into bean's properties. try { for (int i = 0; i < propertyDescriptors.length; i++) { final PropertyDescriptor pd = propertyDescriptors[i]; final Class type = pd.getPropertyType(); final Object propertyValue; if (type.equals(Boolean.TYPE)) { boolean value = genericDialog.getNextBoolean(); propertyValue = Boolean.valueOf(value); } else if (type.equals(Integer.TYPE)) { int value = (int) Math.round(genericDialog.getNextNumber()); propertyValue = new Integer(value); } else if (type.equals(Float.TYPE)) { double value = genericDialog.getNextNumber(); propertyValue = new Float(value); } else if (type.equals(Double.TYPE)) { double value = genericDialog.getNextNumber(); propertyValue = new Double(value); } else { continue; } PropertyUtils.setProperty(bean, pd.getName(), propertyValue); } } catch (IllegalAccessException e) { throw new IJPluginsRuntimeException(e); } catch (InvocationTargetException e) { throw new IJPluginsRuntimeException(e); } catch (NoSuchMethodException e) { throw new IJPluginsRuntimeException(e); } return true; }
From source file:fr.mtlx.odm.ClassAssistant.java
public <V> void setProperty(final String propertyName, final T entry, final Collection<V> multipleValues) throws MappingException { @SuppressWarnings("unchecked") Class<T> c = (Class<T>) checkNotNull(entry).getClass(); final AttributeMetadata meta = metadata.getAttributeMetadata(propertyName); if (meta == null) { throw new MappingException(format("propertyName: unknown property %s", propertyName)); }/*from w ww.jav a 2 s . co m*/ if (!meta.isMultivalued()) { throw new MappingException(format("propertyName: single valued property %s", propertyName)); } final Collection<?> targetValues; targetValues = buildCollection(meta.getCollectionType(), meta.getObjectType(), multipleValues); try { PropertyUtils.setProperty(entry, propertyName, targetValues); } catch (IllegalAccessException | InvocationTargetException e) { throw new MappingException(e); } catch (NoSuchMethodException e) { try { doWithFields(c, (final Field f) -> { boolean secured = !f.isAccessible(); if (secured) { f.setAccessible(true); } f.set(entry, targetValues); if (secured) { f.setAccessible(false); } }, (final Field field) -> { final int modifiers = field.getModifiers(); return !isStatic(modifiers) && (field.getName() == null ? propertyName == null : field.getName().equals(propertyName)); }); } catch (SecurityException | IllegalArgumentException e1) { throw new MappingException(e1); } } }