|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ClassFabUtils.java | 87.5% | 94.7% | 83.3% | 90.9% |
|
1 |
// Copyright 2004 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;
|
|
18 |
import java.lang.reflect.Modifier;
|
|
19 |
import java.lang.reflect.Proxy;
|
|
20 |
|
|
21 |
/**
|
|
22 |
* Static class containing utility methods.
|
|
23 |
*
|
|
24 |
* @author Howard Lewis Ship
|
|
25 |
*/
|
|
26 |
public class ClassFabUtils |
|
27 |
{ |
|
28 |
private static int _uid = 0; |
|
29 |
|
|
30 |
private static final char QUOTE = '"'; |
|
31 |
|
|
32 | 0 |
private ClassFabUtils()
|
33 |
{ |
|
34 |
} |
|
35 |
|
|
36 |
/**
|
|
37 |
* Generates a unique class name, which will be in the default package.
|
|
38 |
*/
|
|
39 |
|
|
40 | 1353 |
public static synchronized String generateClassName(String baseName) |
41 |
{ |
|
42 | 1353 |
return "$" + baseName + "_" + Long.toHexString(System.currentTimeMillis()) + "_" + _uid++; |
43 |
} |
|
44 |
|
|
45 |
/**
|
|
46 |
* Javassist needs the class name to be as it appears in source code, even
|
|
47 |
* for arrays. Invoking getName() on a Class instance representing an array
|
|
48 |
* returns the internal format (i.e, "[...;" or something). This returns it
|
|
49 |
* as it would appear in Java code.
|
|
50 |
*/
|
|
51 | 19993 |
public static String getJavaClassName(Class inputClass) |
52 |
{ |
|
53 | 19993 |
if (inputClass.isArray())
|
54 | 11 |
return getJavaClassName(inputClass.getComponentType()) + "[]"; |
55 |
|
|
56 | 19982 |
return inputClass.getName();
|
57 |
} |
|
58 |
|
|
59 |
/**
|
|
60 |
* Returns true if the method is the standard toString() method. Very few
|
|
61 |
* interfaces will ever include this method as part of the interface, but we
|
|
62 |
* have to be sure.
|
|
63 |
*/
|
|
64 | 1584 |
public static boolean isToString(Method method) |
65 |
{ |
|
66 | 1584 |
if (!method.getName().equals("toString")) |
67 | 1580 |
return false; |
68 |
|
|
69 | 4 |
if (method.getParameterTypes().length > 0)
|
70 | 0 |
return false; |
71 |
|
|
72 | 4 |
return method.getReturnType().equals(String.class); |
73 |
} |
|
74 |
|
|
75 |
/**
|
|
76 |
* Adds a <code>toString()</code> method to a class that returns a fixed,
|
|
77 |
* pre-computed value.
|
|
78 |
*
|
|
79 |
* @param classFab
|
|
80 |
* ClassFab used to construct the new class.
|
|
81 |
* @param toStringResult
|
|
82 |
* fixed result to be returned by the method.
|
|
83 |
*/
|
|
84 | 1350 |
public static void addToStringMethod(ClassFab classFab, String toStringResult) |
85 |
{ |
|
86 | 1350 |
StringBuffer buffer = new StringBuffer("return "); |
87 | 1350 |
buffer.append(QUOTE); |
88 | 1350 |
buffer.append(toStringResult); |
89 | 1350 |
buffer.append(QUOTE); |
90 | 1350 |
buffer.append(";");
|
91 |
|
|
92 | 1350 |
classFab.addMethod(Modifier.PUBLIC, new MethodSignature(String.class, "toString", null, null), buffer |
93 |
.toString()); |
|
94 |
} |
|
95 |
|
|
96 |
/**
|
|
97 |
* Returns the class of an instance. However, if the instance is, in fact, a
|
|
98 |
* JDK proxy, returns the interfaceClass (because JDK proxies do not work
|
|
99 |
* with Javassist).
|
|
100 |
*
|
|
101 |
* @param instance
|
|
102 |
* the object instance to obtain a class from
|
|
103 |
* @param interfaceClass
|
|
104 |
* the interface class to return if the instance is a JDK proxy.
|
|
105 |
*/
|
|
106 | 14 |
public static Class getInstanceClass(Object instance, Class interfaceClass) |
107 |
{ |
|
108 | 14 |
Class instanceClass = instance.getClass(); |
109 |
|
|
110 | 14 |
if (Proxy.isProxyClass(instanceClass))
|
111 | 2 |
return interfaceClass;
|
112 |
|
|
113 | 12 |
return instanceClass;
|
114 |
} |
|
115 |
|
|
116 |
} |
|