Skip to content

Commit

Permalink
Restricted qualifiedName interface
Browse files Browse the repository at this point in the history
  • Loading branch information
oowekyala committed Aug 4, 2017
1 parent 152dddf commit 2771448
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 67 deletions.
Expand Up @@ -11,6 +11,8 @@
import net.sourceforge.pmd.lang.metrics.MetricKey;

/**
* Holds the key creation method until we move it to the MetricKey interface.
*
* @author Clément Fournier
*/
public class MetricKeyUtil {
Expand Down
Expand Up @@ -18,20 +18,12 @@ public interface QualifiedName {


/**
* Returns the operation specific part of the name. It identifies an operation in its namespace.
* Returns the qualified name of the class the resource is located in. If this instance addresses a class, returns
* this instance.
*
* @return The operation string.
* @return The qualified name of the class
*/
String getOperation();


/**
* Returns the class specific part of the name. It identifies a class in the namespace it's declared in. If the
* class is nested inside another, then the array returned contains all enclosing classes in order.
*
* @return The class names array.
*/
String[] getClasses();
QualifiedName getClassName();


/**
Expand Down
Expand Up @@ -38,11 +38,11 @@ public JavaQualifiedName getQualifiedName() {
if (isNested()) {
ASTAnyTypeDeclaration parent = this.getFirstParentOfType(ASTAnyTypeDeclaration.class);
JavaQualifiedName parentQN = parent.getQualifiedName();
qualifiedName = JavaQualifiedName.makeNestedClassOf(parentQN, this.getImage());
qualifiedName = JavaQualifiedName.ofNestedClass(parentQN, this.getImage());
return qualifiedName;
}

qualifiedName = JavaQualifiedName.makeOuterClassOf(this);
qualifiedName = JavaQualifiedName.ofOuterClass(this);
}

return qualifiedName;
Expand Down
Expand Up @@ -52,11 +52,11 @@ public JavaQualifiedName getQualifiedName() {
if (isNested()) {
ASTClassOrInterfaceDeclaration parent = this.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
JavaQualifiedName parentQN = parent.getQualifiedName();
qualifiedName = JavaQualifiedName.makeNestedClassOf(parentQN, this.getImage());
qualifiedName = JavaQualifiedName.ofNestedClass(parentQN, this.getImage());
return qualifiedName;
}

qualifiedName = JavaQualifiedName.makeOuterClassOf(this);
qualifiedName = JavaQualifiedName.ofOuterClass(this);
}

return qualifiedName;
Expand Down
Expand Up @@ -47,7 +47,7 @@ public void setContainsComment() {
@Override
public JavaQualifiedName getQualifiedName() {
if (qualifiedName == null) {
qualifiedName = JavaQualifiedName.makeOperationOf(this);
qualifiedName = JavaQualifiedName.ofOperation(this);
}
return qualifiedName;
}
Expand Down
Expand Up @@ -34,11 +34,11 @@ public JavaQualifiedName getQualifiedName() {
if (isNested()) {
ASTAnyTypeDeclaration parent = this.getFirstParentOfType(ASTAnyTypeDeclaration.class);
JavaQualifiedName parentQN = parent.getQualifiedName();
qualifiedName = JavaQualifiedName.makeNestedClassOf(parentQN, this.getImage());
qualifiedName = JavaQualifiedName.ofNestedClass(parentQN, this.getImage());
return qualifiedName;
}

qualifiedName = JavaQualifiedName.makeOuterClassOf(this);
qualifiedName = JavaQualifiedName.ofOuterClass(this);
}

return qualifiedName;
Expand Down
Expand Up @@ -119,7 +119,7 @@ public ASTNameList getThrows() {
@Override
public JavaQualifiedName getQualifiedName() {
if (qualifiedName == null) {
qualifiedName = JavaQualifiedName.makeOperationOf(this);
qualifiedName = JavaQualifiedName.ofOperation(this);
}
return qualifiedName;
}
Expand Down
Expand Up @@ -37,13 +37,13 @@ private JavaQualifiedName() {
*
* @return The qualified name of the node
*/
/* default */ static JavaQualifiedName makeOperationOf(ASTMethodDeclaration node) {
/* default */ static JavaQualifiedName ofOperation(ASTMethodDeclaration node) {
JavaQualifiedName parentQname = node.getFirstParentOfType(ASTAnyTypeDeclaration.class)
.getQualifiedName();

return makeOperationOf(parentQname,
node.getMethodName(),
node.getFirstDescendantOfType(ASTFormalParameters.class));
return ofOperation(parentQname,
node.getMethodName(),
node.getFirstDescendantOfType(ASTFormalParameters.class));
}


Expand All @@ -54,17 +54,17 @@ private JavaQualifiedName() {
*
* @return The qualified name of the node
*/
/* default */ static JavaQualifiedName makeOperationOf(ASTConstructorDeclaration node) {
/* default */ static JavaQualifiedName ofOperation(ASTConstructorDeclaration node) {
ASTClassOrInterfaceDeclaration parent = node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);

return makeOperationOf(parent.getQualifiedName(),
parent.getImage(),
node.getFirstDescendantOfType(ASTFormalParameters.class));
return ofOperation(parent.getQualifiedName(),
parent.getImage(),
node.getFirstDescendantOfType(ASTFormalParameters.class));
}


/** Factorises the functionality of makeOperationof() */
private static JavaQualifiedName makeOperationOf(JavaQualifiedName parent, String opName, ASTFormalParameters params) {
private static JavaQualifiedName ofOperation(JavaQualifiedName parent, String opName, ASTFormalParameters params) {
JavaQualifiedName qname = new JavaQualifiedName();

qname.packages = parent.packages;
Expand All @@ -83,7 +83,7 @@ private static JavaQualifiedName makeOperationOf(JavaQualifiedName parent, Strin
*
* @return The qualified name of the nested class
*/
/* default */ static JavaQualifiedName makeNestedClassOf(JavaQualifiedName parent, String className) {
/* default */ static JavaQualifiedName ofNestedClass(JavaQualifiedName parent, String className) {
JavaQualifiedName qname = new JavaQualifiedName();
qname.packages = parent.packages;
if (parent.classes[0] != null) {
Expand All @@ -103,7 +103,7 @@ private static JavaQualifiedName makeOperationOf(JavaQualifiedName parent, Strin
*
* @return The qualified name of the node
*/
/* default */ static JavaQualifiedName makeOuterClassOf(ASTAnyTypeDeclaration node) {
/* default */ static JavaQualifiedName ofOuterClass(ASTAnyTypeDeclaration node) {
ASTPackageDeclaration pkg = node.getFirstParentOfType(ASTCompilationUnit.class)
.getFirstChildOfType(ASTPackageDeclaration.class);

Expand All @@ -116,7 +116,7 @@ private static JavaQualifiedName makeOperationOf(JavaQualifiedName parent, Strin


// Might be useful with type resolution
public static JavaQualifiedName makeClassOf(Class<?> clazz) {
public static JavaQualifiedName ofClass(Class<?> clazz) {
throw new UnsupportedOperationException();
}

Expand All @@ -141,7 +141,7 @@ public static JavaQualifiedName makeClassOf(Class<?> clazz) {
*
* @return A qualified name instance corresponding to the parsed string.
*/
public static JavaQualifiedName parseName(String name) {
public static JavaQualifiedName ofString(String name) {
JavaQualifiedName qname = new JavaQualifiedName();

Matcher matcher = FORMAT.matcher(name);
Expand Down Expand Up @@ -180,11 +180,13 @@ private static String getOperationName(String methodName, ASTFormalParameters pa
return sb.toString();
}


@Override
public boolean isClass() {
return classes[0] != null && operation == null;
}


@Override
public boolean isOperation() {
return operation != null;
Expand All @@ -200,16 +202,41 @@ public String[] getPackages() {
return packages;
}

@Override

/**
* Returns the class specific part of the name. It identifies a class in the namespace it's declared in. If the
* class is nested inside another, then the array returned contains all enclosing classes in order.
*
* @return The class names array.
*/
public String[] getClasses() {
return classes;
}

@Override

/**
* Returns the operation specific part of the name. It identifies an operation in its namespace.
*
* @return The operation string.
*/
public String getOperation() {
return operation;
}


@Override
public JavaQualifiedName getClassName() {
if (isClass()) {
return this;
}

JavaQualifiedName qname = new JavaQualifiedName();
qname.classes = this.classes;
qname.packages = this.packages;
return qname;
}


@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Expand Up @@ -22,10 +22,10 @@ class JavaMetricsVisitor extends JavaParserVisitorReducedAdapter {

private final Stack<ClassStats> stack = new Stack<>();
private final PackageStats toplevel;
private final ProjectMemoizer<ASTAnyTypeDeclaration, ASTMethodOrConstructorDeclaration> memoizer;
private final JavaProjectMemoizer memoizer;


JavaMetricsVisitor(PackageStats toplevel, ProjectMemoizer<ASTAnyTypeDeclaration, ASTMethodOrConstructorDeclaration> memoizer) {
JavaMetricsVisitor(PackageStats toplevel, JavaProjectMemoizer memoizer) {
this.toplevel = toplevel;
this.memoizer = memoizer;
}
Expand Down
Expand Up @@ -29,21 +29,26 @@
*
* <p>The standard version of the metric complies with McCabe's original definition [3]:
*
* <ul> <li>+1 for every control flow statement ({@code if, case, catch, throw, do, while, for, break, continue}) and
* <ul>
* <li>+1 for every control flow statement ({@code if, case, catch, throw, do, while, for, break, continue}) and
* conditional expression ({@code ? : }). Notice switch cases count as one, but not the switch itself: the point is that
* a switch should have the same complexity value as the equivalent series of {@code if} statements. <li>{@code else},
* {@code finally} and {@code default} don't count; <li>+1 for every boolean operator ({@code &&, ||}) in the guard
* condition of a control flow statement. That's because Java has short-circuit evaluation semantics for boolean
* operators, which makes every boolean operator kind of a control flow statement in itself. </ul>
* a switch should have the same complexity value as the equivalent series of {@code if} statements.
* <li>{@code else}, {@code finally} and {@code default} don't count;
* <li>+1 for every boolean operator ({@code &&, ||}) in the guard condition of a control flow statement. That's because
* Java has short-circuit evaluation semantics for boolean operators, which makes every boolean operator kind of a
* control flow statement in itself.
* </ul>
*
* <p>Version {@link CycloVersion#IGNORE_BOOLEAN_PATHS}: Boolean operators are not counted, which means that empty
* fall-through cases in {@code switch} statements are not counted as well.
*
* <p>References:
*
* <ul> <li> [1] Lanza, Object-Oriented Metrics in Practice, 2005. <li> [2] McCabe, A Complexity Measure, in Proceedings
* of the 2nd ICSE (1976). <li> [3] <a href="https://docs.sonarqube.org/display/SONAR/Metrics+-+Complexity">Sonarqube
* online documentation</a> </ul>
* <ul>
* <li> [1] Lanza, Object-Oriented Metrics in Practice, 2005.
* <li> [2] McCabe, A Complexity Measure, in Proceedings of the 2nd ICSE (1976).
* <li> [3] <a href="https://docs.sonarqube.org/display/SONAR/Metrics+-+Complexity">Sonarqube online documentation</a>
* </ul>
*
* @author Clément Fournier
* @since June 2017
Expand Down
Expand Up @@ -218,8 +218,8 @@ public void testMethodOverload() {

@Test
public void testParseClass() {
JavaQualifiedName outer = JavaQualifiedName.parseName("foo.bar.Bzaz");
JavaQualifiedName nested = JavaQualifiedName.parseName("foo.bar.Bzaz$Bolg");
JavaQualifiedName outer = JavaQualifiedName.ofString("foo.bar.Bzaz");
JavaQualifiedName nested = JavaQualifiedName.ofString("foo.bar.Bzaz$Bolg");

assertEquals(1, outer.getClasses().length);
assertEquals("Bzaz", outer.getClasses()[0]);
Expand All @@ -232,8 +232,8 @@ public void testParseClass() {

@Test
public void testParsePackages() {
JavaQualifiedName packs = JavaQualifiedName.parseName("foo.bar.Bzaz$Bolg");
JavaQualifiedName nopacks = JavaQualifiedName.parseName(".Bzaz");
JavaQualifiedName packs = JavaQualifiedName.ofString("foo.bar.Bzaz$Bolg");
JavaQualifiedName nopacks = JavaQualifiedName.ofString(".Bzaz");

assertNotNull(packs.getPackages());
assertEquals("foo", packs.getPackages()[0]);
Expand All @@ -244,21 +244,21 @@ public void testParsePackages() {

@Test
public void testParseOperation() {
JavaQualifiedName noparams = JavaQualifiedName.parseName("foo.bar.Bzaz$Bolg#bar()");
JavaQualifiedName params = JavaQualifiedName.parseName("foo.bar.Bzaz#bar(String, int)");
JavaQualifiedName noparams = JavaQualifiedName.ofString("foo.bar.Bzaz$Bolg#bar()");
JavaQualifiedName params = JavaQualifiedName.ofString("foo.bar.Bzaz#bar(String, int)");

assertEquals("bar()", noparams.getOperation());
assertEquals("bar(String, int)", params.getOperation());
}

@Test
public void testParseMalformed() {
assertNull(JavaQualifiedName.parseName(".foo.bar.Bzaz"));
assertNull(JavaQualifiedName.parseName("foo.bar."));
assertNull(JavaQualifiedName.parseName("foo.bar.Bzaz#foo"));
assertNull(JavaQualifiedName.parseName("foo.bar.Bzaz()"));
assertNull(JavaQualifiedName.parseName("foo.bar.Bzaz#foo(String,)"));
assertNull(JavaQualifiedName.parseName("foo.bar.Bzaz#foo(String , int)"));
assertNull(JavaQualifiedName.ofString(".foo.bar.Bzaz"));
assertNull(JavaQualifiedName.ofString("foo.bar."));
assertNull(JavaQualifiedName.ofString("foo.bar.Bzaz#foo"));
assertNull(JavaQualifiedName.ofString("foo.bar.Bzaz()"));
assertNull(JavaQualifiedName.ofString("foo.bar.Bzaz#foo(String,)"));
assertNull(JavaQualifiedName.ofString("foo.bar.Bzaz#foo(String , int)"));
}


Expand Down
Expand Up @@ -71,7 +71,7 @@ public void testFieldsAreThere() {

final JavaFieldSigMask fieldSigMask = new JavaFieldSigMask();

JavaQualifiedName clazz = JavaQualifiedName.parseName("net.sourceforge.pmd.lang.java"
JavaQualifiedName clazz = JavaQualifiedName.ofString("net.sourceforge.pmd.lang.java"
+ ".metrics.testdata"
+ ".MetricsVisitorTestData");
String[] fieldNames = {"x", "y", "z", "t"};
Expand All @@ -95,7 +95,7 @@ public void testStaticOperationsSig() {
final JavaOperationSigMask operationSigMask = new JavaOperationSigMask();
operationSigMask.restrictRolesTo(Role.STATIC);

JavaQualifiedName q1 = JavaQualifiedName.parseName("net.sourceforge.pmd.lang.java"
JavaQualifiedName q1 = JavaQualifiedName.ofString("net.sourceforge.pmd.lang.java"
+ ".metrics.testdata"
+ ".MetricsVisitorTestData#mystatic1()");

Expand Down
Expand Up @@ -40,7 +40,7 @@ public void setUp() {

@Test
public void testAddClass() {
JavaQualifiedName qname = JavaQualifiedName.parseName("org.foo.Boo");
JavaQualifiedName qname = JavaQualifiedName.ofString("org.foo.Boo");

assertNull(pack.getClassStats(qname, false));
assertNotNull(pack.getClassStats(qname, true));
Expand Down Expand Up @@ -75,7 +75,7 @@ public void testAddField() {

ASTFieldDeclaration node = getOrderedNodes(ASTFieldDeclaration.class, TEST).get(0);

JavaQualifiedName qname = JavaQualifiedName.parseName("org.foo.Boo");
JavaQualifiedName qname = JavaQualifiedName.ofString("org.foo.Boo");
String fieldName = "bar";
JavaFieldSignature signature = JavaFieldSignature.buildFor(node);

Expand Down

0 comments on commit 2771448

Please sign in to comment.