Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » JDT - Find Length of IF Statement(Find Total length of IF Statement)
JDT - Find Length of IF Statement [message #1754321] Thu, 16 February 2017 11:37 Go to next message
Suresh P is currently offline Suresh PFriend
Messages: 27
Registered: May 2013
Junior Member
How to find the length of IF Statements using JDT?
I'm referring to the class org.eclipse.jdt.core.dom.IfStatement in JDT.

Thanks
Re: JDT - Find Length of IF Statement [message #1754326 is a reply to message #1754321] Thu, 16 February 2017 12:06 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
You didn't say which unit of measurement you're interested in, but what's wrong with the obvious IfStatement.getLength()? (Well, actually it's ASTNode.getLength() ...)
Re: JDT - Find Length of IF Statement [message #1754331 is a reply to message #1754326] Thu, 16 February 2017 12:35 Go to previous messageGo to next message
Suresh P is currently offline Suresh PFriend
Messages: 27
Registered: May 2013
Junior Member
hi Stephan,

I need to find the number of Line Numbers (StartLineNo & EndLineNo) in the IF Statement. ASTNode.getLength() will give the count of the characters in the Statement.

Thanks
Re: JDT - Find Length of IF Statement [message #1754332 is a reply to message #1754326] Thu, 16 February 2017 12:38 Go to previous messageGo to next message
Suresh P is currently offline Suresh PFriend
Messages: 27
Registered: May 2013
Junior Member
hi Stephan,

I need to find the Line Numbers (StartLineNo & EndLineNo) in the IF Statement. ASTNode.getLength() will give the count of the characters in the Statement.


For Example,
#43:		if (field == null) {
#44:			if (other.field != null)
#45:				return false;
#46:		} else if (!field.equals(other.field))
#47:			return false;



Thanks
Re: JDT - Find Length of IF Statement [message #1754333 is a reply to message #1754332] Thu, 16 February 2017 12:45 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
line numbers are not a property of the AST (since the "A" means: abstract). Line information can be obtained using IDocument.getLineOfOffest()
Re: JDT - Find Length of IF Statement [message #1754374 is a reply to message #1754333] Fri, 17 February 2017 05:00 Go to previous messageGo to next message
Suresh P is currently offline Suresh PFriend
Messages: 27
Registered: May 2013
Junior Member
Hi Stephan,

I am able to get the Starting LineNo of IF Statement by using below code. Is there a way that I could find the End LineNo of the same IF statement?

public boolean visit(org.eclipse.jdt.core.dom.IfStatement node) {
	int nodeType = node.getExpression().getNodeType();
	if (nodeType == Expression.INFIX_EXPRESSION) {
		InfixExpression inf = (InfixExpression) node.getExpression();
		int startLineNo = getCompilationUnit().getLineNumber(inf.getStartPosition());
	}
}


If that is not possible, can you let me know how to use IDocument.getLineOfOffest() to get the Start and End Line numbers in my scenario? It seems IInvocationContext need to be used to get the Offset and Length as shown below,
int selectionOffset= context.getSelectionOffset();
int selectionLength= context.getSelectionLength();


Re: JDT - Find Length of IF Statement [message #1754461 is a reply to message #1754374] Sat, 18 February 2017 00:34 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
I didn't know about CompilationUnit.getLineNumber(), but this seems to be even more suitable than going via the IDocument.
Have you tried to get the line number for (node.getStartPosition() + node.getLength() - 1) ?

I don't know how IInvocationContext would come into the picture. Are you implementing a quick assist based on the selection in the editor?
Re: JDT - Find Length of IF Statement [message #1755228 is a reply to message #1754461] Wed, 01 March 2017 10:38 Go to previous messageGo to next message
Suresh P is currently offline Suresh PFriend
Messages: 27
Registered: May 2013
Junior Member
hi Stephan,
Yes Im implementing quick assist based on the selection in the editor, using JDT/AST.

Can you let me know how to find the starting line number of the Method Declaration (excluding the Javadocs).

Below code gives me the Line number, but it starts with the JavaDoc comment line number. But I need to get the line number at the Method declaration level.


      public boolean visit(MethodDeclaration node) {
			CompilationUnit cu = getCompilationUnit();
			int startLineNumber =  getCompilationUnit().getLineNumber(node.getStartPosition()));
     ---
     }


Sample Method:

In below sample code, I need to get the Line Number as #14 (from method signature), but Im getting line number as #10 (from Javadoc).

 #10:   /**
#11:     * Method used for extracting the given parameter form the request.return
#12:     * null if empty
#13:     */
#14:     private String getString(HttpServletRequest request, String param)
#15:    {
#16:



Re: JDT - Find Length of IF Statement [message #1755354 is a reply to message #1755228] Thu, 02 March 2017 15:09 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
I don't know the answer from the top of my head, either, but for questions like this I recommend the AST view, that lets you interactively inspect the correlation between source and AST incl. various properties like positions and bindings: https://eclipse.org/jdt/ui/astview/index.php
Re: JDT - Find Length of IF Statement [message #1755407 is a reply to message #1755354] Fri, 03 March 2017 05:35 Go to previous messageGo to next message
Suresh P is currently offline Suresh PFriend
Messages: 27
Registered: May 2013
Junior Member
I'm using ASTView plugin for quite some time now, but I couldnt find a solution for my above specified problem using ASTView. ASTView doesn't provide the details of line numbers in method declaration (with/wihout Javadocs).

Does some one has a solution for this problem? message #1755228 -
Re: JDT - Find Length of IF Statement [message #1755513 is a reply to message #1755407] Sat, 04 March 2017 09:38 Go to previous message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
well, you'll have to go by the source positions (characters) to infer the line numbers.
Previous Topic:JDT - MarkerResolution - How to show HTML content in Proposal window
Next Topic:JSP error page doesn't work in eclipse internal browser but works in chrome
Goto Forum:
  


Current Time: Tue Apr 23 16:57:43 GMT 2024

Powered by FUDForum. Page generated in 0.04684 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top