KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > util > Util


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.util;
5
6 import com.tc.aspectwerkz.reflect.ReflectionInfo;
7
8 /**
9  * Utility methods and constants used in the AspectWerkz system.
10  *
11  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
12  */

13 public final class Util {
14   public static final Integer JavaDoc INTEGER_DEFAULT_VALUE = new Integer JavaDoc(0);
15
16   public static final Float JavaDoc FLOAT_DEFAULT_VALUE = new Float JavaDoc(0.0f);
17
18   public static final Double JavaDoc DOUBLE_DEFAULT_VALUE = new Double JavaDoc(0.0d);
19
20   public static final Long JavaDoc LONG_DEFAULT_VALUE = new Long JavaDoc(0L);
21
22   public static final Boolean JavaDoc BOOLEAN_DEFAULT_VALUE = new Boolean JavaDoc(false);
23
24   public static final Character JavaDoc CHARACTER_DEFAULT_VALUE = new Character JavaDoc('\u0000');
25
26   public static final Short JavaDoc SHORT_DEFAULT_VALUE;
27
28   public static final Byte JavaDoc BYTE_DEFAULT_VALUE;
29
30   static {
31     byte b = 0;
32     BYTE_DEFAULT_VALUE = new Byte JavaDoc(b);
33     short s = 0;
34     SHORT_DEFAULT_VALUE = new Short JavaDoc(s);
35   }
36
37   /**
38    * Calculates the hash for the class name and the meta-data.
39    *
40    * @param className the class name
41    * @param info the meta-data
42    * @return the hash
43    */

44   public static Integer JavaDoc calculateHash(final String JavaDoc className, final ReflectionInfo info) {
45     if (className == null) {
46       throw new IllegalArgumentException JavaDoc("class name can not be null");
47     }
48     if (info == null) {
49       throw new IllegalArgumentException JavaDoc("info can not be null");
50     }
51     int hash = 17;
52     hash = (37 * hash) + className.hashCode();
53     hash = (37 * hash) + info.hashCode();
54     Integer JavaDoc hashKey = new Integer JavaDoc(hash);
55     return hashKey;
56   }
57
58   /**
59    * Removes the AspectWerkz specific elements from the stack trace. <p/>TODO: how to mess w/ the stacktrace in JDK
60    * 1.3.x?
61    *
62    * @param exception the Throwable to modify the stack trace on
63    * @param className the name of the fake origin class of the exception
64    */

65   public static void fakeStackTrace(final Throwable JavaDoc exception, final String JavaDoc className) {
66     if (exception == null) {
67       throw new IllegalArgumentException JavaDoc("exception can not be null");
68     }
69     if (className == null) {
70       throw new IllegalArgumentException JavaDoc("class name can not be null");
71     }
72
73     // final List newStackTraceList = new ArrayList();
74
// final StackTraceElement[] stackTrace = exception.getStackTrace();
75
// int i;
76
// for (i = 1; i < stackTrace.length; i++) {
77
// if (stackTrace[i].getClassName().equals(className)) break;
78
// }
79
// for (int j = i; j < stackTrace.length; j++) {
80
// newStackTraceList.add(stackTrace[j]);
81
// }
82
//
83
// final StackTraceElement[] newStackTrace =
84
// new StackTraceElement[newStackTraceList.size()];
85
// int k = 0;
86
// for (Iterator it = newStackTraceList.iterator(); it.hasNext(); k++) {
87
// final StackTraceElement element = (StackTraceElement)it.next();
88
// newStackTrace[k] = element;
89
// }
90
// exception.setStackTrace(newStackTrace);
91
}
92
93   /**
94    * Returns a String representation of a classloader Avoid to do a toString() if the resulting string is too long
95    * (occurs on Tomcat)
96    *
97    * @param loader
98    * @return String representation (toString or FQN@hashcode)
99    */

100   public static String JavaDoc classLoaderToString(ClassLoader JavaDoc loader) {
101     if ((loader != null) && (loader.toString().length() < 120)) {
102       return loader.toString() + "@" + loader.hashCode();
103     } else if (loader != null) {
104       return loader.getClass().getName() + "@" + loader.hashCode();
105     } else {
106       return "null";
107     }
108   }
109
110   /**
111    * Helper method to support Java 1.4 like Boolean.valueOf(boolean) in Java 1.3
112    *
113    * @param b
114    * @return
115    */

116   public static Boolean JavaDoc booleanValueOf(boolean b) {
117     return b ? Boolean.TRUE : Boolean.FALSE;
118   }
119 }
Popular Tags