[java-opensaml] branch main updated: OSJ-369 - Eliminate class exceptions from string-based context accessors
Scott Cantor
cantor.2 at osu.edu
Thu Jan 19 18:59:08 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=aec6c36801dac091ac091c7ee09c3f723c63710a
The following commit(s) were added to refs/heads/main by this push:
new aec6c3680 OSJ-369 - Eliminate class exceptions from string-based context accessors
aec6c3680 is described below
commit aec6c36801dac091ac091c7ee09c3f723c63710a
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Thu Jan 19 13:59:06 2023 -0500
OSJ-369 - Eliminate class exceptions from string-based context accessors
https://shibboleth.atlassian.net/browse/OSJ-369
---
.../opensaml/messaging/context/BaseContext.java | 46 ++++++++++++++++------
.../messaging/context/BaseContextTest.java | 25 +++++++++---
2 files changed, 54 insertions(+), 17 deletions(-)
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 7d52c1626..82f534c7d 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
@@ -96,18 +96,32 @@ public abstract class BaseContext implements Iterable<BaseContext> {
*
* @param <T> the type of subcontext being operated on
* @param clazz the class type to obtain
+ *
* @return the held instance of the class, or null
*/
@Nullable public <T extends BaseContext> T getSubcontext(@Nonnull final Class<T> clazz) {
return getSubcontext(clazz, false);
}
+ /**
+ * Get a subcontext of the current context, creating it if it does not exist.
+ *
+ * @param <T> the type of subcontext being operated on
+ * @param clazz the class type to obtain
+ *
+ * @return the held instance of the class, or null
+ */
+ @Nonnull public <T extends BaseContext> T getOrCreateSubcontext(@Nonnull final Class<T> clazz) {
+ return Constraint.isNotNull(getSubcontext(clazz, true), "Auto-creation of subcontext failed");
+ }
+
/**
* Get a subcontext of the current context.
*
* @param <T> the type of subcontext being operated on
* @param clazz the class type to obtain
* @param autocreate flag indicating whether the subcontext instance should be auto-created
+ *
* @return the held instance of the class, or null
*/
@Nullable public <T extends BaseContext> T getSubcontext(@Nonnull final Class<T> clazz, final boolean autocreate) {
@@ -135,30 +149,38 @@ public abstract class BaseContext implements Iterable<BaseContext> {
* Get a subcontext of the current context.
*
* @param className the name of the class type to obtain
+ *
* @return the held instance of the class, or null
- * @throws ClassNotFoundException if the named class does not exist
*/
- @Nullable public BaseContext getSubcontext(@Nonnull @NotEmpty final String className)
- throws ClassNotFoundException {
+ @Nullable public BaseContext getSubcontext(@Nonnull @NotEmpty final String className) {
return getSubcontext(className, false);
}
+
+ /**
+ * Get a subcontext of the current context, creating it if necessary.
+ *
+ * @param className the name of the class type to obtain
+ *
+ * @return the held instance of the class, or null
+ */
+ @Nullable public BaseContext getOrCreateSubcontext(@Nonnull @NotEmpty final String className) {
+ return Constraint.isNotNull(getSubcontext(className, true), "Auto-creation of subcontext failed");
+ }
+
/**
* Get a subcontext of the current context.
*
- * <p>As of V3.4.0, if autocreate is false, this method will respond to a {@link ClassNotFoundException}
+ * <p>If autocreate is false, this method will respond to a {@link ClassNotFoundException}
* by attempting to locate a matching subcontext based on the simple class name of the children and
- * return the first match. If no match is found, it will proceed with the throw, but a future version
- * of the API will eliminate that from the signature and simply return a null.</p>
- *
+ * return the first match. If no match is found or if auto-creation is set, it will return a null.</p>
*
* @param className the name of the class type to obtain
* @param autocreate flag indicating whether the subcontext instance should be auto-created
+ *
* @return the held instance of the class, or null
- * @throws ClassNotFoundException if the named class does not exist
*/
- @Nullable public BaseContext getSubcontext(@Nonnull @NotEmpty final String className, final boolean autocreate)
- throws ClassNotFoundException {
+ @Nullable public BaseContext getSubcontext(@Nonnull @NotEmpty final String className, final boolean autocreate) {
try {
return getSubcontext(Class.forName(className).asSubclass(BaseContext.class), autocreate);
} catch (final ClassNotFoundException e) {
@@ -169,7 +191,9 @@ public abstract class BaseContext implements Iterable<BaseContext> {
}
}
}
- throw e;
+
+ log.warn("Trapped ClassNotFoundException on input: " + className);
+ return null;
}
}
diff --git a/opensaml-messaging-api/src/test/java/org/opensaml/messaging/context/BaseContextTest.java b/opensaml-messaging-api/src/test/java/org/opensaml/messaging/context/BaseContextTest.java
index 92f0b9598..8d0df4850 100644
--- a/opensaml-messaging-api/src/test/java/org/opensaml/messaging/context/BaseContextTest.java
+++ b/opensaml-messaging-api/src/test/java/org/opensaml/messaging/context/BaseContextTest.java
@@ -193,7 +193,10 @@ public class BaseContextTest {
Assert.assertNull(parent1.getSubcontext(TestContext.class, false));
}
- public void testStringAccess() throws ClassNotFoundException {
+ /**
+ * Test accessing context by class name.
+ */
+ public void testStringAccess() {
TestContext parent = new TestContext();
parent.addSubcontext(new TestContext());
@@ -202,7 +205,10 @@ public class BaseContextTest {
Assert.assertTrue(child instanceof TestContext);
}
- public void testStringAccessMissing() throws ClassNotFoundException {
+ /**
+ * Test accessing missing context by class name.
+ */
+ public void testStringAccessMissing() {
TestContext parent = new TestContext();
parent.addSubcontext(new TestContext());
@@ -210,7 +216,10 @@ public class BaseContextTest {
Assert.assertNull(child);
}
- public void testSimpleStringAccess() throws ClassNotFoundException {
+ /**
+ * Test accessing context by truncated class name.
+ */
+ public void testSimpleStringAccess() {
TestContext parent = new TestContext();
parent.addSubcontext(new TestContext());
@@ -219,12 +228,16 @@ public class BaseContextTest {
Assert.assertTrue(child instanceof TestContext);
}
- @Test(expectedExceptions = ClassNotFoundException.class)
- public void testSimpleStringError() throws ClassNotFoundException {
+ /**
+ * Test accessing context using non-existent truncated class name.
+ */
+ @Test
+ public void testSimpleStringError() {
TestContext parent = new TestContext();
parent.addSubcontext(new TestContext());
- parent.getSubcontext("NoContext");
+ // This threw in V4, is now returning null.
+ Assert.assertNull(parent.getSubcontext("NoContext"));
}
}
\ No newline at end of file
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list