Project

Profile

Help

Java extensions and matcing functions

Added by Anonymous over 15 years ago

Legacy ID: #7496075 Legacy Poster: Meelis Saluvee (dominioon)

Hi! There is one tricky limitation while creating Saxon java extension methods. We have a Labels class similar to the following one. But using it with saxon9 is not possible. The problem is that the Java compiler creates 2 almost identical get() methods if you implement/extend a class with generic definitions: * public java.lang.String SaxonAndMap$Labels.get(java.lang.Object) * public volatile java.lang.Object SaxonAndMap$Labels.get(java.lang.Object) Saxon is not able to choose the right one and throws an exception. import net.sf.saxon.TransformerFactoryImpl; import org.junit.Assert; import org.junit.Test; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import java.io.ByteArrayOutputStream; import java.io.StringReader; import java.util.HashMap; public class SaxonAndMap { private static final String XSL = "<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>\n" + "<xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform\"" + " xmlns:labels=&quot;java/SaxonAndMap$Labels&quot;" + " >" + " <xsl:param name=&quot;labels&quot;/>\n" + " <xsl:output method=&quot;html&quot; encoding=&quot;UTF-8&quot;/>\n" + " \n" + " <xsl:template match=&quot;/&quot;>\n" + " <xsl:value-of select=&quot;labels:get($labels, 'foo')&quot;/>\n" + " </xsl:template>\n" + "</xsl:stylesheet>"; private static final String XML = "<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?><root/>"; private String transform() throws Exception { System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl"); TransformerFactory transformerFactory = TransformerFactoryImpl.newInstance(); Transformer transformer = transformerFactory.newTransformer(new StreamSource(new StringReader(XSL))); transformer.setParameter("labels", new Labels()); ByteArrayOutputStream os = new ByteArrayOutputStream(); transformer.transform(new StreamSource(new StringReader(XML)), new StreamResult(os)); return os.toString(); } @Test public void labelsTest() throws Exception { Assert.assertEquals("BAR", transform()); } public static class Labels extends HashMap<String, String> { public Labels() { { put("foo", "BAR"); } } @Override public String get(Object key) { return super.get(key); } } }


Replies (2)

RE: Java extensions and matcing functions - Added by Anonymous over 15 years ago

Legacy ID: #7496855 Legacy Poster: Michael Kay (mhkay)

Thanks for the information. I'll look into this. Presumably the code should choose one of the methods in preference to the other - not sure which one!

RE: Java extensions and matcing functions - Added by Anonymous over 15 years ago

Legacy ID: #7504437 Legacy Poster: Vladimir Nesterovsky (vnesterovsky)

Personally, I would design overload resolution rules for extension functions following rules defined for xpath/xslt/xquery functions; namely overload on number of arguments only. This, I think, follows an intuitive expectations.

    (1-2/2)

    Please register to reply