6b55f01e1edcb0481cf0d34c7f880becafb42de3.svn-base 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package org.jeecg.common.util;
  2. /**
  3. * @Author 张代浩
  4. */
  5. public class MyClassLoader extends ClassLoader {
  6. public static Class getClassByScn(String className) {
  7. Class myclass = null;
  8. try {
  9. myclass = Class.forName(className);
  10. } catch (ClassNotFoundException e) {
  11. e.printStackTrace();
  12. throw new RuntimeException(className+" not found!");
  13. }
  14. return myclass;
  15. }
  16. // 获得类的全名,包括包名
  17. public static String getPackPath(Object object) {
  18. // 检查用户传入的参数是否为空
  19. if (object == null) {
  20. throw new java.lang.IllegalArgumentException("参数不能为空!");
  21. }
  22. // 获得类的全名,包括包名
  23. String clsName = object.getClass().getName();
  24. return clsName;
  25. }
  26. public static String getAppPath(Class cls) {
  27. // 检查用户传入的参数是否为空
  28. if (cls == null) {
  29. throw new java.lang.IllegalArgumentException("参数不能为空!");
  30. }
  31. ClassLoader loader = cls.getClassLoader();
  32. // 获得类的全名,包括包名
  33. String clsName = cls.getName() + ".class";
  34. // 获得传入参数所在的包
  35. Package pack = cls.getPackage();
  36. String path = "";
  37. // 如果不是匿名包,将包名转化为路径
  38. if (pack != null) {
  39. String packName = pack.getName();
  40. // 此处简单判定是否是Java基础类库,防止用户传入JDK内置的类库
  41. if (packName.startsWith("java.") || packName.startsWith("javax.")) {
  42. throw new java.lang.IllegalArgumentException("不要传送系统类!");
  43. }
  44. // 在类的名称中,去掉包名的部分,获得类的文件名
  45. clsName = clsName.substring(packName.length() + 1);
  46. // 判定包名是否是简单包名,如果是,则直接将包名转换为路径,
  47. if (packName.indexOf(".") < 0) {
  48. path = packName + "/";
  49. } else {// 否则按照包名的组成部分,将包名转换为路径
  50. int start = 0, end = 0;
  51. end = packName.indexOf(".");
  52. while (end != -1) {
  53. path = path + packName.substring(start, end) + "/";
  54. start = end + 1;
  55. end = packName.indexOf(".", start);
  56. }
  57. path = path + packName.substring(start) + "/";
  58. }
  59. }
  60. // 调用ClassLoader的getResource方法,传入包含路径信息的类文件名
  61. java.net.URL url = loader.getResource(path + clsName);
  62. // 从URL对象中获取路径信息
  63. String realPath = url.getPath();
  64. // 去掉路径信息中的协议名"file:"
  65. int pos = realPath.indexOf("file:");
  66. if (pos > -1) {
  67. realPath = realPath.substring(pos + 5);
  68. }
  69. // 去掉路径信息最后包含类文件信息的部分,得到类所在的路径
  70. pos = realPath.indexOf(path + clsName);
  71. realPath = realPath.substring(0, pos - 1);
  72. // 如果类文件被打包到JAR等文件中时,去掉对应的JAR等打包文件名
  73. if (realPath.endsWith("!")) {
  74. realPath = realPath.substring(0, realPath.lastIndexOf("/"));
  75. }
  76. /*------------------------------------------------------------
  77. ClassLoader的getResource方法使用了utf-8对路径信息进行了编码,当路径
  78. 中存在中文和空格时,他会对这些字符进行转换,这样,得到的往往不是我们想要
  79. 的真实路径,在此,调用了URLDecoder的decode方法进行解码,以便得到原始的
  80. 中文及空格路径
  81. -------------------------------------------------------------*/
  82. try {
  83. realPath = java.net.URLDecoder.decode(realPath, "utf-8");
  84. } catch (Exception e) {
  85. throw new RuntimeException(e);
  86. }
  87. return realPath;
  88. }// getAppPath定义结束
  89. }