1. package org.jr.util;
  2. /**
  3. * Copyright: Copyright (c) 2002-2004
  4. * Company: JavaResearch(http://www.javaresearch.org)
  5. * 最后更新日期:2003年3月1日
  6. * @author Cherami
  7. */
  8. import java.lang.reflect.*;
  9. import java.util.*;
  10. import java.io.*;
  11. /**
  12. * 涉及类的方法等的工具类。
  13. * @since 0.4
  14. */
  15. public class ClassUtil {
  16. /**
  17. * 私有构造方法,防止类的实例化,因为工具类不需要实例化。
  18. */
  19. private ClassUtil() {
  20. }
  21. /**
  22. * 根据类名得到该类所有的pulbic成员方法的方法名。
  23. * @param className 类名
  24. * @return 所有的pulbic成员方法的方法名数组
  25. * @since 0.4
  26. */
  27. public static String[] getMethods(String className) {
  28. String methodNames[];
  29. try {
  30. Class c = Class.forName(className);
  31. Method methods[] = c.getMethods();
  32. methodNames = new String[methods.length];
  33. for (int i = 0; i < methods.length; i++) {
  34. methodNames[i] = methods[i].toString();
  35. }
  36. return methodNames;
  37. }
  38. catch (ClassNotFoundException e) {
  39. e.printStackTrace();
  40. return null;
  41. }
  42. }
  43. /**
  44. * 根据类名得到该类所申明的方法的方法名。
  45. * @param className 类名
  46. * @return 所申明的方法的方法名数组
  47. * @since 0.4
  48. */
  49. public static String[] getDeclaredMethods(String className) {
  50. String methodNames[];
  51. try {
  52. Class c = Class.forName(className);
  53. Method methods[] = c.getDeclaredMethods();
  54. methodNames = new String[methods.length];
  55. for (int i = 0; i < methods.length; i++) {
  56. methodNames[i] = methods[i].toString();
  57. }
  58. return methodNames;
  59. }
  60. catch (ClassNotFoundException e) {
  61. e.printStackTrace();
  62. return null;
  63. }
  64. }
  65. /**
  66. * 判断一个对象数组是否是同质数组。
  67. * 这里的同质是严格的同质,即他们的实际类类型必须完全相同。
  68. * @param objects 要比较的对象数组
  69. * @return 所有元素的类类型完全相同时返回true,如果数组中只有一个元素时也返回true,否则返回false
  70. * @since 0.5
  71. */
  72. public static boolean isSameClassType(Object[] objects) {
  73. if (objects.length == 1) {
  74. return true;
  75. }
  76. Class c = objects[0].getClass();
  77. for (int i = 1; i < objects.length; i++) {
  78. if (!c.equals(objects[i].getClass())) {
  79. return false;
  80. }
  81. }
  82. return true;
  83. }
  84. /**
  85. * 判断一个对象数组是否是同质数组且是指定的类类型。
  86. * 这里的同质是严格的同质,即他们的实际类类型必须完全相同。
  87. * @param objects 要比较的对象数组
  88. * @param c 类类型
  89. * @return 所有元素的类类型和c完全相同时返回true,如果数组中只有一个元素时也返回true,否则返回false
  90. * @since 0.5
  91. */
  92. public static boolean isSameClassType(Object[] objects, Class c) {
  93. if (objects.length == 1) {
  94. return true;
  95. }
  96. for (int i = 1; i < objects.length; i++) {
  97. if (!c.equals(objects[i].getClass())) {
  98. return false;
  99. }
  100. }
  101. return true;
  102. }
  103. /**
  104. * 判断child是否是c的一个子类。
  105. * @param c 父类
  106. * @param child 要判断的可能的子类
  107. * @return child是c的子类的时候返回true,其他所有情况下都返回false
  108. * @since 0.5
  109. */
  110. public static boolean isSubclass(Class c, Class child) {
  111. try {
  112. if (c.isInstance(child.newInstance())) {
  113. return true;
  114. }
  115. return false;
  116. }
  117. catch (IllegalAccessException e) {
  118. return false;
  119. }
  120. catch (InstantiationException ie) {
  121. return false;
  122. }
  123. catch (Exception oe) {
  124. return false;
  125. }
  126. }
  127. /**
  128. * 得到JDK1.4所有的类名集合,只包含org,java,javax包及其子包下的类的类名。
  129. * 每个元素的值只是类名,没有包名以及扩展名java。
  130. * @return 类名的集合
  131. * @since 0.5
  132. */
  133. public static TreeSet getAllClassname() {
  134. TreeSet classnameSet=new TreeSet();
  135. ObjectInputStream p=null;
  136. try {
  137. InputStream istream = ClassUtil.class.getResourceAsStream("allclassname.treeset");
  138. p = new ObjectInputStream(istream);
  139. classnameSet = (TreeSet)p.readObject();
  140. }
  141. catch (IOException e) {
  142. e.printStackTrace();
  143. }
  144. catch (ClassNotFoundException e1) {
  145. e1.printStackTrace();
  146. }
  147. catch (Exception e) {
  148. e.printStackTrace();
  149. }
  150. finally {
  151. if (p!=null) {
  152. try {
  153. p.close();
  154. }
  155. catch (IOException e) {
  156. }
  157. }
  158. }
  159. return classnameSet;
  160. }
  161. }