Formatting and minor changes

This commit is contained in:
David Renz committed 2016-04-15 15:58:35 +02:00
1 parent 116e66bfd9
commit f53f6594de
19 files changed
+215 -235

No files matched your search

@@ -10,7 +10,7 @@ public class ApexLanguageModule extends BaseLanguageModule {
public static final String NAME = "Apex";
public static final String TERSE_NAME = "apex";
public static final String[] EXTENSIONS = {"cls", "trigger"};
public static final String[] EXTENSIONS = { "cls", "trigger" };
public ApexLanguageModule() {
super(NAME, null, TERSE_NAME, ApexRuleChainVisitor.class, EXTENSIONS);
@@ -25,8 +25,7 @@ public class ASTUserClass extends ApexRootNode<UserClass> {
field.setAccessible(true);
Identifier name = (Identifier) field.get(node);
return name.value;
}
catch (Exception e) {
} catch (Exception e) {
e.printStackTrace();
}
return super.getImage();
@@ -25,8 +25,7 @@ public class ASTUserInterface extends ApexRootNode<UserInterface> {
field.setAccessible(true);
Identifier name = (Identifier) field.get(node);
return name.value;
}
catch (Exception e) {
} catch (Exception e) {
e.printStackTrace();
}
return super.getImage();
@@ -20,68 +20,74 @@ public abstract class AbstractApexNode<T extends AstNode> extends AbstractNode i
this.node = node;
}
void calculateLineNumbers(SourceCodePositioner positioner) {
if (!hasRealLoc()) {
return;
}
void calculateLineNumbers(SourceCodePositioner positioner) {
if (!hasRealLoc()) {
return;
}
RealLoc loc = (RealLoc)node.getLoc();
int startOffset = loc.startIndex;
int endOffset = loc.endIndex;
RealLoc loc = (RealLoc) node.getLoc();
int startOffset = loc.startIndex;
int endOffset = loc.endIndex;
this.beginLine = positioner.lineNumberFromOffset(startOffset);
this.beginColumn = positioner.columnFromOffset(this.beginLine, startOffset);
this.endLine = positioner.lineNumberFromOffset(endOffset);
this.endColumn = positioner.columnFromOffset(this.endLine, endOffset) - 1; // end column is inclusive
if (this.endColumn < 0) {
this.endColumn = 0;
}
}
this.beginLine = positioner.lineNumberFromOffset(startOffset);
this.beginColumn = positioner.columnFromOffset(this.beginLine, startOffset);
this.endLine = positioner.lineNumberFromOffset(endOffset);
this.endColumn = positioner.columnFromOffset(this.endLine, endOffset) - 1; // end
// column
// is
// inclusive
if (this.endColumn < 0) {
this.endColumn = 0;
}
}
@Override
public int getBeginLine() {
if (this.beginLine > 0) {
return this.beginLine;
}
Node parent = jjtGetParent();
if (parent != null) {
return parent.getBeginLine();
}
throw new RuntimeException("Unable to determine beginning line of Node.");
}
@Override
public int getBeginColumn() {
if (this.beginColumn > 0) {
return this.beginColumn;
}
Node parent = jjtGetParent();
if (parent != null) {
return parent.getBeginColumn();
}
throw new RuntimeException("Unable to determine beginning column of Node.");
}
@Override
public int getEndLine() {
if (this.endLine > 0) {
return this.endLine;
}
Node parent = jjtGetParent();
if (parent != null) {
return parent.getEndLine();
}
throw new RuntimeException("Unable to determine ending line of Node.");
}
@Override
public int getEndColumn() {
if (this.endColumn > 0) {
return this.endColumn;
}
Node parent = jjtGetParent();
if (parent != null) {
return parent.getEndColumn();
}
throw new RuntimeException("Unable to determine ending column of Node.");
}
@Override
public int getBeginLine() {
if (this.beginLine > 0) {
return this.beginLine;
}
Node parent = jjtGetParent();
if (parent != null) {
return parent.getBeginLine();
}
throw new RuntimeException("Unable to determine beginning line of Node.");
}
@Override
public int getBeginColumn() {
if (this.beginColumn > 0) {
return this.beginColumn;
}
Node parent = jjtGetParent();
if (parent != null) {
return parent.getBeginColumn();
}
throw new RuntimeException("Unable to determine beginning column of Node.");
}
@Override
public int getEndLine() {
if (this.endLine > 0) {
return this.endLine;
}
Node parent = jjtGetParent();
if (parent != null) {
return parent.getEndLine();
}
throw new RuntimeException("Unable to determine ending line of Node.");
}
@Override
public int getEndColumn() {
if (this.endColumn > 0) {
return this.endColumn;
}
Node parent = jjtGetParent();
if (parent != null) {
return parent.getEndColumn();
}
throw new RuntimeException("Unable to determine ending column of Node.");
}
/**
* Accept the visitor. *
@@ -106,17 +112,14 @@ public abstract class AbstractApexNode<T extends AstNode> extends AbstractNode i
try {
Loc loc = node.getLoc();
return loc instanceof RealLoc;
}
catch (UnexpectedCodePathException e) {
} catch (UnexpectedCodePathException e) {
return false;
}
catch (IndexOutOfBoundsException e) {
} catch (IndexOutOfBoundsException e) {
// bug in apex-jorje? happens on some ReferenceExpression nodes
return false;
}
catch (NullPointerException e) {
// bug in apex-jorje?
return false;
} catch (NullPointerException e) {
// bug in apex-jorje?
return false;
}
}
@@ -126,10 +129,10 @@ public abstract class AbstractApexNode<T extends AstNode> extends AbstractNode i
}
public String getLocation() {
if (hasRealLoc()) {
return String.valueOf(node.getLoc());
} else {
return "no location";
}
if (hasRealLoc()) {
return String.valueOf(node.getLoc());
} else {
return "no location";
}
}
}
@@ -25,7 +25,7 @@ public class ApexParser {
protected final ApexParserOptions parserOptions;
private Map<Integer, String> suppressMap;
private String suppressMarker = "NOPMD"; // that's the default value
private String suppressMarker = "NOPMD";
public ApexParser(ApexParserOptions parserOptions) {
this.parserOptions = parserOptions;
@@ -57,8 +57,7 @@ public class ApexParser {
ApexNode<Compilation> tree = treeBuilder.build(astRoot);
return tree;
}
catch (IOException e) {
} catch (IOException e) {
throw new ParseException(e);
}
}
@@ -128,8 +128,7 @@ public class CompilerService {
.invoke(CompilerStage.ADDITIONAL_VALIDATE);
operation.invoke(compilerContext, unit);
}
}
catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
}
}
@@ -22,8 +22,7 @@ public class DumpFacade {
this.dump(node, prefix);
try {
writer.flush();
}
catch (IOException e) {
} catch (IOException e) {
throw new RuntimeException("Problem flushing PrintWriter.", e);
}
}
@@ -35,8 +34,7 @@ public class DumpFacade {
visit((ApexNode<?>) node.jjtGetChild(i), data + " ");
}
return data;
}
else {
} else {
return data;
}
}
@@ -115,21 +115,14 @@ public abstract class AbstractApexRule extends AbstractRule implements ApexParse
for (Object element : nodes) {
if (element instanceof ASTUserClass) {
visit((ASTUserClass) element, ctx);
}
else if (element instanceof ASTUserInterface) {
} else if (element instanceof ASTUserInterface) {
visit((ASTUserInterface) element, ctx);
}
else if (element instanceof ASTUserTrigger) {
} else if (element instanceof ASTUserTrigger) {
visit((ASTUserTrigger) element, ctx);
}
}
}
//
// The following APIs are identical to those in ApexParserVisitorAdapter.
// Due to Java single inheritance, it preferred to extend from the more
// complex Rule base class instead of from relatively simple Visitor.
//
@Override
public Object visit(ApexNode<?> node, Object data) {
node.childrenAccept(this, data);
@@ -18,7 +18,6 @@ import net.sourceforge.pmd.lang.rule.XPathRule;
public class ApexRuleChainVisitor extends AbstractRuleChainVisitor {
protected void indexNodes(List<Node> nodes, RuleContext ctx) {
// Visit Nodes in DFS order
Stack<Node> stack = new Stack<>();
stack.addAll(nodes);
Collections.reverse(stack);
@@ -34,11 +33,9 @@ public class ApexRuleChainVisitor extends AbstractRuleChainVisitor {
}
protected void visit(Rule rule, Node node, RuleContext ctx) {
// Rule better either be a ApexParserVisitor, or a XPathRule
if (rule instanceof XPathRule) {
((XPathRule) rule).evaluate(node, ctx);
}
else {
} else {
((ApexNode<?>) node).jjtAccept((ApexParserVisitor) rule, ctx);
}
}
@@ -29,7 +29,7 @@ public class AvoidLogicInTriggerRule extends AbstractApexRule {
}
n = n.jjtGetParent();
}
return false;
}
}
@@ -40,7 +40,7 @@ public class AvoidSoqlInLoopsRule extends AbstractApexRule {
}
n = n.jjtGetParent();
}
return false;
}
}
@@ -24,139 +24,141 @@ import net.sourceforge.pmd.stat.DataPoint;
import net.sourceforge.pmd.util.NumericConstants;
/**
* Abstract superclass for NCSS counting methods. Counts tokens according to <a
* href="http://www.kclee.de/clemens/java/javancss/">JavaNCSS rules</a>.
* Abstract superclass for NCSS counting methods. Counts tokens according to
* <a href="http://www.kclee.de/clemens/java/javancss/">JavaNCSS rules</a>.
*
* @author ported from Java original of Jason Bennett
*/
public abstract class AbstractNcssCountRule extends AbstractStatisticalApexRule {
private Class<?> nodeClass;
private Class<?> nodeClass;
/**
* Count the nodes of the given type using NCSS rules.
*
* @param nodeClass class of node to count
*/
protected AbstractNcssCountRule(Class<?> nodeClass) {
this.nodeClass = nodeClass;
}
/**
* Count the nodes of the given type using NCSS rules.
*
* @param nodeClass
* class of node to count
*/
protected AbstractNcssCountRule(Class<?> nodeClass) {
this.nodeClass = nodeClass;
}
@Override
public Object visit(ApexNode<?> node, Object data) {
int numNodes = 0;
@Override
public Object visit(ApexNode<?> node, Object data) {
int numNodes = 0;
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
ApexNode<?> n = (ApexNode<?>) node.jjtGetChild(i);
Integer treeSize = (Integer) n.jjtAccept(this, data);
numNodes += treeSize.intValue();
}
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
ApexNode<?> n = (ApexNode<?>) node.jjtGetChild(i);
Integer treeSize = (Integer) n.jjtAccept(this, data);
numNodes += treeSize.intValue();
}
if (this.nodeClass.isInstance(node)) {
// Add 1 to account for base node
numNodes++;
DataPoint point = new DataPoint();
point.setNode(node);
point.setScore(1.0 * numNodes);
point.setMessage(getMessage());
addDataPoint(point);
}
if (this.nodeClass.isInstance(node)) {
// Add 1 to account for base node
numNodes++;
DataPoint point = new DataPoint();
point.setNode(node);
point.setScore(1.0 * numNodes);
point.setMessage(getMessage());
addDataPoint(point);
}
return Integer.valueOf(numNodes);
}
return Integer.valueOf(numNodes);
}
/**
* Count the number of children of the given Java node. Adds one to count
* the node itself.
*
* @param node java node having children counted
* @param data node data
* @return count of the number of children of the node, plus one
*/
protected Integer countNodeChildren(Node node, Object data) {
Integer nodeCount = null;
int lineCount = 0;
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
nodeCount = (Integer) ((ApexNode<?>) node.jjtGetChild(i)).jjtAccept(this, data);
lineCount += nodeCount.intValue();
}
return ++lineCount;
}
/**
* Count the number of children of the given node. Adds one to count the
* node itself.
*
* @param node
* node having children counted
* @param data
* node data
* @return count of the number of children of the node, plus one
*/
protected Integer countNodeChildren(Node node, Object data) {
Integer nodeCount = null;
int lineCount = 0;
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
nodeCount = (Integer) ((ApexNode<?>) node.jjtGetChild(i)).jjtAccept(this, data);
lineCount += nodeCount.intValue();
}
return ++lineCount;
}
@Override
public Object visit(ASTForLoopStatement node, Object data) {
return countNodeChildren(node, data);
}
@Override
public Object visit(ASTForLoopStatement node, Object data) {
return countNodeChildren(node, data);
}
@Override
public Object visit(ASTForEachStatement node, Object data) {
return countNodeChildren(node, data);
}
@Override
public Object visit(ASTForEachStatement node, Object data) {
return countNodeChildren(node, data);
}
@Override
public Object visit(ASTDoLoopStatement node, Object data) {
return countNodeChildren(node, data);
}
@Override
public Object visit(ASTDoLoopStatement node, Object data) {
return countNodeChildren(node, data);
}
@Override
public Object visit(ASTIfBlockStatement node, Object data) {
@Override
public Object visit(ASTIfBlockStatement node, Object data) {
Integer lineCount = countNodeChildren(node, data);
return lineCount;
}
Integer lineCount = countNodeChildren(node, data);
return lineCount;
}
@Override
public Object visit(ASTIfElseBlockStatement node, Object data) {
@Override
public Object visit(ASTIfElseBlockStatement node, Object data) {
Integer lineCount = countNodeChildren(node, data);
lineCount++;
Integer lineCount = countNodeChildren(node, data);
lineCount++;
return lineCount;
}
return lineCount;
}
@Override
public Object visit(ASTWhileLoopStatement node, Object data) {
return countNodeChildren(node, data);
}
@Override
public Object visit(ASTWhileLoopStatement node, Object data) {
return countNodeChildren(node, data);
}
@Override
public Object visit(ASTBreakStatement node, Object data) {
return NumericConstants.ONE;
}
@Override
public Object visit(ASTBreakStatement node, Object data) {
return NumericConstants.ONE;
}
@Override
public Object visit(ASTTryCatchFinallyBlockStatement node, Object data) {
return countNodeChildren(node, data);
}
@Override
public Object visit(ASTTryCatchFinallyBlockStatement node, Object data) {
return countNodeChildren(node, data);
}
@Override
public Object visit(ASTContinueStatement node, Object data) {
return NumericConstants.ONE;
}
@Override
public Object visit(ASTContinueStatement node, Object data) {
return NumericConstants.ONE;
}
@Override
public Object visit(ASTReturnStatement node, Object data) {
return countNodeChildren(node, data);
}
@Override
public Object visit(ASTReturnStatement node, Object data) {
return countNodeChildren(node, data);
}
@Override
public Object visit(ASTThrowStatement node, Object data) {
return countNodeChildren(node, data);
}
@Override
public Object visit(ASTThrowStatement node, Object data) {
return countNodeChildren(node, data);
}
@Override
public Object visit(ASTStatement node, Object data) {
return NumericConstants.ONE;
}
@Override
public Object visit(ASTStatement node, Object data) {
return NumericConstants.ONE;
}
@Override
public Object visit(ASTVariableDeclaration node, Object data) {
return countNodeChildren(node, data);
}
@Override
public Object visit(ASTVariableDeclaration node, Object data) {
return countNodeChildren(node, data);
}
@Override
public Object visit(ASTMethodCallExpression node, Object data) {
return countNodeChildren(node, data);
}
@Override
public Object visit(ASTMethodCallExpression node, Object data) {
return countNodeChildren(node, data);
}
}
@@ -19,10 +19,10 @@ public class ExcessiveClassLengthRule extends ExcessiveLengthRule {
@Override
public Object visit(ASTUserClass node, Object data) {
if(node.getNode().getModifiers().getModifiers().not(IS_TEST)) {
if (node.getNode().getModifiers().getModifiers().not(IS_TEST)) {
return super.visit(node, data);
}
return data;
}
}
@@ -14,7 +14,7 @@ import net.sourceforge.pmd.lang.apex.ast.ASTMethod;
import net.sourceforge.pmd.util.NumericConstants;
/**
* @author aglover
* @author ported from Java original of aglover
* <p/>
* Class Name: ExcessivePublicCount
* <p/>
@@ -23,7 +23,7 @@ public class AvoidDeeplyNestedIfStmtsRule extends AbstractApexRule {
public Object visit(ASTUserClass node, Object data) {
depth = 0;
depthLimit = getProperty(PROBLEM_DEPTH_DESCRIPTOR);
return super.visit(node, data);
}
@@ -35,7 +35,7 @@ public class AvoidDeeplyNestedIfStmtsRule extends AbstractApexRule {
addViolation(data, node);
}
depth--;
return data;
}
}
@@ -7,14 +7,6 @@ import net.sourceforge.pmd.lang.apex.ast.ApexNode;
import net.sourceforge.pmd.lang.apex.rule.AbstractStatisticalApexRule;
import net.sourceforge.pmd.stat.DataPoint;
/**
* This is a common super class for things which have excessive length.
* <p/>
* i.e. LongMethod and LongClass rules.
* <p/>
* To implement an ExcessiveLength rule, you pass in the Class of node you want
* to check, and this does the rest for you.
*/
public class ExcessiveLengthRule extends AbstractStatisticalApexRule {
private Class<?> nodeClass;
@@ -151,8 +151,7 @@ public class VariableNamingConventionsRule extends AbstractApexRule {
new Object[] { varName });
}
return data;
}
else if (!isFinal) {
} else if (!isFinal) {
String normalizedVarName = normalizeVariableName(varName, prefixes, suffixes);
if (normalizedVarName.indexOf('_') >= 0) {
@@ -48,10 +48,12 @@ public class ApexParserTest {
ApexNode<Compilation> rootNode = parse(code);
assertPosition(rootNode, 1, 14, 6, 2); // whole source code, well from the beginning of the class name
assertPosition(rootNode, 1, 14, 6, 2); // whole source code, well from
// the beginning of the class
// name
// Modifier of the class - doesn't work. This node just sees the
// identifier ("SimpleClass")
//assertPosition(rootNode.jjtGetChild(0), 1, 1, 1, 6); // "public"
// assertPosition(rootNode.jjtGetChild(0), 1, 1, 1, 6); // "public"
// "method1" - starts with identifier until end of its block statement
Node method1 = rootNode.jjtGetChild(1);
@@ -72,20 +74,20 @@ public class ApexParserTest {
@Test
public void verifyEndLine() {
String code = "public class SimpleClass {\n" // line 1
+ " public void method1() {\n" // line 2
+ " }\n" // line 3
+ " public void method2() {\n" // line 4
+ " public void method2() {\n" // line 4
+ " }\n" // line 5
+ "}\n"; // line 6
ApexNode<Compilation> rootNode = parse(code);
Node method1 = rootNode.jjtGetChild(1);
assertEquals("Wrong begin line", 2, method1.getBeginLine());
assertEquals("Wrong end line", 3, method1.getEndLine());
Node method2 = rootNode.jjtGetChild(2);
assertEquals("Wrong begin line", 4, method2.getBeginLine());
assertEquals("Wrong end line", 5, method2.getEndLine());
@@ -103,14 +105,12 @@ public class ApexParserTest {
ApexNode<Compilation> rootNode = parse(sourceCode);
}
}
}
catch (IOException e) {
} catch (IOException e) {
Assert.fail();
}
}
// TEST HELPER
private static void assertPosition(Node node, int beginLine, int beginColumn, int endLine, int endColumn) {
@@ -16,8 +16,8 @@ public class CodesizeRulesTest extends SimpleAggregatorTst {
addRule(RULESET, "ExcessiveParameterList");
addRule(RULESET, "ExcessivePublicCount");
addRule(RULESET, "StdCyclomaticComplexity");
addRule(RULESET, "NcssConstructorCount");
addRule(RULESET, "NcssMethodCount");
addRule(RULESET, "NcssTypeCount");
addRule(RULESET, "NcssConstructorCount");
addRule(RULESET, "NcssMethodCount");
addRule(RULESET, "NcssTypeCount");
}
}