Skip to content

Commit 1c3932f

Browse files
committed
8264766: ClassCastException during template compilation (Variable cannot be cast to Param)
Reviewed-by: naoto
1 parent f6f82c3 commit 1c3932f

File tree

2 files changed

+70
-6
lines changed

2 files changed

+70
-6
lines changed

src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/SymbolTable.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
33
*/
44
/*
55
* Licensed to the Apache Software Foundation (ASF) under one or more
@@ -35,7 +35,7 @@
3535
* @author Jacek Ambroziak
3636
* @author Santiago Pericas-Geertsen
3737
* @author Morten Jorgensen
38-
* @LastModified: Oct 2017
38+
* @LastModified: June 2021
3939
*/
4040
final class SymbolTable {
4141

@@ -94,14 +94,14 @@ public Template lookupTemplate(QName name) {
9494

9595
public Variable addVariable(Variable variable) {
9696
if (_variables == null) _variables = new HashMap<>();
97-
final String name = variable.getName().getStringRep();
98-
return (Variable)_variables.put(name, variable);
97+
VariableBase v = _variables.put(variable.getName().getStringRep(), variable);
98+
return v instanceof Variable ? (Variable)v : null;
9999
}
100100

101101
public Param addParam(Param parameter) {
102102
if (_variables == null) _variables = new HashMap<>();
103-
final String name = parameter.getName().getStringRep();
104-
return (Param)_variables.put(name, parameter);
103+
VariableBase v = _variables.put(parameter.getName().getStringRep(), parameter);
104+
return v instanceof Param ? (Param)v : null;
105105
}
106106

107107
public Variable lookupVariable(QName qname) {
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package transform;
24+
25+
import java.io.StringReader;
26+
import javax.xml.transform.TransformerFactory;
27+
import javax.xml.transform.stream.StreamSource;
28+
import org.testng.annotations.Test;
29+
30+
/*
31+
* @test
32+
* @bug 8264766
33+
* @run testng transform.SymbolTableTest
34+
* @summary Tests SymbolTable
35+
*/
36+
public class SymbolTableTest {
37+
/**
38+
* Verifies that the SymbolTable processes (adds) variables and params
39+
* properly. The SymbolTable holds variables and params in a map, it shall
40+
* therefore perform a type check before cast, or else result in a
41+
* ClassCastException when variables and/or params have the same name (in
42+
* which case the later ones shadow the previous ones).
43+
*
44+
* @throws Exception if the test fails
45+
*/
46+
@Test
47+
public void test() throws Exception {
48+
TransformerFactory transformerFactory = TransformerFactory.newInstance();
49+
String stylesheet = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
50+
"<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">\n" +
51+
" <xsl:variable name=\"background-color\">#f4f4f4</xsl:variable>\n" +
52+
" <xsl:param name=\"background-color\">pp</xsl:param>\n" +
53+
" <xsl:template name=\"tName\"><xsl:param name=\"background-color\">black</xsl:param>\n" +
54+
" OK <xsl:value-of select=\"$background-color\"/>\n" +
55+
" </xsl:template>\n" +
56+
" <xsl:template match=\"/root\">\n" +
57+
" <xsl:call-template name=\"tName\">\n" +
58+
" <xsl:with-param name=\"background-color\" select=\"$background-color\"/>\n" +
59+
" </xsl:call-template>\n" +
60+
" </xsl:template>\n" +
61+
"</xsl:stylesheet>\n";
62+
transformerFactory.newTransformer(new StreamSource(new StringReader(stylesheet)));
63+
}
64+
}

0 commit comments

Comments
 (0)