Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » How to retrieve the Javadoc from a IMethod via AST?
How to retrieve the Javadoc from a IMethod via AST? [message #220020] Sat, 03 December 2005 19:51 Go to next message
Andreas Herz is currently offline Andreas HerzFriend
Messages: 196
Registered: July 2009
Senior Member
Hi

I need the javadoc for additional information in my
(yet another) GUI Designer.

Is the a fast way availabe?!

Additional Info:

- java source code is in the current project
- xurrent project has java nature.


Greetings

Andreas


=============================
My current *wrong* way
=============================

String comment ="";
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(method.getCompilationUnit());
CompilationUnit result = (CompilationUnit) parser.createAST(null);

if(result.getCommentList().size()>0)
{
Object obj= result.getCommentList().get(0);
if(obj instanceof Javadoc)
{
Javadoc jd = (Javadoc)obj;
List tags = jd.tags();
Iterator iter = tags.iterator();
while(iter.hasNext())
{
TagElement tag = (TagElement)iter.next();
Iterator fragIter =tag.fragments().iterator();
while(fragIter.hasNext())
{
Object frag = fragIter.next();
if(frag instanceof TextElement)
{
TextElement text = (TextElement)frag;
comment = comment + text.getText()+"\n";
}
}
}
}
}
return comment;
Re: How to retrieve the Javadoc from a IMethod via AST? [message #220033 is a reply to message #220020] Sun, 04 December 2005 18:25 Go to previous message
Frederic Fusier is currently offline Frederic FusierFriend
Messages: 147
Registered: July 2009
Senior Member
Be careful, the code you've written just store string for the first
javadoc in the list. You're not sure this javadoc is associated with
the method...
So, I would advice, first to retrive the method declaration and then
get its javadoc using getJavadoc() method...

Let say that your method is the first one of the only type in your CU,
you can get text of you comment as follows (looking at your snippet
I assumed this was the case...):

ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(method.getCompilationUnit());
CompilationUnit unit = (CompilationUnit) parser.createAST(null);
AbstractTypeDeclaration typeDeclaration = (AbstractTypeDeclaration)unit.types().get(0);
MethodDeclaration methodDeclaration = (MethodDeclaration) typeDeclaration.bodyDeclarations().get(0);
String comment = methodDeclaration.getJavadoc().toString();

You can also retrieve the method declaration in your AST nodes tree using
start position if you do not know its index in type body declarations list.

HTH

FreeGroup wrote:
> Hi
>
> I need the javadoc for additional information in my
> (yet another) GUI Designer.
>
> Is the a fast way availabe?!
>
> Additional Info:
>
> - java source code is in the current project
> - xurrent project has java nature.
>
>
> Greetings
>
> Andreas
>
>
> =============================
> My current *wrong* way
> =============================
>
> String comment ="";
> ASTParser parser = ASTParser.newParser(AST.JLS3);
> parser.setSource(method.getCompilationUnit());
> CompilationUnit result = (CompilationUnit) parser.createAST(null);
>
> if(result.getCommentList().size()>0)
> {
> Object obj= result.getCommentList().get(0);
> if(obj instanceof Javadoc)
> {
> Javadoc jd = (Javadoc)obj;
> List tags = jd.tags();
> Iterator iter = tags.iterator();
> while(iter.hasNext())
> {
> TagElement tag = (TagElement)iter.next();
> Iterator fragIter =tag.fragments().iterator();
> while(fragIter.hasNext())
> {
> Object frag = fragIter.next();
> if(frag instanceof TextElement)
> {
> TextElement text = (TextElement)frag;
> comment = comment + text.getText()+"\n";
> }
> }
> }
> }
> }
> return comment;
Previous Topic:could not connect to debugger at localhost:32897
Next Topic:Syntax Coloring
Goto Forum:
  


Current Time: Fri Apr 26 18:26:42 GMT 2024

Powered by FUDForum. Page generated in 0.03086 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top