List of usage examples for java.beans PropertyDescriptor getName
public String getName()
From source file:com.seovic.core.objects.DynamicObject.java
public static Map<String, Object> getPropertyMap(Object obj) { Assert.notNull(obj, "Argument cannot be null"); try {// w w w . ja v a 2 s. c o m BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); Map<String, Object> propertyMap = new HashMap<String, Object>(propertyDescriptors.length); for (PropertyDescriptor pd : propertyDescriptors) { Method getter = pd.getReadMethod(); if (getter != null) { propertyMap.put(pd.getName(), getter.invoke(obj)); } } return propertyMap; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:acmi.l2.clientmod.xdat.Controller.java
private static List<PropertySheetItem> loadProperties(Object obj) { Class<?> objClass = obj.getClass(); List<PropertySheetItem> list = new ArrayList<>(); while (objClass != Object.class) { try {//from ww w .ja v a2 s .c o m List<String> names = Arrays.stream(objClass.getDeclaredFields()) .map(field -> field.getName().replace("Prop", "")).collect(Collectors.toList()); BeanInfo beanInfo = Introspector.getBeanInfo(objClass, objClass.getSuperclass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); Arrays.sort(propertyDescriptors, (pd1, pd2) -> Integer.compare(names.indexOf(pd1.getName()), names.indexOf(pd2.getName()))); for (PropertyDescriptor descriptor : propertyDescriptors) { if ("metaClass".equals(descriptor.getName())) continue; if (Collection.class.isAssignableFrom(descriptor.getPropertyType())) continue; AnnotatedElement getter = descriptor.getReadMethod(); if (getter.isAnnotationPresent(Deprecated.class) || getter.isAnnotationPresent(Hide.class)) continue; String description = ""; if (getter.isAnnotationPresent(Description.class)) description = getter.getAnnotation(Description.class).value(); Class<? extends PropertyEditor<?>> propertyEditorClass = null; if (descriptor.getPropertyType() == Boolean.class || descriptor.getPropertyType() == Boolean.TYPE) { propertyEditorClass = BooleanPropertyEditor.class; } else if (getter.isAnnotationPresent(Tex.class)) { propertyEditorClass = TexturePropertyEditor.class; } else if (getter.isAnnotationPresent(Sysstr.class)) { propertyEditorClass = SysstringPropertyEditor.class; } BeanProperty property = new BeanProperty(descriptor, objClass.getSimpleName(), description, propertyEditorClass); list.add(property); } } catch (IntrospectionException e) { e.printStackTrace(); } objClass = objClass.getSuperclass(); } return list; }
From source file:jp.co.ctc_g.jfw.core.util.Beans.java
/** * ???/*from w w w .j a v a 2s. c o m*/ * @param clazz * @return ?? */ public static String[] listPseudoPropertyNames(Class<?> clazz) { List<String> names = new ArrayList<String>(); for (PropertyDescriptor pd : Beans.findPropertyDescriptorsFor(clazz)) { names.add(pd.getName()); } for (Field f : clazz.getFields()) { String n = f.getName(); if (!names.contains(n)) names.add(n); } return names.toArray(new String[0]); }
From source file:gr.interamerican.bo2.utils.JavaBeanUtils.java
/** * Sets the value of a property on an object. * // w w w. j a v a 2s.c o m * value does not need to be strongly typed, {@link BeanUtils} * will handle the conversions. * * @param pd * Descriptor of the property. * @param val * New value for the field. * @param obj * Object on which the field value is changed. */ static void setPropertyUntyped(PropertyDescriptor pd, Object val, Object obj) { try { BeanUtils.setProperty(obj, pd.getName(), val); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } }
From source file:de.micromata.genome.jpa.PropertyEntityCopier.java
static EntityCopyStatus copyPropertyDeep(IEmgr<?> emgr, PropertyDescriptor pd, Class<?> iface, Object target, Object source) {/*from w w w . ja v a 2 s . com*/ Object svalue = null; try { svalue = PropertyDescriptorUtils.readProperty(source, pd); } catch (PropertyAccessException ex) { /** * @logging * @reason Das interne Lesen einer Objekteigenschaft ist nicht mglich. * @action Interner Fehler. Entwickler / Support kontaktieren. */ GLog.warn(GenomeLogCategory.Jpa, "Cannot read bean property: " + source.getClass().getSimpleName() + DOT + pd.getName() + SEMICOLON_SPACE + ex.getMessage(), new LogExceptionAttribute(ex)); return EntityCopyStatus.NONE; } if (svalue == null) { return copyPropertyDefault(emgr, pd, iface, target, source); } Object tvalue; try { tvalue = PropertyDescriptorUtils.readProperty(target, pd); } catch (PropertyAccessException ex) { /** * @logging * @reason Das interne Lesen einer Objekteigenschaft ist nicht mglich. * @action Interner Fehler. Entwickler / Support kontaktieren. */ GLog.warn(GenomeLogCategory.Jpa, "Cannot read bean property: " + target.getClass().getSimpleName() + DOT + pd.getName() + SEMICOLON_SPACE + ex.getMessage(), new LogExceptionAttribute(ex)); return EntityCopyStatus.NONE; } if (tvalue == null) { return copyPropertyDefault(emgr, pd, iface, target, source); } return EmgrCopyUtils.copyTo(emgr, tvalue.getClass(), tvalue, svalue); }
From source file:org.paxml.util.ReflectUtils.java
/** * Set a property for a bean.//from w w w.ja v a 2 s .c om * * @param bean * the bean * @param pd * the property descriptor * @param value * the property value */ public static void callSetter(Object bean, PropertyDescriptor pd, Object value) { Method setter = pd.getWriteMethod(); if (setter == null) { throw new PaxmlRuntimeException( "Property '" + pd.getName() + "' is not settable on class: " + bean.getClass().getName()); } value = coerceType(value, setter.getParameterTypes()[0]); try { setter.invoke(bean, value); } catch (Exception e) { throw new PaxmlRuntimeException("Cannot call setter on property: " + pd.getName(), e); } }
From source file:gr.interamerican.bo2.utils.JavaBeanUtils.java
/** * Finds the properties of a bean.//from w w w . ja va 2 s .c o m * * <li>If the bean is a concrete class the properties of the bean, for which * there exists a setter method, a getter method or both. Properties of * super-types are returned as well.</li> * * <li>If the bean is an abstract class, an empty array is returned</li> * * <li>If the bean is an interface, the properties of the bean are returned. * The method also queries all super-interfaces and fetches their properties * as well.</li> * * @param type * the bean * @return Returns the property descriptors of a java bean. * * TODO: Support properties of abstract classes. */ public static PropertyDescriptor[] getBeansProperties(Class<?> type) { ArrayList<PropertyDescriptor> propertyDescriptor = new ArrayList<PropertyDescriptor>(); for (PropertyDescriptor p : PropertyUtils.getPropertyDescriptors(type)) { if (!propertyDescriptor.contains(p) && !p.getName().equals("class")) { //$NON-NLS-1$ propertyDescriptor.add(p); } } if (type.isInterface()) { Class<?>[] classArray = type.getInterfaces(); PropertyDescriptor[] pdArray; for (Class<?> next : classArray) { pdArray = getBeansProperties(next); for (PropertyDescriptor pd : pdArray) { if (!propertyDescriptor.contains(pd)) { propertyDescriptor.add(pd); } } } } return propertyDescriptor.toArray(new PropertyDescriptor[0]); }
From source file:jp.go.nict.langrid.p2pgridbasis.data.langrid.converter.ConvertUtil.java
static public DataAttributes encode(Object data) throws DataConvertException { setLangridConverter();//ww w.j a va2s. c om logger.debug("##### encode #####"); try { DataAttributes attr = new DataAttributes(); for (PropertyDescriptor descriptor : PropertyUtils.getPropertyDescriptors(data)) { if (PropertyUtils.isReadable(data, descriptor.getName())) { if (descriptor.getName().equalsIgnoreCase("supportedLanguages")) { //supportedLanguages attr.setAttribute(descriptor.getName(), (String) converter.lookup(descriptor.getPropertyType()).convert(String.class, PropertyUtils.getProperty(data, descriptor.getName()))); continue; } else if (descriptor.getName().equalsIgnoreCase("instance")) { // // continue; } else if (descriptor.getName().equalsIgnoreCase("wsdl")) { // // continue; } else if (descriptor.getName().equalsIgnoreCase("interfaceDefinitions")) { // // ServiceType type = (ServiceType) data; Map<String, ServiceInterfaceDefinition> map = new HashMap<String, ServiceInterfaceDefinition>(); map = type.getInterfaceDefinitions(); String str = ""; try { for (ServiceInterfaceDefinition s : map.values()) { str = str + "ProtocolId=" + s.getProtocolId() + "\n"; str = str + "Definition=" + Base64.encode(StreamUtil.readAsBytes(s.getDefinition().getBinaryStream())) + "\n"; str = str + "###ServiceInterfaceDefinition###\n"; } } catch (IOException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } attr.setAttribute("interfaceDefinition_list", str); continue; } else if (descriptor.getName().equalsIgnoreCase("allowedAppProvision")) { Service s = (Service) data; String value = ""; for (String str : s.getAllowedAppProvision()) { value = value + str + "\n"; } attr.setAttribute("allowedAppProvision", value); continue; } else if (descriptor.getName().equalsIgnoreCase("allowedUse")) { Service s = (Service) data; String value = ""; for (String str : s.getAllowedUse()) { value = value + str + "\n"; } attr.setAttribute("allowedUse", value); continue; } else if (descriptor.getName().equalsIgnoreCase("supportedDomains")) { Grid g = (Grid) data; List<Domain> list = g.getSupportedDomains(); String value = ""; for (Domain d : list) { value = value + d.getDomainId() + "\n"; } attr.setAttribute("supportedDomain_list", value); continue; } else if (descriptor.getName().equalsIgnoreCase("partnerServiceNamespaceURIs")) { //partnerServiceNamespaceURIs BPELService s = (BPELService) data; List<String> list = s.getPartnerServiceNamespaceURIs(); String value = ""; for (String str : list) { value = value + str + "\n"; } attr.setAttribute("partnerServiceNamespaceURI_list", value); continue; } else if (descriptor.getName().equalsIgnoreCase("serviceDeployments")) { //ServiceDeployment Service s = (Service) data; String str = ""; for (ServiceDeployment sd : s.getServiceDeployments()) { str = str + "GridId=" + sd.getGridId() + "\n"; str = str + "ServiceId=" + sd.getServiceId() + "\n"; str = str + "NodeId=" + sd.getNodeId() + "\n"; str = str + "ServicePath=" + sd.getServicePath() + "\n"; str = str + "Enabled=" + String.valueOf(sd.isEnabled()) + "\n"; str = str + "CreateTime=" + String.valueOf(sd.getCreatedDateTime().getTimeInMillis()) + "\n"; str = str + "UpdateTime=" + String.valueOf(sd.getUpdatedDateTime().getTimeInMillis()) + "\n"; if (sd.getDisabledByErrorDate() != null) { str = str + "ErrorDate=" + String.valueOf(sd.getDisabledByErrorDate().getTimeInMillis()) + "\n"; } if (sd.getDeployedDateTime() != null) { str = str + "DeployedTime=" + String.valueOf(sd.getDeployedDateTime().getTimeInMillis()) + "\n"; } str = str + "###ServiceDeployment###\n"; } attr.setAttribute("deployment_list", str); continue; } else if (descriptor.getName().equalsIgnoreCase("serviceEndpoints")) { //ServiceEndpoint StringBuilder str = new StringBuilder(); Service s = (Service) data; for (ServiceEndpoint se : s.getServiceEndpoints()) { str.append("GridId=" + se.getGridId() + "\n"); str.append("ProtocolId=" + se.getProtocolId() + "\n"); str.append("ServiceId=" + se.getServiceId() + "\n"); str.append("Enabled=" + String.valueOf(se.isEnabled()) + "\n"); str.append("Url=" + se.getUrl().toString() + "\n"); str.append("AuthUserName=" + se.getAuthUserName() + "\n"); str.append("AuthPassword=" + se.getAuthPassword() + "\n"); str.append("DisableReason=" + se.getDisableReason() + "\n"); str.append("CreateTime=" + String.valueOf(se.getCreatedDateTime().getTimeInMillis()) + "\n"); str.append("UpdateTime=" + String.valueOf(se.getUpdatedDateTime().getTimeInMillis()) + "\n"); if (se.getDisabledByErrorDate() != null) { str.append("ErrorDate=" + String.valueOf(se.getDisabledByErrorDate().getTimeInMillis()) + "\n"); } str.append("###ServiceEndpoint###\n"); } attr.setAttribute("endpoint_list", str.toString()); continue; } else if (descriptor.getName().equalsIgnoreCase("invocations")) { //Invocation String str = ""; Service s = (Service) data; for (Invocation in : s.getInvocations()) { str = str + "InvocationName=" + in.getInvocationName() + "\n"; str = str + "OwnerServiceGridId=" + in.getOwnerServiceGridId() + "\n"; str = str + "OwnerServiceId=" + in.getOwnerServiceId() + "\n"; str = str + "ServiceGridId=" + in.getServiceGridId() + "\n"; str = str + "ServiceId=" + in.getServiceId() + "\n"; str = str + "ServiceName=" + in.getServiceName() + "\n"; str = str + "CreateTime=" + String.valueOf(in.getCreatedDateTime().getTimeInMillis()) + "\n"; str = str + "UpdateTime=" + String.valueOf(in.getUpdatedDateTime().getTimeInMillis()) + "\n"; str = str + "###Invocation###\n"; } attr.setAttribute("invocations_list", str); continue; } else if (descriptor.getName().equalsIgnoreCase("metaAttributes")) { //metaAttributes String str = ""; if (data.getClass().getName().endsWith("ResourceType")) { ResourceType r = (ResourceType) data; for (ResourceMetaAttribute a : r.getMetaAttributes().values()) { str = str + "DomainId=" + a.getDomainId() + "\n"; str = str + "AttributeId=" + a.getAttributeId() + "\n"; str = str + "###MetaAttribute###\n"; } } else if (data.getClass().getName().endsWith("ServiceType")) { ServiceType s = (ServiceType) data; for (ServiceMetaAttribute a : s.getMetaAttributes().values()) { str = str + "DomainId=" + a.getDomainId() + "\n"; str = str + "AttributeId=" + a.getAttributeId() + "\n"; str = str + "###MetaAttribute###\n"; } } else { logger.info("metaAttributes : " + data.getClass().getName()); } attr.setAttribute("metaAttribute_list", str); continue; } else if (descriptor.getName().equalsIgnoreCase("attributes")) { //attribute String str = ""; if (data.getClass().getName().endsWith("User")) { User u = (User) data; for (UserAttribute a : u.getAttributes()) { str = str + "attribute_GridId=" + a.getGridId() + "\n"; str = str + "attribute_Id=" + a.getUserId() + "\n"; str = str + "attribute_Name=" + a.getName() + "\n"; str = str + "attribute_Value=" + a.getValue() + "\n"; str = str + "attribute_CreateTime=" + String.valueOf(a.getCreatedDateTime().getTimeInMillis()) + "\n"; str = str + "attribute_UpdateTime=" + String.valueOf(a.getUpdatedDateTime().getTimeInMillis()) + "\n"; str = str + "###Attribute###\n"; } } else if (data.getClass().getName().endsWith("Service")) { Service s = (Service) data; for (ServiceAttribute a : s.getAttributes()) { str = str + "attribute_GridId=" + a.getGridId() + "\n"; str = str + "attribute_Id=" + a.getServiceId() + "\n"; str = str + "attribute_Name=" + a.getName() + "\n"; str = str + "attribute_Value=" + a.getValue() + "\n"; str = str + "attribute_CreateTime=" + String.valueOf(a.getCreatedDateTime().getTimeInMillis()) + "\n"; str = str + "attribute_UpdateTime=" + String.valueOf(a.getUpdatedDateTime().getTimeInMillis()) + "\n"; str = str + "###Attribute###\n"; } } else if (data.getClass().getName().endsWith("Node")) { Node s = (Node) data; for (NodeAttribute a : s.getAttributes()) { str = str + "attribute_GridId=" + a.getGridId() + "\n"; str = str + "attribute_Id=" + a.getNodeId() + "\n"; str = str + "attribute_Name=" + a.getName() + "\n"; str = str + "attribute_Value=" + a.getValue() + "\n"; str = str + "attribute_CreateTime=" + String.valueOf(a.getCreatedDateTime().getTimeInMillis()) + "\n"; str = str + "attribute_UpdateTime=" + String.valueOf(a.getUpdatedDateTime().getTimeInMillis()) + "\n"; str = str + "###Attribute###\n"; } } else if (data.getClass().getName().endsWith("Grid")) { Grid s = (Grid) data; for (GridAttribute a : s.getAttributes()) { str = str + "attribute_GridId=" + a.getGridId() + "\n"; str = str + "attribute_Name=" + a.getName() + "\n"; str = str + "attribute_Value=" + a.getValue() + "\n"; str = str + "attribute_CreateTime=" + String.valueOf(a.getCreatedDateTime().getTimeInMillis()) + "\n"; str = str + "attribute_UpdateTime=" + String.valueOf(a.getUpdatedDateTime().getTimeInMillis()) + "\n"; str = str + "###Attribute###\n"; } } else if (data.getClass().getName().endsWith("Resource")) { Resource s = (Resource) data; for (ResourceAttribute a : s.getAttributes()) { str = str + "attribute_GridId=" + a.getGridId() + "\n"; str = str + "attribute_Id=" + a.getResourceId() + "\n"; str = str + "attribute_Name=" + a.getName() + "\n"; str = str + "attribute_Value=" + a.getValue() + "\n"; str = str + "attribute_CreateTime=" + String.valueOf(a.getCreatedDateTime().getTimeInMillis()) + "\n"; str = str + "attribute_UpdateTime=" + String.valueOf(a.getUpdatedDateTime().getTimeInMillis()) + "\n"; str = str + "###Attribute###\n"; } } attr.setAttribute("attribute_list", str); continue; } else if (data instanceof Service && (descriptor.getName().equals("alternateServiceId") || descriptor.getName().equals("useAlternateServices"))) { // // continue; } //Read OK if (data instanceof BPELService && descriptor.getName().equals("transferExecution")) { // ignore } else { attr.setAttribute(descriptor.getName(), BeanUtils.getProperty(data, descriptor.getName())); } } else if (descriptor.getPropertyType().isArray()) { logger.debug("name : " + descriptor.getName() + " isArray"); // // attr.setAttribute(descriptor.getName(), (String) converter.lookup(descriptor.getPropertyType()) .convert(String.class, PropertyUtils.getProperty(data, descriptor.getName()))); } else { logger.debug("Name : " + descriptor.getName()); for (Method m : data.getClass().getMethods()) { if (m.getName().equalsIgnoreCase("get" + descriptor.getName()) || m.getName().equalsIgnoreCase("is" + descriptor.getName())) { if (m.getParameterTypes().length != 0) { // // logger.debug("class : " + data.getClass().getName()); logger.debug("?:Skip"); break; } else { // // logger.debug("value : " + m.invoke(data)); } attr.setAttribute(descriptor.getName(), m.invoke(data).toString()); break; } } } } return attr; } catch (InvocationTargetException e) { throw new DataConvertException(e); } catch (IllegalArgumentException e) { throw new DataConvertException(e); } catch (IllegalAccessException e) { throw new DataConvertException(e); } catch (NoSuchMethodException e) { throw new DataConvertException(e); } }
From source file:com.github.hateoas.forms.spring.SpringActionDescriptor.java
static List<ActionParameterType> findBeanInfo(final Class<?> beanType, final List<ActionParameterType> previous) throws IntrospectionException, NoSuchFieldException { // TODO support Option provider by other method args? final BeanInfo beanInfo = Introspector.getBeanInfo(beanType); final PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); // add input field for every setter for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { String propertyName = propertyDescriptor.getName(); if (isDefinedAlready(propertyName, previous)) { continue; }//from ww w.j ava2 s.c o m final Method writeMethod = propertyDescriptor.getWriteMethod(); ActionParameterType type = null; if (writeMethod != null) { Field field = getFormAnnotated(propertyName, beanType); if (field != null) { type = new FieldParameterType(propertyName, field); } else { MethodParameter methodParameter = new MethodParameter(propertyDescriptor.getWriteMethod(), 0); type = new MethodParameterType(propertyName, methodParameter, propertyDescriptor.getReadMethod()); } } if (type == null) { continue; } previous.add(type); } return previous; }
From source file:gr.interamerican.bo2.utils.JavaBeanUtils.java
/** * Copies common properties of an object to another. * /*from w ww. j a va2 s . com*/ * The method will copy all properties of the source object that have their * name included in the properties array, to the target object. The property * will be copied only if it has the same type in both objects or if the type * of the first object's property is assignable to the type of the second * object's property. Source and target object don't need to be instances of * the same class. * * If either the source or the target objects are null the method will have * no effect on either object. * * @param source * Object who's properties will be copied to the target object. * @param target * Object to which the properties will be copied. * @param properties * Names of properties to copy. If this array is empty or null * then all properties of source will be copied to the target. */ public static void copyProperties(Object source, Object target, String[] properties) { if (source == null || target == null) { return; } Map<String, PropertyDescriptor> targetProperties = JavaBeanUtils.getBeansPropertiesMap(target.getClass()); Map<String, PropertyDescriptor> sourceProperties = JavaBeanUtils.getBeansPropertiesMap(source.getClass()); Map<String, PropertyDescriptor> propertiesToCopy = new HashMap<String, PropertyDescriptor>(); if (properties != null && properties.length != 0) { propertiesToCopy = new HashMap<String, PropertyDescriptor>(); for (int i = 0; i < properties.length; i++) { PropertyDescriptor pd = sourceProperties.get(properties[i].trim()); if (pd != null) { propertiesToCopy.put(pd.getName(), pd); } } } else { propertiesToCopy = sourceProperties; } for (PropertyDescriptor sourcePD : propertiesToCopy.values()) { String name = sourcePD.getName(); PropertyDescriptor targetPD = targetProperties.get(name); if (targetPD != null) { copyProperty(source, target, sourcePD, targetPD); } } }