You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReflectionUtil.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package com.uqac.rthoni.java_rmi.common;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.UnsupportedEncodingException;
  5. import java.net.JarURLConnection;
  6. import java.net.URL;
  7. import java.net.URLDecoder;
  8. import java.util.*;
  9. import java.util.jar.JarEntry;
  10. import java.util.jar.JarFile;
  11. import java.util.stream.Collectors;
  12. /**
  13. * Created by robin on 9/16/16.
  14. */
  15. public class ReflectionUtil {
  16. public static Vector<Class> getClassesForPackage(String pckgname)
  17. throws ClassNotFoundException {
  18. Vector<Class> classes = new Vector<>();
  19. ArrayList<File> directories = new ArrayList<>();
  20. try {
  21. ClassLoader cld = Thread.currentThread().getContextClassLoader();
  22. Enumeration<URL> resources = cld.getResources(pckgname.replace('.', '/'));
  23. while (resources.hasMoreElements()) {
  24. URL res = resources.nextElement();
  25. if (res.getProtocol().equalsIgnoreCase("jar")){
  26. JarURLConnection conn = (JarURLConnection) res.openConnection();
  27. JarFile jar = conn.getJarFile();
  28. for (JarEntry e: Collections.list(jar.entries())){
  29. if(e.getName().startsWith(pckgname.replace('.', '/'))
  30. && e.getName().endsWith(".class") && !e.getName().contains("$")){
  31. String className = e.getName().replace("/",".").substring(0,e.getName().length() - 6);
  32. classes.add(Class.forName(className));
  33. }
  34. }
  35. }else
  36. directories.add(new File(URLDecoder.decode(res.getPath(), "UTF-8")));
  37. }
  38. } catch (Exception e) {
  39. throw new ClassNotFoundException(String.format("Failed to get jar info: %s", e.getMessage()));
  40. }
  41. // For every directory identified capture all the .class files
  42. for (File directory : directories) {
  43. if (directory.exists()) {
  44. // Get the list of the files contained in the package
  45. String[] files = directory.list();
  46. for (String file : files) {
  47. // we are only interested in .class files
  48. if (file.endsWith(".class")) {
  49. // removes the .class extension
  50. classes.add(Class.forName(pckgname + '.' + file.substring(0, file.length() - 6)));
  51. }
  52. }
  53. } else {
  54. throw new ClassNotFoundException(String.format("Failed to get class file for %s", directory.getName()));
  55. }
  56. }
  57. return classes;
  58. }
  59. public static Vector<Class> getClassesOfSuperClass(String thePackage, Class superClass)
  60. throws ClassNotFoundException {
  61. Vector<Class> classList = new Vector<>();
  62. classList.addAll(getClassesForPackage(thePackage).stream()
  63. .filter(discovered -> Collections.singletonList(discovered.getSuperclass())
  64. .contains(superClass)).collect(Collectors.toList()));
  65. return classList;
  66. }
  67. public static Object toObject(Class className, String value)
  68. {
  69. if (className == Boolean.class || className == boolean.class) {
  70. return Boolean.parseBoolean(value);
  71. }
  72. if (className == Byte.class || className == byte.class) {
  73. return Byte.parseByte(value);
  74. }
  75. if (className == Short.class || className == short.class) {
  76. return Short.parseShort(value);
  77. }
  78. if (className == Integer.class || className == int.class) {
  79. return Integer.parseInt(value);
  80. }
  81. if (className == Long.class || className == long.class) {
  82. return Long.parseLong(value);
  83. }
  84. if (className == Float.class || className == float.class) {
  85. return Float.parseFloat(value);
  86. }
  87. if (className == Double.class || className == double.class) {
  88. return Double.parseDouble(value);
  89. }
  90. return value;
  91. }
  92. public static Class getClass(String className, List<ClassLoader> loaders) throws ClassNotFoundException {
  93. if (className.equals("boolean")) {
  94. return boolean.class;
  95. }
  96. if (className.equals("byte")) {
  97. return byte.class;
  98. }
  99. if (className.equals("short")) {
  100. return short.class;
  101. }
  102. if (className.equals("int")) {
  103. return int.class;
  104. }
  105. if (className.equals("long")) {
  106. return long.class;
  107. }
  108. if (className.equals("float")) {
  109. return float.class;
  110. }
  111. if (className.equals("double")) {
  112. return double.class;
  113. }
  114. for (ClassLoader classLoader : loaders) {
  115. try {
  116. return (Class) Class.forName(className, true, classLoader);
  117. } catch (ClassNotFoundException e) {
  118. }
  119. }
  120. return Class.forName(className);
  121. }
  122. public static Object newInstance(String className, List<ClassLoader> loaders)
  123. throws IllegalAccessException, InstantiationException, ClassNotFoundException {
  124. Class c = getClass(className, loaders);
  125. return c.newInstance();
  126. }
  127. }