"The method ... is ambiguous for ..."

Discussion in 'Java' started by Ian Wilson, May 10, 2007.

  1. Ian Wilson

    Ian Wilson Guest

    I'm getting an error message I don't understand.

    The message is "The method newCanonicalizationMethod(String,
    C14NMethodParameterSpec) is ambiguous for the type XMLSignatureFactory"

    The code is "Code Fragment 2" from

    Which I cut and pasted into a new class in Eclipse WST. I then used
    Eclipse' Ctrl+O to generate the includes, where the class was available
    in several packages, I picked the packages mentioned earlier in the
    article (see below for full code)

    My plan was use the error messages to guide me in adding suitable code
    to create any undefined variables etc.

    When I look at the API docs for XMLSignatureFactory I only see one
    method named newCanonicalizationMethod so I can't see how it can be
    ambiguous. It looks OK to me.


    Why is it ambiguous and what can I do about it?

    import java.util.ArrayList;
    import java.util.Collections;

    import javax.xml.crypto.dom.DOMStructure;
    import javax.xml.crypto.dsig.CanonicalizationMethod;
    import javax.xml.crypto.dsig.DigestMethod;
    import javax.xml.crypto.dsig.Reference;
    import javax.xml.crypto.dsig.SignatureMethod;
    import javax.xml.crypto.dsig.SignedInfo;
    import javax.xml.crypto.dsig.XMLSignature;
    import javax.xml.crypto.dsig.XMLSignatureFactory;
    import javax.xml.crypto.dsig.dom.DOMSignContext;
    import javax.xml.crypto.dsig.keyinfo.KeyInfo;
    import javax.xml.crypto.dsig.keyinfo.KeyInfoFactory;

    public class IWSignXML {
    public static void main(String[] args) {

    XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");
    DigestMethod digestMethod = fac.newDigestMethod
    ("http://www.w3.org/2000/09/xmldsig#sha1", null);
    Reference ref = fac.newReference("#10",digestMethod);
    ArrayList<Reference> refList = new ArrayList<Reference>();
    CanonicalizationMethod cm = fac.newCanonicalizationMethod(
    SignatureMethod sm = fac.newSignatureMethod(
    SignedInfo signedInfo =fac.newSignedInfo(cm,sm,refList);
    DOMSignContext signContext = null;
    signContext = new DOMSignContext(privKey,securityHeader);
    signContext.setURIDereferencer(new URIResolverImpl());
    KeyInfoFactory keyFactory = KeyInfoFactory.getInstance();
    DOMStructure domKeyInfo = new DOMStructure(tokenReference);
    KeyInfo keyInfo =
    XMLSignature signature = fac.newXMLSignature(signedInfo,keyInfo);


    P.S. This is a bit of an X Y problem. The error message is Y, the X is
    how to read an XML document and insert a digital signature of a part of
    it (the Body element of a SOAP message).

    P.P.S. Its actually X Y Z, the Z being that I want to check my Perl code
    is canonicalising XML correctly by comparing its output with some SHA-1
    digests produced by Java. If I could canonicalize XML and compute an
    SHA-1 digest I'd be happy.
    Ian Wilson, May 10, 2007
    1. Advertisements

  2. Ian Wilson

    Ian Wilson Guest

    By using the Sun command line compiler (javac) instead of Eclipse, I got
    a more explicit error message:

    "IWSignXML.java:25: reference to newCanonicalizationMethod is ambiguous,
    both method newCanonicalizationMethod
    (java.lang.String,javax.xml.crypto.dsig.spec.C14NMethodParameterSpec) in
    javax.xml.crypto.dsig.XMLSignatureFactory and method
    (java.lang.String,javax.xml.crypto.XMLStructure) in
    javax.xml.crypto.dsig.XMLSignatureFactory match"

    The cause was the use of null as a second parameter (which matches any
    object) combined with the existence of another method with the same name
    and a different two element signature which wasn't in the JavaDocs I was

    I changed the above code to
    C14NMethodParameterSpec nullspec = null;
    CanonicalizationMethod cm = fac.newCanonicalizationMethod(

    And the compiler was happy.
    Ian Wilson, May 10, 2007
    1. Advertisements

  3. Ian Wilson

    Oliver Wong Guest

    Perhaps you're already aware of this trick, but the fix I usually use
    for cases like these is to cast the null:

    CanonicalizationMethod cm = fac.newCanonicalizationMethod(

    - Oliver
    Oliver Wong, May 10, 2007
  4. Ian Wilson

    Ian Wilson Guest

    I wasn't already aware of that trick. Thanks for passing it on.
    Ian Wilson, May 11, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.