Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jdt-core-dev] org.eclipse.jdt.core.dom.TryStatement

Hi Kevin,
 
At least I know you can find the text offset of the finally block. (Text between '{' and '}')
 
You can look up methods ASTNode#getStartPosition(), getLength(), getSourceRange(), and have some experiments. The offset of 'finally' keyword can be explored and computed based on this information.
 
BTW, CompilationUnit#getCommentList() only returns you Comment nodes containing no text content. But you can still get its original source range.
 
Best regards,
Dongqing

From:Kevin Connor Arpe <kevinarpe@xxxxxxxxx>
Date:2013-06-15 17:25
Subject:[jdt-core-dev] org.eclipse.jdt.core.dom.TryStatement
To:"jdt-core-dev"<jdt-core-dev@xxxxxxxxxxx>
Cc:
 
Hello,

As mentioned in a previous mail to this list, I am new hacker on the ASTParser et al.  I am trying to use ASTParser et al for a syntax highlighting project.  If I am using ASTParser for entirely the wrong purposes, please bonk me on the head and point me in the right direction.  =)

Question: How do I find the text offset of the "finally" keyword in a try/finally statement?  (I'm actually on the hunt for all Java keywords, but I want to keep this e-mail tightly focused so as to ask a Good Question.)

So far, my code is trying to walk the text between
  1. the end of the try block, via TryStatement.getBody(), or last catch clause, via TryStatement.catchClauses(),
  2. and the start of the finally block, via TryStatement.getFinally().
This is a bit tricky as a I need to avoid comments, via CompilationUnit.getCommentList(), and catch clauses .  A simple text search for "finally" won't work as the term may be embedded with comments, or even finally blocks within a catch block(!).

Imagine a slightly dreadful segment of code as this:
try {
    // do stuff
}
catch (/* finally! */ Exception e) {
    try {
        // try this next
    }
    finally {
        // nope
    }
}
// finally
/* more and more finally!
blah, blah, blah
*/
finally {  // <-- how to find this keyword's text offset?
    // shutdown
}

Is there a better way?

Thanks,
Arpe

Back to the top