Uploaded image for project: 'MINA'
  1. MINA
  2. DIRMINA-899

IoSession.getAttribute() doesn't store default value

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.0.4
    • Fix Version/s: 2.0.8
    • Component/s: Core
    • Labels:
      None

      Description

      The JavaDocs for IoSession.getAttribute(Object key, Object defaultValue) state that if the key doesn't exist yet, the default value is stored in the map.
      http://mina.apache.org/report/trunk/apidocs/org/apache/mina/core/session/IoSession.html#getAttribute(java.lang.Object, java.lang.Object)

      The actual implementation (in DefaultIoSessionAttributeMap) reads

              public Object getAttribute(IoSession session, Object key, Object defaultValue) {
                  if (key == null) {
                      throw new IllegalArgumentException("key");
                  }
      
                  Object answer = attributes.get(key);
                  if (answer == null) {
                      return defaultValue;
                  }
                  
                  return answer;
              }
      

      It never stores the default value.

      Can you (preferably) fix that, or (less preferably) correct the JavaDoc?

        Activity

        Hide
        slaks Schabse Laks added a comment -

        setAttributeIfAbsent does behave as documented, but annoyingly returns the old value rather than the new value.

        Show
        slaks Schabse Laks added a comment - setAttributeIfAbsent does behave as documented, but annoyingly returns the old value rather than the new value.
        Hide
        elecharny Emmanuel Lecharny added a comment -

        Fixed a long time ago. We now store the value, even if the key does not exist :

                public Object getAttribute(IoSession session, Object key, Object defaultValue) {
                    if (key == null) {
                        throw new IllegalArgumentException("key");
                    }
        
                    if (defaultValue == null) {
                        return attributes.get(key);
                    }
        
                    Object object = attributes.putIfAbsent(key, defaultValue);
        ...
        
        Show
        elecharny Emmanuel Lecharny added a comment - Fixed a long time ago. We now store the value, even if the key does not exist : public Object getAttribute(IoSession session, Object key, Object defaultValue) { if (key == null ) { throw new IllegalArgumentException( "key" ); } if (defaultValue == null ) { return attributes.get(key); } Object object = attributes.putIfAbsent(key, defaultValue); ...

          People

          • Assignee:
            Unassigned
            Reporter:
            slaks Schabse Laks
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development