KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > datastore > ClassRecord


1 /*
2  * @(#)ClassRecord.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.codecoverage.v2.datastore;
28
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.LinkedList JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule;
36 import net.sourceforge.groboutils.codecoverage.v2.util.ClassSignatureUtil;
37
38 /**
39  * Contains data associated with a parsed class. Each class is associated
40  * with a collection of analysis modules' marks.
41  *
42  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
43  * @version $Date: 2004/04/15 05:48:26 $
44  * @since December 15, 2002
45  */

46 public class ClassRecord
47 {
48     private String JavaDoc className;
49     private long classCRC;
50     private String JavaDoc sourceFileName;
51     private Map JavaDoc methodToIndex;
52     private String JavaDoc[] methodSignatures;
53     private AnalysisModuleSet amSet;
54     private List JavaDoc marks[]; // each index is for the analysis module
55

56     public ClassRecord( String JavaDoc className, long classCRC, String JavaDoc sourceFileName,
57             String JavaDoc[] methSigs, AnalysisModuleSet ams )
58     {
59         // we can have an empty set of methods.
60
if (className == null || className.length() <= 0 ||
61             methSigs == null || ams == null || sourceFileName == null)
62         {
63             throw new IllegalArgumentException JavaDoc( "No null args." );
64         }
65         
66         this.className = className;
67         this.classCRC = classCRC;
68         this.sourceFileName = sourceFileName;
69         int len = methSigs.length;
70         if (len > Short.MAX_VALUE)
71         {
72             throw new IllegalStateException JavaDoc(
73                 "Too many methods. There is a maximum internal count of "+
74                 Short.MAX_VALUE+"." );
75         }
76         this.methodSignatures = new String JavaDoc[ len ];
77         this.methodToIndex = new HashMap JavaDoc();
78         for (int i = 0; i < len; ++i)
79         {
80             if (methSigs[i] == null)
81             {
82                 throw new IllegalArgumentException JavaDoc( "No null args." );
83             }
84             
85             this.methodSignatures[i] = methSigs[i];
86             this.methodToIndex.put( methSigs[i], new Short JavaDoc( (short)i ) );
87         }
88         
89         // make a copy of the set, so it isn't changed underneath us.
90
this.amSet = new AnalysisModuleSet( ams );
91         len = this.amSet.getAnalysisModuleCount();
92         this.marks = new List JavaDoc[ len ];
93         for (int i = 0; i < len; ++i)
94         {
95             this.marks[i] = new LinkedList JavaDoc();
96         }
97     }
98     
99     
100     /**
101      * Gets the name of the corresponding class for this record.
102      *
103      * @return the class name.
104      */

105     public String JavaDoc getClassName()
106     {
107         return this.className;
108     }
109     
110     
111     /**
112      * Returns the cyclic redundancy check (CRC) for the class in this record.
113      *
114      * @return the class CRC.
115      */

116     public long getClassCRC()
117     {
118         return this.classCRC;
119     }
120     
121     
122     /**
123      * Returns the implementation-specific class signature for the class this
124      * record refers to. It should be a String which is unique for this class,
125      * even if multiple class files have the same class name (note that if the
126      * class files are identical, it makes perfect sense to return identical
127      * signatures).
128      *
129      * @return the unique signature for the class in this record.
130      */

131     public String JavaDoc getClassSignature()
132     {
133         return ClassSignatureUtil.getInstance().
134             createClassSignature( getClassName(), getClassCRC() );
135     }
136     
137     
138     /**
139      * Returns the name of the Java source file.
140      *
141      * @return the source file name
142      */

143     public String JavaDoc getSourceFileName()
144     {
145         return this.sourceFileName;
146     }
147     
148     
149     /**
150      * Returns a copy of the internal analysis module set.
151      *
152      * @return a copy of the set of analysis modules.
153      */

154     public AnalysisModuleSet getAnalysisModuleSet()
155     {
156         return new AnalysisModuleSet( this.amSet );
157     }
158     
159     
160     /**
161      * Returns a list of known method signatures for this class.
162      *
163      * @return all method signatures known for this class, in the correct
164      * sorted order.
165      */

166     public String JavaDoc[] getMethods()
167     {
168         int len = this.methodSignatures.length;
169         String JavaDoc s[] = new String JavaDoc[ len ];
170         System.arraycopy( this.methodSignatures, 0, s, 0, len );
171         return s;
172     }
173     
174     
175     /**
176      * Returns the index (as a short) for the given method signature. If the
177      * signature is not registered, then <tt>-1</tt> will be returned.
178      *
179      * @param methodSignature the signature to find the corresponding index
180      * for in this class.
181      * @return the index for <tt>methodSignature</tt> if it is in this method,
182      * or <tt>-1</tt> if it is not in the list.
183      * @exception IllegalArgumentException if <tt>methodSignature</tt> is
184      * <tt>null</tt>.
185      */

186     public short getMethodIndex( String JavaDoc methodSignature )
187     {
188         if (methodSignature == null)
189         {
190             throw new IllegalArgumentException JavaDoc( "No null args." );
191         }
192         Short JavaDoc i = (Short JavaDoc)this.methodToIndex.get( methodSignature );
193         if (i == null)
194         {
195             return -1;
196         }
197         return i.shortValue();
198     }
199     
200     
201     /**
202      * Returns the total number of method signatures for this class.
203      *
204      * @return the method signature count.
205      */

206     public int getMethodCount()
207     {
208         return this.methodSignatures.length;
209     }
210     
211     
212     /**
213      * Returns the method signature at the given index.
214      *
215      * @param index the index of the method signature to find.
216      * @return the method signature at index <tt>index</tt>.
217      * @exception IllegalArgumentException if <tt>index</tt> is not within the
218      * bounds of [0 .. <tt>getMethodCount()</tt>-1 ].
219      */

220     public String JavaDoc getMethodAt( short index )
221     {
222         int iindex = (int)index;
223         if (iindex < 0 || iindex >= this.methodSignatures.length)
224         {
225             throw new IllegalArgumentException JavaDoc( "Index out of bounds [0.."+
226                 this.methodSignatures.length+")" );
227         }
228         return this.methodSignatures[ iindex ];
229     }
230     
231     
232     /**
233      * Adds a mark record, and ensures that it is unique upon insertion.
234      *
235      * @param mr the new mark to add. Adding this mark will complete its
236      * internal data structure.
237      */

238     public void addMark( MarkRecord mr )
239     {
240         if (mr == null)
241         {
242             throw new IllegalArgumentException JavaDoc( "No null args." );
243         }
244         mr.processMark( this, getAnalysisModuleSet() );
245         int moduleIndex = (int)mr.getAnalysisModuleIndex();
246         
247         Iterator JavaDoc iter = this.marks[ moduleIndex ].iterator();
248         boolean add = true;
249         while (iter.hasNext())
250         {
251             MarkRecord listRecord = (MarkRecord)iter.next();
252             if (listRecord.equals( mr ))
253             {
254                 add = false;
255                 break;
256             }
257         }
258         if (add)
259         {
260             this.marks[ moduleIndex ].add( mr );
261         }
262     }
263     
264     
265     public MarkRecord[] getMarksForAnalysisModule( String JavaDoc measureName )
266     {
267         int moduleIndex = (int)this.amSet.getMeasureIndex( measureName );
268         if (moduleIndex < 0 || moduleIndex >= this.marks.length)
269         {
270             throw new IllegalArgumentException JavaDoc(
271                 "Unknown analysis module '"+measureName+
272                 "' (index = "+moduleIndex+")" );
273         }
274         
275         List JavaDoc list = this.marks[ moduleIndex ];
276         MarkRecord mr[] = (MarkRecord[])list.toArray(
277             new MarkRecord[ list.size() ] );
278         return mr;
279     }
280     
281     
282     public MarkRecord[] getMarksForAnalysisModule( IAnalysisModule am )
283     {
284         return getMarksForAnalysisModule( am.getMeasureName() );
285     }
286 }
287
288
Popular Tags