Search in sources :

Example 6 with RuleContext

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());
}
Also used : ParserOptions(net.sourceforge.pmd.lang.ParserOptions) RuleContext(net.sourceforge.pmd.RuleContext) Node(net.sourceforge.pmd.lang.ast.Node) StringReader(java.io.StringReader) LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion) Parser(net.sourceforge.pmd.lang.Parser) Test(org.junit.Test)

Example 7 with RuleContext

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);
}
Also used : RuleContext(net.sourceforge.pmd.RuleContext) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)

Example 8 with RuleContext

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);
}
Also used : RuleContext(net.sourceforge.pmd.RuleContext) Node(net.sourceforge.pmd.lang.ast.Node) BigInteger(java.math.BigInteger) ASTArguments(net.sourceforge.pmd.lang.java.ast.ASTArguments) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)

Example 9 with RuleContext

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);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) RuleContext(net.sourceforge.pmd.RuleContext)

Example 10 with RuleContext

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);
}
Also used : RuleContext(net.sourceforge.pmd.RuleContext)

Aggregations

RuleContext (net.sourceforge.pmd.RuleContext)51 Test (org.junit.Test)19 RuleSets (net.sourceforge.pmd.RuleSets)17 RuleSetFactory (net.sourceforge.pmd.RuleSetFactory)15 Report (net.sourceforge.pmd.Report)14 Node (net.sourceforge.pmd.lang.ast.Node)13 RuleSet (net.sourceforge.pmd.RuleSet)11 StringReader (java.io.StringReader)9 RuleViolation (net.sourceforge.pmd.RuleViolation)9 IOException (java.io.IOException)8 PMDConfiguration (net.sourceforge.pmd.PMDConfiguration)8 PMDException (net.sourceforge.pmd.PMDException)8 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)8 ArrayList (java.util.ArrayList)7 SourceCodeProcessor (net.sourceforge.pmd.SourceCodeProcessor)7 RuleSetNotFoundException (net.sourceforge.pmd.RuleSetNotFoundException)6 LanguageVersion (net.sourceforge.pmd.lang.LanguageVersion)6 DummyNode (net.sourceforge.pmd.lang.ast.DummyNode)5 FooRule (net.sourceforge.pmd.FooRule)4 Parser (net.sourceforge.pmd.lang.Parser)4