cvs commit: spice/sandbox/metaclass/src/java/org/realityforge/metaclass/tools/packer ClassDescriptorPacker.java

Peter Donald <[email protected]> Tue, 18 Nov 2003 03:12:35 -0800
Newsgroups gmane.comp.java.spice.cvs
Message-ID <[email protected]>
donaldp     03/11/18 03:12:35

  Added:       sandbox/metaclass/src/java/org/realityforge/metaclass/tools/packer
                        ClassDescriptorPacker.java
  Log:
  Implement Compacting of ClassDescriptors prior to serialization.
  
  Revision  Changes    Path
  1.1                  spice/sandbox/metaclass/src/java/org/realityforge/metaclass/tools/packer/ClassDescriptorPacker.java
  
  Index: ClassDescriptorPacker.java
  ===================================================================
  /*
   * Copyright (C) The Spice Group. All rights reserved.
   *
   * This software is published under the terms of the Spice
   * Software License version 1.1, a copy of which has been included
   * with this distribution in the LICENSE.txt file.
   */
  package org.realityforge.metaclass.tools.packer;
  
  import java.util.ArrayList;
  import org.realityforge.metaclass.model.ClassDescriptor;
  import org.realityforge.metaclass.model.FeatureDescriptor;
  import org.realityforge.metaclass.model.FieldDescriptor;
  import org.realityforge.metaclass.model.MethodDescriptor;
  
  /**
   * Utility class to shrink ClassDescriptor by field and method
   * descriptors without any attributes. If keepEmptyMethods is
   * true the packer will retain any methods with no attributes.
   * This option is useful if you want to retain access to the names
   * of the parameters in methods and constructors.
   *
   * @author <a href="mailto:peter at realityforge.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2003/11/18 11:12:35 $
   */
  public class ClassDescriptorPacker
  {
      /**
       * Flag indicating whether the compacter
       * should methods with no attributes.
       */
      private final boolean m_keepEmptyMethods;
  
      /**
       * Create packer.
       *
       * @param keepEmptyMethods flag to keep empty methods
       */
      public ClassDescriptorPacker( final boolean keepEmptyMethods )
      {
          m_keepEmptyMethods = keepEmptyMethods;
      }
  
      public ClassDescriptor pack( final ClassDescriptor descriptor )
      {
          final FieldDescriptor[] fields =
              packFields( descriptor.getFields() );
          final MethodDescriptor[] methods =
              packMethods( descriptor.getMethods() );
          if( 0 == fields.length &&
              0 == methods.length &&
              isEmpty( descriptor ) )
          {
              return null;
          }
          else
          {
              return new ClassDescriptor( descriptor.getName(),
                                          descriptor.getDeclaredAttributes(),
                                          descriptor.getAttributes(),
                                          fields,
                                          methods );
          }
      }
  
      /**
       * Return the list of methods that have attributes.
       *
       * @param methods the complete set of methods
       * @return the list of methods that have attributes.
       */
      MethodDescriptor[] packMethods( final MethodDescriptor[] methods )
      {
          if( m_keepEmptyMethods )
          {
              return methods;
          }
          else
          {
              final ArrayList set = new ArrayList();
  
              for( int i = 0; i < methods.length; i++ )
              {
                  final MethodDescriptor method = methods[ i ];
                  if( !isEmpty( method ) )
                  {
                      set.add( method );
                  }
              }
  
              return (MethodDescriptor[])set.
                  toArray( new MethodDescriptor[ set.size() ] );
          }
      }
  
      /**
       * Return the list of fields that have attributes.
       *
       * @param fields the complete set of fields
       * @return the list of fields that have attributes.
       */
      FieldDescriptor[] packFields( final FieldDescriptor[] fields )
      {
          final ArrayList set = new ArrayList();
  
          for( int i = 0; i < fields.length; i++ )
          {
              final FieldDescriptor field = fields[ i ];
              if( !isEmpty( field ) )
              {
                  set.add( field );
              }
          }
  
          return (FieldDescriptor[])set.
              toArray( new FieldDescriptor[ set.size() ] );
      }
  
      /**
       * Return true if feature has no attributes.
       *
       * @param descriptor the descriptor
       * @return true if feature has no attributes
       */
      boolean isEmpty( final FeatureDescriptor descriptor )
      {
          return
              0 == descriptor.getAttributes().length &&
              0 == descriptor.getDeclaredAttributes().length;
      }
  }
  
  
  


-------------------------------------------------------
This SF. Net email is sponsored by: GoToMyPC
GoToMyPC is the fast, easy and secure way to access your computer from
any Web browser or wireless device. Click here to Try it Free!
https://www.gotomypc.com/tr/OSDN/AW/Q4_2003/t/g22lp?Target=mm/g22lp.tmpl