From 1c1e97e7e49a726088ed5752bc620fd68274209f Mon Sep 17 00:00:00 2001 From: oowekyala Date: Sun, 16 Jul 2017 14:44:50 +0200 Subject: [PATCH] Added factory methods for custom metric keys --- .../pmd/lang/java/oom/api/ClassMetricKey.java | 80 +++++++++++++++---- .../lang/java/oom/api/OperationMetricKey.java | 49 ++++++++++-- .../pmd/lang/java/oom/metrics/AtfdMetric.java | 9 +-- .../lang/java/oom/metrics/CycloMetric.java | 10 +-- .../pmd/lang/java/oom/metrics/LocMetric.java | 8 +- .../pmd/lang/java/oom/metrics/NcssMetric.java | 9 +-- .../pmd/lang/java/oom/DataStructureTest.java | 54 +++++-------- .../java/oom/ParameterizedMetricKeyTest.java | 15 +--- 8 files changed, 136 insertions(+), 98 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/api/ClassMetricKey.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/api/ClassMetricKey.java index e82edc7917..49b6ef1739 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/api/ClassMetricKey.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/api/ClassMetricKey.java @@ -5,10 +5,10 @@ package net.sourceforge.pmd.lang.java.oom.api; import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration; -import net.sourceforge.pmd.lang.java.oom.metrics.AtfdMetric; -import net.sourceforge.pmd.lang.java.oom.metrics.CycloMetric; -import net.sourceforge.pmd.lang.java.oom.metrics.LocMetric; -import net.sourceforge.pmd.lang.java.oom.metrics.NcssMetric; +import net.sourceforge.pmd.lang.java.oom.metrics.AtfdMetric.AtfdClassMetric; +import net.sourceforge.pmd.lang.java.oom.metrics.CycloMetric.CycloClassMetric; +import net.sourceforge.pmd.lang.java.oom.metrics.LocMetric.LocClassMetric; +import net.sourceforge.pmd.lang.java.oom.metrics.NcssMetric.NcssClassMetric; import net.sourceforge.pmd.lang.java.oom.metrics.WmcMetric; /** @@ -16,20 +16,40 @@ import net.sourceforge.pmd.lang.java.oom.metrics.WmcMetric; */ public enum ClassMetricKey implements MetricKey { - /** Access to Foreign Data. */ - ATFD(new AtfdMetric.ClassMetric()), + /** + * Access to Foreign Data. + * + * @see net.sourceforge.pmd.lang.java.oom.metrics.AtfdMetric + */ + ATFD(new AtfdClassMetric()), - /** Weighed Method Count. */ + /** + * Weighed Method Count. + * + * @see WmcMetric + */ WMC(new WmcMetric()), - /** Cyclomatic complexity. */ - CYCLO(new CycloMetric.ClassMetric()), + /** + * Cyclomatic complexity. + * + * @see net.sourceforge.pmd.lang.java.oom.metrics.CycloMetric + */ + CYCLO(new CycloClassMetric()), - /** Non Commenting Source Statements. */ - NCSS(new NcssMetric.ClassMetric()), + /** + * Non Commenting Source Statements. + * + * @see net.sourceforge.pmd.lang.java.oom.metrics.NcssMetric + */ + NCSS(new NcssClassMetric()), - /** Lines of Code. */ - LOC(new LocMetric.ClassMetric()); + /** + * Lines of Code. + * + * @see net.sourceforge.pmd.lang.java.oom.metrics.LocMetric + */ + LOC(new LocClassMetric()); private final ClassMetric calculator; @@ -49,5 +69,37 @@ public enum ClassMetricKey implements MetricKey { public boolean supports(ASTAnyTypeDeclaration node) { return calculator.supports(node); } - + + + /** + * Creates a new metric key holding a metric which can be computed on a class. + * + * TODO:cf Generify and move to MetricKey after upgrading compiler to 1.8 + * + * @param metric The metric to use + * @param name The name of the metric + * + * @return The metric key + */ + public static MetricKey of(final Metric metric, final String name) { + return new MetricKey() { + @Override + public String name() { + return name; + } + + + @Override + public Metric getCalculator() { + return metric; + } + + + @Override + public boolean supports(ASTAnyTypeDeclaration node) { + return metric.supports(node); + } + }; + } + } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/api/OperationMetricKey.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/api/OperationMetricKey.java index 5fcba168c5..3eae5891d0 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/api/OperationMetricKey.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/api/OperationMetricKey.java @@ -5,10 +5,10 @@ package net.sourceforge.pmd.lang.java.oom.api; import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration; -import net.sourceforge.pmd.lang.java.oom.metrics.AtfdMetric; -import net.sourceforge.pmd.lang.java.oom.metrics.CycloMetric; -import net.sourceforge.pmd.lang.java.oom.metrics.LocMetric; -import net.sourceforge.pmd.lang.java.oom.metrics.NcssMetric; +import net.sourceforge.pmd.lang.java.oom.metrics.AtfdMetric.AtfdOperationMetric; +import net.sourceforge.pmd.lang.java.oom.metrics.CycloMetric.CycloOperationMetric; +import net.sourceforge.pmd.lang.java.oom.metrics.LocMetric.LocOperationMetric; +import net.sourceforge.pmd.lang.java.oom.metrics.NcssMetric.NcssOperationMetric; /** * Keys identifying standard operation metrics. @@ -16,16 +16,16 @@ import net.sourceforge.pmd.lang.java.oom.metrics.NcssMetric; public enum OperationMetricKey implements MetricKey { /** Access to Foreign Data. */ - ATFD(new AtfdMetric.OperationMetric()), + ATFD(new AtfdOperationMetric()), /** Cyclomatic complexity. */ - CYCLO(new CycloMetric.OperationMetric()), + CYCLO(new CycloOperationMetric()), /** Non Commenting Source Statements. */ - NCSS(new NcssMetric.OperationMetric()), + NCSS(new NcssOperationMetric()), /** Lines of Code. */ - LOC(new LocMetric.OperationMetric()); + LOC(new LocOperationMetric()); private final OperationMetric calculator; @@ -46,4 +46,37 @@ public enum OperationMetricKey implements MetricKey of(final Metric metric, final String name) { + return new MetricKey() { + @Override + public String name() { + return name; + } + + + @Override + public Metric getCalculator() { + return metric; + } + + + @Override + public boolean supports(ASTMethodOrConstructorDeclaration node) { + return metric.supports(node); + } + }; + } + + } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/AtfdMetric.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/AtfdMetric.java index b6ce406b8a..58ce0895a8 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/AtfdMetric.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/AtfdMetric.java @@ -22,12 +22,7 @@ import net.sourceforge.pmd.lang.java.oom.signature.Signature.Visibility; public final class AtfdMetric { - private AtfdMetric() { - - } - - - public static final class OperationMetric extends AbstractOperationMetric { + public static final class AtfdOperationMetric extends AbstractOperationMetric { @Override // TODO:cf public double computeFor(ASTMethodOrConstructorDeclaration node, MetricVersion version) { @@ -48,7 +43,7 @@ public final class AtfdMetric { } } - public static final class ClassMetric extends AbstractClassMetric { + public static final class AtfdClassMetric extends AbstractClassMetric { @Override public double computeFor(ASTAnyTypeDeclaration node, MetricVersion version) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/CycloMetric.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/CycloMetric.java index 39980f5502..8641ee4b0c 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/CycloMetric.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/CycloMetric.java @@ -48,11 +48,6 @@ import net.sourceforge.pmd.lang.java.oom.metrics.visitors.StandardCycloVisitor; */ public final class CycloMetric { - - private CycloMetric() { - - } - // TODO:cf Cyclo should develop factorized boolean operators to count them /** Variants of CYCLO. */ @@ -61,7 +56,7 @@ public final class CycloMetric { IGNORE_BOOLEAN_PATHS } - public static final class OperationMetric extends AbstractOperationMetric { + public static final class CycloOperationMetric extends AbstractOperationMetric { @Override public double computeFor(ASTMethodOrConstructorDeclaration node, MetricVersion version) { @@ -75,12 +70,11 @@ public final class CycloMetric { } } - public static final class ClassMetric extends AbstractClassMetric { + public static final class CycloClassMetric extends AbstractClassMetric { @Override public double computeFor(ASTAnyTypeDeclaration node, MetricVersion version) { return 1 + Metrics.get(OperationMetricKey.CYCLO, node, version, ResultOption.AVERAGE); } } - } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/LocMetric.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/LocMetric.java index b35894e5ef..526f4c78d2 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/LocMetric.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/LocMetric.java @@ -19,12 +19,8 @@ import net.sourceforge.pmd.lang.java.oom.api.MetricVersion; */ public final class LocMetric { - private LocMetric() { - } - - - public static final class OperationMetric extends AbstractOperationMetric { + public static final class LocOperationMetric extends AbstractOperationMetric { @Override public boolean supports(ASTMethodOrConstructorDeclaration node) { @@ -38,7 +34,7 @@ public final class LocMetric { } } - public static final class ClassMetric extends AbstractClassMetric { + public static final class LocClassMetric extends AbstractClassMetric { @Override public boolean supports(ASTAnyTypeDeclaration node) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/NcssMetric.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/NcssMetric.java index 70459699b7..4bc067bd99 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/NcssMetric.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/NcssMetric.java @@ -30,18 +30,13 @@ import net.sourceforge.pmd.lang.java.oom.metrics.visitors.JavaNcssVisitor; public final class NcssMetric { - private NcssMetric() { - - } - - /** Variants of NCSS. */ public enum Version implements MetricVersion { /** JavaNCSS compliant cyclo visitor. */ JAVANCSS } - public static final class ClassMetric extends AbstractClassMetric { + public static final class NcssClassMetric extends AbstractClassMetric { @Override public boolean supports(ASTAnyTypeDeclaration node) { @@ -61,7 +56,7 @@ public final class NcssMetric { } - public static final class OperationMetric extends AbstractOperationMetric { + public static final class NcssOperationMetric extends AbstractOperationMetric { @Override public boolean supports(ASTMethodOrConstructorDeclaration node) { diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/DataStructureTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/DataStructureTest.java index fa01499305..bfe405480c 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/DataStructureTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/DataStructureTest.java @@ -27,11 +27,13 @@ import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration; import net.sourceforge.pmd.lang.java.ast.JavaParserVisitorReducedAdapter; import net.sourceforge.pmd.lang.java.ast.QualifiedName; -import net.sourceforge.pmd.lang.java.oom.api.ClassMetric; +import net.sourceforge.pmd.lang.java.oom.api.ClassMetricKey; import net.sourceforge.pmd.lang.java.oom.api.Metric.Version; import net.sourceforge.pmd.lang.java.oom.api.MetricKey; import net.sourceforge.pmd.lang.java.oom.api.MetricVersion; -import net.sourceforge.pmd.lang.java.oom.api.OperationMetric; +import net.sourceforge.pmd.lang.java.oom.api.OperationMetricKey; +import net.sourceforge.pmd.lang.java.oom.metrics.AbstractClassMetric; +import net.sourceforge.pmd.lang.java.oom.metrics.AbstractOperationMetric; import net.sourceforge.pmd.lang.java.oom.signature.FieldSigMask; import net.sourceforge.pmd.lang.java.oom.signature.FieldSignature; import net.sourceforge.pmd.lang.java.oom.signature.OperationSigMask; @@ -46,30 +48,8 @@ import net.sourceforge.pmd.lang.java.oom.testdata.MetricsVisitorTestData; */ public class DataStructureTest extends ParserTst { - MetricKey classMetricKey = new MetricKey() { - @Override - public String name() { - return null; - } - - - @Override - public ClassMetric getCalculator() { - return new RandomMetric(); - } - }; - MetricKey opMetricKey = new MetricKey() { - @Override - public String name() { - return null; - } - - - @Override - public OperationMetric getCalculator() { - return new RandomMetric(); - } - }; + MetricKey classMetricKey = ClassMetricKey.of(new RandomClassMetric(), null); + MetricKey opMetricKey = OperationMetricKey.of(new RandomOperationMetric(), null); private PackageStats pack; @@ -177,12 +157,20 @@ public class DataStructureTest extends ParserTst { assertNotEquals(reference.get(i), real.get(i)); } } - - /** - * Test metric. - */ - private class RandomMetric extends AbstractMetric implements ClassMetric, OperationMetric { + + private class RandomOperationMetric extends AbstractOperationMetric { + + private Random random = new Random(); + + + @Override + public double computeFor(ASTMethodOrConstructorDeclaration node, MetricVersion version) { + return random.nextInt(); + } + } + + private class RandomClassMetric extends AbstractClassMetric { private Random random = new Random(); @@ -193,10 +181,6 @@ public class DataStructureTest extends ParserTst { } - @Override - public double computeFor(ASTMethodOrConstructorDeclaration node, MetricVersion version) { - return random.nextInt(); - } } diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/ParameterizedMetricKeyTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/ParameterizedMetricKeyTest.java index 785199f28e..4b5c405561 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/ParameterizedMetricKeyTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/ParameterizedMetricKeyTest.java @@ -12,7 +12,7 @@ import static org.junit.Assert.assertTrue; import org.junit.Test; -import net.sourceforge.pmd.lang.java.oom.api.ClassMetric; +import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration; import net.sourceforge.pmd.lang.java.oom.api.ClassMetricKey; import net.sourceforge.pmd.lang.java.oom.api.MetricKey; import net.sourceforge.pmd.lang.java.oom.api.MetricVersion; @@ -71,18 +71,7 @@ public class ParameterizedMetricKeyTest { @Test public void testAdHocMetricKey() { - MetricKey adHocKey = new MetricKey() { - @Override - public String name() { - return "metric"; - } - - - @Override - public ClassMetric getCalculator() { - return null; - } - }; + MetricKey adHocKey = ClassMetricKey.of(null, "metric"); MetricVersion adHocVersion = new MetricVersion() { @Override