Community
Participate
Working Groups
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
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
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.
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.
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).
fixes are in - will be in a dev build shortly.
available