Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.asm;
  17. import org.objectweb.asm.ClassReader;
  18. import org.pf4j.Extension;
  19. import org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.util.ArrayList;
  24. import java.util.Collections;
  25. import java.util.List;
  26. /**
  27. * This class holds the parameters of an {@link org.pf4j.Extension}
  28. * annotation defined for a certain class.
  29. *
  30. * @author Andreas Rudolph
  31. * @author Decebal Suiu
  32. */
  33. public final class ExtensionInfo {
  34. private static final Logger log = LoggerFactory.getLogger(ExtensionInfo.class);
  35. private final String className;
  36. int ordinal = 0;
  37. List<String> plugins = new ArrayList<>();
  38. List<String> points = new ArrayList<>();
  39. private ExtensionInfo(String className) {
  40. this.className = className;
  41. }
  42. /**
  43. * Get the name of the class, for which extension info was created.
  44. *
  45. * @return absolute class name
  46. */
  47. public String getClassName() {
  48. return className;
  49. }
  50. /**
  51. * Get the {@link Extension#ordinal()} value, that was assigned to the extension.
  52. *
  53. * @return ordinal value
  54. */
  55. public int getOrdinal() {
  56. return ordinal;
  57. }
  58. /**
  59. * Get the {@link Extension#plugins()} value, that was assigned to the extension.
  60. *
  61. * @return ordinal value
  62. */
  63. public List<String> getPlugins() {
  64. return Collections.unmodifiableList(plugins);
  65. }
  66. /**
  67. * Get the {@link Extension#points()} value, that was assigned to the extension.
  68. *
  69. * @return ordinal value
  70. */
  71. public List<String> getPoints() {
  72. return Collections.unmodifiableList(points);
  73. }
  74. /**
  75. * Load an {@link ExtensionInfo} for a certain class.
  76. *
  77. * @param className absolute class name
  78. * @param classLoader class loader to access the class
  79. * @return the {@link ExtensionInfo}, if the class was annotated with an {@link Extension}, otherwise null
  80. */
  81. public static ExtensionInfo load(String className, ClassLoader classLoader) {
  82. try (InputStream input = classLoader.getResourceAsStream(className.replace('.', '/') + ".class")) {
  83. ExtensionInfo info = new ExtensionInfo(className);
  84. new ClassReader(input).accept(new ExtensionVisitor(info), ClassReader.SKIP_DEBUG);
  85. return info;
  86. } catch (IOException e) {
  87. log.error(e.getMessage(), e);
  88. return null;
  89. }
  90. }
  91. }