@Override
  public List<EventSubscriber<?>> subscribe(final Object o) {
    List<EventSubscriber<?>> subscribers = Collections.emptyList();
    final List<Method> eventHandlers =
        ClassUtils.getAnnotatedMethods(o.getClass(), EventHandler.class);

    if (!eventHandlers.isEmpty()) {
      subscribers = new ArrayList<EventSubscriber<?>>();
      for (final Method m : eventHandlers) {
        // verify that the event handler method is valid
        final Class<? extends SciJavaEvent> eventClass = getEventClass(m);
        if (eventClass == null) {
          log.warn("Invalid EventHandler method: " + m);
          continue;
        }

        // verify that the event handler key isn't already claimed
        final String key = m.getAnnotation(EventHandler.class).key();
        if (!key.isEmpty()) {
          synchronized (keys) {
            if (keys.contains(key)) continue;
            keys.add(key);
          }
        }

        // subscribe the event handler
        subscribers.add(subscribe(eventClass, o, m));
      }
    }
    return subscribers;
  }
  @Override
  public int compareTo(final Prioritized that) {
    if (that == null) return 1;

    // compare priorities
    final int priorityCompare = Priority.compare(this, that);
    if (priorityCompare != 0) return priorityCompare;

    // compare classes
    return ClassUtils.compare(getClass(), that.getClass());
  }
Beispiel #3
0
 private String details(final ModuleInfo info) {
   if (info == null) return "<null>";
   String className, classLocation;
   try {
     final Class<?> c = info.loadDelegateClass();
     className = c.getName();
     classLocation = ClassUtils.getLocation(c).toString();
   } catch (final ClassNotFoundException exc) {
     className = info.getDelegateClassName();
     classLocation = "<invalid>";
   }
   return info.getMenuPath() + " : " + className + " [" + classLocation + "]";
 }
Beispiel #4
0
 /**
  * Gets the URL of the icon associated with this node's {@link MenuEntry}.
  *
  * @see org.scijava.plugin.PluginInfo#getIconURL()
  */
 public URL getIconURL() {
   if (menuEntry == null) return null;
   String iconPath = menuEntry.getIconPath();
   if (iconPath == null || iconPath.isEmpty()) {
     if (isLeaf()) iconPath = DEFAULT_ICON_PATH;
     else return null;
   }
   final String className = moduleInfo.getDelegateClassName();
   try {
     final Class<?> c = ClassUtils.loadClass(className, false);
     final URL iconURL = c.getResource(iconPath);
     if (iconURL == null) {
       if (log != null) log.error("Could not load icon: " + iconPath);
     }
     return iconURL;
   } catch (final IllegalArgumentException exc) {
     final String message = "Could not load icon for class: " + className;
     if (log.isDebug()) log.debug(message, exc);
     else log.error(message);
     return null;
   }
 }
  /**
   * Checks the {@link ObjectService} for a single registered {@link Module} instance of the given
   * {@link ModuleInfo}. In this way, if you want to repeatedly reuse the same module instance
   * instead of creating a new one with each execution, you can register it with the {@link
   * ObjectService} and it will be automatically returned by the {@link #createModule(ModuleInfo)}
   * method (and hence automatically used whenever a {@link #run} method with {@link ModuleInfo}
   * argument is called).
   */
  private Module getRegisteredModuleInstance(final ModuleInfo info) {
    final Class<?> type = ClassUtils.loadClass(info.getDelegateClassName());
    if (type == null || !Module.class.isAssignableFrom(type)) return null;

    // the module metadata's delegate class extends Module, so there is hope
    @SuppressWarnings("unchecked")
    final Class<? extends Module> moduleType = (Class<? extends Module>) type;

    // ask the object service for an instance of the delegate type
    final List<? extends Module> objects = objectService.getObjects(moduleType);
    if (objects == null || objects.isEmpty()) {
      // the object service has no such instances
      return null;
    }
    if (objects.size() > 1) {
      // there are multiple instances; it's not clear which one to use
      log.warn("Ignoring multiple candidate module instances for class: " + type.getName());
      return null;
    }
    // found exactly one instance; return it!
    return objects.get(0);
  }
Beispiel #6
0
 @Override
 public void setOutput(final String name, final Object value) {
   final CommandModuleItem<?> item = info.getOutput(name);
   ClassUtils.setValue(item.getField(), command, value);
 }
Beispiel #7
0
 @Override
 public Object getOutput(final String name) {
   final CommandModuleItem<?> item = info.getOutput(name);
   return ClassUtils.getValue(item.getField(), command);
 }