Bug 79373 - [javadoc] MethodDeclaration.getJavadoc() returns null
Summary: [javadoc] MethodDeclaration.getJavadoc() returns null
Status: CLOSED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.0.1   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.1 M4   Edit
Assignee: Frederic Fusier CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-11-24 07:38 EST by Vedran Lerenc CLA
Modified: 2004-11-25 11:37 EST (History)
0 users

See Also:


Attachments
Full project - attached note with code snippet concerning stand-alone usage of the parser (28.29 KB, application/octet-stream)
2004-11-25 07:59 EST, Vedran Lerenc CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Vedran Lerenc CLA 2004-11-24 07:38:14 EST
Hello,

I was in need for a tool to automatically modify Java Sources (auto-migrate 
code) and thought it the best approach to use Eclipse JDT Core for these 
purposes after I have evaluated my other options with ANTLR, JavaCC and others.

I am almost done and the libraries just offer the right functionality, but I 
fail in getting the JavaDoc comment on methods. I can't believe, it's not 
working, but on the other hand the usage seems to be pretty clear and I 
managed to do all the other stuff I wanted to do (analyzing imports, super 
classes, super interfaces, method signatures, throws clauses and the like).

What still seems to be not working:

    Javadoc javaDoc = methodDecl.getJavadoc();

always returns null for the following test source for both methods:

----------
package com;

import com.sap.security.api.*;

public class SourceImportingNew extends IUser
{
  // C comment
  public com.sapportals.portal.security.usermanagement.IUser methodInNoneOutOld
(IUser user) throws xyz
  {
    return null;
  }

  /**
   * Print a String and then terminate the line.  This method behaves as
   * though it invokes <code>{@link #print(String)}</code> and then
   * <code>{@link #println()}</code>.
   *
   * @param user  The <code>String</code> to be printed.
   */
  public com.sapportals.portal.security.usermanagement.IUser methodInNoneOutOld
(com.sapportals.portal.security.usermanagement.IUser user)
  {
    return null;
  }
}
----------

When I ask for the starting positions of both methods 
(MethodDeclaration.getStartPosition()) I get the correct positions, i.e. the 
position of the method declaration of the first method without the C style 
comment and the position of the JavaDoc comment of the second method.

I use your libraries stand-alone, i.e. outside Eclipse in a standard Java 
application. Here my class path (all JARs copied from your 3.0.1 release):

jdtcore.jar
jface.jar
jfacetext.jar
osgi.jar
resources.jar
runtime.jar
swt.jar
text.jar

Regards,

Vedran
Comment 1 Frederic Fusier CLA 2004-11-24 11:48:34 EST
Unfortunately I cannot reproduce this bug...
Here's the simpler test case I use:
b79373/Test.java
  package b79373;
  import b79373.p.q.r.s.IUser;
  public class Test extends IUser {
    // C comment
    public b79373.a.b.c.d.IUser methodInNoneOutOld(IUser user) {
	return null;
    }
    /**
     * Print a String and then terminate the line. This method behaves as though
     * it invokes <code>{@link #print(String)}</code> and then
     * <code>{@link #println()}</code>.
     * 
     * @param user
     *                   The <code>String</code> to be printed.
     */
    public b79373.a.b.c.d.IUser methodInNoneOutOld(b79373.a.b.c.d.IUser user) {
	return null;
    }
  }
b79373/p/q/r/s/IUser.java:
  package b79373.p.q.r.s;
  public class IUser {}
b79373/a/b/c/d/IUser.java:
  package b79373.a.b.c.d;
  public class IUser {}

For first method declaration getJavadoc() returns null but second one does not!
Could you try this simple example and let me know whether the second method
javadic is null or not, thx
Comment 2 Vedran Lerenc CLA 2004-11-25 00:44:24 EST
Hello,

I tried the example, i.e. I tried my code on Test.java and I got in both cases 
null. I have not created the two other files (IUser), because I use your 
libraries stand-alone without a running Eclipse environment and I am not 
interested in bindings (as stated in my initial bug report - can this be a 
problem? - do you have tried this?). For that purpose neither the file path 
must match the package nor full qualified names must be resolvable.

All other calls to your parser API work correctly in stand-alone mode, except 
for MethodDeclaration.getJavaDoc(), although MethodDeclaration.getStartPosition
() returns the correct position at the beginning of the JavaDoc for the second 
method.

Regards,

Vedran
Comment 3 Frederic Fusier CLA 2004-11-25 05:56:44 EST
With or without bindings I always get a non null javadoc on second method...

Here's the code I used to test it in a JUnit plugin test:
// Get compilation unit
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IFile file = root.getFile(new 
	Path("/Converter/src/b79373/SourceImportingNew.java"));
ICompilationUnit cu = (ICompilationUnit)JavaCore.create(file);
// Parse complation unit
ASTParser parser = ASTParser.newParser(AST.JLS2);
parser.setSource(cu);
parser.setResolveBindings(false);
ASTNode result = parser.createAST(null);
CompilationUnit unit = (CompilationUnit) result;
// First method javadoc comment should be null
MethodDeclaration firstMethod =  (MethodDeclaration) 
	((TypeDeclaration)unit.types().get(0)).bodyDeclarations().get(0);
if (firstMethod.getJavadoc() == null) {
	System.out.println("First method javadoc is null: OK");
} else {
	System.err.println("First method javadoc is not null: KO");
}
// Second method javadoc comment should NOT be null
MethodDeclaration secondMethod =  (MethodDeclaration) 
	((TypeDeclaration)unit.types().get(0)).bodyDeclarations().get(1);
if (secondMethod.getJavadoc() != null) {
	System.out.println("Second method javadoc is not null: OK");
} else {
	System.err.println("Second method javadoc is null: KO");
}

Running this, I get:
First method javadoc is null: OK
Second method javadoc is not null: OK

Of course, this is executed inside Eclipse (in Headleass mode) where DOM AST is
only designed to work. I cannot figure out how you can get an ICompilationUnit
outside eclipse in a standard Java application. May be can you attach the code
you run to let me test it?...
Comment 4 Vedran Lerenc CLA 2004-11-25 07:59:13 EST
Created attachment 16129 [details]
Full project - attached note with code snippet concerning stand-alone usage of the parser

No problem, here some code snippets:

import org.eclipse.jdt.core.*;
import org.eclipse.jdt.core.dom.*;

public class Transformer
{
  private ASTParser parser;

  public Transformer()
  {
    // Create parser
    Map options = new HashMap();
    options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_4);
    parser = ASTParser.newParser(AST.JLS2);
    parser.setCompilerOptions(options);
  }

  public CompilationUnit parse(String content)
  {
    // Parse source
    parser.setSource(content.toCharArray());
    CompilationUnit unitDecl = (CompilationUnit) parser.createAST(null);
    return unitDecl;
  }

  public List analyze(CompilationUnit unitDecl, String originalContent)
  {
    // Do analysis
    // e.g.
    for (Iterator iter = unitDecl.imports().iterator(); iter.hasNext();)
    {
      ImportDeclaration importDecl = (ImportDeclaration) iter.next();
      analyze(importDecl, originalContent, modifications, typeResolver);
    }
    for (Iterator iter = unitDecl.types().iterator(); iter.hasNext();)
    {
      TypeDeclaration typeDecl = (TypeDeclaration) iter.next();
      analyze(typeDecl, originalContent, modifications, typeResolver);
    }
    // ...
  }
}

This stuff works stand-alone just perfectly - navigating from compilation units
to imports, types, sub types, methods or fields works as expected, except for
this MethodDeclaration.getJavadoc() method.

Regards,

Vedran
Comment 5 Frederic Fusier CLA 2004-11-25 09:42:03 EST
OK, thanks for the input. I see what's wrong in your code, you're missing
following compiler option in Transformer default constructor:
options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED);

With this option enabled, javadoc will be parsed and built, then assigned in
method declaration...
Comment 6 Vedran Lerenc CLA 2004-11-25 10:52:33 EST
Thanks, that solved the problem! :-)

I am sorry for the inconvenience. One last question: Are the other available 
settings described somewhere in more detail?

Regards,

Vedran
Comment 7 Frederic Fusier CLA 2004-11-25 11:37:04 EST
Look at JavaCore.getDefaultOptions()