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