Clover coverage report - MetaClass - 1.1
Coverage timestamp: Tue Apr 27 2004 10:46:24 EST
file stats: LOC: 132   Methods: 5
NCLOC: 76   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
ClassDescriptorPacker.java 100% 100% 100% 100%
coverage
 1   
 /*
 2   
  * Copyright (C) The MetaClass Group. All rights reserved.
 3   
  *
 4   
  * This software is published under the terms of the Spice
 5   
  * Software License version 1.1, a copy of which has been included
 6   
  * with this distribution in the LICENSE.txt file.
 7   
  */
 8   
 package org.codehaus.metaclass.tools.packer;
 9   
 
 10   
 import java.util.ArrayList;
 11   
 import org.codehaus.metaclass.model.ClassDescriptor;
 12   
 import org.codehaus.metaclass.model.FeatureDescriptor;
 13   
 import org.codehaus.metaclass.model.FieldDescriptor;
 14   
 import org.codehaus.metaclass.model.MethodDescriptor;
 15   
 
 16   
 /**
 17   
  * Utility class to shrink ClassDescriptor by field and method
 18   
  * descriptors without any attributes. If keepEmptyMethods is
 19   
  * true the packer will retain any methods with no attributes.
 20   
  * This option is useful if you want to retain access to the names
 21   
  * of the parameters in methods and constructors.
 22   
  *
 23   
  * @author Peter Donald
 24   
  * @version $Revision: 1.2 $ $Date: 2003/11/27 08:09:54 $
 25   
  */
 26   
 public class ClassDescriptorPacker
 27   
 {
 28   
     /**
 29   
      * Flag indicating whether the compacter
 30   
      * should methods with no attributes.
 31   
      */
 32   
     private final boolean m_keepEmptyMethods;
 33   
 
 34   
     /**
 35   
      * Create packer.
 36   
      *
 37   
      * @param keepEmptyMethods flag to keep empty methods
 38   
      */
 39  52
     public ClassDescriptorPacker( final boolean keepEmptyMethods )
 40   
     {
 41  52
         m_keepEmptyMethods = keepEmptyMethods;
 42   
     }
 43   
 
 44  30
     public ClassDescriptor pack( final ClassDescriptor descriptor )
 45   
     {
 46  30
         final FieldDescriptor[] fields =
 47   
             packFields( descriptor.getFields() );
 48  30
         final MethodDescriptor[] methods =
 49   
             packMethods( descriptor.getMethods() );
 50  30
         if( 0 == fields.length &&
 51   
             0 == methods.length &&
 52   
             isEmpty( descriptor ) )
 53   
         {
 54  4
             return null;
 55   
         }
 56   
         else
 57   
         {
 58  26
             return new ClassDescriptor( descriptor.getName(),
 59   
                                         descriptor.getDeclaredAttributes(),
 60   
                                         descriptor.getAttributes(),
 61   
                                         fields,
 62   
                                         methods );
 63   
         }
 64   
     }
 65   
 
 66   
     /**
 67   
      * Return the list of methods that have attributes.
 68   
      *
 69   
      * @param methods the complete set of methods
 70   
      * @return the list of methods that have attributes.
 71   
      */
 72  34
     MethodDescriptor[] packMethods( final MethodDescriptor[] methods )
 73   
     {
 74  34
         if( m_keepEmptyMethods )
 75   
         {
 76  6
             return methods;
 77   
         }
 78   
         else
 79   
         {
 80  28
             final ArrayList set = new ArrayList();
 81   
 
 82  28
             for( int i = 0; i < methods.length; i++ )
 83   
             {
 84  4
                 final MethodDescriptor method = methods[ i ];
 85  4
                 if( !isEmpty( method ) )
 86   
                 {
 87  2
                     set.add( method );
 88   
                 }
 89   
             }
 90   
 
 91  28
             return (MethodDescriptor[])set.
 92   
                 toArray( new MethodDescriptor[ set.size() ] );
 93   
         }
 94   
     }
 95   
 
 96   
     /**
 97   
      * Return the list of fields that have attributes.
 98   
      *
 99   
      * @param fields the complete set of fields
 100   
      * @return the list of fields that have attributes.
 101   
      */
 102  32
     FieldDescriptor[] packFields( final FieldDescriptor[] fields )
 103   
     {
 104  32
         final ArrayList set = new ArrayList();
 105   
 
 106  32
         for( int i = 0; i < fields.length; i++ )
 107   
         {
 108  4
             final FieldDescriptor field = fields[ i ];
 109  4
             if( !isEmpty( field ) )
 110   
             {
 111  2
                 set.add( field );
 112   
             }
 113   
         }
 114   
 
 115  32
         return (FieldDescriptor[])set.
 116   
             toArray( new FieldDescriptor[ set.size() ] );
 117   
     }
 118   
 
 119   
     /**
 120   
      * Return true if feature has no attributes.
 121   
      *
 122   
      * @param descriptor the descriptor
 123   
      * @return true if feature has no attributes
 124   
      */
 125  44
     boolean isEmpty( final FeatureDescriptor descriptor )
 126   
     {
 127  44
         return
 128   
             0 == descriptor.getAttributes().length &&
 129   
             0 == descriptor.getDeclaredAttributes().length;
 130   
     }
 131   
 }
 132