2

I would like to write some external documentation to a class I have been writing. I would like to give a concise list of available methods, basically corresponding to the "Structure" view given by IntelliJ. I can even copy&paste from the view, however only the method names will be copied without the method signature or the return value.

Is there any quick way to create a Structure overview of a class for documentation purposes from IntelliJ?

Thanks!

EDIT: It seems I haven't made myself fully clear and I apologize for that. To be concrete, I would like to document the following class: https://github.com/JULIELab/jcore-base/blob/2.3.0-SNAPSHOT/jcore-utilities/src/main/java/de/julielab/jcore/utility/JCoReAnnotationTools.java

The output format I seek is a text file that should look like as follows: My wished output but in plain text

This is a screenshot from IntelliJ's "Structure" Panel that has also been pointed out by @Tiru. What is the easiest way to get this exact information as plain text?

JavaDoc is a possibility but is rather verbose in comparison, requiring quite some postprocessing. Since IntelliJ is already creating this kind of concise overview, I hoped there would be a possibility to extract it to text.

Makoto
  • 104,088
  • 27
  • 192
  • 230
khituras
  • 1,081
  • 10
  • 25
  • 1
    From what you say, if I were you I would generate a javadoc : https://www.jetbrains.com/help/idea/generating-javadoc-reference-for-a-project.html Does it corresponds to what you want ? – Thibault D. Jul 09 '18 at 09:42
  • Thank you for your comment. Please see my updated answer. – khituras Jul 09 '18 at 13:14
  • 1
    Just a little FYI: you're not supposed to add answers to your question. Accepting the one that helped the most is sufficient. If you have *more* to add then you can post your own answer. – Makoto Jul 09 '18 at 15:49

3 Answers3

0

Yes, you can get the list of method easily with a shortcut (cmd + F12 / ctrl + F12). This will give you the list of methods and class variables. You can select and use it in your document.

For example:

enter image description here

Another way is through Structure in the Side pane:

enter image description here

Or through diagrams:

enter image description here

Sample diagram:

enter image description here

Thiru
  • 2,541
  • 4
  • 25
  • 39
  • Thank you for your answer. However, I would like to get the information in plain text. Not just within IntelliJ or as a screenshot. – khituras Jul 09 '18 at 13:15
  • You can select the method name and press (cmd +c or ctrl +c), it will copy to your clipboard. – Thiru Jul 09 '18 at 13:20
  • Yes, I described that in my original question. But it will only copy the method names, not the signature or return value. Do you know any way to get this additional information? – khituras Jul 09 '18 at 14:39
  • I’m afraid that I have say no. Sorry! – Thiru Jul 10 '18 at 07:27
0

You could do this yourself via reflection. I made a little start. It's easy to write this to a file. I'll leave that as an exercise for the reader.

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.stream.Collectors;

class Scratch {
    public static void main(String[] args) {
        StringBuilder stringMethods = findMethods(String.class);
        System.out.println(stringMethods);
    }

    private static StringBuilder findMethods(Class clas) {
        StringBuilder builder = new StringBuilder();

        Method[] methods = clas.getMethods();
        for (Method method : methods) {
            builder.append(" ")
                   .append(method.getName())
                   .append("(");

            Class<?>[] parameterTypes = method.getParameterTypes();
            for (int i = 0; i < parameterTypes.length; i++) {
                Class<?> aClass = parameterTypes[i];
                builder.append(aClass.getSimpleName());
                if (i < parameterTypes.length - 1) {
                    builder.append(", ");
                }
            }

            builder.append(")");

            builder.append(": ")
                   .append(method.getReturnType().equals(Void.class) ? "void" : method.getReturnType().getSimpleName())
                   .append(" ");

            if (method.getExceptionTypes().length > 0) {
                builder.append(" throws ")
                       .append(Arrays.stream(method.getExceptionTypes()).map(Class::getSimpleName).collect(Collectors.joining(", ")));
            }

            builder.append(System.lineSeparator());

        }
        return builder;

    }

}
SurfMan
  • 1,685
  • 12
  • 19
  • Ha, the answer even got a downvote and still I accept it. I accept it for the reason that it currently seems to be the sole possibility to create the output I want (with slight changes, see the updated question). Of course, I would have wished for some built-in solutation from the IDE, but I still don't know about one. – khituras Jul 09 '18 at 15:24
  • Since Makoto - correctly - removed my exact solution from my question: I ended up using this code with two changes: I only output the declared methods and I removed the exceptions. I will keep this as the accepted answer because it actually answers my question. However, Makoto also correctly noted that my work will most likely be more efficient and effective in the long run if I just stick to JavaDoc generation. – khituras Jul 09 '18 at 15:58
0

I hate to be the one to have to say this, but...

Javadoc is the only portable thing that will reliably generate what you're looking for.

The big thing to note is that you don't have to write any Javadoc yourself; you can just invoke the tool and it will generate what you want.

In newer versions of Java, the generated Javadoc is searchable as well.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • You're right as far as the automatic generation of API documentation is concerned. However, I am writing a short overview of multiple classes and JavaDoc would just be too much. I want an overview of methods and explanation in the project's README.md file. – khituras Jul 09 '18 at 15:26
  • @khituras: I respectfully disagree. Since you plan to document your code in *some* capacity, using the standard way to document it makes the most practical sense. It's also the least typing-intensive, since at this rate, you're going to have to type out all of those classes on your own. No other tool really exists to give you what you're looking for. – Makoto Jul 09 '18 at 15:30
  • My first answer to this comment was insisting on my original view. Thinking more about it, I changed my mind. Saying: JavaDoc really would give (almost) everything I am doing right now, just not in the README of the project. However, it would move the documentation of classes where they belong - into the classes - and make the documentation generation process easier. Thank you for this remark. However now I will need to think about how to make the JavaDoc easily available to the users. – khituras Jul 09 '18 at 15:56