KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > util > Utils


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.util;
10
11 import java.lang.reflect.Array JavaDoc;
12 import java.lang.reflect.Method JavaDoc;
13
14 /**
15  * Several utility functions for the JMX implementation
16  *
17  * @version $Revision: 1.18 $
18  */

19 public class Utils
20 {
21    /**
22     * This methods load a class given the classloader and the name of the class, and work for
23     * extended names of primitive types. <p>
24     * If you try to do ClassLoader.loadClass("boolean") it barfs it cannot find the class,
25     * so this method cope with this problem.
26     */

27    public static Class JavaDoc loadClass(ClassLoader JavaDoc loader, String JavaDoc name) throws ClassNotFoundException JavaDoc
28    {
29       if (name == null) throw new ClassNotFoundException JavaDoc("null");
30
31       name = name.trim();
32       if (name.equals("boolean"))
33          return boolean.class;
34       else if (name.equals("byte"))
35          return byte.class;
36       else if (name.equals("char"))
37          return char.class;
38       else if (name.equals("short"))
39          return short.class;
40       else if (name.equals("int"))
41          return int.class;
42       else if (name.equals("long"))
43          return long.class;
44       else if (name.equals("float"))
45          return float.class;
46       else if (name.equals("double"))
47          return double.class;
48       else if (name.equals("java.lang.String"))
49          return String JavaDoc.class;
50       else if (name.equals("java.lang.Object"))
51          return Object JavaDoc.class;
52       else if (name.startsWith("["))
53       {
54          // It's an array, figure out how many dimensions
55
int dimension = 0;
56          while (name.charAt(dimension) == '[')
57          {
58             ++dimension;
59          }
60          char type = name.charAt(dimension);
61          Class JavaDoc cls = null;
62          switch (type)
63          {
64             case 'Z':
65                cls = boolean.class;
66                break;
67             case 'B':
68                cls = byte.class;
69                break;
70             case 'C':
71                cls = char.class;
72                break;
73             case 'S':
74                cls = short.class;
75                break;
76             case 'I':
77                cls = int.class;
78                break;
79             case 'J':
80                cls = long.class;
81                break;
82             case 'F':
83                cls = float.class;
84                break;
85             case 'D':
86                cls = double.class;
87                break;
88             case 'L':
89                // Strip the semicolon at the end
90
String JavaDoc n = name.substring(dimension + 1, name.length() - 1);
91                cls = loadClass(loader, n);
92                break;
93          }
94
95          if (cls == null)
96          {
97             throw new ClassNotFoundException JavaDoc(name);
98          }
99          else
100          {
101             int[] dim = new int[dimension];
102             return Array.newInstance(cls, dim).getClass();
103          }
104       }
105       else
106       {
107          if (loader != null)
108             return loader.loadClass(name);
109          else
110             return Class.forName(name, false, null);
111       }
112    }
113
114    /**
115     * Returns the classes whose names are specified by the <code>names</code> argument, loaded with the
116     * specified classloader.
117     */

118    public static Class JavaDoc[] loadClasses(ClassLoader JavaDoc loader, String JavaDoc[] names) throws ClassNotFoundException JavaDoc
119    {
120       int n = names.length;
121       Class JavaDoc[] cls = new Class JavaDoc[n];
122       for (int i = 0; i < n; ++i)
123       {
124          String JavaDoc name = names[i];
125          cls[i] = loadClass(loader, name);
126       }
127       return cls;
128    }
129
130    /**
131     * Returns true is the given method is a JMX attribute getter method
132     */

133    public static boolean isAttributeGetter(Method JavaDoc m)
134    {
135       if (m == null) return false;
136
137       String JavaDoc name = m.getName();
138       Class JavaDoc retType = m.getReturnType();
139       Class JavaDoc[] params = m.getParameterTypes();
140       if (retType != Void.TYPE && params.length == 0)
141       {
142          if (name.startsWith("get") && name.length() > 3)
143             return true;
144          else if (name.startsWith("is") && name.length() > 2 && retType == Boolean.TYPE) return true;
145       }
146       return false;
147    }
148
149    /**
150     * Returns true if the method is a JMX attribute setter method
151     */

152    public static boolean isAttributeSetter(Method JavaDoc m)
153    {
154       if (m == null) return false;
155
156       String JavaDoc name = m.getName();
157       Class JavaDoc retType = m.getReturnType();
158       Class JavaDoc[] params = m.getParameterTypes();
159       if (retType == Void.TYPE && params.length == 1 && name.startsWith("set") && name.length() > 3)
160       {
161          return true;
162       }
163       return false;
164    }
165
166    public static boolean wildcardMatch(String JavaDoc pattern, String JavaDoc string)
167    {
168       int stringLength = string.length();
169       int stringIndex = 0;
170       for (int patternIndex = 0; patternIndex < pattern.length(); ++patternIndex)
171       {
172          char c = pattern.charAt(patternIndex);
173          if (c == '*')
174          {
175             // Recurse with the pattern without this '*' and the actual string, until
176
// match is found or we inspected the whole string
177
while (stringIndex < stringLength)
178             {
179                if (wildcardMatch(pattern.substring(patternIndex + 1), string.substring(stringIndex)))
180                {
181                   return true;
182                }
183                // No match found, try a shorter string, since we are matching '*'
184
++stringIndex;
185             }
186          }
187          else if (c == '?')
188          {
189             // Increment the string index, since '?' match a single char in the string
190
++stringIndex;
191             if (stringIndex > stringLength)
192             {
193                return false;
194             }
195          }
196          else
197          {
198             // A normal character in the pattern, must match the one in the string
199
if (stringIndex >= stringLength || c != string.charAt(stringIndex))
200             {
201                return false;
202             }
203             ++stringIndex;
204          }
205       }
206
207       // I've inspected the whole pattern, but not the whole string
208
return stringIndex == stringLength;
209    }
210
211    public static boolean arrayEquals(Object JavaDoc[] arr1, Object JavaDoc[] arr2)
212    {
213       if (arr1 == null && arr2 == null) return true;
214       if (arr1 == null ^ arr2 == null) return false;
215       if (!arr1.getClass().equals(arr2.getClass())) return false;
216       if (arr1.length != arr2.length) return false;
217
218       for (int i = 0; i < arr1.length; ++i)
219       {
220          Object JavaDoc obj1 = arr1[i];
221          Object JavaDoc obj2 = arr2[i];
222          if (obj1 == null ^ obj2 == null) return false;
223          if (obj1 != null && !obj1.equals(obj2)) return false;
224       }
225       return true;
226    }
227
228    public static boolean arrayEquals(byte[] arr1, byte[] arr2)
229    {
230       if (arr1 == null && arr2 == null) return true;
231       if (arr1 == null ^ arr2 == null) return false;
232       if (!arr1.getClass().equals(arr2.getClass())) return false;
233       if (arr1.length != arr2.length) return false;
234
235       for (int i = 0; i < arr1.length; ++i)
236       {
237          byte b1 = arr1[i];
238          byte b2 = arr2[i];
239          if (b1 != b2) return false;
240       }
241       return true;
242    }
243
244    public static int arrayHashCode(Object JavaDoc[] arr)
245    {
246       int hash = 0;
247       if (arr != null)
248       {
249          // Avoid that 2 arrays of length 0 but different classes return same hash
250
hash ^= arr.getClass().hashCode();
251          for (int i = 0; i < arr.length; ++i)
252          {
253             hash ^= arr[i] == null ? 0 : arr[i].hashCode();
254          }
255       }
256       return hash;
257    }
258
259    public static int arrayHashCode(byte[] arr)
260    {
261       int hash = 0;
262       if (arr != null)
263       {
264          // Avoid that 2 arrays of length 0 but different classes return same hash
265
hash ^= arr.getClass().hashCode();
266          for (int i = 0; i < arr.length; ++i)
267          {
268             hash ^= arr[i];
269          }
270       }
271       return hash;
272    }
273
274    public static char[] arrayCopy(char[] chars)
275    {
276       if (chars == null) return null;
277       char[] copy = new char[chars.length];
278       System.arraycopy(chars, 0, copy, 0, chars.length);
279       return copy;
280    }
281 }
282
Popular Tags