001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.aspectwerkz.util;
005:
006: import com.tc.aspectwerkz.reflect.ReflectionInfo;
007:
008: /**
009: * Utility methods and constants used in the AspectWerkz system.
010: *
011: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
012: */
013: public final class Util {
014: public static final Integer INTEGER_DEFAULT_VALUE = new Integer(0);
015:
016: public static final Float FLOAT_DEFAULT_VALUE = new Float(0.0f);
017:
018: public static final Double DOUBLE_DEFAULT_VALUE = new Double(0.0d);
019:
020: public static final Long LONG_DEFAULT_VALUE = new Long(0L);
021:
022: public static final Boolean BOOLEAN_DEFAULT_VALUE = new Boolean(
023: false);
024:
025: public static final Character CHARACTER_DEFAULT_VALUE = new Character(
026: '\u0000');
027:
028: public static final Short SHORT_DEFAULT_VALUE;
029:
030: public static final Byte BYTE_DEFAULT_VALUE;
031:
032: static {
033: byte b = 0;
034: BYTE_DEFAULT_VALUE = new Byte(b);
035: short s = 0;
036: SHORT_DEFAULT_VALUE = new Short(s);
037: }
038:
039: /**
040: * Calculates the hash for the class name and the meta-data.
041: *
042: * @param className the class name
043: * @param info the meta-data
044: * @return the hash
045: */
046: public static Integer calculateHash(final String className,
047: final ReflectionInfo info) {
048: if (className == null) {
049: throw new IllegalArgumentException(
050: "class name can not be null");
051: }
052: if (info == null) {
053: throw new IllegalArgumentException("info can not be null");
054: }
055: int hash = 17;
056: hash = (37 * hash) + className.hashCode();
057: hash = (37 * hash) + info.hashCode();
058: Integer hashKey = new Integer(hash);
059: return hashKey;
060: }
061:
062: /**
063: * Removes the AspectWerkz specific elements from the stack trace. <p/>TODO: how to mess w/ the stacktrace in JDK
064: * 1.3.x?
065: *
066: * @param exception the Throwable to modify the stack trace on
067: * @param className the name of the fake origin class of the exception
068: */
069: public static void fakeStackTrace(final Throwable exception,
070: final String className) {
071: if (exception == null) {
072: throw new IllegalArgumentException(
073: "exception can not be null");
074: }
075: if (className == null) {
076: throw new IllegalArgumentException(
077: "class name can not be null");
078: }
079:
080: // final List newStackTraceList = new ArrayList();
081: // final StackTraceElement[] stackTrace = exception.getStackTrace();
082: // int i;
083: // for (i = 1; i < stackTrace.length; i++) {
084: // if (stackTrace[i].getClassName().equals(className)) break;
085: // }
086: // for (int j = i; j < stackTrace.length; j++) {
087: // newStackTraceList.add(stackTrace[j]);
088: // }
089: //
090: // final StackTraceElement[] newStackTrace =
091: // new StackTraceElement[newStackTraceList.size()];
092: // int k = 0;
093: // for (Iterator it = newStackTraceList.iterator(); it.hasNext(); k++) {
094: // final StackTraceElement element = (StackTraceElement)it.next();
095: // newStackTrace[k] = element;
096: // }
097: // exception.setStackTrace(newStackTrace);
098: }
099:
100: /**
101: * Returns a String representation of a classloader Avoid to do a toString() if the resulting string is too long
102: * (occurs on Tomcat)
103: *
104: * @param loader
105: * @return String representation (toString or FQN@hashcode)
106: */
107: public static String classLoaderToString(ClassLoader loader) {
108: if ((loader != null) && (loader.toString().length() < 120)) {
109: return loader.toString() + "@" + loader.hashCode();
110: } else if (loader != null) {
111: return loader.getClass().getName() + "@"
112: + loader.hashCode();
113: } else {
114: return "null";
115: }
116: }
117:
118: /**
119: * Helper method to support Java 1.4 like Boolean.valueOf(boolean) in Java 1.3
120: *
121: * @param b
122: * @return
123: */
124: public static Boolean booleanValueOf(boolean b) {
125: return b ? Boolean.TRUE : Boolean.FALSE;
126: }
127: }
|