Doclet - Get Generics List

I am writing a doclet extending com.sun.javadoc.Doclet.

When I want to document an ArrayList as a method field, I want to get a generic type (for example, when documenting an ArrayList<String> I want to get information that this is a list containing strings).

I can only get information that this is an ArrayList. Even when called

 ParameterizedType paramType = fieldType.asParameterizedType(); 

I get java.util.ArrayList and not a generic type.

Does anyone know how to get a generic list type?

Here is a sample code:

 FieldDoc[] fields = classDoc.fields(); for (int k = 0; k < fields.length; k++) { Type fieldType = fields[k].type(); if (fieldType.asParameterizedType() != null) { ParameterizedType paramType = fieldType.asParameterizedType(); String qualiName = paramType.qualifiedTypeName(); // this equals "java.util.ArrayList" fieldType.asParameterizedType().toString(); // this equals "java.util.ArrayList" fieldType.asParameterizedType().typeName(); // this equals "ArrayList" } } 
+7
source share
4 answers

I ran generics that got lost in types when writing a custom doclet in Java 1.6. I was able to fix this by adding the method below to my doclet.

 /** * NOTE: Without this method present and returning LanguageVersion.JAVA_1_5, * Javadoc will not process generics because it assumes LanguageVersion.JAVA_1_1 * @return language version (hard coded to LanguageVersion.JAVA_1_5) */ public static LanguageVersion languageVersion() { return LanguageVersion.JAVA_1_5; } 
+21
source

Could it be that the output is java.util.ArrayList<String> and you do not see the <String> , since your browser interprets it as an unknown HTML tag?

You will need to exit < for HTML output.

If this is not a problem, just show the code so that we can reproduce the problem.


After looking at your example, I can reproduce it using this document:

 package de.fencing_game.paul.examples.doclet; import com.sun.javadoc.*; import java.util.ArrayList; /** * A test doclet to see how generic fields work. * * Inspired by the question <a href="http://stackoverflow.com/q/5731619/600500">Doclet- Get generics of a list</a> on Stackoverflow. */ public class GenericTestDoclet<Y> extends Doclet { public ArrayList<? extends Y> stringList; /** * Erstellt ein(e(n)) neu(e(n)) <code>GenericTestDoclet</code>. * */ public GenericTestDoclet() { } public ArrayList<? extends Y> getList() { return stringList; } public void printType(Type fieldType, DocErrorReporter err) { err.printNotice("type: " + fieldType); if (fieldType.asParameterizedType() != null) { ParameterizedType paramType = fieldType.asParameterizedType(); err.printNotice("paramType:" + paramType); String qualiName = paramType.qualifiedTypeName(); err.printNotice("qualiName: " + qualiName); String typeName = fieldType.asParameterizedType().typeName(); err.printNotice("typeName: " + typeName); Type[] parameters = paramType.typeArguments(); err.printNotice("parameters.length: " + parameters.length); for(Type p : parameters) { err.printNotice("param: " + p); } } err.printNotice(""); } public void listFields(ClassDoc classDoc, DocErrorReporter err) { FieldDoc[] fields = classDoc.fields(); for (int k = 0; k < fields.length; k++) { err.printNotice("field: " + fields[k]); Type fieldType = fields[k].type(); printType(fieldType, err); } } public void listMethods(ClassDoc classDoc, DocErrorReporter err) { MethodDoc[] methods = classDoc.methods(); for (int k = 0; k < methods.length; k++) { err.printNotice("method: " + methods[k]); Type returnType = methods[k].returnType(); printType(returnType, err); } } /** * The entry point of the doclet. * @return true if all the included elements have enough documentation, * false if some documentation is missing. */ public static boolean start(RootDoc root) { GenericTestDoclet<?> d = new GenericTestDoclet<Integer>(); for(ClassDoc clazz : root.classes()) { d.listFields(clazz, root); d.listMethods(clazz, root); } return true; } } 

Exit, if applied by itself:

 field: de.fencing_game.paul.examples.doclet.GenericTestDoclet.stringList type: java.util.ArrayList paramType:java.util.ArrayList qualiName: java.util.ArrayList typeName: ArrayList parameters.length: 0 method: de.fencing_game.paul.examples.doclet.GenericTestDoclet.getList() type: java.util.ArrayList paramType:java.util.ArrayList qualiName: java.util.ArrayList typeName: ArrayList parameters.length: 0 [more methods omitted] 

This shows that there can also be light-hearted return types.

I wanted to say that I already used this method to print the type name recursively , but this shows that my LaTeX-Doclet is not actually even using the recursive method that I created (instead, the types are printed from the type compiler tree ).

But one way or another, this should be possible, because somehow the Standard Doclet tunes to create the correct output .

+1
source

After enabling Java 1.5 mode with Trex response, you can access the generic fieldType types with

 Type[] typeArguments = fieldType.asParameterizedType().typeArguments() 
+1
source

Assuming ArrayList is a member field of your class, it looks like this: answer will provide you with what you need.

A snippet of code from this answer shows a general idea (but look at this question / answer):

 Field stringListField = Test.class.getDeclaredField("stringList"); ParameterizedType stringListType = (ParameterizedType) stringListField.getGenericType(); Class<?> stringListClass = (Class<?>) stringListType.getActualTypeArguments()[0]; System.out.println(stringListClass); // class java.lang.String. 
-2
source

All Articles