List of usage examples for java.beans PropertyDescriptor getName
public String getName()
From source file:io.fabric8.devops.ProjectConfigs.java
/** * Configures the given {@link ProjectConfig} with a map of key value pairs from * something like a JBoss Forge command/* w w w. j a v a 2 s. c o m*/ */ public static void configureProperties(ProjectConfig config, Map map) { Class<? extends ProjectConfig> clazz = config.getClass(); BeanInfo beanInfo = null; try { beanInfo = Introspector.getBeanInfo(clazz); } catch (IntrospectionException e) { LOG.warn("Could not introspect " + clazz.getName() + ". " + e, e); } if (beanInfo != null) { PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor descriptor : propertyDescriptors) { Method writeMethod = descriptor.getWriteMethod(); if (writeMethod != null) { String name = descriptor.getName(); Object value = map.get(name); if (value != null) { Object safeValue = null; Class<?> propertyType = descriptor.getPropertyType(); if (propertyType.isInstance(value)) { safeValue = value; } else { PropertyEditor editor = descriptor.createPropertyEditor(config); if (editor == null) { editor = PropertyEditorManager.findEditor(propertyType); } if (editor != null) { String text = value.toString(); editor.setAsText(text); safeValue = editor.getValue(); } else { LOG.warn("Cannot update property " + name + " with value " + value + " of type " + propertyType.getName() + " on " + clazz.getName()); } } if (safeValue != null) { try { writeMethod.invoke(config, safeValue); } catch (Exception e) { LOG.warn("Failed to set property " + name + " with value " + value + " on " + clazz.getName() + " " + config + ". " + e, e); } } } } } } String flow = null; Object flowValue = map.get("pipeline"); if (flowValue == null) { flowValue = map.get("flow"); } if (flowValue != null) { flow = flowValue.toString(); } config.setPipeline(flow); }
From source file:ReflectUtil.java
/** * Fetches the property descriptor for the named property of the supplied class. To * speed things up a cache is maintained of propertyName to PropertyDescriptor for * each class used with this method. If there is no property with the specified name, * returns null./*from ww w. ja va 2 s .c om*/ * * @param clazz the class who's properties to examine * @param property the String name of the property to look for * @return the PropertyDescriptor or null if none is found with a matching name */ public static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String property) { Map<String, PropertyDescriptor> pds = propertyDescriptors.get(clazz); if (pds == null) { try { BeanInfo info = Introspector.getBeanInfo(clazz); PropertyDescriptor[] descriptors = info.getPropertyDescriptors(); pds = new HashMap<String, PropertyDescriptor>(); for (PropertyDescriptor descriptor : descriptors) { pds.put(descriptor.getName(), descriptor); } propertyDescriptors.put(clazz, pds); } catch (IntrospectionException ie) { throw new RuntimeException("Could not examine class '" + clazz.getName() + "' using Introspector.getBeanInfo() to determine property information.", ie); } } return pds.get(property); }
From source file:de.iteratec.iteraplan.businesslogic.exchange.common.vbb.impl.util.VisualVariableHelper.java
/** * Derives all visual variables from a given class and adds the variables as EAttributes to the supplied EClass. * @param vbbClass the class that defines the visual variables via annotated methods {@link VisualVariable} * @param eClass the EClass to which the introspected variables should be added as EAttributes *///from ww w . j a v a 2s . c om public static void addAllVisualVariables(Class<? extends VBB> vbbClass, EClass eClass) { for (PropertyDescriptor vvCandidate : PropertyUtils.getPropertyDescriptors(vbbClass)) { VisualVariable vvAnnotation = getVVAnnotation(vvCandidate); if (vvAnnotation != null) { EAttribute att = EcoreFactory.eINSTANCE.createEAttribute(); att.setName(vvCandidate.getName()); att.setLowerBound(vvAnnotation.mandatory() ? 1 : 0); setMultiplicityAndDataType(eClass.getEPackage(), att, vvCandidate); if (att.getEType() != null) { eClass.getEStructuralFeatures().add(att); } else { LOGGER.warn("Could not introspect type for field " + vvCandidate.getName() + "."); } } } }
From source file:eu.squadd.reflections.mapper.ServiceModelTranslator.java
/** * this is Java Reflections translation method doing the magic where JAXB cannot * it is type independent as long as it is a valid java object * it cannot be an interface, abstract class and cannot use generics * //w w w . ja va2s . c o m * @param <T> * @param sourceClass - type of the source object * @param destClass - type of destination object * @param source - source object itself * @return - translated destination object */ public static <T> T transposeModel(Class sourceClass, Class<T> destClass, Object source) { Object destInstance = null; try { destInstance = ConstructorUtils.invokeConstructor(destClass, null); BeanInfo destInfo = Introspector.getBeanInfo(destClass); PropertyDescriptor[] destProps = destInfo.getPropertyDescriptors(); BeanInfo sourceInfo = Introspector.getBeanInfo(sourceClass, Object.class); PropertyDescriptor[] sourceProps = sourceInfo.getPropertyDescriptors(); for (PropertyDescriptor sourceProp : sourceProps) { String name = sourceProp.getName(); Method getter = sourceProp.getReadMethod(); Class<?> sType; try { sType = sourceProp.getPropertyType(); } catch (NullPointerException ex) { System.err .println("The type of source field cannot be determined, field skipped, name: " + name); continue; } Object sValue; try { sValue = getter.invoke(source); } catch (NullPointerException ex) { System.err.println("Cannot obtain the value from field, field skipped, name: " + name); continue; } for (PropertyDescriptor destProp : destProps) { if (destProp.getName().equals(name)) { if (assignPropertyValue(sourceProp, sValue, destProp, destInstance)) System.out.println( "Destination property assigned, source name: " + name + ", type: " + sType); else System.err.println( "Failed to assign property, source name: " + name + ", type: " + sType); break; } } } } catch (InvocationTargetException | IntrospectionException | IllegalAccessException | IllegalArgumentException | NoSuchMethodException | InstantiationException ex) { Logger.getLogger(ServiceModelTranslator.class.getName()).log(Level.SEVERE, null, ex); } return destClass.cast(destInstance); }
From source file:es.logongas.ix3.util.ReflectionUtil.java
/** * Obtiene el valor de la propiedad de un Bean * * @param obj El objeto Bean/*from ww w . java2 s . c om*/ * @param propertyName El nombre de la propiedad. Se permiten * "subpropiedades" separadas por "." * @return El valor de la propiedad */ static public Object getValueFromBean(Object obj, String propertyName) { try { Object value; BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); if ((propertyName == null) || (propertyName.trim().isEmpty())) { throw new RuntimeException("El parametro propertyName no puede ser null o estar vacio"); } String leftPropertyName; //El nombre de la propiedad antes del primer punto String rigthPropertyName; //El nombre de la propiedad antes del primer punto int indexPoint = propertyName.indexOf("."); if (indexPoint < 0) { leftPropertyName = propertyName; rigthPropertyName = null; } else if ((indexPoint > 0) && (indexPoint < (propertyName.length() - 1))) { leftPropertyName = propertyName.substring(0, indexPoint); rigthPropertyName = propertyName.substring(indexPoint + 1); } else { throw new RuntimeException("El punto no puede estar ni al principio ni al final"); } Method readMethod = null; for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { if (propertyDescriptor.getName().equals(leftPropertyName)) { readMethod = propertyDescriptor.getReadMethod(); } } if (readMethod == null) { throw new RuntimeException("No existe la propiedad:" + leftPropertyName); } if (rigthPropertyName != null) { Object valueProperty = readMethod.invoke(obj); value = getValueFromBean(valueProperty, rigthPropertyName); } else { value = readMethod.invoke(obj); } return value; } catch (Exception ex) { throw new RuntimeException(ex); } }
From source file:com.qcadoo.model.internal.classconverter.ModelXmlToClassConverterTest.java
@BeforeClass public static void init() throws Exception { MODEL_XML_TO_CLASSCONVERTER.setBeanClassLoader(ClassLoader.getSystemClassLoader()); for (Class<?> clazz : MODEL_XML_TO_CLASSCONVERTER.convert(Utils.FULL_FIRST_ENTITY_XML_RESOURCE, Utils.FULL_SECOND_ENTITY_XML_RESOURCE, Utils.FULL_THIRD_ENTITY_XML_RESOURCE, Utils.OTHER_FIRST_ENTITY_XML_RESOURCE, Utils.OTHER_SECOND_ENTITY_XML_RESOURCE)) { classes.put(clazz.getCanonicalName(), clazz); }/* ww w.j av a 2s. c o m*/ for (PropertyDescriptor propertyDescriptor : PropertyUtils.getPropertyDescriptors( classes.get(ClassNameUtils.getFullyQualifiedClassName("full", "firstEntity")))) { propertyDescriptors.put(propertyDescriptor.getName(), propertyDescriptor); } }
From source file:es.logongas.ix3.util.ReflectionUtil.java
/** * Este mtodo//from w w w .j ava2 s . co m * * @param clazz * @param propertyName El nombre de la propiedad permite que sean varias * "nested" con puntos. Ej: "prop1.prop2.prop3" * @return */ private static PropertyDescriptor getPropertyDescriptor(Class clazz, String propertyName) { try { BeanInfo beanInfo = Introspector.getBeanInfo(clazz); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); if ((propertyName == null) || (propertyName.trim().isEmpty())) { throw new RuntimeException("El parametro propertyName no puede ser null o estar vacio"); } String leftPropertyName; //El nombre de la propiedad antes del primer punto String rigthPropertyName; //El nombre de la propiedad despues del primer punto int indexPoint = propertyName.indexOf("."); if (indexPoint < 0) { leftPropertyName = propertyName; rigthPropertyName = null; } else if ((indexPoint > 0) && (indexPoint < (propertyName.length() - 1))) { leftPropertyName = propertyName.substring(0, indexPoint); rigthPropertyName = propertyName.substring(indexPoint + 1); } else { throw new RuntimeException("El punto no puede estar ni al principio ni al final"); } PropertyDescriptor propertyDescriptorFind = null; for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { if (propertyDescriptor.getName().equals(leftPropertyName)) { propertyDescriptorFind = propertyDescriptor; break; } } if (propertyDescriptorFind == null) { throw new RuntimeException("No existe el propertyDescriptorFind de " + leftPropertyName); } if (rigthPropertyName != null) { Method readMethod = propertyDescriptorFind.getReadMethod(); if (readMethod == null) { throw new RuntimeException("No existe el metodo 'get' de " + leftPropertyName); } Class readClass = readMethod.getReturnType(); return getPropertyDescriptor(readClass, rigthPropertyName); } else { return propertyDescriptorFind; } } catch (Exception ex) { throw new RuntimeException(ex); } }
From source file:es.logongas.ix3.util.ReflectionUtil.java
/** * Establecer el valor en un bena/* w w w . j ava 2 s .c o m*/ * * @param obj El objeto al que se establece el valor * @param propertyName El nombre de la propieda a establecer el valor. Se * permiten "subpropiedades" separadas por "." * @param value El valor a establecer. */ static public void setValueToBean(Object obj, String propertyName, Object value) { try { if ((propertyName == null) || (propertyName.trim().isEmpty())) { throw new RuntimeException("El parametro propertyName no puede ser null o estar vacio"); } if (obj == null) { throw new RuntimeException("El parametro obj no puede ser null"); } BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); String leftPropertyName; //El nombre de la propiedad antes del primer punto String rigthPropertyName; //El nombre de la propiedad antes del primer punto int indexPoint = propertyName.indexOf("."); if (indexPoint < 0) { leftPropertyName = propertyName; rigthPropertyName = null; } else if ((indexPoint > 0) && (indexPoint < (propertyName.length() - 1))) { leftPropertyName = propertyName.substring(0, indexPoint); rigthPropertyName = propertyName.substring(indexPoint + 1); } else { throw new RuntimeException("El punto no puede estar ni al principio ni al final"); } Method readMethod = null; Method writeMethod = null; for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { if (propertyDescriptor.getName().equals(leftPropertyName)) { readMethod = propertyDescriptor.getReadMethod(); writeMethod = propertyDescriptor.getWriteMethod(); } } if (rigthPropertyName != null) { if (readMethod == null) { throw new RuntimeException("No existe la propiedad de lectura:" + leftPropertyName); } Object valueProperty = readMethod.invoke(obj); setValueToBean(valueProperty, rigthPropertyName, value); } else { if (writeMethod == null) { throw new RuntimeException("No existe la propiedad de escritura:" + leftPropertyName); } writeMethod.invoke(obj, new Object[] { value }); } } catch (Exception ex) { throw new RuntimeException("obj:" + obj + " propertyName=" + propertyName + " value=" + value, ex); } }
From source file:com.github.dozermapper.core.util.ReflectionUtils.java
private static PropertyDescriptor findPropDescriptorByName(List<PropertyDescriptor> propDescriptors, String name) {/*from ww w . jav a 2 s . c om*/ PropertyDescriptor result = null; for (PropertyDescriptor pd : propDescriptors) { if (pd.getName().equals(name)) { result = pd; break; } } return result; }
From source file:org.springframework.data.solr.server.support.SolrServerUtils.java
/** * Solr property names do not match the getters/setters used for them. Check on any write method, try to find the * according property and set the value for it. Will ignore all other, and nested properties * //from www .j a va2 s .c o m * @param source * @param target */ private static void copyProperties(SolrServer source, SolrServer target) { BeanWrapperImpl wrapperImpl = new BeanWrapperImpl(source); for (PropertyDescriptor pd : wrapperImpl.getPropertyDescriptors()) { Method writer = pd.getWriteMethod(); if (writer != null) { try { Field property = ReflectionUtils.findField(source.getClass(), pd.getName()); if (property != null) { ReflectionUtils.makeAccessible(property); Object o = ReflectionUtils.getField(property, source); if (o != null) { writer.invoke(target, o); } } } catch (Exception e) { logger.warn("Could not copy property value for: " + pd.getName(), e); } } } }