[收集]静态加载class到classpath,并揣摸注解是webservice的类
动态加载class到classpath,并判断注解是webservice的类
原文
少年中国说原文俱舍论原文大医精诚原文注音大学原文和译文对照归藏易原文
:
如何判断某个文件夹中的的class是webservice类 首先要查找出文件夹中的所有以.class文件
1. /***
2. * 检索class文件
3. * @param file 文件路径 4. * @param packageName 包名
5. * @return class集合
6. * @throws ClassNotFoundException 7. */
8. public List
> findFile(File file, String packageName) throws ClassNot
FoundException {
9. List> serviceClassList = new LinkedList>();
10. File[] files = file.listFiles();
11. for (File fileElement : files) { 12. StringBuilder packageBuilder = new StringBuilder(packageName);
13. if (fileElement.isDirectory()) { 14. if (packageBuilder.length() > 0) { 15. packageBuilder.append("."); 16. }
17. packageBuilder.append(fileElement.getName());
18. serviceClassList.addAll(findFile(fileElement, packageBuilder.toString()));
19. } else if (fileElement.getName().endsWith(".class")){
20. StringBuilder className = new StringBuilder(packageName);
21. if (packageName != null && packageName.length() > 0) {
22. className.append(".");
23. }
24. className.append(fileElement.getName().replaceAll(".class", ""));
25.
26. }
27. }
28. return serviceClassList;
}
这个方法用于查找class文件。可以根据一个文件夹路径file查找这个文件夹的所有文件,包括文件夹,一直找出最底层的文件。
找到class文件的同时把这个class加载到classpath里。
29. try {
30. String name = className.toString(); 31. System.out.println("file:\\"+fileElement.getPath());
32. ClassLoaderUtils cl =new ClassLoaderUtils(name,new URL("file:\\"+fil
eElement.getPath()));
33. serviceClassList.add(Class.forName(name, false, cl));
34. } catch (Exception e1) {
35. // TODO Auto-generated catch block 36. e1.printStackTrace();
37. }
ClassLoaderUtils cl =new ClassLoaderUtils(name,new URL("file:\\"+fileElement.getPath()))
加载到classpath,ClassLoaderUtils有个构造函数。
参数1:这个是一个包名的字符串
参数2:URL对象地址是class文件的绝对路径
ClassLoaderUtils这个类继承了ClassLoader抽象类 ClassLoader 类是一个抽象类。每个Class对象都包含一个对定义它的ClassLoader的引用。System Class Loader是一个特殊的用户自定义类装载器,由JVM的实现者提供,在编程者不特别指定装载器的
情况下默认装载用户类。系统类装载器可以通过ClassLoader.getSystemClassLoader()方法得到。
然而,有些类可能并非源自一个文件;它们可能源自其他来源(如网络),也可能是由应用程序构造的。defineClass方法将一个byte数组转换为Class类的实例。这种新定义的类的实例可以使用Class.newInstance来创建。
例如,应用程序可以创建一个网络类加载器,从服务器中下载类文件 ClassLoader类的构造方法
使用方法 getSystemClassLoader() 返回的 ClassLoader 创建一个新的类加载器,将该加载器作为父类加载器。
这样加载class到classpath就可以了,下面要判断这个class的注解信息。
1. 1. /***
2. 2. * 判断注解是WebService
3. 3. * @param cla class
4. 4. * @return
5. 5. */
6. 6. public boolean getAnnotations(Class cla){ 7. 7. Annotation annos[] = cla.getAnnotations(); 8. 8. for(Annotation a : annos){ 9. 9. if(a.annotationType().getSimpleName().equals("WebService")){
10. 10. return true;
11. 11. }
12. 12. }
13. 13. return false;
14. 14. }
Class类有个方法getAnnotations可以获得当前类的所有注解信息,通过遍历Annotation 可以判断这个类有没有包含@webservice这样的注解信息,这样就可以判断出哪个类是webservice类了。
最后:测试方法详见InquiryWebservice 的main方法
ClassLoaderUtils .java
1. package com.util;
2. import java.io.ByteArrayOutputStream; 3. import java.io.FileNotFoundException; 4. import java.io.IOException;
5. import java.io.InputStream;
6. import java.net.MalformedURLException; 7. import java.net.URL;
8. import java.net.URLConnection;
9.
10. public class ClassLoaderUtils extends ClassLoader { 11. // private static ClassLoaderUtils cl = null; 12. // private static boolean flag = true; 13. private InputStream classFile = null; 14. private String name = null;
15. /**
16. * 构造方法
17. * @param name String 类全名
18. * @param url URL 类路径
19. * @throws FileNotFoundException 20. * @throws IOException
21. */
22. public ClassLoaderUtils(String name, URL url) { 23. super(getSystemClassLoader()); 24. this.name = name + ".class"; 25. // 打开URL指定的资源
26. URLConnection con;
27. try {
28. con = url.openConnection(); 29. InputStream classIs = con.getInputStream(); 30. this.classFile = classIs;
31. ByteArrayOutputStream baos = new ByteArrayOutputStream();
32. byte buf[] = new byte[1024]; 33. // 读取文件流
34. for (int i = 0; (i = classIs.read(buf)) != -1;) { 35. baos.write(buf, 0, i);
36. }
37. // 创建新的类对象
38. byte[] data = baos.toByteArray(); 39. defineClass(name,data, 0, data.length); 40. classIs.close();
41. baos.close();
42. } catch (IOException e) {
43. // TODO Auto-generated catch block 44. e.printStackTrace();
45. }
46.
47. }
48.
49. /**
50. * 重载 getResourceAsStream() 是为了返回该类的文件流。 51. * @return an InputStream of the class bytes, or null 52. */
53. public InputStream getResourceAsStream(String resourceName) {
54. try {
55. if (resourceName.equals(name)) { 56. return this.classFile; 57. }
58. } catch (Exception e) { 59. return null;
60. }
61. return null;
62. }
63. public static void main(String[] args) { 64. try {
65. ClassLoaderUtils cl =new ClassLoaderUtils("com.jaxws.service.TestWebser
vice",new URL("file:\\E:\\workspace\\Jaxws_Service\\WebRoot\\WEB-INF\\classes\\
com\\jaxws\\service\\TestWebservice.class")); 66. } catch (MalformedURLException e) { 67. // TODO Auto-generated catch block 68. e.printStackTrace();
69. }
70. }
71. }
InquiryWebservice .java
38. package com.util;
39.
40. import java.io.File;
41. import java.lang.annotation.Annotation; 42. import java.net.URL;
43. import java.util.LinkedList;
44. import java.util.List;
45. public class InquiryWebservice { 46. /***
47. * 检索class文件,并设置classpath 48. * @param file 文件路径 49. * @param packageName 包名 50. * @return class集合
51. * @throws ClassNotFoundException 52. */
53. public List> findFile(File file, String packageName) throws ClassNot
FoundException {
54. List> serviceClassList = new LinkedList>();
55. File[] files = file.listFiles();
56. for (File fileElement : files) {
57. StringBuilder packageBuilder = new StringBuilder(packageName);
58. if (fileElement.isDirectory()) { 59. if (packageBuilder.length() > 0) { 60. packageBuilder.append("."); 61. }
62. packageBuilder.append(fileElement.getName());
63. serviceClassList.addAll(findFile(fileElement, packageBuilder.toString()));
64. } else if (fileElement.getName().endsWith(".class")){
65. StringBuilder className = new StringBuilder(packageName);
66. if (packageName != null && packageName.length() > 0) {
67. className.append("."); 68. }
69. className.append(fileElement.getName().replaceAll(".class", ""));
70. try {
71. String name = className.toString(); 72. System.out.println("file:\\"+fileElement.getPath());
73. ClassLoaderUtils cl =new ClassLoaderUtils(name,new URL("file:\\"+fil
eElement.getPath()));
74. serviceClassList.add(Class.forName(name, false, cl));
75. } catch (Exception e1) {
76. // TODO Auto-generated catch block 77. e1.printStackTrace();
78. }
79. }
80. }
81. return serviceClassList;
82. }
83. /***
84. * 判断注解是WebService
85. * @param cla class
86. * @return
87. */
88. public boolean getAnnotations(Class cla){ 89. Annotation annos[] = cla.getAnnotations(); 90. for(Annotation a : annos){ 91. if(a.annotationType().getSimpleName().equals("WebService")){
92. return true;
93. }
94. }
95. return false;
96. }
97. public static void main(String[] args) { 98. String root = "E:\\workspace\\Jaxws_Service\\WebRoot\\WEB-INF\\classes";
99. InquiryWebservice inquiryWebservice=new InquiryWebservice();
100. try {
101. List> services = inquiryWebservice.findFile(new File(root), "");
102. for (int i = 0; i < services.size(); i++) { 103. Class> cla=services.get(i); 104. // System.out.println(cla.getName()); 105. if(inquiryWebservice.getAnnotations(cla)){ 106. System.out.println(cla.getName()); 107. }
108. }
109. } catch (ClassNotFoundException e) { 110. // TODO Auto-generated catch block 111. e.printStackTrace();
112. }
113.
114. }
115.
116.
117. }