Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-6696

@attribute on groovy.util.Node does not work when meta class is modified

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.2.2
    • Fix Version/s: None
    • Component/s: XML Processing
    • Labels:
      None

      Description

      if a closure is added to the meta class it seems that the node.@attribute is not working anymore
      however it is still working when we use quotes, i.e. node.'@attribute'

      here is some tests that reproduce the probleme, they have to be run alone

          @Test
          public void working() throws Exception {
              //given
              def root = new XmlParser().parseText('''<node id="12">value</node>''')
      
              //when
              String id = root.@id;
      
              //then
              assertEquals("12", id);
          }
          @Test
          public void notWorking() throws Exception {
              //given
              Node.metaClass.findParent = { closure ->
                  def parent = delegate.parent()
                  if(parent == null){
                      return null
                  }
                  if(closure(parent)){
                      return parent
                  }
                  return parent.findParent(closure)
              }
      
              def root = new XmlParser().parseText('''<node id="12">value</node>''')
      
              //when
              String id = root.@id;
      
              //then
              assertEquals("12", id);
          }
          @Test
          public void workingOnlyWithQuotes() throws Exception {
              //given
              Node.metaClass.findParent = { closure ->
                  def parent = delegate.parent()
                  if(parent == null){
                      return null
                  }
                  if(closure(parent)){
                      return parent
                  }
                  return parent.findParent(closure)
              }
      
              def root = new XmlParser().parseText('''<node id="12">value</node>''')
      
              //when
              String id = root.'@id';
      
              //then
              assertEquals("12", id);
          }
      

        Attachments

          Activity

            People

            • Assignee:
              jwagenleitner John Wagenleitner
              Reporter:
              baptiste.mesta Baptiste Mesta
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: