KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > util > ChecksumUtil


1 /*
2  * @(#)ChecksumUtil.java
3  *
4  * Copyright (C) 2002-2004 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.util;
28
29 import java.util.Comparator JavaDoc;
30 import java.util.zip.CRC32 JavaDoc;
31 import java.util.zip.Checksum JavaDoc;
32
33
34 /**
35  * A "universal" method to extract a checksum from the various kinds of
36  * data the codecoverage package needs.
37  * <P>
38  * This is made such that the equivalent types between the BCEL library and
39  * JDI library return the same CRC.
40  *
41  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
42  * @version $Date: 2004/04/15 05:48:26 $
43  * @since Feb 3, 2003
44  */

45 public class ChecksumUtil
46 {
47     protected static ChecksumUtil s_instance = new ChecksumUtil();
48     
49     // ensure that we ALWAYS get the same collator, no matter the
50
// executing environment.
51
protected final Comparator JavaDoc StringCollator =
52         java.text.Collator.getInstance(new java.util.Locale JavaDoc("en", "US", ""));
53     
54     /**
55      * Creates a new finder using the default (system) classpath.
56      */

57     protected ChecksumUtil()
58     {
59         // do nothing
60
}
61     
62     
63     public static ChecksumUtil getInstance()
64     {
65         return s_instance;
66     }
67     
68     
69     
70     public long checksum( byte[] buff )
71     {
72         Checksum JavaDoc crc = createChecksum();
73         update( crc, buff );
74         return crc.getValue();
75     }
76     
77     
78     /*
79     public long checksum( JavaClass jc )
80     {
81         Checksum crc = createChecksum();
82         update( crc, jc );
83         return crc.getValue();
84     }
85     */

86     
87     
88     /*
89     public long checksum( ReferenceType rt )
90     {
91         Checksum crc = createChecksum();
92         update( crc, rt );
93         return crc.getValue();
94     }
95     */

96     
97     
98     /*
99     public long checksum( org.apache.bcel.classfile.Method m )
100     {
101         Checksum crc = createChecksum();
102         update( crc, m );
103         return crc.getValue();
104     }
105     */

106     
107     
108     /*
109     public long checksum( Code c )
110     {
111         Checksum crc = createChecksum();
112         update( crc, c );
113         return crc.getValue();
114     }
115     */

116     
117     
118     /*
119     public long checksum( com.sun.jdi.Method m )
120     {
121         Checksum crc = createChecksum();
122         update( crc, m );
123         return crc.getValue();
124     }
125     */

126     
127     
128     
129     //------------------------------------------------------------------------
130
// Factory methods
131

132     protected Checksum JavaDoc createChecksum()
133     {
134         return new CRC32 JavaDoc();
135     }
136     
137     
138     protected void update( Checksum JavaDoc crc, byte[] buff )
139     {
140         if (buff != null)
141         {
142             crc.update( buff, 0, buff.length );
143         }
144     }
145     
146     
147     /*
148     protected void update( Checksum crc, JavaClass jc )
149     {
150         // If JDI allowed for "bytecodes()" on the class level, as
151         // opposed to the Method level, then we could just do this:
152         
153         // update( crc, jc.getBytes() );
154         
155         // However, JDI only allows bytecodes() on the method level,
156         // so we must parse the checksum on a per-method basis.
157         
158         // Note that to ensure that the correct CRC is generated, we
159         // need to ensure that the ORDER of methods is identical across
160         // all runs and all types!
161         
162         org.apache.bcel.classfile.Method ms[] = jc.getMethods();
163         ArrayList list = new ArrayList();
164         for (int i = 0; i < ms.length; ++i)
165         {
166             list.add( ms[i] );
167         }
168         Iterator iter = sortList( list.iterator(), new Comparator()
169             {
170                 public int compare( Object o1, Object o2 )
171                 {
172                     org.apache.bcel.classfile.Method m1 =
173                         (org.apache.bcel.classfile.Method)o1;
174                     org.apache.bcel.classfile.Method m2 =
175                         (org.apache.bcel.classfile.Method)o2;
176                     
177                     return StringCollator.compare( m1.getSignature(),
178                         m2.getSignature() );
179                 }
180                 public boolean equals( Object o1 )
181                 {
182                     if (o1 == null) return false;
183                     if (o1 == this) return true;
184                     return (o1.getClass().equals( this.getClass() ));
185                 }
186             } );
187         while (iter.hasNext())
188         {
189             update( crc, (org.apache.bcel.classfile.Method)iter.next() );
190         }
191     }
192     */

193     
194     /*
195     protected void update( Checksum crc, ReferenceType rt )
196     {
197         // JDI does not have a "bytecodes()" method on ReferenceType,
198         // but does on methods.
199         
200         // Note that to ensure that the correct CRC is generated, we
201         // need to ensure that the ORDER of methods is identical across
202         // all runs and all types!
203
204         Iterator iter = sortList( rt.methods().iterator(), new Comparator()
205             {
206                 public int compare( Object o1, Object o2 )
207                 {
208                     com.sun.jdi.Method m1 =
209                         (com.sun.jdi.Method)o1;
210                     com.sun.jdi.Method m2 =
211                         (com.sun.jdi.Method)o2;
212                     
213                     return StringCollator.compare( m1.signature(),
214                         m2.signature() );
215                 }
216                 public boolean equals( Object o1 )
217                 {
218                     if (o1 == null) return false;
219                     if (o1 == this) return true;
220                     return (o1.getClass().equals( this.getClass() ));
221                 }
222             } );
223         while (iter.hasNext())
224         {
225             update( crc, (com.sun.jdi.Method)iter.next() );
226         }
227     }
228     */

229     
230     /*
231     protected void update( Checksum crc, org.apache.bcel.classfile.Method m )
232     {
233         update( crc, m.getCode() );
234     }
235     */

236     
237     /*
238     protected void update( Checksum crc, Code c )
239     {
240         // abstract methods don't have code.
241         if (c != null)
242         {
243             update( crc, c.getCode() );
244         }
245     }
246     */

247     
248     
249     /*
250     protected void update( Checksum crc, com.sun.jdi.Method m )
251     {
252         // abstract methods may not have any bytecodes.
253         // thus, this called method needs to be null-proof.
254         update( crc, m.bytecodes() );
255     }
256     */

257     
258     
259     /*
260      * Sorts the given iteration with the given comparator.
261      * Made generic enough to allow an iterator. Should allow a
262      * Collection to simplify this method, but it's simple enough.
263     protected Iterator sortList( Iterator list, Comparator c )
264     {
265         TreeSet ts = new TreeSet( c );
266         while (list.hasNext())
267         {
268             ts.add( list.next() );
269         }
270         return ts.iterator();
271     }
272      */

273 }
274
Popular Tags