Bug 150467 - [ast] Using ASTParser cannot get Javadoc comments
Summary: [ast] Using ASTParser cannot get Javadoc comments
Status: RESOLVED FIXED
Alias: None
Product: AspectJ
Classification: Tools
Component: Compiler (show other bugs)
Version: 1.5.2RC1   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 1.5.3   Edit
Assignee: aspectj inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-07-12 19:41 EDT by Andrew Eisenberg CLA
Modified: 2006-08-09 02:56 EDT (History)
0 users

See Also:


Attachments
failing testcase and proposed fix (4.09 KB, application/zip)
2006-07-27 10:45 EDT, Helen Beeken CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Eisenberg CLA 2006-07-12 19:41:37 EDT
Hi,

I am using the AspectJ org.aspectj.org.eclipse.jdt.core.dom.ASTParser class to parse some aspectJ code, but javadoc comments do not seem to be handled correctly.  Here is a code snippet that illustrates this:


    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource("/** I have a doc comment */ aspect X { }".toCharArray());
    parser.setCompilerOptions(Collections.EMPTY_MAP);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    CompilationUnit cu = (CompilationUnit) parser.createAST(null);
    System.out.println(((TypeDeclaration) cu.types().get(0)).getJavadoc());  // prints "null"
    System.out.println(cu.getCommentList().get(0));                          // prints "/**\n */"


The first System.out.println should print the same as the second, but it prints null.

thanks!

ps- I'm not sure if I am using version 1.5.2RC1 or 1.5.2.  The plugin extension is 1.5.2.20060619065212
Comment 1 Andrew Eisenberg CLA 2006-07-12 19:50:31 EDT
I've found another odd thing.  Doc comments do not seem to be stored at all for DeclareParentsDeclaration nodes:

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource("aspect X {/** I have a doc comment */declare parents : Y implements Z; }".toCharArray());
    parser.setCompilerOptions(Collections.EMPTY_MAP);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    CompilationUnit cu = (CompilationUnit) parser.createAST(null);
    System.out.println(((DeclareParentsDeclaration) ((TypeDeclaration) cu.types().get(0)).bodyDeclarations().get(0)).getJavadoc());  // prints "null"
    System.out.println(cu.getCommentList().size());           // prints 0

The comment list size should be 1, but it is 0

This does not happen for any other kind of node that I have tried (FieldDeclaration, MethodDeclaration, AdviceDeclaration, PointcutDeclaration).

thanks,
--a
Comment 2 Helen Beeken CLA 2006-07-26 11:55:00 EDT
Looking at the first problem raised.....running in a pure java project with the following code

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource("/** I have a doc comment */ class X { }".toCharArray());
    parser.setCompilerOptions(Collections.EMPTY_MAP);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    CompilationUnit cu = (CompilationUnit) parser.createAST(null);
    TypeDeclaration t = (TypeDeclaration)cu.types().get(0);
    System.out.println(((TypeDeclaration) cu.types().get(0)).getJavadoc());
    //prints "null"
    System.out.println(cu.getCommentList().get(0));                        
    //prints "/**\n */"

prints the same as aspectj does with the aspect. I'm therefore not entirely sure we should expect the two print statements to be the same.

Looking at the second problem raised......as reported, for DeclareDeclaration's the comment list size is 0. This value is worked out by using CompilationnitDeclaration.comments which is populated during parsing. However, with DeclareDeclaration this isn't set.
Comment 3 Helen Beeken CLA 2006-07-27 08:46:37 EDT
The problem is occuring because the comment information recorded against the CommentRecorderScanner instance in CommentRecorderParser is not being pushed onto the stack before the scanner.commentPtr is reset to -1. In the case of PointcutDeclaration (which works as expected) the scanner.comment information (commentPtr, commentStarts and commentStops) is set via CommentRecorderScanner.recordComment(). Later, we enter Parser.consumeRule(int) with argument 648 and consumeModifiers(). This has a call to resetModifiers() which in CommentRecorderParser pushes the comments onto the stack. The scanner.commentPtr is then set to -1. 

In the failing case of DeclareDeclaration we don't go via this route because there are no modifiers to consume. Instead, we correctly set the comments via CommentRecorderScanner.recordComment(), however, within Parser.consumeToken() we are a declare and so set scanner.commentPtr to be -1. Therefore, when we come to call CommentRecorderParser.endParse(), this.scanner.commentPtr is -1 and so the comments are not pushed onto the stack and not recorded against the CompilationUnitDeclaration.
Comment 4 Helen Beeken CLA 2006-07-27 10:45:35 EDT
Created attachment 46881 [details]
failing testcase and proposed fix

This zip file contains the following:

* pr150467-ajdt-core-patch.txt - apply this to the org.aspectj.adjt.core project. This contains a testcase which is a modified version of the code in comment #1.

* CommentRecorderParser.java - this is the modified file to be put in jdtcore-for-aspectj.jar which contains the proposed fix. There is only one change to this file and this is marked with the bug number and is the addition of the following method:

protected void consumeToken(int type) {
	if (type == TokenNamedeclare) {
		pushOnCommentsStack(0, this.scanner.commentPtr);
	}
	super.consumeToken(type);
}

By adding this, in the case of declare statements we first push any comments onto the stack before the commentPtr is reset when calling super.consumeToken(type).
Comment 5 Andrew Clement CLA 2006-08-08 10:27:37 EDT
fixes are in - will be in a dev build shortly.
Comment 6 Andrew Clement CLA 2006-08-09 02:56:34 EDT
available