Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jdt-core-dev] Possible bug in Binding2JavaModel.findCompilationUnit()

In fact this is not a bug.
Binding2JavaModel uses IProject.findType method and this method is
specified to be limited to primary types only (types that matches the
compilation unit name).

See Java Doc:

      /**
       * Returns the first type found following this project's classpath
       * with the given package name and type qualified name
       * or <code>null</code> if none is found.
       * The package name is a dot-separated name.
       * The type qualified name is also a dot-separated name. For example,
       * a class B defined as a member type of a class A should have the
       * type qualified name "A.B".
       *
       * Note that in order to be found, a type name (or its toplevel
enclosing
       * type name) must match its corresponding compilation unit name. As
a
       * consequence, secondary types cannot be found using this
functionality.
       * Secondary types can however be explicitely accessed through their
enclosing
       * unit or found by the <code>SearchEngine</code>.
       *
       * @param packageName the given package name
       * @param typeQualifiedName the given type qualified name
       * @exception JavaModelException if this element does not exist or if
an
       *          exception occurs while accessing its corresponding
resource
       * @return the first type found following this project's classpath
       * with the given package name and type qualified name
       * or <code>null</code> if none is found
       * @see IType#getTypeQualifiedName(char)

       * @since 2.0
       */
      IType findType(String packageName, String typeQualifiedName) throws
JavaModelException;

You need to use a different way to find the compilation unit.

Olivier



                                                                                                                                                   
                      "Dr. Robert M.                                                                                                               
                      Fuhrer"                    To:       jdt-core-dev@xxxxxxxxxxx                                                                
                      <rfuhrer@xxxxxxxxxx        cc:                                                                                               
                      .com>                      Subject:  [jdt-core-dev] Possible bug in Binding2JavaModel.findCompilationUnit()                  
                      Sent by:                                                                                                                     
                      jdt-core-dev-admin@                                                                                                          
                      eclipse.org                                                                                                                  
                                                                                                                                                   
                                                                                                                                                   
                      03/27/03 01:49 PM                                                                                                            
                      Please respond to                                                                                                            
                      jdt-core-dev                                                                                                                 
                                                                                                                                                   
                                                                                                                                                   




I may have found a bug in

  Binding2JavaModel.findCompilationUnit(ITypeBinding, IJavaProject)

For the code sequence

ICompilationUnit  cu     = <refers to "Test.java">;
MethodDeclaration decl   = <refers to X.foo()>;
ASTNode           parent = decl.getParent();
TypeDeclaration   type   = (TypeDeclaration) parent;
ICompilationUnit  declUnit =
  Binding2JavaModel.findCompilationUnit(type.resolveBinding(),
                                        fCu.getJavaProject());

when run in the context of the following *single* compilation unit,
findCompilationUnit() returns null. Note the non-public top-level class,
which the Eclipse Java compiler handles properly, but which I suspect
is the cause of the problem. I verified in the debugger the claims given
in <>'s regarding the nature of the entities to which cu and decl refer.

public class Test {
  public static void main(String[] args) {
    String s = (String) new X().foo();
  }
}

class X {
  public Object foo() { return new String(); }
}

I don't think findCompilationUnit() should be returning null in this case.
Am I wrong in thinking that?  If I am wrong, how can I find the compilation
unit containing such a class?



===================================================
Dr. Robert M. Fuhrer, rfuhrer@xxxxxxxxxxxxxx
IBM T.J. Watson Research Center









Back to the top