Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8264766

ClassCastException during template compilation (Variable cannot be cast to Param)

    XMLWordPrintable

Details

    Description

      A DESCRIPTION OF THE PROBLEM :
      When compiling xslt template ClassCastException is thrown, which is unexpected. Example code below. If template is invalid TransformerConfigurationException should be thrown.
      Problem is in SymbolTable.java

         public Param addParam(Param parameter) {
              if (_variables == null) _variables = new HashMap<>();
              final String name = parameter.getName().getStringRep();
              return (Param)_variables.put(name, parameter);
          }

      _variables can contain Param and Variable objects, so this cast causes ClassCastException



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the example code

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      No exception should be thrown.
      ACTUAL -
      java.lang.ClassCastException: class com.sun.org.apache.xalan.internal.xsltc.compiler.Variable cannot be cast to class com.sun.org.apache.xalan.internal.xsltc.compiler.Param (com.sun.org.apache.xalan.internal.xsltc.compiler.Variable and com.sun.org.apache.xalan.internal.xsltc.compiler.Param are in module java.xml of loader 'bootstrap')
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.compiler.SymbolTable.addParam(SymbolTable.java:104)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.compiler.Param.parseContents(Param.java:137)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.compiler.Stylesheet.parseOwnChildren(Stylesheet.java:587)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.compiler.Stylesheet.parseContents(Stylesheet.java:559)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.compiler.Parser.createAST(Parser.java:398)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.compile(XSLTC.java:493)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.compile(XSLTC.java:570)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:1018)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:817)
      at Scratch.main(scratch_1.java:23)
      ERROR: 'Could not compile stylesheet'
      FATAL ERROR: 'class com.sun.org.apache.xalan.internal.xsltc.compiler.Variable cannot be cast to class com.sun.org.apache.xalan.internal.xsltc.compiler.Param (com.sun.org.apache.xalan.internal.xsltc.compiler.Variable and com.sun.org.apache.xalan.internal.xsltc.compiler.Param are in module java.xml of loader 'bootstrap')'
                 :class com.sun.org.apache.xalan.internal.xsltc.compiler.Variable cannot be cast to class com.sun.org.apache.xalan.internal.xsltc.compiler.Param (com.sun.org.apache.xalan.internal.xsltc.compiler.Variable and com.sun.org.apache.xalan.internal.xsltc.compiler.Param are in module java.xml of loader 'bootstrap')
      Exception in thread "main" javax.xml.transform.TransformerConfigurationException: class com.sun.org.apache.xalan.internal.xsltc.compiler.Variable cannot be cast to class com.sun.org.apache.xalan.internal.xsltc.compiler.Param (com.sun.org.apache.xalan.internal.xsltc.compiler.Variable and com.sun.org.apache.xalan.internal.xsltc.compiler.Param are in module java.xml of loader 'bootstrap')
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:1059)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:817)
      at Scratch.main(scratch_1.java:23)
      Caused by: java.lang.ClassCastException: class com.sun.org.apache.xalan.internal.xsltc.compiler.Variable cannot be cast to class com.sun.org.apache.xalan.internal.xsltc.compiler.Param (com.sun.org.apache.xalan.internal.xsltc.compiler.Variable and com.sun.org.apache.xalan.internal.xsltc.compiler.Param are in module java.xml of loader 'bootstrap')
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.compiler.SymbolTable.addParam(SymbolTable.java:104)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.compiler.Param.parseContents(Param.java:137)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.compiler.Stylesheet.parseOwnChildren(Stylesheet.java:587)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.compiler.Stylesheet.parseContents(Stylesheet.java:559)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.compiler.Parser.createAST(Parser.java:398)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.compile(XSLTC.java:493)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.compile(XSLTC.java:570)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:1018)
      ... 2 more
      ---------
      java.lang.ClassCastException: class com.sun.org.apache.xalan.internal.xsltc.compiler.Variable cannot be cast to class com.sun.org.apache.xalan.internal.xsltc.compiler.Param (com.sun.org.apache.xalan.internal.xsltc.compiler.Variable and com.sun.org.apache.xalan.internal.xsltc.compiler.Param are in module java.xml of loader 'bootstrap')
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.compiler.SymbolTable.addParam(SymbolTable.java:104)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.compiler.Param.parseContents(Param.java:137)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.compiler.Stylesheet.parseOwnChildren(Stylesheet.java:587)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.compiler.Stylesheet.parseContents(Stylesheet.java:559)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.compiler.Parser.createAST(Parser.java:398)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.compile(XSLTC.java:493)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.compile(XSLTC.java:570)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:1018)
      at java.xml/com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:817)
      at Scratch.main(scratch_1.java:23)


      ---------- BEGIN SOURCE ----------

      import javax.xml.transform.TransformerFactory;
      import javax.xml.transform.stream.StreamSource;
      import java.io.StringReader;

      class Scratch {

          public static void main(String[] args) throws Exception {
              TransformerFactory transformerFactory = TransformerFactory.newInstance();
              String stylesheet = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                      "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">\n" +
                      " <xsl:variable name=\"background-color\">#f4f4f4</xsl:variable>\n" +
                      " <xsl:param name=\"background-color\">pp</xsl:param>\n" +
                      " <xsl:template name=\"tName\"><xsl:param name=\"background-color\">black</xsl:param>\n" +
                      " OK <xsl:value-of select=\"$background-color\"/>\n" +
                      " </xsl:template>\n" +
                      " <xsl:template match=\"/root\">\n" +
                      " <xsl:call-template name=\"tName\">\n" +
                      " <xsl:with-param name=\"background-color\" select=\"$background-color\"/>\n" +
                      " </xsl:call-template>\n" +
                      " </xsl:template>\n" +
                      "</xsl:stylesheet>\n";
              transformerFactory.newTransformer(new StreamSource(new StringReader(stylesheet)));

          }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


      Attachments

        Issue Links

          Activity

            People

              joehw Joe Wang
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: