You are viewing a plain text version of this content. The canonical link for it is here.
Posted to soap-dev@xml.apache.org by Ed Keen <ed...@interactiveportal.com> on 2001/03/19 18:54:52 UTC

[patch] BeanSerializer incorporating excluded methods

I have made a modification to the BeanSerializer class that allows me to
explicitly specify methods to exclude from the serialization process.  It is
very straightforward.  I considered using the BeanInfo class (as Matthew
Duftler suggested), but I have a couple of problems with  it:  

1.  We are not using JavaBeans for anything in our project.  Adding the
BeanInfo classes would be solely for use in our soap infrastructure, which
seems to be somewhat of an overkill.

2.  BeanInfo makes you explicitly list all of the *included* methods to be
exposed.  I would prefer to have a way to enumerate only the *excluded*
methods.  This way, I don't have to update my BeanInfo class every time I
add a simple getter/setter to my class.

I have made some very slight modifications to BeanSerializer.java and have
something very workable:  

1.  I wrote an interface called "SoapSerializable."  Here is the code for
it:
----------------
package org.apache.soap.encoding.soapenc;

public interface SoapSerializable
{
	public String excludedSoapMethods();

}
----------------

2.  I made the following change to the "BeanSerializer.marshall" method
----------------
	....
	PropertyDescriptor[] properties = getPropertyDescriptors(javaType);

	String excludedMethods = "";
	if (src instanceof SoapSerializable)  
		excludedMethods =
((SoapSerializable)src).excludedSoapMethods();

	for (int i = 0; i < properties.length; i++)
	{
		String propName = properties[i].getName();
		Class propType = properties[i].getPropertyType();

		// Serialize every property except the "class" property.
		if (!propType.equals(Class.class))
      	{
			Method propReadMethod =
properties[i].getReadMethod();

			// Only serialize readable properties.
			boolean excludeMethod = (propReadMethod == null || 
	
excludedMethods.indexOf(propReadMethod.getName()) != -1);

			
			if (!excludeMethod)
			{
				Object propValue = null;
				...

----------------

The net effect of making these simple changes is that I can now have my bean
class (eg, "Account.java") implement SoapSerializable.  Then, all I have to
do is implement the "excludedSoapMethods" method from that interface, and
bingo, those methods will not be serialized.  Here is an example of what I
am talking about:

class declaration:
public class Account implements Serializable, Cloneable, SoapSerializable

implementation of interface:
   public String excludedSoapMethods()  {

        String excludedMethods = "getHTMLProperties getAttributeMap
getPKAttribute ";
        excludedMethods += "getDataClass getParameterNames
getCreditCardNumber";

        return excludedMethods;

    }

---------------

This implementation works a lot more smoothly for us, and it avoids us
having to implement the BeanInfo class.  I would like to see this patch (or
a derivative of it) incorporated into the BeanSerializer class.  We can
always make our own serializer class, but I feel that this would benefit
many other people besides just us.

Thanks,
Ed