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.

ClassUtils.java 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 2016 Decebal Suiu
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.pf4j.util;
  17. import java.lang.reflect.Modifier;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. /**
  21. * @author Decebal Suiu
  22. */
  23. public class ClassUtils {
  24. public static List<String> getAllInterfacesNames(Class<?> aClass) {
  25. return toString(getAllInterfaces(aClass));
  26. }
  27. public static List<Class<?>> getAllInterfaces(Class<?> aClass) {
  28. List<Class<?>> list = new ArrayList<>();
  29. while (aClass != null) {
  30. Class<?>[] interfaces = aClass.getInterfaces();
  31. for (Class<?> anInterface : interfaces) {
  32. if (!list.contains(anInterface)) {
  33. list.add(anInterface);
  34. }
  35. List<Class<?>> superInterfaces = getAllInterfaces(anInterface);
  36. for (Class<?> superInterface : superInterfaces) {
  37. if (!list.contains(superInterface)) {
  38. list.add(superInterface);
  39. }
  40. }
  41. }
  42. aClass = aClass.getSuperclass();
  43. }
  44. return list;
  45. }
  46. /*
  47. public static List<String> getAllAbstractClassesNames(Class<?> aClass) {
  48. return toString(getAllInterfaces(aClass));
  49. }
  50. public static List getAllAbstractClasses(Class aClass) {
  51. List<Class<?>> list = new ArrayList<>();
  52. Class<?> superclass = aClass.getSuperclass();
  53. while (superclass != null) {
  54. if (Modifier.isAbstract(superclass.getModifiers())) {
  55. list.add(superclass);
  56. }
  57. superclass = superclass.getSuperclass();
  58. }
  59. return list;
  60. }
  61. */
  62. /**
  63. * Uses {@link Class#getSimpleName()} to convert from {@link Class} to {@link String}.
  64. *
  65. * @param classes
  66. * @return
  67. */
  68. private static List<String> toString(List<Class<?>> classes) {
  69. List<String> list = new ArrayList<>();
  70. for (Class<?> aClass : classes) {
  71. list.add(aClass.getSimpleName());
  72. }
  73. return list;
  74. }
  75. }