+
Skip to content

Map iterators usage optimization. SAST #40064

Open
@Anchels

Description

@Anchels

Description

This method accesses the value of a Map entry, using a key that was retrieved from a keySet iterator. It is more efficient to use an iterator on the entrySet of the map, to avoid the Map.get(key) lookup.

Discussion

No response

Motivation

Perfomance optimization

Details

Consider using entrySet instead of keySet at the following cases:

  1. for (String key: inputTypes.keySet()) {
    builder.append("<INPUT TYPE=\"HIDDEN\" NAME=\"").append(key).append("\"").append(" VALUE=\"").append(escapeAttribute(inputTypes.get(key))).append("\"/>");
    }

  2. public static AttributeStatementType createAttributeStatement(Map<String, Object> attributes) {
    AttributeStatementType attrStatement = null;
    int i = 0;
    Set<String> keys = attributes.keySet();
    for (String key : keys) {
    if (i == 0) {
    // Deal with the X500 Profile of SAML2
    attrStatement = new AttributeStatementType();
    i++;
    }
    // if the attribute contains roles, add each role as an attribute.
    if (AttributeConstants.ROLES.equalsIgnoreCase(key)) {
    Object value = attributes.get(key);
    if (value instanceof Collection<?>) {
    Collection<?> roles = (Collection<?>) value;
    attrStatement = createAttributeStatement(new ArrayList(roles));
    }
    } else {
    AttributeType att;
    Object value = attributes.get(key);
    String uri = X500SAMLProfileConstants.getOID(key);
    if (StringUtil.isNotNull(uri)) {
    att = getX500Attribute(uri);
    att.setFriendlyName(key);
    } else {
    att = new AttributeType(key);
    att.setFriendlyName(key);
    att.setNameFormat(JBossSAMLURIConstants.ATTRIBUTE_FORMAT_URI.get());
    }
    if (Collection.class.isInstance(value)) {
    Collection collection = (Collection) value;
    Iterator iterator = collection.iterator();
    while (iterator.hasNext()) {
    att.addAttributeValue(iterator.next());
    }
    } else if (String.class.isInstance(value)) {
    att.addAttributeValue(value);
    } else {
    throw new RuntimeException("Unsupported attribute value [" + value + "]. Values must be a string, even if using a Collection.");
    }
    attrStatement.addAttribute(new ASTChoiceType(att));
    }
    }
    return attrStatement;
    }

  3. Map<QName, String> otherAttribs = attributeType.getOtherAttributes();
    if (otherAttribs != null) {
    List<String> nameSpacesDealt = new ArrayList<>();
    Iterator<QName> keySet = otherAttribs.keySet().iterator();
    while (keySet != null && keySet.hasNext()) {
    QName qname = keySet.next();
    String ns = qname.getNamespaceURI();
    if (!nameSpacesDealt.contains(ns)) {
    StaxUtil.writeNameSpace(writer, qname.getPrefix(), ns);
    nameSpacesDealt.add(ns);
    }
    String attribValue = otherAttribs.get(qname);
    StaxUtil.writeAttribute(writer, qname, attribValue);
    }
    }

  4. public String getPrefix(String namespaceURI) {
    for (String key : nsMap.keySet()) {
    String value = nsMap.get(key);
    if (value.equals(namespaceURI)) {
    return key;
    }
    }
    return null;
    }

  5. if (attributes != null) {
    Set<String> existingAttrNames = existing.getAttributes().keySet();
    for (String name : existingAttrNames) {
    if (attributes.containsKey(name)) {
    existing.setAttribute(name, attributes.get(name));
    attributes.remove(name);
    } else {
    existing.removeAttribute(name);
    }
    }
    for (String name : attributes.keySet()) {
    existing.setAttribute(name, attributes.get(name));
    }
    }

  6. // the profile should always hold all attributes defined in the config
    for (String attributeName : metadataByAttribute.keySet()) {
    if (!isSupportedAttribute(attributeName) || newAttributes.containsKey(attributeName)) {
    continue;
    }
    List<String> values = EMPTY_VALUE;
    AttributeMetadata metadata = metadataByAttribute.get(attributeName);
    if (user != null && isIncludeAttributeIfNotProvided(metadata)) {
    values = normalizeAttributeValues(attributeName, user.getAttributes().getOrDefault(attributeName, EMPTY_VALUE));
    }
    newAttributes.put(attributeName, values);
    }


Found by Linux Verification Center with SVACE


More similar cases:

#40064 (comment) - Thanks to @akbar1214 for his report

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载