Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8406551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:08:25+00:00 2026-06-09T23:08:25+00:00

I’ve been playing for quite a while now with ASTs and was trying to

  • 0

I’ve been playing for quite a while now with ASTs and was trying to get the line and column information associated to a given node parsed on this plugin from eclipse. According to the documented api here I found that the method getStartPosition() can give me the position on the characters of the file that was parsed, but this is not what I wanted.

I went on the CompilationUnit class api documentation to find out the methods getLineNumber(int position) and getColumnNumber(int position) which from my understanding can do the trick. the position paramater is nothing less than what the getStartPosition() method returns, by doing node.getStartPosition().

Now, the problem is the two methods that get line and column on source file doesn’t seems to be available for all nodes. For instance, the method declaration nodes don’t have them!

How can I get such information on all the tree? I know this is not impossible since I was able to use parsers for other languages that for every ast node had a line and column associated with it. In fact, I believe javaparser is one of them for java, since the class contains attributes for line and column hardcoded. Seeing Eclipse JDT seemed to me much more robust to me and being there for quite a while, I would be surprised that such information would not be possible to acquire.

Edit: Again, the problem is obtaining the line number from things different of the compilation unit that only appears on the root:

<type 'org.eclipse.jdt.core.dom.CompilationUnit'>
1
<type 'org.eclipse.jdt.core.dom.TypeDeclaration'>
<type 'org.eclipse.jdt.core.dom.Javadoc'>
<type 'org.eclipse.jdt.core.dom.TagElement'>
<type 'org.eclipse.jdt.core.dom.TextElement'>
<type 'org.eclipse.jdt.core.dom.TextElement'>
<type 'org.eclipse.jdt.core.dom.TextElement'>
<type 'org.eclipse.jdt.core.dom.TextElement'>
<type 'org.eclipse.jdt.core.dom.TextElement'>
<type 'org.eclipse.jdt.core.dom.TextElement'>

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T23:08:26+00:00Added an answer on June 9, 2026 at 11:08 pm

    For posterity, I’m reposting the answer I posted on the jdt-core-dev mailing list. (My answer is not very different from Ryan’s suggestion above)

    Hi Carlos,

    You’re correct about the CompilationUnit.getLineNumber(int) method. You use it as follows:

    int lineNumber = compilationUnit.getLineNumber(node.getStartPosition()) - 1;

    However, I don’t understand where you’re stuck. Why do you need the getLineNumber(..) defined for MethodDeclaration? All you have to do is to find the method declaration node, then find out the corresponding CompilationUnit for it using the code below, and then use the above line of code to find the line number. Am I missing something here?

    ASTParser parser = ASTParser.newParser(AST.JLS3); // or JLS_4 for java 7 and above
    
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    parser.setSource(source); // give your java source here as char array
    parser.setResolveBindings(true);
    
    CompilationUnit compilationUnit = parser.createAST(null);
    

    Cheers!
    Ayush

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.