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 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C) 2012-present the original author or authors.
  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 javax.lang.model.element.AnnotationMirror;
  18. import javax.lang.model.element.AnnotationValue;
  19. import javax.lang.model.element.ExecutableElement;
  20. import javax.lang.model.element.TypeElement;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Map;
  24. /**
  25. * @author Decebal Suiu
  26. */
  27. public class ClassUtils {
  28. private ClassUtils() {}
  29. public static List<String> getAllInterfacesNames(Class<?> aClass) {
  30. return toString(getAllInterfaces(aClass));
  31. }
  32. public static List<Class<?>> getAllInterfaces(Class<?> aClass) {
  33. List<Class<?>> list = new ArrayList<>();
  34. while (aClass != null) {
  35. Class<?>[] interfaces = aClass.getInterfaces();
  36. for (Class<?> anInterface : interfaces) {
  37. if (!list.contains(anInterface)) {
  38. list.add(anInterface);
  39. }
  40. List<Class<?>> superInterfaces = getAllInterfaces(anInterface);
  41. for (Class<?> superInterface : superInterfaces) {
  42. if (!list.contains(superInterface)) {
  43. list.add(superInterface);
  44. }
  45. }
  46. }
  47. aClass = aClass.getSuperclass();
  48. }
  49. return list;
  50. }
  51. /**
  52. * Get a certain annotation of a {@link TypeElement}.
  53. * See <a href="https://stackoverflow.com/a/10167558">stackoverflow.com</a> for more information.
  54. *
  55. * @param typeElement the type element, that contains the requested annotation
  56. * @param annotationClass the class of the requested annotation
  57. * @return the requested annotation or null, if no annotation of the provided class was found
  58. * @throws NullPointerException if <code>typeElement</code> or <code>annotationClass</code> is null
  59. */
  60. public static AnnotationMirror getAnnotationMirror(TypeElement typeElement, Class<?> annotationClass) {
  61. String annotationClassName = annotationClass.getName();
  62. for (AnnotationMirror m : typeElement.getAnnotationMirrors()) {
  63. if (m.getAnnotationType().toString().equals(annotationClassName)) {
  64. return m;
  65. }
  66. }
  67. return null;
  68. }
  69. /**
  70. * Get a certain parameter of an {@link AnnotationMirror}.
  71. * See <a href="https://stackoverflow.com/a/10167558">stackoverflow.com</a> for more information.
  72. *
  73. * @param annotationMirror the annotation, that contains the requested parameter
  74. * @param annotationParameter the name of the requested annotation parameter
  75. * @return the requested parameter or null, if no parameter of the provided name was found
  76. * @throws NullPointerException if <code>annotationMirror</code> is null
  77. */
  78. public static AnnotationValue getAnnotationValue(AnnotationMirror annotationMirror, String annotationParameter) {
  79. for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : annotationMirror.getElementValues().entrySet()) {
  80. if (entry.getKey().getSimpleName().toString().equals(annotationParameter)) {
  81. return entry.getValue();
  82. }
  83. }
  84. return null;
  85. }
  86. /**
  87. * Get a certain annotation parameter of a {@link TypeElement}.
  88. * See <a href="https://stackoverflow.com/a/10167558">stackoverflow.com</a> for more information.
  89. *
  90. * @param typeElement the type element, that contains the requested annotation
  91. * @param annotationClass the class of the requested annotation
  92. * @param annotationParameter the name of the requested annotation parameter
  93. * @return the requested parameter or null, if no annotation for the provided class was found or no annotation parameter was found
  94. * @throws NullPointerException if <code>typeElement</code> or <code>annotationClass</code> is null
  95. */
  96. public static AnnotationValue getAnnotationValue(TypeElement typeElement, Class<?> annotationClass, String annotationParameter) {
  97. AnnotationMirror annotationMirror = getAnnotationMirror(typeElement, annotationClass);
  98. return annotationMirror != null ? getAnnotationValue(annotationMirror, annotationParameter) : null;
  99. }
  100. /**
  101. * Uses {@link Class#getSimpleName()} to convert from {@link Class} to {@link String}.
  102. */
  103. private static List<String> toString(List<Class<?>> classes) {
  104. List<String> list = new ArrayList<>();
  105. for (Class<?> aClass : classes) {
  106. list.add(aClass.getSimpleName());
  107. }
  108. return list;
  109. }
  110. }