Pull corrections

This commit is contained in:
oowekyala
2017-06-16 19:11:05 +02:00
10 changed files with 64 additions and 48 deletions

View File

@ -12,9 +12,13 @@ import java.util.regex.Pattern;
* Represents Qualified Names for use within PackageStats.
* TODO:cf make unit tests once the visitor is working to ensure new implementations won't break it
*/
public class QualifiedName {
public final class QualifiedName {
/** See {@link QualifiedName#parseName(String)}. */
/**
* Pattern specifying the format.
*
* <p>{@code ((\w+\.)+|\.)((\w+)(\$\w+)*)(#(\w+)\(((\w+)(, \w+)*)?\))?}
*/
public static final Pattern FORMAT = Pattern.compile("((\\w+\\.)+|\\.)((\\w+)(\\$\\w+)*)(#(\\w+)\\(((\\w+)(, \\w+)*)?\\))?");
private String[] packages = null; // unnamed package
@ -116,19 +120,20 @@ public class QualifiedName {
/**
* Parses a qualified name given in the format defined for this implementation. The format
* is described as the following regex pattern :
* is specified by a regex pattern (see {@link QualifiedName#FORMAT}). Examples:
*
* <p>{@code ((\w+\.)+|\.)((\w+)(\$\w+)*)(#(\w+)\(((\w+)(, \w+)*)?\))?}
*
* <p>Notes:
* <p>{@code com.company.MyClass$Nested#myMethod(String, int)}
* <ul>
* <li> Group 1 : dot separated packages, or just dot if unnamed package;
* <li> Group 5 : nested classes are separated by a dollar symbol;
* <li> Group 6 : the optional method suffix is separated from the class with a hashtag;
* <li> Group 8 : method arguments. Note the presence of a single space after commas.
* <li> Packages are separated by full stops;
* <li> Nested classes are separated by a dollar symbol;
* <li> The optional method suffix is separated from the class with a hashtag;
* <li> Method arguments are separated by a comma and a single space.
* </ul>
*
* <p>The pattern is available as a static class member.
* <p>{@code .MyClass$Nested}
* <ul>
* <li> A class in the unnamed package is preceded by a single full stop.
* </ul>
*
* @param name The name to parse.
*

View File

@ -18,10 +18,10 @@ public interface ClassMetric extends Metric {
*
* @param node The node.
* @param holder The toplevel package stats (used to help the calculation).
* @param options A possibly empty list of options.
* @param option A possibly empty list of options.
*
* @return The value of the metric.
*/
double computeFor(ASTClassOrInterfaceDeclaration node, PackageStats holder, MetricOption options);
double computeFor(ASTClassOrInterfaceDeclaration node, PackageStats holder, MetricOption option);
}

View File

@ -32,7 +32,7 @@ import net.sourceforge.pmd.lang.java.oom.signature.OperationSignature;
*/
class ClassStats {
private Map<OperationSignature, Set<OperationStats>> operations = new HashMap<>();
private Map<OperationSignature, Map<String, OperationStats>> operations = new HashMap<>();
private Map<FieldSignature, Set<String>> fields = new HashMap<>();
private Map<String, ClassStats> nestedClasses = new HashMap<>();
@ -45,11 +45,21 @@ class ClassStats {
/**
* Finds a ClassStats in the direct children of this class. This cannot be a nested class, for
* that see {@link PackageStats#getClassStats(QualifiedName, boolean)}.
* Finds a ClassStats in the direct children of this class. This can only be a directly nested class, for example
* in the following snippet, A can get B and B can get C but A cannot get C without asking B.
* <pre>
* <code>
* class MyClass { // ClassStats A
* class MyNested { // ClassStats B
* class MyDeeplyNested { // ClassStats C
* }
* }
* }
* </code>
* </pre>
*
* @param className Name of the nested class.
* @param createIfNotFound Create ClassStats if missing.
* @param createIfNotFound Create the requested ClassStats if missing.
*
* @return The new ClassStats or the one that was found. Can return null if createIfNotFound is unset.
*/
@ -68,9 +78,9 @@ class ClassStats {
*/
void addOperation(String name, OperationSignature sig) {
if (!operations.containsKey(sig)) {
operations.put(sig, new HashSet<OperationStats>());
operations.put(sig, new HashMap<String, OperationStats>());
}
operations.get(sig).add(new OperationStats(name));
operations.get(sig).put(name, new OperationStats(name));
}
/**
@ -100,7 +110,7 @@ class ClassStats {
// Indexing on signatures optimises this type of request
for (OperationSignature sig : operations.keySet()) {
if (mask.covers(sig)) {
if (operations.get(sig).contains(new OperationStats(name))) {
if (operations.get(sig).containsKey(name)) {
return true;
}
}
@ -140,17 +150,18 @@ class ClassStats {
*
* @return The result of the computation, or {@code Double.NaN} if it couldn't be performed.
*/
double compute(OperationMetricKey key, ASTMethodOrConstructorDeclaration node, String name, boolean force,
MetricOption options) {
// TODO:cf maybe find a way to optimise this
for (OperationSignature sig : operations.keySet()) {
for (OperationStats stats : operations.get(sig)) {
if (stats.equals(name)) {
return stats.compute(key, node, force, options);
}
}
MetricOption option) {
Map<String, OperationStats> sigMap = operations.get(OperationSignature.buildFor(node));
// TODO:cf the operation signature will be built many times, we might as well store it in the node
if (sigMap == null) {
return Double.NaN;
}
return Double.NaN;
OperationStats stats = sigMap.get(name);
return stats == null ? Double.NaN : stats.compute(key, node, force, option);
}
/**

View File

@ -18,11 +18,11 @@ public interface OperationMetric extends Metric {
*
* @param node The node.
* @param holder The toplevel package stats (used to help the calculation).
* @param options A possibly empty list of options.
* @param option A possibly empty list of options.
*
* @return The value of the metric.
*/
double computeFor(ASTMethodOrConstructorDeclaration node, PackageStats holder, MetricOption options);
double computeFor(ASTMethodOrConstructorDeclaration node, PackageStats holder, MetricOption option);
}

View File

@ -55,14 +55,14 @@ class OperationStats {
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (o instanceof String) {
return o != null && o.equals(name);
} else if (o instanceof OperationStats) {
OperationStats that = (OperationStats) o;
return name != null ? name.equals(that.name) : that.name == null;
} else {
}
if (o == null || getClass() != o.getClass()) {
return false;
}
OperationStats stats = (OperationStats) o;
return name != null ? name.equals(stats.name) : stats.name == null;
}
@Override

View File

@ -26,7 +26,7 @@ import net.sourceforge.pmd.lang.java.oom.signature.Signature.Visibility;
public class AtfdMetric extends AbstractMetric implements ClassMetric, OperationMetric {
@Override
public double computeFor(ASTMethodOrConstructorDeclaration node, PackageStats holder, MetricOption options) {
public double computeFor(ASTMethodOrConstructorDeclaration node, PackageStats holder, MetricOption option) {
if (!isSupported(node)) {
return Double.NaN;
}
@ -47,7 +47,7 @@ public class AtfdMetric extends AbstractMetric implements ClassMetric, Operation
}
@Override
public double computeFor(ASTClassOrInterfaceDeclaration node, PackageStats holder, MetricOption options) {
public double computeFor(ASTClassOrInterfaceDeclaration node, PackageStats holder, MetricOption option) {
// TODO
return 0;
}

View File

@ -40,15 +40,15 @@ import net.sourceforge.pmd.lang.java.oom.metrics.cyclo.CycloPathAwareOperationVi
public class CycloMetric extends AbstractClassMetric implements OperationMetric, ClassMetric {
@Override
public double computeFor(ASTClassOrInterfaceDeclaration node, PackageStats holder, MetricOption options) {
public double computeFor(ASTClassOrInterfaceDeclaration node, PackageStats holder, MetricOption option) {
return sumMetricOverOperations(node, holder, OperationMetricKey.CYCLO, false);
}
@Override
public double computeFor(ASTMethodOrConstructorDeclaration node, PackageStats holder, MetricOption options) {
public double computeFor(ASTMethodOrConstructorDeclaration node, PackageStats holder, MetricOption option) {
CycloOperationVisitor visitor;
if (options.equals(Option.COUNT_SWITCH_STATEMENTS)) {
if (option.equals(Option.COUNT_SWITCH_STATEMENTS)) {
visitor = new CycloOperationVisitor() {
@Override
public Object visit(ASTSwitchStatement node, Object data) {
@ -57,7 +57,7 @@ public class CycloMetric extends AbstractClassMetric implements OperationMetric,
return data;
}
};
} else if (options.equals(Option.COUNT_EXPRESSION_PATHS)) {
} else if (option.equals(Option.COUNT_EXPRESSION_PATHS)) {
visitor = new CycloPathAwareOperationVisitor();
} else {
visitor = new CycloOperationVisitor();

View File

@ -23,12 +23,12 @@ import net.sourceforge.pmd.lang.java.oom.PackageStats;
public class LocMetric extends AbstractClassMetric implements OperationMetric {
@Override
public double computeFor(ASTClassOrInterfaceDeclaration node, PackageStats holder, MetricOption options) {
public double computeFor(ASTClassOrInterfaceDeclaration node, PackageStats holder, MetricOption option) {
return node.getEndLine() - node.getBeginLine();
}
@Override
public double computeFor(ASTMethodOrConstructorDeclaration node, PackageStats holder, MetricOption options) {
public double computeFor(ASTMethodOrConstructorDeclaration node, PackageStats holder, MetricOption option) {
if (node.isAbstract()) {
return 1;
}

View File

@ -47,12 +47,12 @@ public class NcssMetric extends AbstractClassMetric implements OperationMetric {
@Override
public double computeFor(ASTClassOrInterfaceDeclaration node, PackageStats holder, MetricOption options) {
public double computeFor(ASTClassOrInterfaceDeclaration node, PackageStats holder, MetricOption option) {
return ((MutableInt) node.jjtAccept(new NcssCounter(), new MutableInt(1))).getValue();
}
@Override
public double computeFor(ASTMethodOrConstructorDeclaration node, PackageStats holder, MetricOption options) {
public double computeFor(ASTMethodOrConstructorDeclaration node, PackageStats holder, MetricOption option) {
return ((MutableInt) node.jjtAccept(new NcssCounter(), new MutableInt(1))).getValue();
}

View File

@ -23,7 +23,7 @@ import net.sourceforge.pmd.lang.java.oom.PackageStats;
public class WmcMetric extends AbstractClassMetric implements ClassMetric {
@Override
public double computeFor(ASTClassOrInterfaceDeclaration node, PackageStats holder, MetricOption options) {
public double computeFor(ASTClassOrInterfaceDeclaration node, PackageStats holder, MetricOption option) {
return sumMetricOverOperations(node, holder, OperationMetricKey.CYCLO, false);
}
}