1 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 ; 38 import java.util.Comparator ; 39 import java.util.logging.Level ; 40 import java.util.logging.Logger ; 41 42 45 public class JClassDependency implements PersistentDependency { 46 private final static Logger log = Log.open(JClassDependency.class); 47 48 private String _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 60 public JClassDependency(JClass cl) 61 { 62 _className = cl.getName(); 63 } 64 65 71 public JClassDependency(JClass cl, String digest) 72 { 73 _className = cl.getName(); 74 75 String 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 91 public JClassDependency(String className, String digest) 92 { 93 } 94 95 98 public boolean isModified() 99 { 100 return _isDigestModified; 101 } 102 103 106 public String getDigest() 107 { 108 return ""; 109 186 } 187 188 191 public String getJavaCreateString() 192 { 193 return ("new com.caucho.bytecode.JClassDependency(\"" + 194 _className + "\", \"" + getDigest() + "\")"); 195 } 196 197 200 private static void addDigest(MessageDigest 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 211 private static void addDigest(MessageDigest digest, String 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 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 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 <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 <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 |