Added factory methods for custom metric keys
This commit is contained in:
@ -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<ASTAnyTypeDeclaration> {
|
||||
|
||||
/** 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<ASTAnyTypeDeclaration> {
|
||||
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<ASTAnyTypeDeclaration> of(final Metric<ASTAnyTypeDeclaration> metric, final String name) {
|
||||
return new MetricKey<ASTAnyTypeDeclaration>() {
|
||||
@Override
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Metric<ASTAnyTypeDeclaration> getCalculator() {
|
||||
return metric;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean supports(ASTAnyTypeDeclaration node) {
|
||||
return metric.supports(node);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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<ASTMethodOrConstructorDeclaration> {
|
||||
|
||||
/** 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<ASTMethodOrConstructorDeclar
|
||||
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<ASTMethodOrConstructorDeclaration> of(final Metric<ASTMethodOrConstructorDeclaration> metric, final String name) {
|
||||
return new MetricKey<ASTMethodOrConstructorDeclaration>() {
|
||||
@Override
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Metric<ASTMethodOrConstructorDeclaration> getCalculator() {
|
||||
return metric;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean supports(ASTMethodOrConstructorDeclaration node) {
|
||||
return metric.supports(node);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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<ClassMetric> classMetricKey = new MetricKey<ClassMetric>() {
|
||||
@Override
|
||||
public String name() {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ClassMetric getCalculator() {
|
||||
return new RandomMetric();
|
||||
}
|
||||
};
|
||||
MetricKey<OperationMetric> opMetricKey = new MetricKey<OperationMetric>() {
|
||||
@Override
|
||||
public String name() {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public OperationMetric getCalculator() {
|
||||
return new RandomMetric();
|
||||
}
|
||||
};
|
||||
MetricKey<ASTAnyTypeDeclaration> classMetricKey = ClassMetricKey.of(new RandomClassMetric(), null);
|
||||
MetricKey<ASTMethodOrConstructorDeclaration> 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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -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<ClassMetric> adHocKey = new MetricKey<ClassMetric>() {
|
||||
@Override
|
||||
public String name() {
|
||||
return "metric";
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ClassMetric getCalculator() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
MetricKey<ASTAnyTypeDeclaration> adHocKey = ClassMetricKey.of(null, "metric");
|
||||
|
||||
MetricVersion adHocVersion = new MetricVersion() {
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user