获取注释内非-JavaDoc标签的文本,使用Spoon。

huangapple go评论81阅读模式
英文:

Getting text of non-Javadoc tag inside doc comment with Spoon

问题

我想要使用inria-spoon来处理带有自定义非Javadoc标签的Java源文件中的文档注释然而当Spoon处理文档注释时使用`CtMethod#getDocComment()`返回的`String`将会将非Javadoc标签的文本替换为`@unknown`。
我需要保留标签的原始文本以进行进一步的过滤和处理

下面的两个类展示了一个输出示例其中输出为

    @unknown some-value
    @return name of husband

而期望的输出是

    @abc:xyz some-value
    @return name of husband

具有主方法以运行的类是`Spooner`,要处理的源代码是`Carrie.java`。

`Spooner.java`:

    package tryspoon;
    
    import spoon.Launcher;
    import spoon.reflect.declaration.CtMethod;
    import spoon.reflect.visitor.filter.TypeFilter;

    public class Spooner
    {
        private static final String SAMPLE_SOURCE_PATH = "full_local_path_to_source";


        public static void main(String[] args)
        {
            Launcher launcher = new Launcher();
            launcher.addInputResource(SAMPLE_SOURCE_PATH);
            launcher.buildModel();

            String firstDocComment = launcher.getModel()
              .getElements(new TypeFilter<>(CtMethod.class))
              .get(0).getDocComment();

            System.out.println("First Doc comment contents: " + firstDocComment);
    
        }
    }
    
`Carrie.java`:

    package tryspoon;
    
    public class Carrie {
      /**
       * @abc:xyz some-value
       * @return name of husband
       */
      public String husband()
      {
        return "Doug";
      }
    }
英文:

I would like to use inria-spoon to process Java source files having custom non-Javadoc tags inside
Doc comments. However, when Spoon processes Doc comments the String returned using CtMethod#getDocComment() will have replaced the text of non-Javadoc tags with @unknown.
I need to keep the original text of the tag for further filtering and processing.

The two classes below shows an example where the output is

@unknown some-value
@return name of husband

and desired is

@abc:xyz some-value
@return name of husband

Class with main method for running is Spooner, source being processed is Carrie.java.

Spooner.java:

package tryspoon;
import spoon.Launcher;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.visitor.filter.TypeFilter;
public class Spooner
{
private static final String SAMPLE_SOURCE_PATH = &quot;full_local_path_to_source&quot;;
public static void main(String[] args)
{
Launcher launcher = new Launcher();
launcher.addInputResource(SAMPLE_SOURCE_PATH);
launcher.buildModel();
String firstDocComment = launcher.getModel()
.getElements(new TypeFilter&lt;&gt;(CtMethod.class))
.get(0).getDocComment();
System.out.println(&quot;First Doc comment contents: &quot; + firstDocComment);
}
}

Carrie.java:

package tryspoon;
public class Carrie {
/**
* @abc:xyz some-value
* @return name of husband
*/
public String husband()
{
return &quot;Doug&quot;;
}
}

答案1

得分: 1

这将很快在主代码分支中得到修复,当https://github.com/INRIA/spoon/pull/3513合并时。

英文:

This will be soon be fixed in master when https://github.com/INRIA/spoon/pull/3513 is merged.

huangapple
  • 本文由 发表于 2020年9月9日 19:08:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63810364.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定