|
|||||||||||||||||||
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 | 83.3% | 93.3% | 80% | 88.5% |
|
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 |
|
|
20 |
/**
|
|
21 |
* Static class containing utility methods.
|
|
22 |
*
|
|
23 |
* @author Howard Lewis Ship
|
|
24 |
*/
|
|
25 |
public class ClassFabUtils |
|
26 |
{ |
|
27 |
private static int _uid = 0; |
|
28 |
|
|
29 |
private static final char QUOTE = '"'; |
|
30 |
|
|
31 | 0 |
private ClassFabUtils()
|
32 |
{ |
|
33 |
} |
|
34 |
|
|
35 |
/**
|
|
36 |
* Generates a unique class name, which will be in the
|
|
37 |
* default package.
|
|
38 |
*/
|
|
39 |
|
|
40 | 306 |
public static synchronized String generateClassName(String baseName) |
41 |
{ |
|
42 | 306 |
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 for arrays.
|
|
47 |
* Invoking getName() on a Class instance representing an array returns the internal
|
|
48 |
* format (i.e, "[...;" or something). This returns it as it would appear in
|
|
49 |
* Java code.
|
|
50 |
*
|
|
51 |
*/
|
|
52 | 3936 |
public static String getJavaClassName(Class inputClass) |
53 |
{ |
|
54 | 3936 |
if (inputClass.isArray())
|
55 | 11 |
return getJavaClassName(inputClass.getComponentType()) + "[]"; |
56 |
|
|
57 | 3925 |
return inputClass.getName();
|
58 |
} |
|
59 |
|
|
60 |
/**
|
|
61 |
* Returns true if the method is the standard toString() method.
|
|
62 |
* Very few interfaces will ever include this method as part of
|
|
63 |
* the interface, but we have to be sure.
|
|
64 |
*/
|
|
65 | 448 |
public static boolean isToString(Method method) |
66 |
{ |
|
67 | 448 |
if (!method.getName().equals("toString")) |
68 | 445 |
return false; |
69 |
|
|
70 | 3 |
if (method.getParameterTypes().length > 0)
|
71 | 0 |
return false; |
72 |
|
|
73 | 3 |
return method.getReturnType().equals(String.class); |
74 |
} |
|
75 |
|
|
76 |
/**
|
|
77 |
* Adds a <code>toString()</code> method to a class that returns a fixed,
|
|
78 |
* pre-computed value.
|
|
79 |
*
|
|
80 |
* @param classFab ClassFab used to construct the new class.
|
|
81 |
* @param toStringResult fixed result to be returned by the method.
|
|
82 |
*/
|
|
83 | 302 |
public static void addToStringMethod(ClassFab classFab, String toStringResult) |
84 |
{ |
|
85 | 302 |
StringBuffer buffer = new StringBuffer("return "); |
86 | 302 |
buffer.append(QUOTE); |
87 | 302 |
buffer.append(toStringResult); |
88 | 302 |
buffer.append(QUOTE); |
89 | 302 |
buffer.append(";");
|
90 |
|
|
91 | 302 |
classFab.addMethod( |
92 |
Modifier.PUBLIC, |
|
93 |
new MethodSignature(String.class, "toString", null, null), |
|
94 |
buffer.toString()); |
|
95 |
} |
|
96 |
|
|
97 |
} |
|
98 |
|
|