46

Consider a static method in a class, which I have documented it using javadoc:

/**
 * Description here.
 *
 * @param names       - The parameters of the impression request.
 * @param ids         - An intent object to enrich.
 * @param prefix - A prefix.
 */

public static void parse(Map<String, String> names, String ids, String prefix)
    ...

In order to avoid duplicating the description in the overloaded versions of the method, I would like to use a javadoc @link:

 /**
 * Overloaded version with default prefix.
 * {@link #<parse(Map<String, String>, String, String)> [Text]}
 */

public static void parse(Map<String, String> names, String ids, String prefix)

Which gives the following warning:

@link:illegal character: "60" in "#parseBtCategories(Map<String, String>, 
                                                     String, String) Text"

ASCII 60 is <, which is a part of the method signature. It works with Map, String, String) nut this notation can't distinguish two different types of maps.

This seems to be a known bug. Is there a good workaround?

1
  • Just to make sure: Are you really using {@link#<parse with the < before parse? Is this a new syntax added lately? Commented Mar 3, 2012 at 17:11

3 Answers 3

31

Similar to David Conrad solution, you can use the full signature as a link description, using syntax:

{@link class#method(signature) text-to-display}

Remember to escape < and >. For example:

 {@link #parse(Map, String, String) parse(Map&lt;String, String&gt;, String, String)}
1
  • And how should this document the return type?
    – Lawrence
    Commented Oct 19, 2015 at 14:45
27

The parameterized types are NOT part of the method's signature.

Java implements Generics with Type Erasure. The concept of Type Erasure is that the generic types are only available at compile time, at which point they are "erased"; meaning they are stripped from the class's bytecode. Thus they are not accessible at runtime and are not part of method's signature.

So, there's no real reason for them to be part of the Javadoc link's signature, because you cannot overload two methods with generic types that resolve to the same raw types: there cannot be an ambiguity on the generic types in your source's signature.

Additionally, Javadoc supports HTML tags and I assume this could be another reason why it bites the dust here, but I really doubt the Javadoc processing tool was this badly implemented.

9
  • 51
    I don't think that argument holds water. Just because the compiler implements generics with type erasure doesn't preclude the documentation from including generic type parameters. Generics and documentation are for people, but bytecode is for the JVM. Type erasure falls more strongly on the side of the JVM, so it isn't germane here. Commented Mar 2, 2012 at 23:34
  • 36
    In a case I ran into, it was describing the type of a parameter, and the link looked like {@link List<Customer>}. If you erase that, the javadoc ends up simply describing the parameter as a List. That isn't informative to the caller. Why make it a link at all? So they can click on it. In that case, a good solution is {@link List}&lt;{@link Customer}&gt;, which lets them click on either the link to List or to Customer, depending on what they want to see. Commented Mar 27, 2013 at 22:46
  • 3
    The purpose of java doc is to offer data about WHAT is expected during the design process. Therefor, it is also my strong opinion that Javadoc should allow the generics, to offer the developer all information there is to know about what to expect back or what to offer as arguments.
    – Lawrence
    Commented Mar 18, 2015 at 13:30
  • 4
    The first two paragraphs need additional qualification. Parameterized types are part of a method's signature at compile time. During compile time, type erasure maps signatures with parameterized types to signatures with raw types. See for example JLS 4.6: Type Erasure. Commented Dec 9, 2015 at 20:15
  • 1
    I tend to work around the problem by doing something like this: {@link EventHandler}{@code <}{@link MouseEvent}{@code >} Commented Jun 13, 2017 at 19:09
3

It may not be what you're looking for, but I have learnt to live with something like * @return {@link List} of {@link RfRequestSummaryDto}

1
  • I've seen couple libraries and frameworks do it like this. Seems like a convention at this point.
    – Dragas
    Commented Dec 16, 2019 at 13:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.