Skip to content

Commit

Permalink
Added defensive null checking to cleanup null warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
david-waltermire committed Jan 2, 2025
1 parent 1d8f93d commit 0e18db2
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public String getName() {

@Override
public String getVersion() {
return CLOSEST_TAG;
return BUILD_VERSION;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import gov.nist.secauto.metaschema.core.qname.IEnhancedQName;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
import gov.nist.secauto.oscal.lib.OscalModelConstants;
import gov.nist.secauto.oscal.lib.model.metadata.AbstractProperty;
import gov.nist.secauto.oscal.lib.model.metadata.IProperty;

import java.net.URI;
import java.util.List;
Expand Down Expand Up @@ -201,7 +201,7 @@ public static IBooleanItem hasNamespace(
nodeNamespace = IAnyUriItem.cast(ObjectUtils.notNull(ns.toAtomicItem())).asUri();
}

String nodeNamespaceString = AbstractProperty.normalizeNamespace(nodeNamespace).toString();
String nodeNamespaceString = IProperty.normalizeNamespace(nodeNamespace).toString();
return IBooleanItem.valueOf(namespaces.stream()
.map(node -> nodeNamespaceString.equals(node.asString()))
.anyMatch(bool -> bool));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import gov.nist.secauto.metaschema.core.qname.IEnhancedQName;
import gov.nist.secauto.metaschema.core.util.CollectionUtil;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
import gov.nist.secauto.oscal.lib.model.Property;

import java.net.URI;
Expand All @@ -23,22 +24,13 @@
public abstract class AbstractProperty implements IProperty {

@NonNull
public static IEnhancedQName qname(URI namespace, @NonNull String name) {
return IEnhancedQName.of(normalizeNamespace(namespace).toASCIIString(), name);
public static IEnhancedQName qname(@NonNull URI namespace, @NonNull String name) {
return IEnhancedQName.of(ObjectUtils.notNull(namespace.toASCIIString()), name);
}

@NonNull
public static IEnhancedQName qname(@NonNull String name) {
return IEnhancedQName.of(OSCAL_NAMESPACE.toString(), name);
}

@NonNull
public static URI normalizeNamespace(URI namespace) {
URI retval = namespace;
if (retval == null) {
retval = OSCAL_NAMESPACE;
}
return retval;
return IEnhancedQName.of(ObjectUtils.notNull(IProperty.normalizeNamespace(null).toASCIIString()), name);
}

@SuppressWarnings("null")
Expand All @@ -58,12 +50,14 @@ public static List<Property> merge(@NonNull List<Property> original, @NonNull Li

@Override
public boolean isNamespaceEqual(@NonNull URI namespace) {
return normalizeNamespace(getNs()).equals(namespace);
return IProperty.normalizeNamespace(getNs()).equals(namespace);
}

@NonNull
public IEnhancedQName getQName() {
return qname(getNs(), getName());
return qname(
IProperty.normalizeNamespace(getNs()),
ObjectUtils.requireNonNull(getName()));
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,29 @@
import java.net.URI;

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;

public interface IProperty {
@NonNull
static URI normalizeNamespace(@Nullable URI namespace) {
URI retval = namespace;
if (retval == null) {
retval = OSCAL_NAMESPACE;
}
return retval;
}

@SuppressWarnings("null")
@NonNull
URI OSCAL_NAMESPACE = URI.create("http://csrc.nist.gov/ns/oscal");
@SuppressWarnings("null")
@NonNull
URI RMF_NAMESPACE = URI.create("http://csrc.nist.gov/ns/rmf");

@Nullable
String getName();

@Nullable
URI getNs();

boolean isNamespaceEqual(@NonNull URI namespace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@ public void visit(@NonNull IModuleNodeItem module, @NonNull DynamicContext conte
visitMetaschema(module, context);
}

private void handleAllowedValuesAtLocation(@NonNull IDefinitionNodeItem<?, ?> itemLocation, DynamicContext context) {
private void handleAllowedValuesAtLocation(
@NonNull IDefinitionNodeItem<?, ?> itemLocation,
@NonNull DynamicContext context) {
itemLocation.getDefinition().getAllowedValuesConstraints().stream()
.forEachOrdered(allowedValues -> {
ISequence<?> result = allowedValues.getTarget().evaluate(itemLocation, context);
result.stream().forEachOrdered(target -> {
assert target != null;
handleAllowedValues(allowedValues, itemLocation, (IDefinitionNodeItem<?, ?>) target);
});
});
Expand All @@ -77,18 +80,21 @@ private void handleAllowedValues(

@Override
public Void visitFlag(IFlagNodeItem item, DynamicContext context) {
assert context != null;
handleAllowedValuesAtLocation(item, context);
return super.visitFlag(item, context);
}

@Override
public Void visitField(IFieldNodeItem item, DynamicContext context) {
assert context != null;
handleAllowedValuesAtLocation(item, context);
return super.visitField(item, context);
}

@Override
public Void visitAssembly(IAssemblyNodeItem item, DynamicContext context) {
assert context != null;
handleAllowedValuesAtLocation(item, context);

return super.visitAssembly(item, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ protected IIndexer resolveImport(
try {
IRootAssemblyNodeItem importedCatalogRoot = ObjectUtils.requireNonNull(getRoot(importedCatalog, CATALOG));
Catalog catalogCopy = (Catalog) OscalBindingContext.instance().deepCopy(
(IBoundObject) ObjectUtils.requireNonNull(importedCatalogRoot).getValue(),
ObjectUtils.requireNonNull((IBoundObject) importedCatalogRoot.getValue()),
null);

importedCatalog = INodeItemFactory.instance().newDocumentNodeItem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ public boolean isApplicableTo(@NonNull Object obj) {
Property prop = (Property) obj;
actualName = prop.getName();
actualClass = prop.getClazz();
actualNamespace = prop.getNs() == null ? IProperty.OSCAL_NAMESPACE.toString() : prop.getNs().toString();
actualNamespace = IProperty.normalizeNamespace(prop.getNs()).toASCIIString();
break;
}
case PART: {
Expand All @@ -461,7 +461,7 @@ public boolean isApplicableTo(@NonNull Object obj) {
if (partId != null) {
actualId = partId;
}
actualNamespace = part.getNs() == null ? IProperty.OSCAL_NAMESPACE.toString() : part.getNs().toString();
actualNamespace = IProperty.normalizeNamespace(part.getNs()).toASCIIString();
break;
}
case LINK:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import gov.nist.secauto.metaschema.core.metapath.item.node.IModelNodeItem;
import gov.nist.secauto.metaschema.core.util.CollectionUtil;
import gov.nist.secauto.metaschema.core.util.CustomCollectors;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
import gov.nist.secauto.oscal.lib.model.Link;
import gov.nist.secauto.oscal.lib.profile.resolver.support.IEntityItem;

Expand Down Expand Up @@ -59,7 +60,7 @@ protected void handleUnselected(
URI linkHref = link.getHref();
URI sourceUri = item.getSource();

URI resolved = visitorContext.getUriResolver().resolve(linkHref, sourceUri);
URI resolved = visitorContext.getUriResolver().resolve(ObjectUtils.requireNonNull(linkHref), sourceUri);
if (LOGGER.isTraceEnabled()) {
LOGGER.atTrace().log("At path '{}', remapping orphaned URI '{}' to '{}'",
contextItem.toPath(IPathFormatter.METAPATH_PATH_FORMATER),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ private static void resolveRole(@NonNull IEntityItem entity, @NonNull Context co
.forEach(child -> handleProperty(ObjectUtils.notNull((IAssemblyNodeItem) child), context));
item.getModelItemsByName(OscalModelConstants.QNAME_LINK)
.forEach(child -> handleLink(ObjectUtils.notNull((IAssemblyNodeItem) child), context));
ROLE_MARKUP_METAPATH.evaluate(item).getValue()
ROLE_MARKUP_METAPATH.evaluate(item)
.forEach(child -> handleMarkup(ObjectUtils.notNull((IFieldNodeItem) child), context));
}

Expand All @@ -352,7 +352,7 @@ private static void resolveParty(@NonNull IEntityItem entity, @NonNull Context c
.forEach(child -> handleProperty(ObjectUtils.notNull((IAssemblyNodeItem) child), context));
item.getModelItemsByName(OscalModelConstants.QNAME_LINK)
.forEach(child -> handleLink(ObjectUtils.notNull((IAssemblyNodeItem) child), context));
PARTY_MARKUP_METAPATH.evaluate(item).getValue()
PARTY_MARKUP_METAPATH.evaluate(item)
.forEach(child -> handleMarkup(ObjectUtils.notNull((IFieldNodeItem) child), context));
}

Expand All @@ -362,7 +362,7 @@ public static void resolveLocation(@NonNull IEntityItem entity, @NonNull Context
.forEach(child -> handleProperty(ObjectUtils.notNull((IAssemblyNodeItem) child), context));
item.getModelItemsByName(OscalModelConstants.QNAME_LINK)
.forEach(child -> handleLink(ObjectUtils.notNull((IAssemblyNodeItem) child), context));
LOCATION_MARKUP_METAPATH.evaluate(item).getValue()
LOCATION_MARKUP_METAPATH.evaluate(item)
.forEach(child -> handleMarkup(ObjectUtils.notNull((IFieldNodeItem) child), context));
}

Expand All @@ -383,7 +383,7 @@ public static void resolveResource(@NonNull IEntityItem entity, @NonNull Context
}
});

RESOURCE_MARKUP_METAPATH.evaluate(item).getValue()
RESOURCE_MARKUP_METAPATH.evaluate(item)
.forEach(child -> handleMarkup(ObjectUtils.notNull((IFieldNodeItem) child), context));
}

Expand All @@ -394,7 +394,7 @@ public static void resolveParameter(@NonNull IEntityItem entity, @NonNull Contex
.forEach(child -> handleProperty(ObjectUtils.notNull((IAssemblyNodeItem) child), context));
item.getModelItemsByName(OscalModelConstants.QNAME_LINK)
.forEach(child -> handleLink(ObjectUtils.notNull((IAssemblyNodeItem) child), context));
PARAM_MARKUP_METAPATH.evaluate(item).getValue()
PARAM_MARKUP_METAPATH.evaluate(item)
.forEach(child -> handleMarkup(ObjectUtils.notNull((IFieldNodeItem) child), context));
}

Expand Down

0 comments on commit 0e18db2

Please sign in to comment.