List of usage examples for org.apache.commons.beanutils DynaClass getName
public String getName();
getName() method of java.lang.ClassDynaClass implementation class to support different dynamic classes, with different sets of properties. From source file:org.apache.ddlutils.dynabean.SqlDynaBean.java
/** * {@inheritDoc}// w w w . ja v a 2s .c om */ public String toString() { StringBuffer result = new StringBuffer(); DynaClass type = getDynaClass(); DynaProperty[] props = type.getDynaProperties(); result.append(type.getName()); result.append(": "); for (int idx = 0; idx < props.length; idx++) { if (idx > 0) { result.append(", "); } result.append(props[idx].getName()); result.append(" = "); result.append(get(props[idx].getName())); } return result.toString(); }
From source file:org.apache.struts.action.DynaActionForm.java
/** * <p>Render a String representation of this object.</p> * * @return A string representation of this object. *//* w w w .j a va 2 s. c om*/ public String toString() { StringBuffer sb = new StringBuffer("DynaActionForm[dynaClass="); DynaClass dynaClass = getDynaClass(); if (dynaClass == null) { return sb.append("null]").toString(); } sb.append(dynaClass.getName()); DynaProperty[] props = dynaClass.getDynaProperties(); if (props == null) { props = new DynaProperty[0]; } for (int i = 0; i < props.length; i++) { sb.append(','); sb.append(props[i].getName()); sb.append('='); Object value = get(props[i].getName()); if (value == null) { sb.append("<NULL>"); } else if (value.getClass().isArray()) { int n = Array.getLength(value); sb.append("{"); for (int j = 0; j < n; j++) { if (j > 0) { sb.append(','); } sb.append(Array.get(value, j)); } sb.append("}"); } else if (value instanceof List) { int n = ((List) value).size(); sb.append("{"); for (int j = 0; j < n; j++) { if (j > 0) { sb.append(','); } sb.append(((List) value).get(j)); } sb.append("}"); } else if (value instanceof Map) { int n = 0; Iterator keys = ((Map) value).keySet().iterator(); sb.append("{"); while (keys.hasNext()) { if (n > 0) { sb.append(','); } n++; Object key = keys.next(); sb.append(key); sb.append('='); sb.append(((Map) value).get(key)); } sb.append("}"); } else { sb.append(value); } } sb.append("]"); return (sb.toString()); }
From source file:org.jaffa.dwr.converters.FlexBeanConverter.java
@Override public Object convertInbound(Class paramType, InboundVariable iv, InboundContext inctx) throws MarshallException { String value = iv.getValue(); // If the text is null then the whole bean is null if (value.trim().equals(ProtocolConstants.INBOUND_NULL)) { return null; }/* ww w.j a va 2 s . c om*/ if (!value.startsWith(ProtocolConstants.INBOUND_MAP_START)) { throw new MarshallException(paramType, Messages.getString("BeanConverter.FormatError", ProtocolConstants.INBOUND_MAP_START)); } if (!value.endsWith(ProtocolConstants.INBOUND_MAP_END)) { throw new MarshallException(paramType, Messages.getString("BeanConverter.FormatError", ProtocolConstants.INBOUND_MAP_START)); } value = value.substring(1, value.length() - 1); try { FlexBean bean; if (instanceType != null) { bean = (FlexBean) instanceType.newInstance(); } else { bean = (FlexBean) paramType.newInstance(); } Map properties = getPropertyMapFromObject(bean, false, true); // Loop through the properties passed in Map tokens = extractInboundTokens(paramType, value); // CUSTOM CODE: Determine the appropriate FlexClass, so that the properties are formatted to the correct layout { String key = "dynaClass"; String val = (String) tokens.remove(key); Property property = (Property) properties.get(key); if (val != null && property != null) { Class propType = FlexClass.class; //property.getPropertyType(); String[] split = ParseUtil.splitInbound(val); String splitValue = split[LocalUtil.INBOUND_INDEX_VALUE]; String splitType = split[LocalUtil.INBOUND_INDEX_TYPE]; InboundVariable nested = new InboundVariable(iv.getLookup(), null, splitType, splitValue); TypeHintContext incc = createTypeHintContext(inctx, property); Object output = converterManager.convertInbound(propType, nested, inctx, incc); property.setValue(bean, output); // The input from the client may merely pass the name of the FlexClass // Recreate the FlexClass to load all it's FlexProperties and then recreate the FlexBean DynaClass flexClass = bean.getDynaClass(); if (flexClass != null) { flexClass = FlexClass.instance(flexClass.getName()); bean = FlexBean.instance((FlexClass) flexClass); } } } // We should put the new object into the working map in case it // is referenced later nested down in the conversion process. if (instanceType != null) { inctx.addConverted(iv, instanceType, bean); } else { inctx.addConverted(iv, paramType, bean); } for (Iterator it = tokens.entrySet().iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); String key = (String) entry.getKey(); String val = (String) entry.getValue(); Property property = (Property) properties.get(key); if (property == null) { // CUSTOM CODE: Instead of logging a warning, assume that the inbound token // is valid, and create a descriptor for it, such that it invokes the setter // on the FlexBean Class propType = bean.getDynaClass() != null && bean.getDynaClass().getDynaProperty(key) != null ? bean.getDynaClass().getDynaProperty(key).getType() : String.class; property = new FlexDescriptor(key, propType); } Class propType = property.getPropertyType(); String[] split = ParseUtil.splitInbound(val); String splitValue = split[LocalUtil.INBOUND_INDEX_VALUE]; String splitType = split[LocalUtil.INBOUND_INDEX_TYPE]; InboundVariable nested = new InboundVariable(iv.getLookup(), null, splitType, splitValue); TypeHintContext incc = createTypeHintContext(inctx, property); Object output = converterManager.convertInbound(propType, nested, inctx, incc); property.setValue(bean, output); } return bean; } catch (MarshallException ex) { throw ex; } catch (Exception ex) { throw new MarshallException(paramType, ex); } }
From source file:org.jaffa.dwr.converters.FlexCriteriaBeanConverter.java
@Override public Object convertInbound(Class paramType, InboundVariable iv, InboundContext inctx) throws MarshallException { String value = iv.getValue(); // If the text is null then the whole bean is null if (value.trim().equals(ProtocolConstants.INBOUND_NULL)) { return null; }/* ww w.j av a2s. c o m*/ if (!value.startsWith(ProtocolConstants.INBOUND_MAP_START)) { throw new MarshallException(paramType, Messages.getString("BeanConverter.FormatError", ProtocolConstants.INBOUND_MAP_START)); } if (!value.endsWith(ProtocolConstants.INBOUND_MAP_END)) { throw new MarshallException(paramType, Messages.getString("BeanConverter.FormatError", ProtocolConstants.INBOUND_MAP_START)); } value = value.substring(1, value.length() - 1); try { FlexCriteriaBean bean; if (instanceType != null) { bean = (FlexCriteriaBean) instanceType.newInstance(); } else { bean = (FlexCriteriaBean) paramType.newInstance(); } Map properties = getPropertyMapFromObject(bean, false, true); // Loop through the properties passed in Map tokens = extractInboundTokens(paramType, value); // CUSTOM CODE: Determine the appropriate FlexClass, so that the properties are formatted to the correct layout { String key = "dynaClass"; String val = (String) tokens.remove(key); Property property = (Property) properties.get(key); if (val != null && property != null) { Class propType = FlexClass.class; //property.getPropertyType(); String[] split = ParseUtil.splitInbound(val); String splitValue = split[LocalUtil.INBOUND_INDEX_VALUE]; String splitType = split[LocalUtil.INBOUND_INDEX_TYPE]; InboundVariable nested = new InboundVariable(iv.getLookup(), null, splitType, splitValue); TypeHintContext incc = createTypeHintContext(inctx, property); Object output = converterManager.convertInbound(propType, nested, inctx, incc); property.setValue(bean, output); // The input from the client may merely pass the name of the FlexClass // Recreate the FlexClass to load all it's FlexProperties and then recreate the FlexCriteriaBean DynaClass flexClass = bean.getDynaClass(); if (flexClass != null) { flexClass = FlexClass.instance(flexClass.getName()); bean = FlexCriteriaBean.instance((FlexClass) flexClass); } } } // We should put the new object into the working map in case it // is referenced later nested down in the conversion process. if (instanceType != null) { inctx.addConverted(iv, instanceType, bean); } else { inctx.addConverted(iv, paramType, bean); } for (Iterator it = tokens.entrySet().iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); String key = (String) entry.getKey(); String val = (String) entry.getValue(); Property property = (Property) properties.get(key); if (property == null) { // CUSTOM CODE: Instead of logging a warning, assume that the inbound token // is valid, and create a descriptor for it, such that it invokes the setter // on the FlexCriteriaBean Class propType = bean.getDynaClass() != null && bean.getDynaClass().getDynaProperty(key) != null ? bean.getDynaClass().getDynaProperty(key).getType() : String.class; propType = findCriteriaFieldClass(propType); property = new FlexDescriptor(key, propType); } Class propType = property.getPropertyType(); String[] split = ParseUtil.splitInbound(val); String splitValue = split[LocalUtil.INBOUND_INDEX_VALUE]; String splitType = split[LocalUtil.INBOUND_INDEX_TYPE]; InboundVariable nested = new InboundVariable(iv.getLookup(), null, splitType, splitValue); TypeHintContext incc = createTypeHintContext(inctx, property); Object output = converterManager.convertInbound(propType, nested, inctx, incc); property.setValue(bean, output); } return bean; } catch (MarshallException ex) { throw ex; } catch (Exception ex) { throw new MarshallException(paramType, ex); } }
From source file:org.vaadin.viritin.ListContainer.java
public static Class<?> getNestedPropertyType(DynaClass bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, ClassNotFoundException, NoSuchFieldException { if (bean == null) { // The type is not properly initilized yet, just leave it as generic // Object return Object.class; }//www. ja v a 2 s . co m // Resolve nested references while (resolver.hasNested(name)) { String next = resolver.next(name); if (resolver.isIndexed(next) || resolver.isMapped(next)) { String property = resolver.getProperty(next); Class<?> clazz = Class.forName(bean.getName()); Class<?> detectTypeParameter = detectTypeParameter(clazz, property, resolver.isIndexed(name) ? 0 : 1); bean = WrapDynaClass.createDynaClass(detectTypeParameter); return getNestedPropertyType(bean, resolver.remove(name)); } DynaProperty db = bean.getDynaProperty(next); bean = WrapDynaClass.createDynaClass(db.getType()); name = resolver.remove(name); } if (resolver.isMapped(name) || resolver.isIndexed(name)) { String property = resolver.getProperty(name); Class<?> clazz = Class.forName(bean.getName()); return detectTypeParameter(clazz, property, resolver.isIndexed(name) ? 0 : 1); } Class<?> type; DynaProperty dynaProperty = bean.getDynaProperty(name); if (dynaProperty != null) { type = dynaProperty.getType(); } else { //happens for default methods Method method = obtainGetterOfProperty(bean, name); type = method.getReturnType(); } if (type.isPrimitive() == true) { // Vaadin can't handle primitive types in _all_ places, so use // wrappers instead. FieldGroup works, but e.g. Table in _editable_ // mode fails for some reason return ClassUtils.primitiveToWrapper(type); } return type; }
From source file:org.vaadin.viritin.ListContainer.java
/** * Explicitly resolves the getter bypassing commons.beanutils * @param beanClass/*w ww . j a v a2 s . c om*/ * @param propertyName * @return getter method of the property * @throws ClassNotFoundException if class forName fails * @throws NoSuchMethodException if there is no getter * @throws SecurityException if there are constraints by the security manager */ private static Method obtainGetterOfProperty(DynaClass beanClass, String propertyName) throws ClassNotFoundException, NoSuchMethodException, SecurityException { Class<?> clazz = Class.forName(beanClass.getName()); return clazz.getMethod("get" + firstLetterUppercase(propertyName)); }