Ejemplo n.º 1
0
  /**
   * Returns methods implemented by a particular interface.
   *
   * @param interfaceName
   * @return
   */
  public static List<Method> getInterfaceMethods(final String interfaceName) {
    final List<Method> result = Lists.newArrayList();

    try {
      final Class<?> interfaceClass = Class.forName(interfaceName);

      // add interface methods to the list
      for (final Method method : interfaceClass.getMethods()) {
        result.add(method);
      }
    } catch (final ClassNotFoundException cnfe) {
      // just emit a warning and return an empty list
      System.err.println("warning: unable to get class for interface " + interfaceName);
    }

    return result;
  }
Ejemplo n.º 2
0
  /**
   * Returns set of all effective interfaces implied by a given interface. This set is constructed
   * by traversing the interface inheritance hierarchy from the given interface to the root(s) of
   * the hierarchy, collecting all interfaces along the way, including the starting interface.
   *
   * @param itfClass interface class where the traversal should start
   * @return set of all effective interfaces implied by the given interface
   */
  public static Set<Class<?>> getEffectiveInterfaces(final Class<?> itfClass) {
    //
    // Traverse the hierarchy in a breadth-first manner. Add newly visited
    // interfaces to the result set and their super interfaces to the queue
    // of interfaces to visit.
    //
    final Set<Class<?>> result = Sets.newHashSet();

    final Queue<Class<?>> queue = Lists.newLinkedList();
    queue.add(itfClass);

    while (!queue.isEmpty()) {
      final Class<?> itf = queue.poll();

      result.add(itf);
      for (final Class<?> superItf : itf.getInterfaces()) {
        if (!result.contains(superItf)) {
          queue.add(superItf);
        }
      }
    }

    return result;
  }