Example #1
0
  /**
   * For a given elment find the root of the tree, start JavaDoc processing for the whole tree and
   * cache all results.
   *
   * @param element any Java element of an tree
   */
  private void cache(IJavaElement element) throws ConQATException {

    IResource rootNode = ResourceTraversalUtils.returnRoot(element);

    Context context = new Context();

    PrintWriter errorWriter =
        new PrintWriter(new Stream2LoggerAdapter(LOGGER, Level.DEBUG, "JavaDoc Error"));
    PrintWriter warningWriter =
        new PrintWriter(new Stream2LoggerAdapter(LOGGER, Level.DEBUG, "JavaDoc Warning"));

    // do not store info messages
    PrintWriter infoWriter = new PrintWriter(new NullOutputStream());

    // This is correct, as the messager attaches itself to the context.
    new SimpleMessager(context, errorWriter, warningWriter, infoWriter);

    JavadocTool tool = JavadocTool.make0(context);

    ModifierFilter showAccess = new ModifierFilter(ModifierFilter.ALL_ACCESS);
    String encoding = determineEncoding(rootNode);
    String docLocale = StringUtils.EMPTY_STRING;
    boolean breakiterator = false;
    ListBuffer<String[]> options = new ListBuffer<String[]>();
    ListBuffer<String> includedElements = addAllChildren(rootNode);
    boolean docClasses = false;
    ListBuffer<String> subPackages = new ListBuffer<String>();
    ListBuffer<String> excludedPackages = new ListBuffer<String>();
    boolean quiet = false;

    try {
      RootDocImpl rootDoc =
          tool.getRootDocImpl(
              docLocale,
              encoding,
              showAccess,
              includedElements.toList(),
              options.toList(),
              breakiterator,
              subPackages.toList(),
              excludedPackages.toList(),
              docClasses,
              false,
              quiet);

      if (rootDoc == null) {
        throw new ConQATException("Could not analyze JavaDoc for " + rootNode);
      }

      Map<String, IJavaResource> classLookup =
          TraversalUtils.createIdToNodeMap((IJavaResource) rootNode);
      ClassDoc[] classes = rootDoc.classes();
      for (ClassDoc doc : classes) {
        IJavaResource tmpElement = classLookup.get(doc.qualifiedName());
        if (tmpElement instanceof IJavaElement) {
          cache.put(((IJavaElement) tmpElement).getUniformPath(), doc);
        }
      }
    } catch (Throwable ex) {
      // The dreaded JavaDoc implementation may throw all kinds of stuff,
      // including Errors. Hence, we catch throwable here. Additionally,
      // we minimally support debugging by extracting a somewhat
      // reasonable message.

      String message = ex.getMessage();
      if (message == null) {
        message = ex.getClass().getName();
        message += StringUtils.obtainStackTrace(ex);
      }

      throw new ConQATException(message, ex);
    } finally {
      errorWriter.close();
      warningWriter.close();
      infoWriter.close();
    }
  }