use of net.sourceforge.pmd.RuleContext in project pmd by pmd.
the class StdCyclomaticComplexityRuleTest method entryStackMustBeEmpty.
/**
* Make sure the entry stack is empty, if show classes complexity is
* disabled.
*
* @see <a href="https://sourceforge.net/p/pmd/bugs/1501/">bug #1501</a>
*/
@Test
public void entryStackMustBeEmpty() {
StdCyclomaticComplexityRule rule = new StdCyclomaticComplexityRule();
rule.setProperty(StdCyclomaticComplexityRule.SHOW_CLASSES_COMPLEXITY_DESCRIPTOR, Boolean.FALSE);
RuleContext ctx = new RuleContext();
LanguageVersion javaLanguageVersion = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.8");
ParserOptions parserOptions = javaLanguageVersion.getLanguageVersionHandler().getDefaultParserOptions();
Parser parser = javaLanguageVersion.getLanguageVersionHandler().getParser(parserOptions);
Node node = parser.parse("test", new StringReader("public class SampleClass {}"));
rule.apply(Arrays.asList(node), ctx);
Assert.assertTrue(rule.entryStack.isEmpty());
}
use of net.sourceforge.pmd.RuleContext in project pmd by pmd.
the class UnnecessaryWrapperObjectCreationRule method visit.
public Object visit(ASTPrimaryPrefix node, Object data) {
if (node.jjtGetNumChildren() == 0 || !(node.jjtGetChild(0) instanceof ASTName)) {
return super.visit(node, data);
}
String image = ((ASTName) node.jjtGetChild(0)).getImage();
if (image.startsWith("java.lang.")) {
image = image.substring(10);
}
boolean checkBoolean = ((RuleContext) data).getLanguageVersion().compareTo(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.5")) >= 0;
if (PREFIX_SET.contains(image) || checkBoolean && "Boolean.valueOf".equals(image)) {
ASTPrimaryExpression parent = (ASTPrimaryExpression) node.jjtGetParent();
if (parent.jjtGetNumChildren() >= 3) {
Node n = parent.jjtGetChild(2);
if (n instanceof ASTPrimarySuffix) {
ASTPrimarySuffix suffix = (ASTPrimarySuffix) n;
image = suffix.getImage();
if (SUFFIX_SET.contains(image) || checkBoolean && "booleanValue".equals(image)) {
super.addViolation(data, node);
return data;
}
}
}
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.RuleContext in project pmd by pmd.
the class BigIntegerInstantiationRule method visit.
@Override
public Object visit(ASTAllocationExpression node, Object data) {
Node type = node.jjtGetChild(0);
if (!(type instanceof ASTClassOrInterfaceType)) {
return super.visit(node, data);
}
boolean jdk15 = ((RuleContext) data).getLanguageVersion().compareTo(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.5")) >= 0;
if ((TypeHelper.isA((ASTClassOrInterfaceType) type, BigInteger.class) || jdk15 && TypeHelper.isA((ASTClassOrInterfaceType) type, BigDecimal.class)) && !node.hasDescendantOfType(ASTArrayDimsAndInits.class)) {
ASTArguments args = node.getFirstChildOfType(ASTArguments.class);
if (args.getArgumentCount() == 1) {
ASTLiteral literal = node.getFirstDescendantOfType(ASTLiteral.class);
if (literal == null || literal.jjtGetParent().jjtGetParent().jjtGetParent().jjtGetParent().jjtGetParent() != args) {
return super.visit(node, data);
}
String img = literal.getImage();
if (literal.isStringLiteral()) {
img = img.substring(1, img.length() - 1);
}
if ("0".equals(img) || "1".equals(img) || jdk15 && "10".equals(img)) {
addViolation(data, node);
return data;
}
}
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.RuleContext in project pmd by pmd.
the class GenericClassCounterRule method addAMatch.
private void addAMatch(Node node, Object data) {
// We have a match, we increment
RuleContext ctx = (RuleContext) data;
AtomicLong total = (AtomicLong) ctx.getAttribute(counterLabel);
total.incrementAndGet();
// And we keep a ref on the node for the report generation
this.matches.add(node);
}
use of net.sourceforge.pmd.RuleContext in project pmd by pmd.
the class AbstractRule method addViolationWithMessage.
/**
* @see RuleViolationFactory#addViolation(RuleContext, Rule, Node, String,
* Object[])
*/
public void addViolationWithMessage(Object data, Node node, String message, int beginLine, int endLine) {
RuleContext ruleContext = (RuleContext) data;
ruleContext.getLanguageVersion().getLanguageVersionHandler().getRuleViolationFactory().addViolation(ruleContext, this, node, message, beginLine, endLine, null);
}
Aggregations