Ever heard of Gregor Samsa? That’s the main character of a novella written by Franz Kafka: The Metamorphosis (original in German: Die Verwandlung). You can read it on Gutenberg here: https://www.gutenberg.org/ebooks/22367.

So, what’s the relation to Java? Well, the title could also be translated as “The Transformation”, which gives a hint: Did you ever write a XSLT stylesheet and it has thrown a runtime exception? Did you notice gregor samsa hidden in the stacktrace?

Example:

Caused by: java.lang.NumberFormatException: For input string: "2.0"
	at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
	at java.base/java.lang.Integer.parseInt(Integer.java:565)
	at java.base/java.lang.Integer.parseInt(Integer.java:662)
	at jdk.translet/die.verwandlung.GregorSamsa.template$dot$0()
	at jdk.translet/die.verwandlung.GregorSamsa.applyTemplates()
	at jdk.translet/die.verwandlung.GregorSamsa.applyTemplates()
	at jdk.translet/die.verwandlung.GregorSamsa.transform()
	at java.xml/com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet.transform(AbstractTranslet.java:627)
	at java.xml/com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:783)
	at java.xml/com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:395)

The class GregorSamsa

The class GregorSamsa appears to be in package die.verwandlung in the java module jdk.translet. When searching for this class, you won’t find it, because it is generated when the XSLT is compiled.

You can also see, that the JDK ships with a copy of Apache Xalan which implements XSLT version 1.0.

In order to reproduce the stacktrace from above, you can use the following Java code snippet; save it in a file (e.g. Example.java) and run it with java (JDK 25+):

import module java.xml;

String TEMPLATE = """
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:java="http://xml.apache.org/xalan/java"
    version="1.0">
  <xsl:output method="text"/>
  <xsl:template match="parseInt">
    <xsl:text>parseInt=</xsl:text>
    <xsl:value-of select="java:java.lang.Integer.parseInt(string(@x))"/>
  </xsl:template>
</xsl:stylesheet>
""";

String XML = "<parseInt x='2.0'/>";

void main() throws Exception {
  var result = new StringWriter();

  var transformerFactory = TransformerFactory.newDefaultInstance();
  transformerFactory.setFeature("jdk.xml.enableExtensionFunctions", true);
  var templates = transformerFactory.newTemplates(new StreamSource(new StringReader(TEMPLATE)));
  var transformer = templates.newTransformer();
  transformer.transform(new StreamSource(new StringReader(XML)), new StreamResult(result));

  IO.println(result.toString());
}

The transformation

The class name is in the source code of Xalan:

    /** 
     * As Gregor Samsa awoke one morning from uneasy dreams he found himself
     * transformed in his bed into a gigantic insect. He was lying on his hard,
     * as it were armour plated, back, and if he lifted his head a little he
     * could see his big, brown belly divided into stiff, arched segments, on
     * top of which the bed quilt could hardly keep in position and was about
     * to slide off completely. His numerous legs, which were pitifully thin
     * compared to the rest of his bulk, waved helplessly before his eyes.
     * "What has happened to me?", he thought. It was no dream....
     */
    protected final static String DEFAULT_TRANSLET_NAME = "GregorSamsa";

(org.apache.xalan.xsltc.trax.TransformerFactoryImpl#L104-L114)

The JDK sources additionally provide the package name:

    /**
     * The package name prefix for all generated translet classes
     */
    private static final String DEFAULT_TRANSLATE_PACKAGE = "die.verwandlung";

(com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl#L149-L152)

One entry point into the transformation of XSLT into Java bytecode (and the creation of the class GregorSamsa) can be found in org.apache.xalan.xsltc.compiler.Stylesheet#translate. They use Apache Commons Bytecode Engineering Library (BCEL) to generate the bytecode. So - as JDK is using a shaded version of Xalan, they also need to shade BCEL. And they did: com.sun.org.apache.bcel.internal.

For each template a separate method will be generated. In the end, the XSLT stylesheet is pure JVM code and runs fast and even benefits from the runtime optimizations (like JIT).

The JDK implementation additionally defines the (synthetic) Java module jdk.translet: com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl#defineTransletClasses

Some note on security

The sample code uses the feature “extension functions”, which allows stylesheets to call Java. This is by default disabled and has to be enabled explicitly (see the setFeature call in the example). If you process data with a stylesheet not under your control (e.g. a user supplied XSLT file), then you would open up a pretty simple code execution possibility - because almost everything can then be called from the stylesheet. So, the sample code is just a demonstration for GregorSamsa to appear in the stacktrace and not production ready code.

References