1. /*
  2. * $Header$
  3. * $Revision$
  4. * $Date$
  5. *
  6. * ====================================================================
  7. *
  8. * The Apache Software License, Version 1.1
  9. *
  10. * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
  11. * reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * 2. Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in
  22. * the documentation and/or other materials provided with the
  23. * distribution.
  24. *
  25. * 3. The end-user documentation included with the redistribution, if
  26. * any, must include the following acknowlegement:
  27. * "This product includes software developed by the
  28. * Apache Software Foundation (http://www.apache.org/)."
  29. * Alternately, this acknowlegement may appear in the software itself,
  30. * if and wherever such third-party acknowlegements normally appear.
  31. *
  32. * 4. The names "The Jakarta Project", "Commons", and "Apache Software
  33. * Foundation" must not be used to endorse or promote products derived
  34. * from this software without prior written permission. For written
  35. * permission, please contact apache@apache.org.
  36. *
  37. * 5. Products derived from this software may not be called "Apache"
  38. * nor may "Apache" appear in their names without prior written
  39. * permission of the Apache Group.
  40. *
  41. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  42. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  43. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  44. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  45. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  46. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  47. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  48. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  49. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  50. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  51. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  52. * SUCH DAMAGE.
  53. * ====================================================================
  54. *
  55. * This software consists of voluntary contributions made by many
  56. * individuals on behalf of the Apache Software Foundation. For more
  57. * information on the Apache Software Foundation, please see
  58. * <http://www.apache.org/>.
  59. *
  60. */
  61. package org.apache.commons.discovery.tools;
  62. import java.lang.reflect.Constructor;
  63. import java.lang.reflect.InvocationTargetException;
  64. import java.lang.reflect.Method;
  65. import java.lang.reflect.Modifier;
  66. import org.apache.commons.discovery.DiscoveryException;
  67. import org.apache.commons.discovery.log.DiscoveryLogFactory;
  68. import org.apache.commons.logging.Log;
  69. /**
  70. * @author Richard A. Sitze
  71. */
  72. public class ClassUtils {
  73. private static Log log = DiscoveryLogFactory.newLog(ClassUtils.class);
  74. public static void setLog(Log _log) {
  75. log = _log;
  76. }
  77. /**
  78. * Get package name.
  79. * Not all class loaders 'keep' package information,
  80. * in which case Class.getPackage() returns null.
  81. * This means that calling Class.getPackage().getName()
  82. * is unreliable at best.
  83. */
  84. public static String getPackageName(Class clazz) {
  85. Package clazzPackage = clazz.getPackage();
  86. String packageName;
  87. if (clazzPackage != null) {
  88. packageName = clazzPackage.getName();
  89. } else {
  90. String clazzName = clazz.getName();
  91. packageName = clazzName.substring(0, clazzName.lastIndexOf('.'));
  92. }
  93. return packageName;
  94. }
  95. /**
  96. * @return Method 'public static returnType methodName(paramTypes)',
  97. * if found to be <strong>directly</strong> implemented by clazz.
  98. */
  99. public static Method findPublicStaticMethod(Class clazz,
  100. Class returnType,
  101. String methodName,
  102. Class[] paramTypes) {
  103. boolean problem = false;
  104. Method method = null;
  105. // verify '<methodName>(<paramTypes>)' is directly in class.
  106. try {
  107. method = clazz.getDeclaredMethod(methodName, paramTypes);
  108. } catch(NoSuchMethodException e) {
  109. problem = true;
  110. log.debug("Class " + clazz.getName() + ": missing method '" + methodName + "(...)", e);
  111. }
  112. // verify 'public static <returnType>'
  113. if (!problem &&
  114. !(Modifier.isPublic(method.getModifiers()) &&
  115. Modifier.isStatic(method.getModifiers()) &&
  116. method.getReturnType() == returnType)) {
  117. if (log.isDebugEnabled()) {
  118. if (!Modifier.isPublic(method.getModifiers())) {
  119. log.debug(methodName + "() is not public");
  120. }
  121. if (!Modifier.isStatic(method.getModifiers())) {
  122. log.debug(methodName + "() is not static");
  123. }
  124. if (method.getReturnType() != returnType) {
  125. log.debug("Method returns: " + method.getReturnType().getName() + "@@" + method.getReturnType().getClassLoader());
  126. log.debug("Should return: " + returnType.getName() + "@@" + returnType.getClassLoader());
  127. }
  128. }
  129. problem = true;
  130. method = null;
  131. }
  132. return method;
  133. }
  134. /**
  135. * Instantiate a new
  136. */
  137. public static Object newInstance(Class impl, Class paramClasses[], Object params[])
  138. throws DiscoveryException,
  139. InstantiationException,
  140. IllegalAccessException,
  141. NoSuchMethodException,
  142. InvocationTargetException
  143. {
  144. if (paramClasses == null || params == null) {
  145. return impl.newInstance();
  146. } else {
  147. Constructor constructor = impl.getConstructor(paramClasses);
  148. return constructor.newInstance(params);
  149. }
  150. }
  151. /**
  152. * Throws exception if <code>impl</code> does not
  153. * implement or extend the SPI.
  154. */
  155. public static void verifyAncestory(Class spi, Class impl)
  156. throws DiscoveryException
  157. {
  158. if (spi == null) {
  159. throw new DiscoveryException("No interface defined!");
  160. }
  161. if (impl == null) {
  162. throw new DiscoveryException("No implementation defined for " + spi.getName());
  163. }
  164. if (!spi.isAssignableFrom(impl)) {
  165. throw new DiscoveryException("Class " + impl.getName() +
  166. " does not implement " + spi.getName());
  167. }
  168. }
  169. }