Java源码示例:org.pf4j.Extension

示例1
private void checkServiceModules(String pluginId, List<Class<? extends ServiceModule>> extensions)
    throws ServiceLoadingException {
  int numServiceModules = extensions.size();
  if (numServiceModules == 1) {
    return;
  }
  String message;
  if (numServiceModules == 0) {
    message = String.format("A plugin (%s) must provide exactly one service module as "
            + "an extension, but no modules found.%nCheck that your %s implementation "
            + "is annotated with @%s",
        pluginId, ServiceModule.class.getSimpleName(), Extension.class.getSimpleName());
  } else {
    message = String.format("A plugin (%s) must provide exactly one service module as "
            + "an extension, but %d modules found:%n%s.%nMultiple modules are not currently "
            + "supported, but please let us know if you need them.",
        pluginId, numServiceModules, extensions);
  }
  throw new ServiceLoadingException(message);
}
 
示例2
private <T> ExtensionWrapper<T> createExtensionWrapper(T extension) {
  int ordinal = 0;
  Class<?> extensionClass = extension.getClass();
  if (extensionClass.isAnnotationPresent(Extension.class)) {
    ordinal = extensionClass.getAnnotation(Extension.class).ordinal();
  }
  ExtensionDescriptor descriptor = new ExtensionDescriptor(ordinal, extensionClass);
  return new ExtensionWrapper<T>(descriptor, (cls) -> extension);
}