Skip to content

Commit

Permalink
new Java metrics to count lambdas, anonymous classes and null checks
Browse files Browse the repository at this point in the history
  • Loading branch information
BasLeijdekkers committed Sep 4, 2016
1 parent 33a4a2a commit 849b5a0
Show file tree
Hide file tree
Showing 11 changed files with 393 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!--
~ Copyright 2005-2016 Sixth and Red River Software, Bas Leijdekkers
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<html>
<body>
<table><tr><td valign="top" height="150">
<font face="verdana" size="-1">
Calculates the number of anonymous classes in each package.
</font>
</td></tr>
<tr><td height="50"><font face="verdana" size="-1">Abbreviation: <b>AnoC</b></font></td></tr>
<tr><td height="20"><font face="verdana" size="-2">Powered by MetricsReloaded</font></td></tr>
</table>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!--
~ Copyright 2005-2016 Sixth and Red River Software, Bas Leijdekkers
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<html>
<body>
<table><tr><td valign="top" height="150">
<font face="verdana" size="-1">
Calculates the number of lambda expressions in each <b>package</b>.
</font>
</td></tr>
<tr><td height="50"><font face="verdana" size="-1">Abbreviation: <b>LAMBDA</b></font></td></tr>
<tr><td height="20"><font face="verdana" size="-2">Powered by MetricsReloaded</font></td></tr>
</table>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!--
~ Copyright 2005-2016 Sixth and Red River Software, Bas Leijdekkers
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<html>
<body>
<table><tr><td valign="top" height="150">
<font face="verdana" size="-1">
Calculates the number of comparisons with <code>null</code> in each <b>method</b>.
</font>
</td></tr>
<tr><td height="50"><font face="verdana" size="-1">Abbreviation: <b>NULL</b></font></td></tr>
<tr><td height="20"><font face="verdana" size="-2">Powered by MetricsReloaded</font></td></tr>
</table>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ private static void initializeMethodMetrics(Collection<Metric> metrics) {
metrics.add(new NumImplementationsMethodMetric());
metrics.add(new NumLoopsMetric());
metrics.add(new NumMethodCallsMetric());
metrics.add(new NumNullChecksMetric());
metrics.add(new NumOverridesMethodMetric());
metrics.add(new NumParametersMetric());
metrics.add(new NumReturnPointsMetric());
Expand Down Expand Up @@ -236,6 +237,7 @@ private static void initializePackageMetrics(Collection<Metric> metrics) {
metrics.add(new NumAbstractClassesRecursivePackageMetric());
metrics.add(new NumAnnotationClassesPackageMetric());
metrics.add(new NumAnnotationClassesRecursivePackageMetric());
metrics.add(new NumAnonymousClassesPackageMetric());
metrics.add(new NumClassesPackageMetric());
metrics.add(new NumClassesRecursivePackageMetric());
metrics.add(new NumConcreteClassesPackageMetric());
Expand All @@ -247,6 +249,7 @@ private static void initializePackageMetrics(Collection<Metric> metrics) {
metrics.add(new NumEnumClassesRecursivePackageMetric());
metrics.add(new NumInterfacesPackageMetric());
metrics.add(new NumInterfacesRecursivePackageMetric());
metrics.add(new NumLambdasPackageMetric());
metrics.add(new NumLeafClassesPackageMetric());
metrics.add(new NumLeafClassesRecursivePackageMetric());
metrics.add(new NumMethodsPackageMetric());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ lines.of.xml.display.name=Lines of XML
lines.of.xml.abbreviation=L(XML)
number.of.abstract.classes.display.name=Number of abstract classes
number.of.abstract.classes.abbreviation=AC
number.of.anonymous.classes.display.name=Number of anonymous classes
number.of.anonymous.classes.abbreviation=AnoC
number.of.annotation.interfaces.display.name=Number of annotation interfaces
number.of.annotation.interfaces.abbreviation=ANC
number.of.classes.display.name=Number of classes
Expand Down Expand Up @@ -184,6 +186,8 @@ number.of.leaf.classes.display.name=Number of leaf classes
number.of.leaf.classes.abbreviation=LEAF
number.of.methods.display.name=Number of methods
number.of.methods.abbreviation=METH
number.of.lambdas.display.name=Number of lambdas
number.of.lambdas.abbreviation=LAMBDA
number.of.product.classes.display.name=Number of product classes
number.of.product.classes.abbreviation=Cp
number.of.root.classes.display.name=Number of root classes
Expand Down Expand Up @@ -322,6 +326,8 @@ number.of.branch.statements.display.name=Number of branch statements
number.of.branch.statements.abbreviation=BRANCH
number.of.control.statements.display.name=Number of control statements
number.of.control.statements.abbreviation=CONTROL
number.of.null.checks.display.name=Number of null checks
number.of.null.checks.abbreviation=NULL
number.of.exceptions.caught.display.name=Number of exceptions caught
number.of.exceptions.caught.abbreviation=CAUGHT
number.of.exceptions.thrown.display.name=Number of exceptions thrown
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2005-2016 Sixth and Red River Software, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.sixrr.stockmetrics.methodCalculators;

import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.sixrr.metrics.utils.MethodUtils;

/**
* @author Bas Leijdekkers
*/
public class NumNullChecksCalculator extends MethodCalculator {
private int methodNestingDepth = 0;
private int elementCount = 0;

@Override
protected PsiElementVisitor createVisitor() {
return new Visitor();
}

private class Visitor extends JavaRecursiveElementVisitor {

@Override
public void visitMethod(PsiMethod method) {
if (methodNestingDepth == 0) {
elementCount = 0;
}
methodNestingDepth++;
super.visitMethod(method);
methodNestingDepth--;
if (methodNestingDepth == 0 && !MethodUtils.isAbstract(method)) {
postMetric(method, elementCount);
}
}

@Override
public void visitPolyadicExpression(PsiPolyadicExpression expression) {
super.visitPolyadicExpression(expression);
final IElementType tokenType = expression.getOperationTokenType();
if (!JavaTokenType.EQEQ.equals(tokenType) && !JavaTokenType.NE.equals(tokenType)) {
return;
}
for (PsiExpression operand : expression.getOperands()) {
if (operand instanceof PsiLiteralExpression && PsiType.NULL.equals(operand.getType())) {
elementCount++;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2005-2016 Sixth and Red River Software, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.sixrr.stockmetrics.methodMetrics;

import com.sixrr.metrics.MetricCalculator;
import com.sixrr.metrics.MetricType;
import com.sixrr.stockmetrics.i18n.StockMetricsBundle;
import com.sixrr.stockmetrics.methodCalculators.NumControlStatementsCalculator;
import com.sixrr.stockmetrics.methodCalculators.NumNullChecksCalculator;
import org.jetbrains.annotations.NotNull;

/**
* @author Bas Leijdekkers
*/
public class NumNullChecksMetric extends MethodMetric {

@NotNull
@Override
public String getDisplayName() {
return StockMetricsBundle.message("number.of.null.checks.display.name");
}

@NotNull
@Override
public String getAbbreviation() {
return StockMetricsBundle.message("number.of.null.checks.abbreviation");
}

@NotNull
@Override
public MetricType getType() {
return MetricType.Count;
}

@NotNull
@Override
public MetricCalculator createCalculator() {
return new NumNullChecksCalculator();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2005-2016 Sixth and Red River Software, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.sixrr.stockmetrics.packageCalculators;

import com.intellij.psi.PsiAnonymousClass;
import com.intellij.psi.PsiClass;

/**
* @author Bas Leijdekkers
*/
public class NumAnonymousClassesPackageCalculator extends ClassCountingPackageCalculator {

@Override
public boolean satisfies(PsiClass aClass) {
return aClass instanceof PsiAnonymousClass;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2005-2016 Sixth and Red River Software, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.sixrr.stockmetrics.packageCalculators;

import com.intellij.psi.*;

/**
* @author Bas Leijdekkers
*/
public class NumLambdasPackageCalculator extends ElementCountPackageCalculator {

@Override
protected PsiElementVisitor createVisitor() {
return new Visitor();
}

private class Visitor extends JavaRecursiveElementVisitor {

@Override
public void visitJavaFile(PsiJavaFile file) {
super.visitJavaFile(file);
createCount(file);
}

@Override
public void visitLambdaExpression(PsiLambdaExpression expression) {
super.visitLambdaExpression(expression);
incrementCount(expression, 1);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2005-2016 Sixth and Red River Software, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.sixrr.stockmetrics.packageMetrics;

import com.sixrr.metrics.MetricCalculator;
import com.sixrr.metrics.MetricType;
import com.sixrr.stockmetrics.i18n.StockMetricsBundle;
import com.sixrr.stockmetrics.packageCalculators.NumAnonymousClassesPackageCalculator;
import org.jetbrains.annotations.NotNull;

/**
* @author Bas Leijdekkers
*/
public class NumAnonymousClassesPackageMetric extends PackageMetric {

@NotNull
@Override
public String getDisplayName() {
return StockMetricsBundle.message("number.of.anonymous.classes.display.name");
}

@NotNull
@Override
public String getAbbreviation() {
return StockMetricsBundle.message("number.of.anonymous.classes.abbreviation");
}

@NotNull
@Override
public MetricType getType() {
return MetricType.Count;
}

@NotNull
@Override
public MetricCalculator createCalculator() {
return new NumAnonymousClassesPackageCalculator();
}
}
Loading

0 comments on commit 849b5a0

Please sign in to comment.