Search in sources :

Example 76 with ASTCompilationUnit

use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd by pmd.

the class XPathRuleTest method testFollowingSibling.

/**
 * Following sibling check: See https://sourceforge.net/p/pmd/bugs/1209/
 *
 * @throws Exception
 *             any error
 */
@Test
public void testFollowingSibling() throws Exception {
    final String SOURCE = "public class dummy {\n" + "  public String toString() {\n" + "    String test = \"bad example\";\n" + "    test = \"a\";\n" + "    return test;\n" + "  }\n" + "}";
    LanguageVersion language = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion();
    ParserOptions parserOptions = language.getLanguageVersionHandler().getDefaultParserOptions();
    Parser parser = language.getLanguageVersionHandler().getParser(parserOptions);
    ASTCompilationUnit cu = (ASTCompilationUnit) parser.parse("test", new StringReader(SOURCE));
    RuleContext ruleContext = new RuleContext();
    ruleContext.setLanguageVersion(language);
    String xpath = "//Block/BlockStatement/following-sibling::BlockStatement";
    // XPATH version 1.0
    XPathRuleQuery xpathRuleQuery = new JaxenXPathRuleQuery();
    xpathRuleQuery.setXPath(xpath);
    xpathRuleQuery.setProperties(new HashMap<PropertyDescriptor<?>, Object>());
    xpathRuleQuery.setVersion(XPathRuleQuery.XPATH_1_0);
    List<Node> nodes = xpathRuleQuery.evaluate(cu, ruleContext);
    assertEquals(2, nodes.size());
    assertEquals(4, nodes.get(0).getBeginLine());
    assertEquals(5, nodes.get(1).getBeginLine());
    // XPATH version 2.0
    xpathRuleQuery = new SaxonXPathRuleQuery();
    xpathRuleQuery.setXPath(xpath);
    xpathRuleQuery.setProperties(new HashMap<PropertyDescriptor<?>, Object>());
    xpathRuleQuery.setVersion(XPathRuleQuery.XPATH_2_0);
    nodes = xpathRuleQuery.evaluate(cu, ruleContext);
    assertEquals(2, nodes.size());
    assertEquals(4, nodes.get(0).getBeginLine());
    assertEquals(5, nodes.get(1).getBeginLine());
}
Also used : ParserOptions(net.sourceforge.pmd.lang.ParserOptions) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) RuleContext(net.sourceforge.pmd.RuleContext) PropertyDescriptor(net.sourceforge.pmd.properties.PropertyDescriptor) SaxonXPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.SaxonXPathRuleQuery) Node(net.sourceforge.pmd.lang.ast.Node) Parser(net.sourceforge.pmd.lang.Parser) JaxenXPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.JaxenXPathRuleQuery) StringReader(java.io.StringReader) LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion) XPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.XPathRuleQuery) JaxenXPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.JaxenXPathRuleQuery) SaxonXPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.SaxonXPathRuleQuery) Test(org.junit.Test)

Example 77 with ASTCompilationUnit

use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd by pmd.

the class XPathRuleTest method testImageOfPrimarySuffix.

/**
 * Test for problem reported in bug #1219 PrimarySuffix/@Image does not work
 * in some cases in xpath 2.0
 *
 * @throws Exception
 *             any error
 */
@Test
public void testImageOfPrimarySuffix() throws Exception {
    final String SUFFIX = "import java.io.File;\n" + "\n" + "public class TestSuffix {\n" + "    public static void main(String args[]) {\n" + "        new File(\"subdirectory\").list();\n" + "    }\n" + "}";
    LanguageVersion language = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion();
    ParserOptions parserOptions = language.getLanguageVersionHandler().getDefaultParserOptions();
    Parser parser = language.getLanguageVersionHandler().getParser(parserOptions);
    ASTCompilationUnit cu = (ASTCompilationUnit) parser.parse("test", new StringReader(SUFFIX));
    RuleContext ruleContext = new RuleContext();
    ruleContext.setLanguageVersion(language);
    String xpath = "//PrimarySuffix[@Image='list']";
    // XPATH version 1.0
    XPathRuleQuery xpathRuleQuery = new JaxenXPathRuleQuery();
    xpathRuleQuery.setXPath(xpath);
    xpathRuleQuery.setProperties(new HashMap<PropertyDescriptor<?>, Object>());
    xpathRuleQuery.setVersion(XPathRuleQuery.XPATH_1_0);
    List<Node> nodes = xpathRuleQuery.evaluate(cu, ruleContext);
    assertEquals(1, nodes.size());
    // XPATH version 1.0 Compatibility
    xpathRuleQuery = new SaxonXPathRuleQuery();
    xpathRuleQuery.setXPath(xpath);
    xpathRuleQuery.setProperties(new HashMap<PropertyDescriptor<?>, Object>());
    xpathRuleQuery.setVersion(XPathRuleQuery.XPATH_1_0_COMPATIBILITY);
    nodes = xpathRuleQuery.evaluate(cu, ruleContext);
    assertEquals(1, nodes.size());
    // XPATH version 2.0
    xpathRuleQuery = new SaxonXPathRuleQuery();
    xpathRuleQuery.setXPath(xpath);
    xpathRuleQuery.setProperties(new HashMap<PropertyDescriptor<?>, Object>());
    xpathRuleQuery.setVersion(XPathRuleQuery.XPATH_2_0);
    nodes = xpathRuleQuery.evaluate(cu, ruleContext);
    assertEquals(1, nodes.size());
}
Also used : ParserOptions(net.sourceforge.pmd.lang.ParserOptions) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) RuleContext(net.sourceforge.pmd.RuleContext) PropertyDescriptor(net.sourceforge.pmd.properties.PropertyDescriptor) SaxonXPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.SaxonXPathRuleQuery) Node(net.sourceforge.pmd.lang.ast.Node) Parser(net.sourceforge.pmd.lang.Parser) JaxenXPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.JaxenXPathRuleQuery) StringReader(java.io.StringReader) LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion) XPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.XPathRuleQuery) JaxenXPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.JaxenXPathRuleQuery) SaxonXPathRuleQuery(net.sourceforge.pmd.lang.rule.xpath.SaxonXPathRuleQuery) Test(org.junit.Test)

Example 78 with ASTCompilationUnit

use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd-eclipse-plugin by pmd.

the class PMDGenerateASTAction method generateAST.

/**
 * Generate a AST for a file
 *
 * @param file
 *            a file
 */
private void generateAST(IFile file) {
    LOG.info("Generating AST for file " + file.getName());
    ByteArrayOutputStream byteArrayOutputStream = null;
    ByteArrayInputStream astInputStream = null;
    try {
        JavaParser parser = new JavaParser(new JavaCharStream(file.getContents()));
        parser.setJdkVersion(Integer.MAX_VALUE);
        ASTCompilationUnit compilationUnit = parser.CompilationUnit();
        byteArrayOutputStream = new ByteArrayOutputStream();
        IAstWriter astWriter = PMDPlugin.getDefault().getAstWriter();
        astWriter.write(byteArrayOutputStream, compilationUnit);
        byteArrayOutputStream.flush();
        IFile astFile = createASTFile(file);
        if (astFile != null) {
            if (astFile.exists()) {
                astFile.delete(false, null);
            }
            astInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            astFile.create(astInputStream, false, null);
        }
    } catch (CoreException e) {
        showErrorById(StringKeys.ERROR_CORE_EXCEPTION, e);
    } catch (ParseException e) {
        showErrorById(StringKeys.ERROR_PMD_EXCEPTION, e);
    } catch (WriterException e) {
        showErrorById(StringKeys.ERROR_PMD_EXCEPTION, e);
    } catch (IOException e) {
        showErrorById(StringKeys.ERROR_IO_EXCEPTION, e);
    } finally {
        IOUtil.closeQuietly(byteArrayOutputStream);
        IOUtil.closeQuietly(astInputStream);
    }
}
Also used : JavaParser(net.sourceforge.pmd.lang.java.ast.JavaParser) IFile(org.eclipse.core.resources.IFile) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) IAstWriter(net.sourceforge.pmd.eclipse.runtime.writer.IAstWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ParseException(net.sourceforge.pmd.lang.java.ast.ParseException) IOException(java.io.IOException) JavaCharStream(net.sourceforge.pmd.lang.ast.JavaCharStream) WriterException(net.sourceforge.pmd.eclipse.runtime.writer.WriterException)

Example 79 with ASTCompilationUnit

use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd-eclipse-plugin by pmd.

the class ASTContentProvider method withoutHiddenOnes.

private List<Node> withoutHiddenOnes(Object parent) {
    List<Node> kids = new ArrayList<Node>();
    if (includeComments && parent instanceof ASTCompilationUnit) {
        // if (!hiddenNodeTypes.contains(Comment.class)) {
        List<Comment> comments = ((ASTCompilationUnit) parent).getComments();
        kids.addAll(comments);
    // }
    }
    AbstractNode node = (AbstractNode) parent;
    int kidCount = node.jjtGetNumChildren();
    for (int i = 0; i < kidCount; i++) {
        Node kid = node.jjtGetChild(i);
        // if (hiddenNodeTypes.contains(kid.getClass())) continue;
        if (!includeImports && kid instanceof ASTImportDeclaration) {
            continue;
        }
        if (!includeComments && kid instanceof Comment) {
            continue;
        }
        kids.add(kid);
    }
    Collections.sort(kids, BY_LINE_NUMBER);
    return kids;
}
Also used : Comment(net.sourceforge.pmd.lang.java.ast.Comment) ASTImportDeclaration(net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) AbstractNode(net.sourceforge.pmd.lang.ast.AbstractNode) AbstractNode(net.sourceforge.pmd.lang.ast.AbstractNode) Node(net.sourceforge.pmd.lang.ast.Node) ArrayList(java.util.ArrayList)

Aggregations

ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)79 Test (org.junit.Test)66 Constraint (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint)36 AbstractJavaTypeNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)27 StringReader (java.io.StringReader)8 RuleContext (net.sourceforge.pmd.RuleContext)8 LanguageVersionHandler (net.sourceforge.pmd.lang.LanguageVersionHandler)8 ASTImportDeclaration (net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration)7 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)6 Node (net.sourceforge.pmd.lang.ast.Node)5 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)5 ArrayList (java.util.ArrayList)4 ParserOptions (net.sourceforge.pmd.lang.ParserOptions)3 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 Method (java.lang.reflect.Method)2 List (java.util.List)2 LanguageVersion (net.sourceforge.pmd.lang.LanguageVersion)2 Parser (net.sourceforge.pmd.lang.Parser)2 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)2