Install the Eclipse Metrics Plugin. To create a HTML report (with optional XML and CSV) right-click a project -> Export -> Other -> Metrics.
You can adjust the Lines of Code metrics by ignoring blank and comment-only lines or exclude Javadoc if you want. To do this check the tab at Preferences -> Metrics -> LoC.
That's it. There is no special option to exclude curly braces {}.
The plugin offers an alternative metric to LoC called Number of Statements. This is what the author has to say about it:
This metric represents the number of statements in a method. I consider it a more robust measure than Lines of Code since the latter is fragile with respect to different formatting conventions.
Edit:
After you clarified your question, I understand that you need a view for real-time metrics violations, like compiler warnings or errors. You also need a reporting functionality to create reports for your boss. The plugin I described above is for reporting because you have to export the metrics when you want to see them.
One possible way to count lines of code in Eclipse:
using the Search / File... menu, select File Search tab, specify \n[\s]* for Containing text (this will not count empty lines), and tick Regular expression.
另一种方法是使用另一个 loc 实用程序,例如 LocMetrics。
它还列出了许多其他 loc 工具。
与 Eclipse 的集成并不总是存在(就像与 Metrics2,您可以查看它,因为它是比 指标),但是至少这些工具可以根据逻辑线进行推理(通过对终端分号和终端花括号求和来计算)。
您还可以检查 eclipse-metrics 是否更适合您的期望。
Another way would by to use another loc utility, like LocMetrics for instance.
It also lists many other loc tools.
The integration with Eclipse wouldn't be always there (as it would be with Metrics2, which you can check out because it is a more recent version than Metrics), but at least those tools can reason in term of logical lines (computed by summing the terminal semicolons and terminal curly braces).
You can also check with eclipse-metrics is more adapted to what you expect.
I created a Eclipse plugin, which can count the lines of source code.
It support Kotlin, Java, Java Script, JSP, XML, C/C++, C#, and many other file types.
Please take a look at it.
Any feedback would be appreciated!
知道人们对代码行的构成有不同的看法)。特别是,人们常常不确定他们是否真正想要代码行数或语句数。例如,如果您有以下非常长的一行,里面充满了语句,那么您想要报告什么,是 1 个 LOC 还是数百个语句?
{ a = 1; b = 2; if (a==c) b++; /* etc. for another 1000 characters */ }
当有人问你如何称呼 LOC 时,请确保你可以回答,即使只是“我对 LOC 的定义是 Metrics2 的定义”。一般来说,对于最常见的格式化代码(与我的示例不同),流行的工具会给出相当相似的数字,因此 Metrics2、SonarQube 等应该都没有问题,只要您一致地使用它们。换句话说,不要使用一种工具计算某些代码的 LOC,并将该值与使用不同工具测量的该代码的较新版本进行比较。
The first thing to do is to determine your definition of "line of code" (LOC). In both your question
It counts a line with just one } as a line and he doesn't want that to
count as "its not a line, its a style choice"
and in the answers, e.g.,
You can adjust the Lines of Code metrics by ignoring blank and comment-only lines or exclude Javadoc if you want
you can tell that people have different opinions as to what constitutes a line of code. In particular, people are often imprecise about whether they really want the number of lines of code or the number of statements. For example, if you have the following really long line filled with statements, what do you want to report, 1 LOC or hundreds of statements?
{ a = 1; b = 2; if (a==c) b++; /* etc. for another 1000 characters */ }
And when somebody asks you what you are calling a LOC, make sure you can answer, even if it is just "my definition of a LOC is Metrics2's definition". In general, for most commonly formatted code (unlike my example), the popular tools will give numbers fairly similar, so Metrics2, SonarQube, etc. should all be fine, as long as you use them consistently. In other words, don't count the LOC of some code using one tool and compare that value to a later version of that code that was measured with a different tool.
ProjectCodeMeter counts LLOC (logical lines of code) exactly as you described (only effective lines). it integrates into eclipse as external code metrics tool, it's not real-time though, it generates a report.actually it counts many source code metrics such as complexity, arithmetic intricacy, hard coded strings, numeric constants.. even estimates development time in hours.
You have to install it as a web-app running the analysis off your source code repository, but it also has an Eclipse plugin.
It's overkill if you just want to know, as a one-off, how many lines of code there are in your project. If you want to track metrics through time, compare across projects, fire warnings when a threshold is exceeded, etc., it's fantastic.
Disclosure: I have no financial relationship with SonarSource.
发布评论
评论(10)
总是有“暴力”:
搜索->文件
在“包含文本”中键入以下内容-> <代码>^.*$。然后选中“正则表达式”复选框
在“文件名模式”中键入以下内容 ->
*.java
单击“搜索”
检查“搜索”选项卡中的匹配项数。
There's always the "brute force":
Search->File
Type the following in "Containing text" ->
^.*$
. Then check the "Regular Expression" checkboxType the following in "File name patterns" ->
*.java
Click "Search"
Check the number of matches in the "Search" Tab.
安装 Eclipse Metrics 插件。要创建 HTML 报告(带有可选的 XML 和 CSV)
右键单击项目 ->导出->其他->指标
。您可以通过忽略空白行和仅注释行来调整代码行指标,或者根据需要排除 Javadoc。为此,请检查
Preferences -> 中的选项卡指标-> LoC。
就是这样。没有特殊选项可以排除大括号
{}
。该插件提供了 LoC 的替代指标,称为语句数。对此,作者是这样说的:
编辑:
在您澄清问题后,我知道您需要一个实时指标违规的视图,例如编译器警告或错误。您还需要报告功能来为老板创建报告。我上面描述的插件用于报告,因为当您想查看指标时必须导出它们。
Install the Eclipse Metrics Plugin. To create a HTML report (with optional XML and CSV)
right-click a project -> Export -> Other -> Metrics
.You can adjust the Lines of Code metrics by ignoring blank and comment-only lines or exclude Javadoc if you want. To do this check the tab at
Preferences -> Metrics -> LoC
.That's it. There is no special option to exclude curly braces
{}
.The plugin offers an alternative metric to LoC called Number of Statements. This is what the author has to say about it:
Edit:
After you clarified your question, I understand that you need a view for real-time metrics violations, like compiler warnings or errors. You also need a reporting functionality to create reports for your boss. The plugin I described above is for reporting because you have to export the metrics when you want to see them.
如果在 OSX 或 *NIX 上使用
从 *.java 文件中获取 java 代码的所有实际行 从
*.java 文件中获取所有行,其中包括空行和注释
获取每个文件的信息,这将为您提供 [ 文件路径 + " ," + 行数]
If on OSX or *NIX use
Get all actual lines of java code from *.java files
Get all lines from the *.java files, which includes empty lines and comments
Get information per File, this will give you [ path to file + "," + number of lines ]
在 Eclipse 中计算代码行数的一种可能方法:
使用“搜索/文件...”菜单,选择“文件搜索”选项卡,为“包含文本”指定 \n[\s]*(这不会计算空行),然后勾选“正则表达式” 。
帽子提示:www.monblocnotes.com/node/2030
One possible way to count lines of code in Eclipse:
using the Search / File... menu, select File Search tab, specify \n[\s]* for Containing text (this will not count empty lines), and tick Regular expression.
Hat tip: www.monblocnotes.com/node/2030
另一种方法是使用另一个 loc 实用程序,例如 LocMetrics。
它还列出了许多其他 loc 工具。
与 Eclipse 的集成并不总是存在(就像与 Metrics2,您可以查看它,因为它是比 指标),但是至少这些工具可以根据逻辑线进行推理(通过对终端分号和终端花括号求和来计算)。
您还可以检查 eclipse-metrics 是否更适合您的期望。
Another way would by to use another loc utility, like LocMetrics for instance.
It also lists many other loc tools.
The integration with Eclipse wouldn't be always there (as it would be with Metrics2, which you can check out because it is a more recent version than Metrics), but at least those tools can reason in term of logical lines (computed by summing the terminal semicolons and terminal curly braces).
You can also check with eclipse-metrics is more adapted to what you expect.
另一个工具是 Google Analytix,即使您无法运行,它也可以让您运行指标构建项目以防出现错误
Another tool is Google Analytix, which will also allow you to run metrics even if you can`t build the project in case of errors
我创建了一个 Eclipse 插件,它可以计算源代码的行数。
它支持 Kotlin、Java、Java Script、JSP、XML、C/C++、C# 和许多其他文件类型。
请看一下。
任何反馈将不胜感激!
git-hub 存储库在这里
I created a Eclipse plugin, which can count the lines of source code.
It support Kotlin, Java, Java Script, JSP, XML, C/C++, C#, and many other file types.
Please take a look at it.
Any feedback would be appreciated!
the git-hub repository is here
首先要做的是确定“代码行”(LOC) 的定义。在你的两个问题中
,在答案中,例如,
知道人们对代码行的构成有不同的看法)。特别是,人们常常不确定他们是否真正想要代码行数或语句数。例如,如果您有以下非常长的一行,里面充满了语句,那么您想要报告什么,是 1 个 LOC 还是数百个语句?
当有人问你如何称呼 LOC 时,请确保你可以回答,即使只是“我对 LOC 的定义是 Metrics2 的定义”。一般来说,对于最常见的格式化代码(与我的示例不同),流行的工具会给出相当相似的数字,因此 Metrics2、SonarQube 等应该都没有问题,只要您一致地使用它们。换句话说,不要使用一种工具计算某些代码的 LOC,并将该值与使用不同工具测量的该代码的较新版本进行比较。
The first thing to do is to determine your definition of "line of code" (LOC). In both your question
and in the answers, e.g.,
you can tell that people have different opinions as to what constitutes a line of code. In particular, people are often imprecise about whether they really want the number of lines of code or the number of statements. For example, if you have the following really long line filled with statements, what do you want to report, 1 LOC or hundreds of statements?
And when somebody asks you what you are calling a LOC, make sure you can answer, even if it is just "my definition of a LOC is Metrics2's definition". In general, for most commonly formatted code (unlike my example), the popular tools will give numbers fairly similar, so Metrics2, SonarQube, etc. should all be fine, as long as you use them consistently. In other words, don't count the LOC of some code using one tool and compare that value to a later version of that code that was measured with a different tool.
ProjectCodeMeter 完全按照您的描述计算 LLOC(逻辑代码行)(仅有效行)。它集成到 eclipse 作为外部代码度量工具,但它不是实时的,它会生成一份报告。实际上,它会计算许多源代码指标,例如复杂性、算术复杂性、硬编码字符串、数字常量......甚至以小时为单位估计开发时间。
ProjectCodeMeter counts LLOC (logical lines of code) exactly as you described (only effective lines). it integrates into eclipse as external code metrics tool, it's not real-time though, it generates a report.actually it counts many source code metrics such as complexity, arithmetic intricacy, hard coded strings, numeric constants.. even estimates development time in hours.
对于静态分析,我使用并推荐 SonarQube,它运行 几乎所有您可能的指标想要 多种语言,并且在基本版(需要付费来分析我只能用枪指着头来编码的语言)。
您必须将其安装为 Web 应用程序,在源代码存储库中运行分析,但它还有一个 Eclipse 插件。
如果您只是想一次性知道您的项目中有多少行代码,那就太过分了。如果您想跟踪一段时间内的指标、跨项目进行比较、超过阈值时发出火灾警告等,那么这是非常棒的。
披露:我与 SonarSource 没有财务关系。
For static analysis, I've used and recommend SonarQube which runs just about all the metrics you could possibly want on a wide range of languages, and is free in the basic version (you have to pay to analyse the sorts of languages I'd only code in with a gun to my head).
You have to install it as a web-app running the analysis off your source code repository, but it also has an Eclipse plugin.
It's overkill if you just want to know, as a one-off, how many lines of code there are in your project. If you want to track metrics through time, compare across projects, fire warnings when a threshold is exceeded, etc., it's fantastic.
Disclosure: I have no financial relationship with SonarSource.