[java-opensaml] branch main updated: Adjust some ensureXXX methods to raise different exception types.

Scott Cantor cantor.2 at osu.edu
Fri Apr 21 18:33:45 UTC 2023


This is an automated email from the git hooks/post-receive script.

scantor pushed a commit to branch main
in repository java-opensaml.

View the commit online:
http://git.shibboleth.net/view/?p=java-opensaml.git;a=commit;h=540c3fd65236a2243a602f9054b20ac629b69d24

The following commit(s) were added to refs/heads/main by this push:
     new 540c3fd65 Adjust some ensureXXX methods to raise different exception types.
540c3fd65 is described below

commit 540c3fd65236a2243a602f9054b20ac629b69d24
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Fri Apr 21 14:33:43 2023 -0400

    Adjust some ensureXXX methods to raise different exception types.
---
 .../opensaml/core/config/ConfigurationService.java    |  9 +++++++--
 .../java/org/opensaml/core/xml/AbstractXMLObject.java |  8 ++++++--
 .../org/opensaml/messaging/context/BaseContext.java   | 19 +++++++++++++------
 .../opensaml/messaging/context/MessageContext.java    |  6 ++++--
 4 files changed, 30 insertions(+), 12 deletions(-)

diff --git a/opensaml-core-api/src/main/java/org/opensaml/core/config/ConfigurationService.java b/opensaml-core-api/src/main/java/org/opensaml/core/config/ConfigurationService.java
index 037c2e624..cdd2ad7fc 100644
--- a/opensaml-core-api/src/main/java/org/opensaml/core/config/ConfigurationService.java
+++ b/opensaml-core-api/src/main/java/org/opensaml/core/config/ConfigurationService.java
@@ -104,8 +104,13 @@ public class ConfigurationService {
      */
     @Nonnull public static <T extends Object> T ensure(@Nonnull final Class<T> configClass) {
         final String partitionName = getPartitionName();
-        return Constraint.isNotNull(getConfiguration().get(configClass, partitionName),
-                "Configuration instance of type " + configClass.getName() + " was unavailable");
+        final T config = getConfiguration().get(configClass, partitionName);
+        if (config == null) {
+            throw new IllegalStateException("Configuration instance of type "
+                    + configClass.getName() + " was unavailable");
+        }
+        
+        return config;
     }
 
     /**
diff --git a/opensaml-core-api/src/main/java/org/opensaml/core/xml/AbstractXMLObject.java b/opensaml-core-api/src/main/java/org/opensaml/core/xml/AbstractXMLObject.java
index 8fa995479..320f804a8 100644
--- a/opensaml-core-api/src/main/java/org/opensaml/core/xml/AbstractXMLObject.java
+++ b/opensaml-core-api/src/main/java/org/opensaml/core/xml/AbstractXMLObject.java
@@ -111,7 +111,10 @@ public abstract class AbstractXMLObject implements XMLObject {
 
     /** {@inheritDoc} */
     @Nonnull public Element ensureDOM() {
-        return Constraint.isNotNull(dom, "DOM was null");
+        if (dom != null) {
+            return dom;
+        }
+        throw new XMLRuntimeException("DOM was null");
     }
 
     /** {@inheritDoc} */
@@ -496,7 +499,8 @@ public abstract class AbstractXMLObject implements XMLObject {
      */
     protected void setElementQName(@Nonnull final QName name) {
         Constraint.isNotNull(name, "Element QName cannot be null");
-        elementQname = QNameSupport.constructQName(name.getNamespaceURI(), name.getLocalPart(), name.getPrefix());
+        elementQname = QNameSupport.constructQName(name.getNamespaceURI(), QNameSupport.ensureLocalPart(name),
+                name.getPrefix());
         getNamespaceManager().registerElementName(elementQname);
     }
 
diff --git a/opensaml-messaging-api/src/main/java/org/opensaml/messaging/context/BaseContext.java b/opensaml-messaging-api/src/main/java/org/opensaml/messaging/context/BaseContext.java
index adc0b6b42..309265caf 100644
--- a/opensaml-messaging-api/src/main/java/org/opensaml/messaging/context/BaseContext.java
+++ b/opensaml-messaging-api/src/main/java/org/opensaml/messaging/context/BaseContext.java
@@ -17,7 +17,6 @@
 
 package org.opensaml.messaging.context;
 
-import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.util.Iterator;
 
@@ -115,7 +114,12 @@ public abstract class BaseContext implements Iterable<BaseContext> {
      * @return the held instance of the class, or null
      */ 
     @Nonnull public <T extends BaseContext> T ensureSubcontext(@Nonnull final Class<T> clazz) {
-        return Constraint.isNotNull(getSubcontext(clazz, true), "Auto-creation of subcontext failed");
+        final T newContext = getSubcontext(clazz, true);
+        if (newContext == null) {
+            throw new IllegalStateException("Context of type " + clazz.getName() + "did not exist or was not created");
+        }
+        
+        return newContext;
     }
     
     /**
@@ -170,7 +174,12 @@ public abstract class BaseContext implements Iterable<BaseContext> {
      * @return the held instance of the class, or null
      */ 
     @Nullable public BaseContext ensureSubcontext(@Nonnull @NotEmpty final String className) {
-        return Constraint.isNotNull(getSubcontext(className, true), "Auto-creation of subcontext failed");
+        final BaseContext newContext = getSubcontext(className, true);
+        if (newContext == null) {
+            throw new IllegalStateException("Context of type " + className + "did not exist or was not created");
+        }
+        
+        return newContext;
     }
     
     /**
@@ -340,10 +349,8 @@ public abstract class BaseContext implements Iterable<BaseContext> {
      * @return the new subcontext instance
      */
     @Nonnull protected <T extends BaseContext> T createSubcontext(@Nonnull final Class<T> clazz) {
-        final Constructor<T> constructor;
         try {
-            constructor = clazz.getConstructor();
-            return constructor.newInstance();
+            return clazz.getConstructor().newInstance();
         } catch (final SecurityException|NoSuchMethodException|IllegalArgumentException|InstantiationException|
                     IllegalAccessException|InvocationTargetException e) {
             log.error("Error creating subcontext: {}", e.getMessage());
diff --git a/opensaml-messaging-api/src/main/java/org/opensaml/messaging/context/MessageContext.java b/opensaml-messaging-api/src/main/java/org/opensaml/messaging/context/MessageContext.java
index 77db5ff84..b150f2b29 100644
--- a/opensaml-messaging-api/src/main/java/org/opensaml/messaging/context/MessageContext.java
+++ b/opensaml-messaging-api/src/main/java/org/opensaml/messaging/context/MessageContext.java
@@ -20,7 +20,6 @@ package org.opensaml.messaging.context;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
-import net.shibboleth.shared.logic.Constraint;
 import net.shibboleth.shared.logic.ConstraintViolationException;
 
 /**
@@ -57,7 +56,10 @@ public final class MessageContext extends BaseContext {
      * @since 5.0.0
      */
     @Nonnull public Object ensureMessage() {
-        return Constraint.isNotNull(msg, "Message was null");
+        if (msg != null) {
+            return msg;
+        }
+        throw new IllegalStateException("Message was null");
     }
     
     /**

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list