1
2
3
4
5
6
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 public ClassDescriptorPacker( final boolean keepEmptyMethods )
40 {
41 m_keepEmptyMethods = keepEmptyMethods;
42 }
43
44 public ClassDescriptor pack( final ClassDescriptor descriptor )
45 {
46 final FieldDescriptor[] fields =
47 packFields( descriptor.getFields() );
48 final MethodDescriptor[] methods =
49 packMethods( descriptor.getMethods() );
50 if( 0 == fields.length &&
51 0 == methods.length &&
52 isEmpty( descriptor ) )
53 {
54 return null;
55 }
56 else
57 {
58 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 MethodDescriptor[] packMethods( final MethodDescriptor[] methods )
73 {
74 if( m_keepEmptyMethods )
75 {
76 return methods;
77 }
78 else
79 {
80 final ArrayList set = new ArrayList();
81
82 for( int i = 0; i < methods.length; i++ )
83 {
84 final MethodDescriptor method = methods[ i ];
85 if( !isEmpty( method ) )
86 {
87 set.add( method );
88 }
89 }
90
91 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 FieldDescriptor[] packFields( final FieldDescriptor[] fields )
103 {
104 final ArrayList set = new ArrayList();
105
106 for( int i = 0; i < fields.length; i++ )
107 {
108 final FieldDescriptor field = fields[ i ];
109 if( !isEmpty( field ) )
110 {
111 set.add( field );
112 }
113 }
114
115 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 boolean isEmpty( final FeatureDescriptor descriptor )
126 {
127 return
128 0 == descriptor.getAttributes().length &&
129 0 == descriptor.getDeclaredAttributes().length;
130 }
131 }