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.

ExtensionInfo.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. super();
  41. this.className = className;
  42. }
  43. /**
  44. * Get the name of the class, for which extension info was created.
  45. *
  46. * @return absolute class name
  47. */
  48. public String getClassName() {
  49. return className;
  50. }
  51. /**
  52. * Get the {@link Extension#ordinal()} value, that was assigned to the extension.
  53. *
  54. * @return ordinal value
  55. */
  56. public int getOrdinal() {
  57. return ordinal;
  58. }
  59. /**
  60. * Get the {@link Extension#plugins()} value, that was assigned to the extension.
  61. *
  62. * @return ordinal value
  63. */
  64. public List<String> getPlugins() {
  65. return Collections.unmodifiableList(plugins);
  66. }
  67. /**
  68. * Get the {@link Extension#points()} value, that was assigned to the extension.
  69. *
  70. * @return ordinal value
  71. */
  72. public List<String> getPoints() {
  73. return Collections.unmodifiableList(points);
  74. }
  75. /**
  76. * Load an {@link ExtensionInfo} for a certain class.
  77. *
  78. * @param className absolute class name
  79. * @param classLoader class loader to access the class
  80. * @return the {@link ExtensionInfo}, if the class was annotated with an {@link Extension}, otherwise null
  81. */
  82. public static ExtensionInfo load(String className, ClassLoader classLoader) {
  83. try (InputStream input = classLoader.getResourceAsStream(className.replace('.', '/') + ".class")) {
  84. ExtensionInfo info = new ExtensionInfo(className);
  85. new ClassReader(input).accept(new ExtensionVisitor(info), ClassReader.SKIP_DEBUG);
  86. return info;
  87. } catch (IOException e) {
  88. log.error(e.getMessage(), e);
  89. return null;
  90. }
  91. }
  92. }