title: XStream - using a property setter instead of direct field access
body: <p>I'm trying to load an object from XML using XStream.
  By default XStream seems to use direct field access. Thus, when you map a field named 'rolesAsString' to an alias named 'roles', it will take the value from the 'roles' tag and try to set it directly to your 'rolesAsString' field in your class. This works in most cases, but not in my case.</p>
  
  <p>Here is the XML relevant for the question:
  <code><pre>&lt;principal name="admin" roles="role1, role2, role3"&gt;</pre></code></p>
  
  <p>Here is the class:
  <code><pre>
  public class Principal {
      private Set&lt;String&gt; roles = new HashSet&lt;String&gt;();
      public Set&lt;String&gt; getRoles() {
          return roles;
      }</p>
  
  <pre><code>public void setRolesAsString(String roles) {
      for (String role : roles.split(",")) {
          role = role.trim();
          if (!"".equals(role)) { //ignore empty string roles
              this.roles.add(role);
          }
      }
  }
  
  public String getRolesAsString() {
      StringBuilder result = new StringBuilder();
      boolean first = true;
      for (String role : roles) {
          if (first) {
              first = false;
          } else {
              result.append(", ");
          }
          result.append(role);
      }
      return result.toString();
  }
  </code></pre>
  
  <p></pre></code></p>
  
  <p>I am trying to make XStream call setRolesAsString when loading the configuration from the xml file, instead of trying to connect directly to the roles field.</p>
  
  <p>I have tried doing that with JavaBeanConverter converter, but I can't make it work. Code I'm using for that is:
  <code><pre>
  configParser.registerLocalConverter(Principal.class, "roles", new JavaBeanConverter(configParser.getMapper()) {
      @Override
      public boolean canConvert(@SuppressWarnings("rawtypes") Class clazz) {
          return true;//should make XStream to always use this converter for that field.
      }
  });</p>
  
  <p>configParser.aliasAttribute(Resource.class, "roles", "roles");
  </pre></code></p>
  
  <p>Any idea where I'm going wrong on this?</p>
  
  <p>Thanks,
  Stef.</p>