diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ApexQualifiedName.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ApexQualifiedName.java index 720923795..f65605d0a 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ApexQualifiedName.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ApexQualifiedName.java @@ -7,7 +7,6 @@ import java.util.Arrays; import java.util.List; import java.util.Objects; -import java.util.regex.Pattern; import org.apache.commons.lang3.StringUtils; @@ -22,7 +21,6 @@ */ public class ApexQualifiedName implements QualifiedName { - private static final Pattern FORMAT = Pattern.compile("(\\w+)__(\\w+)(.(\\w+))?(#(\\w+))?"); // TODO private final String nameSpace; private final String[] classes; @@ -36,13 +34,11 @@ private ApexQualifiedName(String nameSpace, String[] classes, String operation) } - public String getOperation() { return operation; } - public String[] getClasses() { return Arrays.copyOf(classes, classes.length); } @@ -117,6 +113,20 @@ public boolean equals(Object obj) { } + /** + * Parses a string conforming to the format defined below and returns an ApexQualifiedName. + * + *

Here are some examples of the format: + *

+ * + * @param toParse The string to parse + * + * @return An ApexQualifiedName, or null if the string couldn't be parsed + */ + // private static final Pattern FORMAT = Pattern.compile("(\\w+)__(\\w+)(.(\\w+))?(#(\\w+))?"); // TODO public static ApexQualifiedName ofString(String toParse) { throw new UnsupportedOperationException(); } diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/metrics/impl/CycloMetric.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/metrics/impl/CycloMetric.java index 737aad1b3..1946dfa24 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/metrics/impl/CycloMetric.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/metrics/impl/CycloMetric.java @@ -15,7 +15,7 @@ import net.sourceforge.pmd.lang.apex.metrics.impl.visitors.StandardCycloVisitor; import net.sourceforge.pmd.lang.metrics.MetricVersion; -import apex.jorje.semantic.ast.expression.BooleanExpressionType; +import apex.jorje.data.ast.BooleanOp; /** * See the doc for the Java metric. @@ -44,8 +44,8 @@ public static int booleanExpressionComplexity(ASTStandardCondition expression) { int complexity = 0; for (ASTBooleanExpression sub : subs) { - BooleanExpressionType type = sub.getNode().getBooleanExpressionType(); - if (type != null && (type == BooleanExpressionType.OR || type == BooleanExpressionType.AND)) { + BooleanOp op = sub.getNode().getOp(); + if (op != null && (op == BooleanOp.AND || op == BooleanOp.OR)) { complexity++; } } diff --git a/pmd-apex/src/main/resources/rulesets/apex/metrics.xml b/pmd-apex/src/main/resources/rulesets/apex/metrics.xml index 0709228dc..4d7468f92 100644 --- a/pmd-apex/src/main/resources/rulesets/apex/metrics.xml +++ b/pmd-apex/src/main/resources/rulesets/apex/metrics.xml @@ -11,10 +11,10 @@ + externalInfoUrl="${pmd.website.baseurl}/rules/apex/metrics.html#CyclomaticComplexity"> acu = parseAndVisitForString("public with sharing class MetadataDeployController \n" - + "{\n" - + "\tprivate class Foo {\n" - + "}\n" - + "\n" - + "\tglobal String ZipData { get; set; }\t\n" - + "\t\n" - + "\tpublic MetadataService.AsyncResult AsyncResult {get; private set;}\n" - + "\t\n" - + "\tpublic String getPackageXml(String page)\n" - + "\t{\n" - + "\t\treturn '' + \n" - + "\t\t\t'' + \n" - + " \t\t\t'' + \n" - + " \t\t\t'HelloWorld' +\n" - + " \t\t\t'ApexClass' + \n" - + " \t\t\t'' + \n" - + " \t\t\t'26.0' + \n" - + "\t\t\t'';\t\t\n" - + "\t}\n" - + "\t\n" - + "\tpublic String getHelloWorldMetadata()\n" - + "\t{\n" - + "\t\treturn '' +\n" - + "\t\t\t'' +\n" - + "\t\t\t '28.0' + \n" - + "\t\t\t 'Active' +\n" - + "\t\t\t'';\t\t\n" - + "\t}\n" - + "\t\n" - + "\tpublic String getHelloWorld()\t\n" - + "\t{\n" - + "\t\treturn 'public class HelloWorld' + \n" - + "\t\t\t'{' + \n" - + "\t\t\t\t'public static void helloWorld()' +\n" - + "\t\t\t\t'{' + \n" - + "\t\t\t\t\t'System.debug(\\' Hello World\\');' +\n" - + "\t\t\t\t'}' +\n" - + "\t\t\t'}';\n" - + "\t}" - + "}"); + public void testOperationsAreThere() throws IOException { + ApexNode acu = parseAndVisitForString( + IOUtils.toString(ApexMetricsVisitorTest.class.getResourceAsStream("MetadataDeployController.cls"))); final ApexSignatureMatcher toplevel = ApexMetrics.getFacade().getProjectMirror(); diff --git a/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/metrics/ApexProjectMirrorTest.java b/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/metrics/ApexProjectMirrorTest.java index 6a12068c4..f7e65b4f9 100644 --- a/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/metrics/ApexProjectMirrorTest.java +++ b/pmd-apex/src/test/java/net/sourceforge/pmd/lang/apex/metrics/ApexProjectMirrorTest.java @@ -8,10 +8,12 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Random; +import org.apache.commons.io.IOUtils; import org.junit.Test; import net.sourceforge.pmd.lang.apex.ast.ASTMethod; @@ -34,52 +36,21 @@ */ public class ApexProjectMirrorTest { - private static ApexNode acu - = parseAndVisitForString("public with sharing class MetadataDeployController \n" - + "{\n" - + "\tprivate class Foo {\n" - + "}\n" - + "\n" - + "\tglobal String ZipData { get; set; }\t\n" - + "\t\n" - + "\tpublic MetadataService.AsyncResult AsyncResult {get; private set;}\n" - + "\t\n" - + "\tpublic String getPackageXml(String page)\n" - + "\t{\n" - + "\t\treturn '' + \n" - + "\t\t\t'' + \n" - + " \t\t\t'' + \n" - + " \t\t\t'HelloWorld' +\n" - + " \t\t\t'ApexClass' + \n" - + " \t\t\t'' + \n" - + " \t\t\t'26.0' + \n" - + "\t\t\t'';\t\t\n" - + "\t}\n" - + "\t\n" - + "\tpublic String getHelloWorldMetadata()\n" - + "\t{\n" - + "\t\treturn '' +\n" - + "\t\t\t'' +\n" - + "\t\t\t '28.0' + \n" - + "\t\t\t 'Active' +\n" - + "\t\t\t'';\t\t\n" - + "\t}\n" - + "\t\n" - + "\tpublic String getHelloWorld()\t\n" - + "\t{\n" - + "\t\treturn 'public class HelloWorld' + \n" - + "\t\t\t'{' + \n" - + "\t\t\t\t'public static void helloWorld()' +\n" - + "\t\t\t\t'{' + \n" - + "\t\t\t\t\t'System.debug(\\' Hello World\\');' +\n" - + "\t\t\t\t'}' +\n" - + "\t\t\t'}';\n" - + "\t}" - + "}"); + private static ApexNode acu; private MetricKey> classMetricKey = MetricKeyUtil.of(new RandomClassMetric(), null); private MetricKey opMetricKey = MetricKeyUtil.of(new RandomOperationMetric(), null); + static { + try { + acu = parseAndVisitForString( + IOUtils.toString(ApexMetricsVisitorTest.class.getResourceAsStream("MetadataDeployController.cls"))); + } catch (IOException ioe) { + // Should definitely not happen + } + } + + @Test public void memoizationTest() { diff --git a/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/metrics/MetadataDeployController.cls b/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/metrics/MetadataDeployController.cls new file mode 100644 index 000000000..12af4169b --- /dev/null +++ b/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/metrics/MetadataDeployController.cls @@ -0,0 +1,41 @@ +public with sharing class MetadataDeployController +{ + private class Foo { + } + + global String ZipData { get; set; } + + public MetadataService.AsyncResult AsyncResult {get; private set;} + + public String getPackageXml(String page) + { + return '' + + '' + + '' + + 'HelloWorld' + + 'ApexClass' + + '' + + '26.0' + + ''; + } + + public String getHelloWorldMetadata() + { + return '' + + '' + + '28.0' + + 'Active' + + ''; + } + + public String getHelloWorld() + { + return 'public class HelloWorld' + + '{' + + 'public static void helloWorld()' + + '{' + + 'System.debug(\' Hello World\');' + + '}' + + '}'; + } +} \ No newline at end of file