Menu

#1166 PLSQL XPath Rules Fail for XPath 1.0,

PMD-5.1.1
closed
PMD
3-Major
Bug
5.1.0
2014-07-12
2014-02-16
No

The current PLSQL XPath rules do not specify an XPath version and default to XPath version 1.0, thereby using JaxenXPathRuleQuery to evaluate the rules. Using this against PLSQL code results in a NullPointerException in JaxenXPathRuleQuery.evaluate at line 71.

An effective workaround is to switch to using SaxonXPathRuleQuery, by specifying version "1.0 compatibility" or "2.0" (see attached rules and log files)

    <property name="version">
    <value>1.0 compatibility</value>
    </property>

Test Case Output

HEAPSIZE=2048m LIB_DIR=../../pmd-runtime-libs ./run.sh pmd -debug -f text -R PLSQLXPathNPE.xml -language plsql -d plsql_minimal.pkb >PLSQLXPathNPE.log 2>&1 
...
...
Loaded rule XPath NPE Problem
Using PLSQL version: PLSQL
Processing /Users/sturton/Development/pmd.git/pmd/plsql_minimal.pkb
ENTRY
RETURN
ENTRY
Rule=net.sourceforge.pmd.lang.rule.XPathRule@6d481f12
Node=PrimaryExpression
RuleContext=net.sourceforge.pmd.RuleContext@73e4dd9
Rule Classname=net.sourceforge.pmd.lang.rule.XPathRule
Rule Name=XPath NPE Problem
Error while processing file: /Users/sturton/Development/pmd.git/pmd/plsql_minimal.pkb
java.lang.NullPointerException
    at net.sourceforge.pmd.lang.rule.xpath.JaxenXPathRuleQuery.evaluate(JaxenXPathRuleQuery.java:71)
    at net.sourceforge.pmd.lang.rule.XPathRule.evaluate(XPathRule.java:74)
    at net.sourceforge.pmd.lang.plsql.rule.PLSQLRuleChainVisitor.visit(PLSQLRuleChainVisitor.java:49)
    at net.sourceforge.pmd.lang.rule.AbstractRuleChainVisitor.visitAll(AbstractRuleChainVisitor.java:85)
    at net.sourceforge.pmd.RuleChain.apply(RuleChain.java:66)
    at net.sourceforge.pmd.RuleSets.apply(RuleSets.java:122)
    at net.sourceforge.pmd.SourceCodeProcessor.processSource(SourceCodeProcessor.java:150)
    at net.sourceforge.pmd.SourceCodeProcessor.processSourceCode(SourceCodeProcessor.java:80)
    at net.sourceforge.pmd.SourceCodeProcessor.processSourceCode(SourceCodeProcessor.java:47)
    at net.sourceforge.pmd.processor.PmdRunnable.call(PmdRunnable.java:78)
    at net.sourceforge.pmd.processor.PmdRunnable.call(PmdRunnable.java:25)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

/Users/sturton/Development/pmd.git/pmd/plsql_minimal.pkb    -   Error while processing /Users/sturton/Development/pmd.git/pmd/plsq
9 Attachments

Discussion

  • Stuart Turton

    Stuart Turton - 2014-02-17

    NPE avoided by adding minimal XPathHandler to PLSQLHandler

        @Override
        /**
         * Return minimal XPathHandler to cope with Jaxen XPath Rules.
         */
        public XPathHandler getXPathHandler() {
        return new XPathHandler() {
            public void initialize() {
            }
    
            public void initialize(IndependentContext context) {
            }
    
            public Navigator getNavigator() {
            return new DocumentNavigator();
            }
        };
        }
    
     
  • andres hurtado

    andres hurtado - 2014-02-28

    The source code fragment works, making the change in the master branch,
    But you must indicate property version in the xpath rule property of the xml ruleset:

    <rule ...  class="net.sourceforge.pmd.lang.rule.XPathRule" >
       <description>
        variable name must starts with v_   
       </description>
      <priority>3</priority>
      <properties>
        <property name="version" value="2.0" />
        <property name="xpath">
        <value>
    <![CDATA[
    //VariableOrConstantDeclarator[not (starts-with(upper-case(@Image),'V_'))]
    ]]>
        </value>
        </property>
      </properties>
      <example>
     <![CDATA[
    
     procedure Do_Something (
        p_Input1 in varchar2
     ) ...
     ]]>
        </example>
    </rule>
    

    The change to be added inside net.sourceforge.pmd.lang.plsql.PLSQLHandler is:

        /**
         * Return minimal XPathHandler to cope with Jaxen XPath Rules.
         */
        @Override
        public net.sourceforge.pmd.lang.XPathHandler getXPathHandler() {
        return new net.sourceforge.pmd.lang.XPathHandler() {
            public void initialize() {
            }
    
            public void initialize(net.sf.saxon.sxpath.IndependentContext context) {
            }
    
            public org.jaxen.Navigator getNavigator() {
            return new org.jaxen.dom.DocumentNavigator();
            }
        };
        }    
    
     

    Last edit: andres hurtado 2014-02-28
    • Stuart Turton

      Stuart Turton - 2014-03-03

      PLSQL Rules have already been modified and committed (in c593de73dc38e47906c0a837804b788ca8641889 [c593de7] )

       

      Last edit: Andreas Dangel 2014-04-12
  • Andreas Dangel

    Andreas Dangel - 2014-04-12

    Thank you Stuart!
    I've cherry-picked your commits and this bug will be fixed with the next release.

    There was one additional step needed to get XPath 1.0 (with Jaxon) working: The root node of the plsql AST hierarchy must be marked with the RootNode interface, so that the DocumentNavigator works. In the case of plsql, it's the class ASTInput which serves as the root node.

     
  • Andreas Dangel

    Andreas Dangel - 2014-04-12
    • status: open --> closed
    • assigned_to: Andreas Dangel
    • Milestone: New Tickets --> PMD-Next
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.