[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[news.eclipse.tools] Re: Finding IJavaElements (have sourcefile, start and end line numbers)

Jon,

Use ICompilationUnit.getElementAt(int):

/**
* Returns the smallest element within this compilation unit that
* includes the given source position (that is, a method, field, etc.), or
* <code>null</code> if there is no element other than the compilation
* unit itself at the given position, or if the given position is not
* within the source range of this compilation unit.
*
* @param position a source position inside the compilation unit
* @return the innermost Java element enclosing a given source position or
<code>null</code>
* if none (excluding the compilation unit).
* @exception JavaModelException if the compilation unit does not exist or if
an
* exception occurs while accessing its corresponding resource
*/
IJavaElement getElementAt(int position) throws JavaModelException;

So you will have to convert your line number into a position relative to the
start of the source.

BTW, I'm don't think there is an API method such as findElements(int pos)

Jerome

"Jon Hatcher" <jon.hatcher@xxxxxxxxxx> wrote in message
news:b7lrlu$qpo$1@xxxxxxxxxxxxxxxx
> I am currently using:-
>
> IJavaElement element = IJavaProject.findElement(new Path(relPath));
>
> To find compilation units based on some information I have.  However, I
> also have the start and end line numbers and the column inside that comp.
> unit.  How do I find the method, or field, or type, that this is referring
> to.  I know they are valid, as I set markers which appear correctly.  I
> tried using:-
>
> ICompilationUnit.findElements(int pos)
>
> But couldn't figure out what "pos" is?!  The character position in the
> file?!  Thanks for any help.
> Jon
>
>