Simple Key enum selection

This commit is contained in:
Clément Fournier
2017-03-26 23:51:39 +02:00
committed by Clément Fournier
parent 895a10eaa4
commit 9fedb69d42
8 changed files with 202 additions and 0 deletions

View File

@ -0,0 +1,27 @@
/**
*
*/
package net.sourceforge.pmd.lang.java.metrics;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
/**
* @author Clément Fournier (clement.fournier@insa-rennes.fr)
*
*/
public class AtfdMetric implements ClassMetric, MethodMetric {
@Override
public double computeFor(ASTMethodDeclaration node, DataHolder holder) {
// TODO Auto-generated method stub
return 0;
}
@Override
public double computeFor(ASTClassOrInterfaceDeclaration node, DataHolder holder) {
// TODO Auto-generated method stub
return 0;
}
}

View File

@ -0,0 +1,14 @@
/**
*
*/
package net.sourceforge.pmd.lang.java.metrics;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
/**
* @author Clément Fournier (clement.fournier@insa-rennes.fr)
*
*/
public interface ClassMetric extends Metric {
public double computeFor(ASTClassOrInterfaceDeclaration node, DataHolder holder);
}

View File

@ -0,0 +1,12 @@
/**
*
*/
package net.sourceforge.pmd.lang.java.metrics;
/**
* @author Clément Fournier (clement.fournier@insa-rennes.fr)
*
*/
public class DataHolder {
}

View File

@ -0,0 +1,14 @@
/**
*
*/
package net.sourceforge.pmd.lang.java.metrics;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
/**
* @author Clément Fournier (clement.fournier@insa-rennes.fr)
*
*/
public interface MethodMetric extends Metric {
public double computeFor(ASTMethodDeclaration node, DataHolder holder);
}

View File

@ -0,0 +1,12 @@
/**
*
*/
package net.sourceforge.pmd.lang.java.metrics;
/**
* @author Clément Fournier (clement.fournier@insa-rennes.fr)
*
*/
public interface Metric {
}

View File

@ -0,0 +1,78 @@
/**
*
*/
package net.sourceforge.pmd.lang.java.metrics;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
/**
* @author Clément Fournier (clement.fournier@insa-rennes.fr)
*
*/
public class Metrics {
/* Holds sufficient statistics gathered by the visitor */
private static DataHolder m_holder;
/* References all available metrics */
public static enum Key {
ATFD(new AtfdMetric()),
// ...
WMC(new WmcMetric());
/* The object used to calculate the metric */
private final Metric calculator;
/* Semiprime number, its factors are the flags */
private int flags = 1;
Key(Metric m) {
calculator = m;
if (m instanceof ClassMetric) {
flags *= 2;
}
if (m instanceof MethodMetric) {
flags *= 3;
}
}
boolean isClassMetric() {
return flags % 2 == 0;
}
boolean isMethodMetric() {
return flags % 3 == 0;
}
Metric getCalculator() {
return calculator;
}
}
/**
* Computes a metric identified by its code on the class AST node being
* passed.
*/
public static double get(Key key, ASTClassOrInterfaceDeclaration node) {
if (!key.isClassMetric()) {
throw new UnsupportedOperationException("That metric cannot be computed on a class");
}
return ((ClassMetric) key.getCalculator()).computeFor(node, m_holder);
}
/**
* Computes a metric identified by its code on the method AST node being
* passed.
*/
public static double get(Key key, ASTMethodDeclaration node) {
if (!key.isMethodMetric()) {
throw new UnsupportedOperationException("That metric cannot be computed on a method");
}
return ((MethodMetric) key.getCalculator()).computeFor(node, m_holder);
}
}

View File

@ -0,0 +1,20 @@
/**
*
*/
package net.sourceforge.pmd.lang.java.metrics;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
/**
* @author Clément Fournier (clement.fournier@insa-rennes.fr)
*
*/
public class WmcMetric implements ClassMetric {
@Override
public double computeFor(ASTClassOrInterfaceDeclaration node, DataHolder holder) {
// TODO Auto-generated method stub
return 0;
}
}

View File

@ -0,0 +1,25 @@
/**
*
*/
package net.sourceforge.pmd.lang.java.rule.metrics;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
import net.sourceforge.pmd.lang.java.metrics.Metrics;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
/**
* @author Clément Fournier (clement.fournier@insa-rennes.fr)
*
*/
public class AtfdRule extends AbstractJavaRule {
@Override
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
double atfd = Metrics.get(Metrics.Key.ATFD, node);
if (atfd > .3) {
addViolation(data, node);
}
return data;
}
}