KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > service > ClassFabUtils


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.service;
16
17 import java.lang.reflect.Method JavaDoc;
18 import java.lang.reflect.Modifier JavaDoc;
19 import java.lang.reflect.Proxy JavaDoc;
20
21 /**
22  * Static class containing utility methods.
23  *
24  * @author Howard Lewis Ship
25  */

26 public class ClassFabUtils
27 {
28     private static long _uid = System.currentTimeMillis();
29
30     private static final char QUOTE = '"';
31
32     private ClassFabUtils()
33     {
34     }
35
36     /**
37      * Generates a unique class name, which will be in the default package.
38      */

39
40     public static synchronized String JavaDoc generateClassName(String JavaDoc baseName)
41     {
42         return "$" + baseName + "_" + Long.toHexString(_uid++);
43     }
44
45     /**
46      * Returns a class name derived from the provided interfaceClass. The package part of the
47      * interface name is stripped out, and the result passed to {@link #generateClassName(String)}.
48      *
49      * @since 1.1
50      */

51
52     public static synchronized String JavaDoc generateClassName(Class JavaDoc interfaceClass)
53     {
54         String JavaDoc name = interfaceClass.getName();
55
56         int dotx = name.lastIndexOf('.');
57
58         return generateClassName(name.substring(dotx + 1));
59     }
60
61     /**
62      * Javassist needs the class name to be as it appears in source code, even for arrays. Invoking
63      * getName() on a Class instance representing an array returns the internal format (i.e, "[...;"
64      * or something). This returns it as it would appear in Java code.
65      */

66     public static String JavaDoc getJavaClassName(Class JavaDoc inputClass)
67     {
68         if (inputClass.isArray())
69             return getJavaClassName(inputClass.getComponentType()) + "[]";
70
71         return inputClass.getName();
72     }
73
74     /**
75      * Returns true if the method is the standard toString() method. Very few interfaces will ever
76      * include this method as part of the interface, but we have to be sure.
77      */

78     public static boolean isToString(Method JavaDoc method)
79     {
80         if (!method.getName().equals("toString"))
81             return false;
82
83         if (method.getParameterTypes().length > 0)
84             return false;
85
86         return method.getReturnType().equals(String JavaDoc.class);
87     }
88
89     /**
90      * Adds a <code>toString()</code> method to a class that returns a fixed, pre-computed value.
91      *
92      * @param classFab
93      * ClassFab used to construct the new class.
94      * @param toStringResult
95      * fixed result to be returned by the method.
96      */

97     public static void addToStringMethod(ClassFab classFab, String JavaDoc toStringResult)
98     {
99         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("return ");
100         buffer.append(QUOTE);
101         buffer.append(toStringResult);
102         buffer.append(QUOTE);
103         buffer.append(";");
104
105         classFab.addMethod(Modifier.PUBLIC, new MethodSignature(String JavaDoc.class, "toString", null,
106                 null), buffer.toString());
107     }
108
109     /**
110      * Returns the class of an instance. However, if the instance's class
111      * isn't compatible (externally generated, for instance), then
112      * <code>interfaceClass</code> is returned instead.
113      *
114      * @param instance
115      * the object instance to obtain a class from
116      * @param interfaceClass
117      * the interface class to return if the instance is not compatible
118      */

119     public static Class JavaDoc getInstanceClass(ClassFab classFab, Object JavaDoc instance, Class JavaDoc interfaceClass)
120     {
121         Class JavaDoc instanceClass = instance.getClass();
122
123         if (!classFab.canConvert(instanceClass))
124             return interfaceClass;
125
126         return instanceClass;
127     }
128
129     /**
130      * Returns the class of an instance. However, if the instance is, in fact, a JDK proxy, returns
131      * the interfaceClass (because JDK proxies do not work with Javassist).
132      *
133      * @param instance
134      * the object instance to obtain a class from
135      * @param interfaceClass
136      * the interface class to return if the instance is a JDK proxy.
137      * @deprecated Please use version which takes a ClassFab object.
138      */

139     public static Class JavaDoc getInstanceClass(Object JavaDoc instance, Class JavaDoc interfaceClass)
140     {
141         Class JavaDoc instanceClass = instance.getClass();
142
143         if (Proxy.isProxyClass(instanceClass))
144             return interfaceClass;
145
146         return instanceClass;
147     }
148
149     /**
150      * Adds a method that does nothing. If the method returns a value, it will return null, 0 or
151      * false (depending on the type).
152      *
153      * @since 1.1
154      */

155
156     public static void addNoOpMethod(ClassFab cf, MethodSignature m)
157     {
158         StringBuffer JavaDoc body = new StringBuffer JavaDoc("{ ");
159
160         Class JavaDoc returnType = m.getReturnType();
161
162         if (returnType != void.class)
163         {
164             body.append("return");
165
166             if (returnType.isPrimitive())
167             {
168                 if (returnType == boolean.class)
169                     body.append(" false");
170                 else if (returnType == long.class)
171                     body.append(" 0L");
172                 else if (returnType == float.class)
173                     body.append(" 0.0f");
174                 else if (returnType == double.class)
175                     body.append(" 0.0d");
176                 else
177                     body.append(" 0");
178             }
179             else
180             {
181                 body.append(" null");
182             }
183
184             body.append(";");
185         }
186
187         body.append(" }");
188
189         cf.addMethod(Modifier.PUBLIC, m, body.toString());
190     }
191 }
Popular Tags