Search in sources :

Example 1 with ParserOptions

use of net.sourceforge.pmd.lang.ParserOptions in project pmd by pmd.

the class JavaRuleViolationTest method parse.

private ASTCompilationUnit parse(final String code) {
    final LanguageVersionHandler languageVersionHandler = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler();
    final ParserOptions options = languageVersionHandler.getDefaultParserOptions();
    final ASTCompilationUnit ast = (ASTCompilationUnit) languageVersionHandler.getParser(options).parse(null, new StringReader(code));
    // set scope of AST nodes
    ast.jjtAccept(new ScopeAndDeclarationFinder(), null);
    return ast;
}
Also used : ScopeAndDeclarationFinder(net.sourceforge.pmd.lang.java.symboltable.ScopeAndDeclarationFinder) ParserOptions(net.sourceforge.pmd.lang.ParserOptions) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) StringReader(java.io.StringReader) LanguageVersionHandler(net.sourceforge.pmd.lang.LanguageVersionHandler)

Example 2 with ParserOptions

use of net.sourceforge.pmd.lang.ParserOptions 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 3 with ParserOptions

use of net.sourceforge.pmd.lang.ParserOptions 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(ApexLanguageModule.NAME).getDefaultVersion();
    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 4 with ParserOptions

use of net.sourceforge.pmd.lang.ParserOptions in project controller by opendaylight.

the class AbstractGeneratedObjectTest method parse.

protected Node parse(File dstFile) throws IOException {
    assertNotNull(dstFile);
    LOG.debug(Files.toString(dstFile, StandardCharsets.UTF_8));
    Parser parser = new Java17Parser(new ParserOptions());
    return parser.parse(dstFile.toString(), new FileReader(dstFile));
}
Also used : ParserOptions(net.sourceforge.pmd.lang.ParserOptions) FileReader(java.io.FileReader) Java17Parser(net.sourceforge.pmd.lang.java.Java17Parser) Java17Parser(net.sourceforge.pmd.lang.java.Java17Parser) Parser(net.sourceforge.pmd.lang.Parser)

Example 5 with ParserOptions

use of net.sourceforge.pmd.lang.ParserOptions in project pmd by pmd.

the class EcmascriptParserOptionsTest method testEqualsHashcode.

@Test
public void testEqualsHashcode() throws Exception {
    BooleanProperty[] properties = { EcmascriptParserOptions.RECORDING_COMMENTS_DESCRIPTOR, EcmascriptParserOptions.RECORDING_LOCAL_JSDOC_COMMENTS_DESCRIPTOR };
    for (int i = 0; i < properties.length; i++) {
        BooleanProperty property = properties[i];
        MyRule rule = new MyRule();
        rule.setProperty(property, true);
        ParserOptions options1 = rule.getParserOptions();
        rule.setProperty(property, false);
        ParserOptions options2 = rule.getParserOptions();
        rule.setProperty(property, true);
        ParserOptions options3 = rule.getParserOptions();
        rule.setProperty(property, false);
        ParserOptions options4 = rule.getParserOptions();
        verifyOptionsEqualsHashcode(options1, options2, options3, options4);
    }
    EcmascriptParserOptions options1 = new EcmascriptParserOptions();
    options1.setSuppressMarker("foo");
    EcmascriptParserOptions options2 = new EcmascriptParserOptions();
    options2.setSuppressMarker("bar");
    EcmascriptParserOptions options3 = new EcmascriptParserOptions();
    options3.setSuppressMarker("foo");
    EcmascriptParserOptions options4 = new EcmascriptParserOptions();
    options4.setSuppressMarker("bar");
    verifyOptionsEqualsHashcode(options1, options2, options3, options4);
    options1 = new EcmascriptParserOptions();
    options1.setRhinoLanguageVersion(EcmascriptParserOptions.Version.VERSION_DEFAULT);
    options2 = new EcmascriptParserOptions();
    options2.setRhinoLanguageVersion(EcmascriptParserOptions.Version.VERSION_1_8);
    options3 = new EcmascriptParserOptions();
    options3.setRhinoLanguageVersion(EcmascriptParserOptions.Version.VERSION_DEFAULT);
    options4 = new EcmascriptParserOptions();
    options4.setRhinoLanguageVersion(EcmascriptParserOptions.Version.VERSION_1_8);
    verifyOptionsEqualsHashcode(options1, options2, options3, options4);
}
Also used : ParserOptions(net.sourceforge.pmd.lang.ParserOptions) BooleanProperty(net.sourceforge.pmd.properties.BooleanProperty) Test(org.junit.Test)

Aggregations

ParserOptions (net.sourceforge.pmd.lang.ParserOptions)9 Test (org.junit.Test)6 StringReader (java.io.StringReader)5 Parser (net.sourceforge.pmd.lang.Parser)5 RuleContext (net.sourceforge.pmd.RuleContext)4 LanguageVersion (net.sourceforge.pmd.lang.LanguageVersion)4 Node (net.sourceforge.pmd.lang.ast.Node)4 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)3 LanguageVersionHandler (net.sourceforge.pmd.lang.LanguageVersionHandler)2 JaxenXPathRuleQuery (net.sourceforge.pmd.lang.rule.xpath.JaxenXPathRuleQuery)2 SaxonXPathRuleQuery (net.sourceforge.pmd.lang.rule.xpath.SaxonXPathRuleQuery)2 XPathRuleQuery (net.sourceforge.pmd.lang.rule.xpath.XPathRuleQuery)2 BooleanProperty (net.sourceforge.pmd.properties.BooleanProperty)2 PropertyDescriptor (net.sourceforge.pmd.properties.PropertyDescriptor)2 FileReader (java.io.FileReader)1 Java17Parser (net.sourceforge.pmd.lang.java.Java17Parser)1 ScopeAndDeclarationFinder (net.sourceforge.pmd.lang.java.symboltable.ScopeAndDeclarationFinder)1