title: Java SAX XMLFilter not recognizing final variable initialized by a method
body: <p>Java 8.  Eclipse Luna.  I am having bizarre trouble assigning a final variable accessed by an anonymous method body.</p>
  
  <p><strong>The Background</strong></p>
  
  <p>I am validating a namespace-less XML file with my own choice of XSD schema. The file has no <code>xmlns</code> declaration, but the schema has a <code>targetNamespace="mynamespace"</code> declaration. Obviously, the file will fail validation. To make it pass validation regardless, I give a filter to the validator to force it to pretend the XML file has <code>xmlns="mynamespace"</code>, as suggested <a href="https://stackoverflow.com/questions/2991091/java-xsd-validation-of-xml-without-namespace">here</a>. See the code below.</p>
  
  <p><strong>The Problem</strong></p>
  
  <p>Notice the variable <code>targetNamespace</code>. If I initialize it with a string literal namespace, validation passes. If I initialize with a method call that returns the <em>same value</em>, validation fails.</p>
  
  <p><strong>The Code</strong></p>
  
  <pre><code>public static void validate(byte[] xmlFile, byte[] xsdFile)
          throws SAXException, IOException, XMLStreamException, FactoryConfigurationError {
  
      final String targetNamespace = "mynamespace"; // PASS!
      //final String targetNamespace = getTargetNamespace(xsdFile); // FAIL!
  
      XMLFilter namespaceFilter = new XMLFilterImpl(XMLReaderFactory.createXMLReader()) {
          @Override
          public void startElement(String uri, String localName, String qName, Attributes atts)
                  throws SAXException {
              // Make the validator think the XML file's elements have a namespace
              uri = targetNamespace;
  
              super.startElement(uri, localName, qName, atts);
          }
      };
  
      Source xmlSource = new SAXSource(namespaceFilter, new InputSource(new ByteArrayInputStream(xmlFile)));
      Source xsdSource = new StreamSource(new ByteArrayInputStream(xsdFile));
      SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(xsdSource).newValidator().validate(xmlSource);
  }
  
  private static String getTargetNamespace(byte[] xsdFile)
          throws XMLStreamException, FactoryConfigurationError {
      XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new ByteArrayInputStream(xsdFile));
      while (reader.hasNext()) {
          int event = reader.next();
  
          // Get the root element's "targetNamespace" attribute
          if (event == XMLEvent.START_ELEMENT) {
              return reader.getAttributeValue(null, "targetNamespace");
          }
      }
      return null;
  }
  </code></pre>
  
  <p><strong>The Error</strong></p>
  
  <blockquote>
    <p>cvc-complex-type.2.4.a: Invalid content was found starting with element 'childtag'. One of '{"mynamespace":childtag}' is expected.</p>
  </blockquote>
  
  <p>It gets more bizarre.  Validation passes when the method returns <code>"mynamespace"</code> as a string literal.  But validation fails when the method obtains the value of <code>"mynamespace"</code> from the XSD file.  I have verified with <code>String.equals()</code> that the value passed to <code>super.startElement</code> when validation fails is indeed <em>exactly the same</em> as when validation passes!</p>
  
  <p>The only difference I can see here is that it passes when the value assigned to the final variable <code>targetNamespace</code> is ultimately <strong>a constant</strong> (the string literal), and it fails when the value assigned is ultimately <strong>determined by some condition</strong> (derived from the XSD file). Final or effectively final makes no difference.</p>
  
  <p>Is this a known Java bug?  Or is it my lack of understanding of initialization of variables accessed from anonymous methods?  Does the XSD validation really have anything to do with it?</p>