2009/04/15 - Apache HiveMind has been retired.

For more information, please explore the Attic.

Clover coverage report - Code Coverage for hivemind release 1.2.1
Coverage timestamp: Fri Feb 10 2006 16:33:43 PST
file stats: LOC: 170   Methods: 8
NCLOC: 81   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ClassFabUtils.java 95% 97.5% 87.5% 95.6%
coverage coverage
 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;
 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 long _uid = System.currentTimeMillis();
 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  2094 public static synchronized String generateClassName(String baseName)
 41    {
 42  2094 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  2094 public static synchronized String generateClassName(Class interfaceClass)
 53    {
 54  2094 String name = interfaceClass.getName();
 55   
 56  2094 int dotx = name.lastIndexOf('.');
 57   
 58  2094 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  29478 public static String getJavaClassName(Class inputClass)
 67    {
 68  29478 if (inputClass.isArray())
 69  14 return getJavaClassName(inputClass.getComponentType()) + "[]";
 70   
 71  29464 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  2473 public static boolean isToString(Method method)
 79    {
 80  2473 if (!method.getName().equals("toString"))
 81  2469 return false;
 82   
 83  4 if (method.getParameterTypes().length > 0)
 84  0 return false;
 85   
 86  4 return method.getReturnType().equals(String.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  2075 public static void addToStringMethod(ClassFab classFab, String toStringResult)
 98    {
 99  2075 StringBuffer buffer = new StringBuffer("return ");
 100  2075 buffer.append(QUOTE);
 101  2075 buffer.append(toStringResult);
 102  2075 buffer.append(QUOTE);
 103  2075 buffer.append(";");
 104   
 105  2075 classFab.addMethod(Modifier.PUBLIC, new MethodSignature(String.class, "toString", null,
 106    null), buffer.toString());
 107    }
 108   
 109    /**
 110    * Returns the class of an instance. However, if the instance is, in fact, a JDK proxy, returns
 111    * the interfaceClass (because JDK proxies do not work with Javassist).
 112    *
 113    * @param instance
 114    * the object instance to obtain a class from
 115    * @param interfaceClass
 116    * the interface class to return if the instance is a JDK proxy.
 117    */
 118  47 public static Class getInstanceClass(Object instance, Class interfaceClass)
 119    {
 120  47 Class instanceClass = instance.getClass();
 121   
 122  47 if (Proxy.isProxyClass(instanceClass))
 123  2 return interfaceClass;
 124   
 125  45 return instanceClass;
 126    }
 127   
 128    /**
 129    * Adds a method that does nothing. If the method returns a value, it will return null, 0 or
 130    * false (depending on the type).
 131    *
 132    * @since 1.1
 133    */
 134   
 135  9 public static void addNoOpMethod(ClassFab cf, MethodSignature m)
 136    {
 137  9 StringBuffer body = new StringBuffer("{ ");
 138   
 139  9 Class returnType = m.getReturnType();
 140   
 141  9 if (returnType != void.class)
 142    {
 143  8 body.append("return");
 144   
 145  8 if (returnType.isPrimitive())
 146    {
 147  7 if (returnType == boolean.class)
 148  1 body.append(" false");
 149  6 else if (returnType == long.class)
 150  1 body.append(" 0L");
 151  5 else if (returnType == float.class)
 152  1 body.append(" 0.0f");
 153  4 else if (returnType == double.class)
 154  1 body.append(" 0.0d");
 155    else
 156  3 body.append(" 0");
 157    }
 158    else
 159    {
 160  1 body.append(" null");
 161    }
 162   
 163  8 body.append(";");
 164    }
 165   
 166  9 body.append(" }");
 167   
 168  9 cf.addMethod(Modifier.PUBLIC, m, body.toString());
 169    }
 170    }