Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » Get the number of lines in a Java file
Get the number of lines in a Java file [message #318831] Tue, 07 August 2007 12:23 Go to next message
Eclipse UserFriend
Originally posted by: malnatij.lu.unisi.ch

Hi, I need to know the number of lines contained in a java class.

I have my IType object modeling the "class".. now I'm getting its lines of code simply doing:

type.getSource().split("\n").length;


this is (slow and) not accurate and is prone to cross-platform over-simplification...(some uses /n, some /n/r, etc..) are there other ways?
Eclipse itself computes it (in the editor), therefore there should be another, better way.

Any ideas?

Thanks! :)
Re: Get the number of lines in a Java file [message #318852 is a reply to message #318831] Wed, 08 August 2007 03:16 Go to previous messageGo to next message
Eclipse UserFriend
Jappo wrote:

>Hi, I need to know the number of lines contained in a java class.
>
>I have my IType object modeling the "class".. now I'm getting its lines of code simply doing:
>
>
type.getSource().split("\n").length;

>
>this is (slow and) not accurate and is prone to cross-platform over-simplification...(some uses /n, some /n/r, etc..) are there other ways?
>Eclipse itself computes it (in the editor), therefore there should be another, better way.
>
>Any ideas?
>
>
See methods in IDocument.

Dani

>Thanks! :)
>
>
Re: Get the number of lines in a Java file [message #318874 is a reply to message #318852] Wed, 08 August 2007 08:03 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: malnatij.lu.unisi.ch

Thanks, I've tried:

int linesOfCode = new Document(type.getSource()).getNumberOfLines();


I've also tried the other way around, initialize an empty document and computing the lines of code of a given set of lines, but the result is the same as it was with my stupid split :(. It gives me less lines than the ones shown by the eclipse editor.. am I missing something?

ciao :)
Re: Get the number of lines in a Java file [message #318920 is a reply to message #318874] Thu, 09 August 2007 05:27 Go to previous messageGo to next message
Eclipse UserFriend
Jappo wrote:

>Thanks, I've tried:
>
>
int linesOfCode = new Document(type.getSource()).getNumberOfLines();

>
>I've also tried the other way around, initialize an empty document and computing the lines of code of a given set of lines, but the result is the same as it was with my stupid split :(. It gives me less lines than the ones shown by the eclipse editor.. am I missing something?
>
>
Probably. The editor is backed by the document hence it should be the
same if you do everything right.

Dani

>ciao :)
>
>
Re: Get the number of lines in a Java file [message #318946 is a reply to message #318874] Thu, 09 August 2007 08:51 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: automatic.javalobby.org

Maybe the IDocument calculates it based on the document's line encoding, which might be \n. If there's a mixture of \n and \n\r etc. then perhaps the getNumberOfLines() will only do those based on the default line encoding. OTOH an editor might parse any line ending combination.

Just a guess. You could try writing a file with \n\r\r\r\r\r\r\n, and see how many lines the split(), getNumberOfLines(), and the editor gives you?

Alex.
Re: Get the number of lines in a Java file [message #319015 is a reply to message #318946] Thu, 09 August 2007 16:10 Go to previous message
Eclipse UserFriend
Originally posted by: malnatij.lu.unisi.ch

ya, I will investigate and let you all know :)

thanks for the replies :)
Previous Topic:Why is WriteableSet not serializable ?
Next Topic:CommonNavigator and Properties View
Goto Forum:
  


Current Time: Thu Jul 24 18:42:15 EDT 2025

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

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

Back to the top