Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » ASTParser does not parse javadoc comments
icon5.gif  ASTParser does not parse javadoc comments [message #916821] Wed, 19 September 2012 14:08 Go to next message
Michael Doswald is currently offline Michael Doswald
Messages: 2
Registered: September 2012
Junior Member
Hi eclipse community,

I am using the eclipse java source code parser in jdt.core to parse some source files in our internal development tool. It's a maven plugin, so I use the jdt libraries in a standalone fashion (without eclipse).

I need to check the javadoc comments in the class, interface and enum declarations for some specific tags. Unfortunately, the ASTParser doesn't seem to parse the javadoc comments.

The following code shows a test method I implemented to narrow down the problem. Am I doing something wrong here?

@Test
public void testJavaDocParseFailure()
{
  StringWriter classWriter = new StringWriter();
  PrintWriter classStream = new PrintWriter( classWriter );
  classStream.println("package test;                       ");
  classStream.println("                                    ");
  classStream.println("/**                                 ");
  classStream.println(" * This is a test comment           ");
  classStream.println(" */                                 ");
  classStream.println("public class TestClass {}           ");
  
  ASTParser javaParser = ASTParser.newParser(AST.JLS4);
  javaParser.setEnvironment(null, null, null, true);
  javaParser.setStatementsRecovery(true);
  
  Map<String,String> compilerOptions = JavaCore.getDefaultOptions();
  compilerOptions.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_7);
  compilerOptions.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_7);
  compilerOptions.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_7);
  javaParser.setCompilerOptions(compilerOptions);    
  javaParser.setSource( classWriter.toString().toCharArray() );
  
  javaParser.createAST(null).accept( new ASTVisitor(true) {
    @Override
    public boolean visit(Javadoc node)
    {
      System.out.println( "Found javadoc: " + node );
      return true;
    }

    @Override
    public boolean visit(TypeDeclaration node)
    {
      System.out.println( "Found Type " + node.getName().getFullyQualifiedName() + " -> javadoc: " + node.getJavadoc() );
      return super.visit(node);
    }
  });
}


The only output from this code is:
Found Type TestClass -> javadoc: null


I use the following libraries (copied from an eclipse 3.8 install):

org.eclipse.jdt.core_3.8.1.v20120531-0637
org.eclipse.equinox.common_3.6.100.v20120522-1841
org.eclipse.core.resources_3.8.0.v20120522-2034
org.eclipse.core.jobs_3.5.200.v20120521-2346
org.eclipse.core.runtime_3.8.0.v20120521-2346
org.eclipse.osgi_3.8.0.v20120529-1548
org.eclipse.core.contenttype_3.4.200.v20120523-2004
org.eclipse.equinox.preferences_3.5.0.v20120522-1841


Does anybody know what I am doing wrong here?

Regards,
Michael


Re: ASTParser does not parse javadoc comments [message #917881 is a reply to message #916821] Thu, 20 September 2012 14:28 Go to previous messageGo to next message
Michael Rennie is currently offline Michael Rennie
Messages: 65
Registered: July 2009
Location: Canada
Member
When you are creating your parser you have to set the JavaCore.COMPILER_DOC_COMMENT_SUPPORT option to JavaCore.ENABLED and pass that in to the parser with a call to #setCompilerOptions(Map)

For example:

ASTParser parser = ASTParser.newParser(AST.JLS4);
Map options = JavaCore.getOptions();
options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED);
parser.setCompilerOptions(loptions);
Re: ASTParser does not parse javadoc comments [message #918154 is a reply to message #917881] Thu, 20 September 2012 19:57 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan Herrmann
Messages: 1023
Registered: July 2009
Senior Member
One thing puzzles me: COMPILER_DOC_COMMENT_SUPPORT should actually be enabled by default.
I wonder, if JavaCore.getDefaultOptions() is broken when used outside the IDE.
Re: ASTParser does not parse javadoc comments [message #921834 is a reply to message #918154] Mon, 24 September 2012 13:23 Go to previous message
Michael Doswald is currently offline Michael Doswald
Messages: 2
Registered: September 2012
Junior Member
Thanks a lot Michael. This works perfectly. I didn't realize that the JavaCore class also contains compiler option constants. I was only searching the CompilerOptions class and thous couldn't find the correct option.
Previous Topic:mixing v3 and v2
Next Topic:Eclipse and Lion - Kinda lost...
Goto Forum:
  


Current Time: Thu Oct 23 14:34:04 GMT 2014

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

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