0

免责声明:我只是一个低级的练习生,所以如果我犯了一些初级错误,请原谅我:(

我正在编写一个自动 API 生成器,并且这些类需要 JavaDoc 和注释,因为 API 包含的某些值不应该写在 JavaDoc 中(例如 exampleResponse)。

但是,似乎各个方法上方的注释替换了 Javadoc,所以当我想从 JavaDoc 中获取描述时(我想这样做,我不必在注释中再次编写它),我有一个问题。

使用 getJavadoc() 总是返回 null。我也尝试使用 getOrphanComments(),但它返回 null。我误解了文档吗?我假设如果我在一个方法上方写了两条评论,那么最上面的一条会移动到该方法的 orphanComments 中。

有没有办法解决这个问题?

4

2 回答 2

0

MethodDeclaration对象method 比你可以使用的 java doc

if( method.hasComment() && method.getComment() instanceof JavadocComment ){
    JavadocComment javaDoc = (JavadocComment)method.getComment();
    // now you can get the content using 
    String content = javaDoc.getContent();
}
于 2016-03-23T05:01:13.887 回答
0

对于以下类型:

public final String             name;
public final String             signature;
public final String             returnType;      
public final Type               returnFullType; // com.github.javaparser.ast.type.Type
public final String             body;
public final String[]           modifiers;
public final String[]           parameterNames;
public final String[]           parameterTypes;
public final Type[]             parameterFullTypes; // com.github.javaparser.ast.type.Type
public final String[]           exceptions;
public final String             jdComment;
public final MethodDeclaration  nativeJP_API_reference; // com.github.javaparser.ast.body.MethodDeclaration

Method这是我自己的混合物类的构造函数:

Method (MethodDeclaration md)
{
    NodeList<Modifier>      ml  = md.getModifiers();
    NodeList<Parameter>     pl  = md.getParameters();
    NodeList<ReferenceType> te  = md.getThrownExceptions();

    this.nativeJP_API_reference = md;
    this.name                   = md.getNameAsString();
    this.signature              = md.getDeclarationAsString();
    this.returnType             = md.getType().toString();
    this.returnFullType         = md.getType();
    this.body                   = md.getBody().isPresent() ? md.getBody().get().toString() : null;  // In ConstructorDeclaration, this is an Optional<BlockStmt>, not here!
    this.modifiers              = new String[ml.size()];
    this.parameterNames         = new String[pl.size()];
    this.parameterTypes         = new String[pl.size()];
    this.parameterFullTypes     = new com.github.javaparser.ast.type.Type[pl.size()];
    this.exceptions             = new String[te.size()];
    this.jdComment              = md.hasJavaDocComment() ? md.getJavadocComment().get().toString() : null;

    int i = 0;
    for (Parameter p : pl)
    {
        parameterNames[i]       = p.getName().toString();
        parameterTypes[i]       = p.getType().toString();
        parameterFullTypes[i]   = p.getType();
        i++;
    }
    i = 0;
    for (Modifier m : ml)       modifiers[i++]  = m.toString();
    i = 0;
    for (ReferenceType r : te)  exceptions[i++] = r.toString();;
}
于 2019-10-19T03:38:07.700 回答