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.

ExtensionVisitor.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.AnnotationVisitor;
  18. import org.objectweb.asm.ClassVisitor;
  19. import org.objectweb.asm.Opcodes;
  20. import org.objectweb.asm.Type;
  21. import org.pf4j.Extension;
  22. import java.util.Arrays;
  23. /**
  24. * This visitor extracts an {@link ExtensionInfo} from any class,
  25. * that holds an {@link Extension} annotation.
  26. * <p>
  27. * The annotation parameters are extracted from byte code by using the
  28. * <a href="https://asm.ow2.io/">ASM library</a>. This makes it possible to
  29. * access the {@link Extension} parameters without loading the class into
  30. * the class loader. This avoids possible {@link NoClassDefFoundError}'s
  31. * for extensions, that can't be loaded due to missing dependencies.
  32. *
  33. * @author Andreas Rudolph
  34. * @author Decebal Suiu
  35. */
  36. class ExtensionVisitor extends ClassVisitor {
  37. //private static final Logger log = LoggerFactory.getLogger(ExtensionVisitor.class);
  38. private static final int ASM_VERSION = Opcodes.ASM7;
  39. private final ExtensionInfo extensionInfo;
  40. ExtensionVisitor(ExtensionInfo extensionInfo) {
  41. super(ASM_VERSION);
  42. this.extensionInfo = extensionInfo;
  43. }
  44. @Override
  45. public AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
  46. //if (!descriptor.equals("Lorg/pf4j/Extension;")) {
  47. if (!Type.getType(descriptor).getClassName().equals(Extension.class.getName())) {
  48. return super.visitAnnotation(descriptor, visible);
  49. }
  50. return new AnnotationVisitor(ASM_VERSION) {
  51. @Override
  52. public AnnotationVisitor visitArray(final String name) {
  53. if ("ordinal".equals(name) || "plugins".equals(name) || "points".equals(name)) {
  54. return new AnnotationVisitor(ASM_VERSION, super.visitArray(name)) {
  55. @Override
  56. public void visit(String key, Object value) {
  57. //log.debug("Load annotation attribute {} = {} ({})", name, value, value.getClass().getName());
  58. if ("ordinal".equals(name)) {
  59. extensionInfo.ordinal = Integer.parseInt(value.toString());
  60. } else if ("plugins".equals(name)) {
  61. if (value instanceof String) {
  62. //log.debug("found plugin " + value);
  63. extensionInfo.plugins.add((String) value);
  64. } else if (value instanceof String[]) {
  65. //log.debug("found plugins " + Arrays.toString((String[]) value));
  66. extensionInfo.plugins.addAll(Arrays.asList((String[]) value));
  67. } else {
  68. //log.debug("found plugin " + value.toString());
  69. extensionInfo.plugins.add(value.toString());
  70. }
  71. } else if ("points".equals(name)) {
  72. String pointClassName = ((Type) value).getClassName();
  73. //log.debug("found point " + pointClassName);
  74. extensionInfo.points.add(pointClassName);
  75. }
  76. super.visit(key, value);
  77. }
  78. };
  79. }
  80. return super.visitArray(name);
  81. }
  82. };
  83. }
  84. }