View Javadoc

1   /***************************************************************************************
2    * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved.                 *
3    * http://aspectwerkz.codehaus.org                                                    *
4    * ---------------------------------------------------------------------------------- *
5    * The software in this package is published under the terms of the LGPL license      *
6    * a copy of which has been included with this distribution in the license.txt file.  *
7    **************************************************************************************/
8   package org.codehaus.aspectwerkz.util;
9   
10  import org.codehaus.aspectwerkz.reflect.ReflectionInfo;
11  
12  /***
13   * Utility methods and constants used in the AspectWerkz system.
14   *
15   * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
16   */
17  public final class Util {
18      public static final Integer INTEGER_DEFAULT_VALUE = new Integer(0);
19  
20      public static final Float FLOAT_DEFAULT_VALUE = new Float(0.0f);
21  
22      public static final Double DOUBLE_DEFAULT_VALUE = new Double(0.0d);
23  
24      public static final Long LONG_DEFAULT_VALUE = new Long(0L);
25  
26      public static final Boolean BOOLEAN_DEFAULT_VALUE = new Boolean(false);
27  
28      public static final Character CHARACTER_DEFAULT_VALUE = new Character('\u0000');
29  
30      public static final Short SHORT_DEFAULT_VALUE;
31  
32      public static final Byte BYTE_DEFAULT_VALUE;
33  
34      static {
35          byte b = 0;
36          BYTE_DEFAULT_VALUE = new Byte(b);
37          short s = 0;
38          SHORT_DEFAULT_VALUE = new Short(s);
39      }
40  
41      /***
42       * Calculates the hash for the class name and the meta-data.
43       *
44       * @param className the class name
45       * @param info      the meta-data
46       * @return the hash
47       */
48      public static Integer calculateHash(final String className, final ReflectionInfo info) {
49          if (className == null) {
50              throw new IllegalArgumentException("class name can not be null");
51          }
52          if (info == null) {
53              throw new IllegalArgumentException("info can not be null");
54          }
55          int hash = 17;
56          hash = (37 * hash) + className.hashCode();
57          hash = (37 * hash) + info.hashCode();
58          Integer hashKey = new Integer(hash);
59          return hashKey;
60      }
61  
62      /***
63       * Removes the AspectWerkz specific elements from the stack trace. <p/>TODO: how to mess w/ the stacktrace in JDK
64       * 1.3.x?
65       *
66       * @param exception the Throwable to modify the stack trace on
67       * @param className the name of the fake origin class of the exception
68       */
69      public static void fakeStackTrace(final Throwable exception, final String className) {
70          if (exception == null) {
71              throw new IllegalArgumentException("exception can not be null");
72          }
73          if (className == null) {
74              throw new IllegalArgumentException("class name can not be null");
75          }
76  
77          //        final List newStackTraceList = new ArrayList();
78          //        final StackTraceElement[] stackTrace = exception.getStackTrace();
79          //        int i;
80          //        for (i = 1; i < stackTrace.length; i++) {
81          //            if (stackTrace[i].getClassName().equals(className)) break;
82          //        }
83          //        for (int j = i; j < stackTrace.length; j++) {
84          //            newStackTraceList.add(stackTrace[j]);
85          //        }
86          //
87          //        final StackTraceElement[] newStackTrace =
88          //                new StackTraceElement[newStackTraceList.size()];
89          //        int k = 0;
90          //        for (Iterator it = newStackTraceList.iterator(); it.hasNext(); k++) {
91          //            final StackTraceElement element = (StackTraceElement)it.next();
92          //            newStackTrace[k] = element;
93          //        }
94          //        exception.setStackTrace(newStackTrace);
95      }
96  
97      /***
98       * Returns a String representation of a classloader Avoid to do a toString() if the resulting string is too long
99       * (occurs on Tomcat)
100      *
101      * @param loader
102      * @return String representation (toString or FQN@hashcode)
103      */
104     public static String classLoaderToString(ClassLoader loader) {
105         if ((loader != null) && (loader.toString().length() < 120)) {
106             return loader.toString() + "@" + loader.hashCode();
107         } else if (loader != null) {
108             return loader.getClass().getName() + "@" + loader.hashCode();
109         } else {
110             return "null";
111         }
112     }
113 
114     /***
115      * Helper method to support Java 1.4 like Boolean.valueOf(boolean) in Java 1.3
116 
117      * @param b
118      * @return
119      */
120     public static Boolean booleanValueOf(boolean b) {
121         return b?Boolean.TRUE:Boolean.FALSE;
122     }
123 }