/** * Set the object to be edited. * * @param value The object to be edited. */ public void setObject(Object value) { if (!(_type.isInstance(value))) { throw new IllegalArgumentException(value.getClass() + " is not of type " + _type); } _value = value; // Disable event generation. _squelchChangeEvents = true; // Iterate over each property, doing a lookup on the associated editor // and setting the editor's value to the value of the property. Iterator it = _prop2Editor.keySet().iterator(); while (it.hasNext()) { PropertyDescriptor desc = (PropertyDescriptor) it.next(); PropertyEditor editor = (PropertyEditor) _prop2Editor.get(desc); Method reader = desc.getReadMethod(); if (reader != null) { try { Object val = reader.invoke(_value, null); editor.setValue(val); } catch (IllegalAccessException ex) { ex.printStackTrace(); } catch (InvocationTargetException ex) { ex.getTargetException().printStackTrace(); } } } // Enable event generation. _squelchChangeEvents = false; }
protected void getBeanElementProperties(Element element, Object bean, PropertyDescriptor pd) throws IntrospectionException, IllegalAccessException { Element propertyElement = null; Class classOfProperty = pd.getPropertyType(); Object[] argsNone = {}; // If the property is "class" and the type is java.lang.Class.class then // this is the class of the bean, which we've already encoded. // In this special case, return null. if (!((pd.getName().charAt(0) == 'c') && pd.getName().equals("class")) && !classOfProperty.equals(java.lang.Class.class)) { // Don't process property if it is in the list of fields to be ignored boolean omittedField = false; try { Field field = bean.getClass().getDeclaredField(pd.getName()); omittedField = field.isAnnotationPresent(ObjectXmlOmitField.class); } catch (NoSuchFieldException nsfe) { } if (!omittedField) { String propertyName = formatName(pd.getName()); // Hereafter, we're trying to create a representation of the property // based on the property's value. // The very first thing we need to do is get the value of the // property as an object. If we can't do that, we can't get any // representation of the property at all. Object propertyValue = null; try { Method getter = pd.getReadMethod(); if (getter != null) { propertyValue = getter.invoke(bean, argsNone); } } catch (Exception ex) { // couldn't get value System.err.println(ex.getMessage()); } // See if this property's value is something we can represent as a // standard data type that can be converted to an xsd type, // or if it's something that must be represented as a JavaBean. if (propertyElement == null) { PropertyEditor propEditor = PropertyEditorManager.findEditor(classOfProperty); // If the property editor is not null, pass the property's // value to the PropertyEditor, and then ask the PropertyEditor // for the object and then attempt to convert it to a standard type. if ((propEditor != null) || (classOfProperty == java.util.Calendar.class) || (classOfProperty == java.util.Date.class)) { // The object is a standard type // (e.g. a wrapper around a primitive type, a String, or a Date or Calendar object) try { Object object; if ((classOfProperty == java.util.Calendar.class) || (classOfProperty == java.util.Date.class)) object = propertyValue; else { propEditor.setValue(propertyValue); object = propEditor.getValue(); } Element childElement = getStandardObjectElement(document, object, propertyName); if (includeNullValues || !childElement .getAttribute( NamespaceConstants.NSPREFIX_SCHEMA_XSI + ":" + org.apache.axis.Constants.ATTR_TYPE) .equals("anyType")) { if (!includeTypeInfo) { childElement.removeAttribute( NamespaceConstants.NSPREFIX_SCHEMA_XSI + ":" + org.apache.axis.Constants.ATTR_TYPE); childElement.removeAttribute(NamespaceConstants.NSPREFIX_SCHEMA_XSI + ":null"); } try { Field field = bean.getClass().getDeclaredField(propertyName); if (field.isAnnotationPresent(ObjectXmlAsAttribute.class)) { String attrName = null; if (includeTypeInfo) attrName = nsPrefix + ":" + propertyName; else attrName = propertyName; element.setAttribute(attrName, childElement.getFirstChild().getNodeValue()); } else if (field.isAnnotationPresent(ObjectXmlAsValue.class)) element.setTextContent(childElement.getFirstChild().getNodeValue()); else element.appendChild(childElement); } catch (NoSuchFieldException nfse) { element.appendChild(childElement); } } } catch (Exception e) { } } else { // The object is not an XSD-encoded datatype, it must be a JavaBean try { String propertyTypeName = null; if (propertyValue != null) { // get object type Class propertyType = pd.getPropertyType(); propertyTypeName = propertyType.getName(); } getBeanElements(element, propertyName, propertyTypeName, propertyValue); } catch (Exception e) { } } } } } }
public Reference createReference(Object bean) throws NamingException { try { BeanInfo bi = Introspector.getBeanInfo(bean.getClass()); PropertyDescriptor[] pds = bi.getPropertyDescriptors(); List refAddrs = new ArrayList(); String factoryClassLocation = defaultFactoryClassLocation; boolean using_ref_props = referenceProperties.size() > 0; // we only include this so that on dereference we are not surprised to find some properties // missing if (using_ref_props) refAddrs.add( new BinaryRefAddr(REF_PROPS_KEY, SerializableUtils.toByteArray(referenceProperties))); for (int i = 0, len = pds.length; i < len; ++i) { PropertyDescriptor pd = pds[i]; String propertyName = pd.getName(); // System.err.println("Making Reference: " + propertyName); if (using_ref_props && !referenceProperties.contains(propertyName)) { // System.err.println("Not a ref_prop -- continuing."); continue; } Class propertyType = pd.getPropertyType(); Method getter = pd.getReadMethod(); Method setter = pd.getWriteMethod(); if (getter != null && setter != null) // only use properties that are both readable and writable { Object val = getter.invoke(bean, EMPTY_ARGS); // System.err.println( "val: " + val ); if (propertyName.equals("factoryClassLocation")) { if (String.class != propertyType) throw new NamingException( this.getClass().getName() + " requires a factoryClassLocation property to be a string, " + propertyType.getName() + " is not valid."); factoryClassLocation = (String) val; } if (val == null) { RefAddr addMe = new BinaryRefAddr(propertyName, NULL_TOKEN_BYTES); refAddrs.add(addMe); } else if (Coerce.canCoerce(propertyType)) { RefAddr addMe = new StringRefAddr(propertyName, String.valueOf(val)); refAddrs.add(addMe); } else // other Object properties { RefAddr addMe = null; PropertyEditor pe = BeansUtils.findPropertyEditor(pd); if (pe != null) { pe.setValue(val); String textValue = pe.getAsText(); if (textValue != null) addMe = new StringRefAddr(propertyName, textValue); } if (addMe == null) // property editor approach failed addMe = new BinaryRefAddr( propertyName, SerializableUtils.toByteArray( val, indirector, IndirectPolicy.INDIRECT_ON_EXCEPTION)); refAddrs.add(addMe); } } else { // System.err.println(this.getClass().getName() + // ": Skipping " + propertyName + " because it is " + (setter == null ? // "read-only." : "write-only.")); if (logger.isLoggable(MLevel.WARNING)) logger.warning( this.getClass().getName() + ": Skipping " + propertyName + " because it is " + (setter == null ? "read-only." : "write-only.")); } } Reference out = new Reference(bean.getClass().getName(), factoryClassName, factoryClassLocation); for (Iterator ii = refAddrs.iterator(); ii.hasNext(); ) out.add((RefAddr) ii.next()); return out; } catch (Exception e) { // e.printStackTrace(); if (Debug.DEBUG && logger.isLoggable(MLevel.FINE)) logger.log(MLevel.FINE, "Exception trying to create Reference.", e); throw new NamingException("Could not create reference from bean: " + e.toString()); } }