KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > bytecode > JClassDependency


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.bytecode;
31
32 import com.caucho.util.Base64;
33 import com.caucho.util.CharBuffer;
34 import com.caucho.util.Log;
35 import com.caucho.vfs.PersistentDependency;
36
37 import java.security.MessageDigest JavaDoc;
38 import java.util.Comparator JavaDoc;
39 import java.util.logging.Level JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41
42 /**
43  * Representing a class that might change.
44  */

45 public class JClassDependency implements PersistentDependency {
46   private final static Logger JavaDoc log = Log.open(JClassDependency.class);
47   
48   private String JavaDoc _className;
49
50   private boolean _checkFields = true;
51   private boolean _checkStatic = true;
52   private boolean _checkProtected = true;
53   private boolean _checkPrivate = true;
54
55   private boolean _isDigestModified;
56
57   /**
58    * Creates the class dependency.
59    */

60   public JClassDependency(JClass cl)
61   {
62     _className = cl.getName();
63   }
64
65   /**
66    * Create a new dependency with a given digest.
67    *
68    * @param cl the source class
69    * @param digest the MD5 digest
70    */

71   public JClassDependency(JClass cl, String JavaDoc digest)
72   {
73     _className = cl.getName();
74
75     String JavaDoc newDigest = getDigest();
76
77     if (! newDigest.equals(digest)) {
78       if (log.isLoggable(Level.FINE))
79         log.fine(_className + " digest is modified.");
80
81       _isDigestModified = true;
82     }
83   }
84
85   /**
86    * Create a new dependency with a given digest.
87    *
88    * @param cl the source class
89    * @param digest the MD5 digest
90    */

91   public JClassDependency(String JavaDoc className, String JavaDoc digest)
92   {
93   }
94   
95   /**
96    * Returns true if the underlying resource has changed.
97    */

98   public boolean isModified()
99   {
100     return _isDigestModified;
101   }
102
103   /**
104    * Calculates a MD5 digest of the class.
105    */

106   public String JavaDoc getDigest()
107   {
108     return "";
109     /*
110     try {
111       if (_cl == null)
112         return "";
113
114       MessageDigest digest = MessageDigest.getInstance("MD5");
115
116       addDigest(digest, _cl.getName());
117
118       addDigest(digest, _cl.getModifiers());
119
120       Class cl = _cl.getSuperclass();
121       if (cl != null)
122         addDigest(digest, cl.getName());
123
124       Class []interfaces = _cl.getInterfaces();
125       for (int i = 0; i < interfaces.length; i++)
126         addDigest(digest, interfaces[i].getName());
127
128       Field []fields = _cl.getFields();
129
130       Arrays.sort(fields, new FieldComparator());
131
132       if (_checkFields) {
133         for (int i = 0; i < fields.length; i++) {
134           if (Modifier.isPrivate(fields[i].getModifiers()) &&
135               ! _checkPrivate)
136             continue;
137           if (Modifier.isProtected(fields[i].getModifiers()) &&
138               ! _checkProtected)
139             continue;
140           
141           addDigest(digest, fields[i].getName());
142           addDigest(digest, fields[i].getModifiers());
143           addDigest(digest, fields[i].getType().getName());
144         }
145       }
146
147       Method []methods = _cl.getMethods();
148       Arrays.sort(methods, new MethodComparator());
149       
150       for (int i = 0; i < methods.length; i++) {
151         Method method = methods[i];
152
153         if (Modifier.isPrivate(method.getModifiers()) && ! _checkPrivate)
154           continue;
155         if (Modifier.isProtected(method.getModifiers()) && ! _checkProtected)
156           continue;
157         if (Modifier.isStatic(method.getModifiers()) && ! _checkStatic)
158           continue;
159           
160         addDigest(digest, method.getName());
161         addDigest(digest, method.getModifiers());
162         addDigest(digest, method.getName());
163
164         Class []param = method.getParameterTypes();
165         for (int j = 0; j < param.length; j++)
166           addDigest(digest, param[j].getName());
167
168         addDigest(digest, method.getReturnType().getName());
169
170         Class []exn = method.getExceptionTypes();
171         for (int j = 0; j < exn.length; j++)
172           addDigest(digest, exn[j].getName());
173       }
174       
175       byte []digestBytes = new byte[256];
176       
177       int len = digest.digest(digestBytes, 0, digestBytes.length);
178       
179       return digestToBase64(digestBytes, len);
180     } catch (Exception e) {
181       log.log(Level.FINER, e.toString(), e);
182
183       return "";
184     }
185     */

186   }
187
188   /**
189    * Returns a string which will recreate the dependency.
190    */

191   public String JavaDoc getJavaCreateString()
192   {
193     return ("new com.caucho.bytecode.JClassDependency(\"" +
194             _className + "\", \"" + getDigest() + "\")");
195   }
196   
197   /**
198    * Adds the int to the digest.
199    */

200   private static void addDigest(MessageDigest JavaDoc digest, int v)
201   {
202     digest.update((byte) (v >> 24));
203     digest.update((byte) (v >> 16));
204     digest.update((byte) (v >> 8));
205     digest.update((byte) v);
206   }
207   
208   /**
209    * Adds the string to the digest using a UTF8 encoding.
210    */

211   private static void addDigest(MessageDigest JavaDoc digest, String JavaDoc string)
212   {
213     if (string == null)
214       return;
215     
216     int len = string.length();
217     for (int i = 0; i < len; i++) {
218       int ch = string.charAt(i);
219       if (ch < 0x80)
220         digest.update((byte) ch);
221       else if (ch < 0x800) {
222         digest.update((byte) (0xc0 + (ch >> 6)));
223         digest.update((byte) (0x80 + (ch & 0x3f)));
224       }
225       else {
226         digest.update((byte) (0xe0 + (ch >> 12)));
227         digest.update((byte) (0x80 + ((ch >> 6) & 0x3f)));
228         digest.update((byte) (0x80 + (ch & 0x3f)));
229       }
230     }
231   }
232   
233   private String JavaDoc digestToBase64(byte []digest, int len)
234   {
235     CharBuffer cb = CharBuffer.allocate();
236
237     Base64.encode(cb, digest, 0, len);
238
239     return cb.close();
240   }
241
242   public boolean isEqual(Object JavaDoc o)
243   {
244     if (o == this)
245       return true;
246     
247     if (! (o instanceof JClassDependency))
248       return false;
249
250     JClassDependency depend = (JClassDependency) o;
251
252     return _className.equals(depend._className);
253   }
254
255   static class FieldComparator implements Comparator JavaDoc<JField> {
256     public int compare(JField a, JField b)
257     {
258       if (a == b)
259     return 0;
260       else if (a == null)
261     return -1;
262       else if (b == null)
263     return 1;
264       else if (a.equals(b))
265     return 0;
266
267       int cmp = a.getName().compareTo(b.getName());
268       if (cmp != 0)
269     return cmp;
270       
271       cmp = a.getDeclaringClass().getName().compareTo(b.getDeclaringClass().getName());
272       if (cmp != 0)
273     return cmp;
274       
275       return a.getType().getName().compareTo(b.getType().getName());
276     }
277   }
278
279   static class MethodComparator implements Comparator JavaDoc<JMethod> {
280     public int compare(JMethod a, JMethod b)
281     {
282       if (a == b)
283     return 0;
284       else if (a == null)
285     return -1;
286       else if (b == null)
287     return 1;
288       else if (a.equals(b))
289     return 0;
290
291       int cmp = a.getName().compareTo(b.getName());
292       if (cmp != 0)
293     return cmp;
294
295       JClass []paramA = a.getParameterTypes();
296       JClass []paramB = b.getParameterTypes();
297
298       if (paramA.length < paramB.length)
299     return -1;
300       else if (paramB.length < paramA.length)
301     return 1;
302
303       for (int i = 0; i < paramA.length; i++) {
304     cmp = paramA[i].getName().compareTo(paramB[i].getName());
305     if (cmp != 0)
306       return cmp;
307       }
308       
309       cmp = a.getDeclaringClass().getName().compareTo(b.getDeclaringClass().getName());
310       if (cmp != 0)
311     return cmp;
312       
313       return a.getReturnType().getName().compareTo(b.getReturnType().getName());
314     }
315   }
316 }
317
Popular Tags