List of usage examples for java.beans PropertyDescriptor getName
public String getName()
From source file:net.solarnetwork.web.support.JSONView.java
private void generateJavaBeanObject(JsonGenerator json, String key, Object bean, PropertyEditorRegistrar registrar) throws JsonGenerationException, IOException { if (key != null) { json.writeFieldName(key);/*from w ww .j a v a 2 s. c o m*/ } if (bean == null) { json.writeNull(); return; } BeanWrapper wrapper = getPropertyAccessor(bean, registrar); PropertyDescriptor[] props = wrapper.getPropertyDescriptors(); json.writeStartObject(); for (PropertyDescriptor prop : props) { String name = prop.getName(); if (this.getJavaBeanIgnoreProperties() != null && this.getJavaBeanIgnoreProperties().contains(name)) { continue; } if (wrapper.isReadableProperty(name)) { Object propVal = wrapper.getPropertyValue(name); if (propVal != null) { // test for SerializeIgnore Method getter = prop.getReadMethod(); if (getter != null && getter.isAnnotationPresent(SerializeIgnore.class)) { continue; } if (getPropertySerializerRegistrar() != null) { propVal = getPropertySerializerRegistrar().serializeProperty(name, propVal.getClass(), bean, propVal); } else { // Spring does not apply PropertyEditors on read methods, so manually handle PropertyEditor editor = wrapper.findCustomEditor(null, name); if (editor != null) { editor.setValue(propVal); propVal = editor.getAsText(); } } if (propVal instanceof Enum<?> || getJavaBeanTreatAsStringValues() != null && getJavaBeanTreatAsStringValues().contains(propVal.getClass())) { propVal = propVal.toString(); } writeJsonValue(json, name, propVal, registrar); } } } json.writeEndObject(); }
From source file:org.jdal.vaadin.data.ListBeanContainer.java
private void initDefaultProperties() { for (PropertyDescriptor pd : BeanUtils.getPropertyDescriptors(beanClass)) { this.properties.add(pd.getName()); this.propertyDescriptors.put(pd.getName(), pd); }/*from w w w.jav a 2 s .c o m*/ }
From source file:dhbw.ka.mwi.businesshorizon2.ui.process.period.input.AbstractInputPresenter.java
public void processEvent(ShowInputViewEventInterface event) { logger.debug("ShowDirektViewEvent erhalten"); period = event.getPeriod();/*from w ww.j ava2 s. com*/ getView().initForm(); getView().addHeader(period.getYear()); try { for (PropertyDescriptor pd : Introspector.getBeanInfo(period.getClass(), Object.class) .getPropertyDescriptors()) { logger.debug("Processing: " + pd.getName()); if (Arrays.asList(shownProperties).contains(pd.getDisplayName())) { try { String germanName; germanName = germanNamesProperties[Arrays.asList(shownProperties) .indexOf(pd.getDisplayName())]; boolean skipInitialContent = true; for (PropertyDescriptor pdr : Introspector.getBeanInfo(period.getClass(), Object.class) .getPropertyDescriptors()) { if ((pd.getDisplayName() + "Set").equals(pdr.getDisplayName())) { skipInitialContent = !(boolean) pdr.getReadMethod().invoke(period); logger.debug("method found and skipInitialContent set to " + skipInitialContent); } } if (skipInitialContent) { getView().addInputField(germanName); logger.debug("initialContent skipped"); } else { getView().addInputField(germanName, (double) pd.getReadMethod().invoke(period)); logger.debug("initialContent written"); } } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { e.printStackTrace(); } } } } catch (IntrospectionException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:org.brushingbits.jnap.common.bean.cloning.BeanCloner.java
private Object cloneBean(Object bean, Class<?> type) { BeanWrapper source = PropertyAccessorFactory.forBeanPropertyAccess(bean); BeanWrapper copy = PropertyAccessorFactory.forBeanPropertyAccess(BeanUtils.instantiate(type)); // keep instance for circular and multiple references context.setAsVisited(bean);/*from www .ja va 2s . com*/ alreadyCloned.put(bean, copy.getWrappedInstance()); PropertyDescriptor[] beanProperties = copy.getPropertyDescriptors(); for (PropertyDescriptor propertyDescriptor : beanProperties) { String name = propertyDescriptor.getName(); context.pushPath(name); if (copy.isReadableProperty(name) && copy.isWritableProperty(name)) { Object value = source.getPropertyValue(name); copy.setPropertyValue(name, clone(value)); } context.popPath(); } Object beanCopy = copy.getWrappedInstance(); source = null; copy = null; return beanCopy; }
From source file:com.wavemaker.runtime.service.ElementType.java
/** * Create an ElementType with one level of children (populated by looking at the bean properties of javaType). This * method should not be used recursively. *//*from www . j a v a2 s.c o m*/ public ElementType(String name, Class<?> javaType, boolean isList) { this(name, javaType.getName(), isList); PropertyDescriptor[] pds; pds = PropertyUtils.getPropertyDescriptors(javaType); List<ElementType> elements = new ArrayList<ElementType>(pds.length); for (PropertyDescriptor pd : pds) { if (pd.getName().equals("class")) { continue; } if (pd.getReadMethod() == null && pd.getWriteMethod() == null) { continue; } Class<?> klass; Type type; if (pd.getReadMethod() != null) { klass = pd.getReadMethod().getReturnType(); type = pd.getReadMethod().getGenericReturnType(); } else { klass = pd.getWriteMethod().getParameterTypes()[0]; type = pd.getWriteMethod().getGenericParameterTypes()[0]; } ElementType element; if (klass.isArray()) { element = new ElementType(pd.getName(), klass.getComponentType().getName(), true); } else if (Collection.class.isAssignableFrom(klass) && type instanceof ParameterizedType) { ParameterizedType ptype = (ParameterizedType) type; Type aType = ptype.getActualTypeArguments()[0]; element = new ElementType(pd.getName(), ((Class<?>) aType).getName(), true); } else { element = new ElementType(pd.getName(), klass.getName()); } elements.add(element); } this.properties = elements; }
From source file:com.chiralbehaviors.CoRE.phantasm.graphql.schemas.JooqSchema.java
private GraphQLFieldDefinition.Builder buildPrimitive(GraphQLFieldDefinition.Builder builder, PropertyDescriptor field, PhantasmProcessor processor) { builder.name(field.getName()).type((GraphQLOutputType) type(field, processor)).dataFetcher(env -> { Object record = env.getSource(); try {//from ww w. ja v a 2 s. co m return field.getReadMethod().invoke(record); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new IllegalStateException( String.format("unable to invoke %s", field.getReadMethod().toGenericString()), e); } }); return builder; }
From source file:com.gzj.tulip.jade.rowmapper.BeanPropertyRowMapper.java
/** * Initialize the mapping metadata for the given class. * //ww w . ja va2 s . c om * @param mappedClass the mapped class. */ protected void initialize() { this.mappedFields = new HashMap<String, PropertyDescriptor>(); PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass); if (checkProperties) { mappedProperties = new HashSet<String>(); } for (int i = 0; i < pds.length; i++) { PropertyDescriptor pd = pds[i]; if (pd.getWriteMethod() != null) { if (checkProperties) { this.mappedProperties.add(pd.getName()); } this.mappedFields.put(pd.getName().toLowerCase(), pd); for (String underscoredName : underscoreName(pd.getName())) { if (underscoredName != null && !pd.getName().toLowerCase().equals(underscoredName)) { this.mappedFields.put(underscoredName, pd); } } } } }
From source file:com.sunsprinter.diffunit.core.translators.AbstractPropertyDrivenTranslator.java
protected Map<String, PropertyDescriptor> buildAllPropertiesMap(final T object) throws TranslationException { final Map<String, PropertyDescriptor> allPropertiesMap = new LinkedHashMap<String, PropertyDescriptor>(); for (final PropertyDescriptor propertyDescriptor : retrieveAllProperties(object)) { // Skip write-only properties. if (propertyDescriptor.getReadMethod() != null) { allPropertiesMap.put(propertyDescriptor.getName(), propertyDescriptor); }//from w ww .j av a 2s . c o m } return allPropertiesMap; }
From source file:org.brushingbits.jnap.common.bean.visitor.BeanPropertyVisitor.java
protected void handleBean(Object source, Class<?> type) { context.nextLevel();//w w w.java2 s . com visitBean(source, type); BeanWrapper sourceBean = PropertyAccessorFactory.forBeanPropertyAccess(source); PropertyDescriptor[] beanProperties = BeanUtils.getPropertyDescriptors(type); for (PropertyDescriptor propertyDescriptor : beanProperties) { String name = propertyDescriptor.getName(); context.pushPath(name); if (sourceBean.isReadableProperty(name)) { Object value = sourceBean.getPropertyValue(name); visit(value); } context.popPath(); } context.prevLevel(); }
From source file:com.orange.mmp.api.ws.jsonrpc.SimpleBeanSerializer.java
@Override public Object marshall(SerializerState state, Object p, Object o) throws MarshallException { JSONObject jsonObj = new JSONObject(); try {/*w ww . j ava 2 s . c o m*/ BeanInfo beanInfo = Introspector.getBeanInfo(o.getClass(), Object.class); for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) { Method readMethod = propertyDescriptor.getReadMethod(); if (readMethod != null) { Object propValue = readMethod.invoke(o); Object json = ser.marshall(state, o, propValue, propertyDescriptor.getName()); jsonObj.put(propertyDescriptor.getName(), json); } } } catch (JSONException jse) { throw new MarshallException("Failed to marshall Bean"); } catch (IllegalAccessException iae) { throw new MarshallException("Failed to analyse Bean"); } catch (InvocationTargetException ite) { throw new MarshallException("Failed to analyse Bean"); } catch (IntrospectionException ie) { throw new MarshallException("Failed to analyse Bean"); } return jsonObj; }