From 49ae4c40680807140607961edb9c472ea3cf8c9b Mon Sep 17 00:00:00 2001 From: Tilmann Date: Sat, 12 Sep 2026 18:05:48 +0200 Subject: [PATCH] Service discovery no init --- .../java/javax/jdo/spi/JDOImplHelper.java | 22 ++++++++-------- .../java/javax/jdo/spi/JDOImplHelperTest.java | 25 +++++++++++++++++++ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/api/src/main/java/javax/jdo/spi/JDOImplHelper.java b/api/src/main/java/javax/jdo/spi/JDOImplHelper.java index 10fd95c4..20c00890 100644 --- a/api/src/main/java/javax/jdo/spi/JDOImplHelper.java +++ b/api/src/main/java/javax/jdo/spi/JDOImplHelper.java @@ -584,17 +584,19 @@ public static void registerAuthorizedStateManagerClasses(Collection smClasses SecurityManager sm = LegacyJava.getSecurityManager(); if (sm != null) { sm.checkPermission(JDOPermission.SET_STATE_MANAGER); - synchronized (authorizedStateManagerClasses) { - for (Iterator it = smClasses.iterator(); it.hasNext(); ) { - Object smClass = it.next(); - if (!(smClass instanceof Class)) { - throw new ClassCastException( - msg.msg( - "ERR_StateManagerClassCast", // NOI18N - smClass.getClass().getName())); - } - registerAuthorizedStateManagerClass((Class) it.next()); + } + synchronized (authorizedStateManagerClasses) { + for (Object smClass : smClasses) { + if (!(smClass instanceof Class)) { + throw new ClassCastException( + msg.msg( + "ERR_StateManagerClassCast", // NOI18N + smClass.getClass().getName())); } + // register the element that was just validated (the loop formerly advanced the + // iterator a second time here, registering the unvalidated successor instead and + // throwing NoSuchElementException on odd-sized collections) + registerAuthorizedStateManagerClass((Class) smClass); } } } diff --git a/api/src/test/java/javax/jdo/spi/JDOImplHelperTest.java b/api/src/test/java/javax/jdo/spi/JDOImplHelperTest.java index 9c1831a1..bca21996 100644 --- a/api/src/test/java/javax/jdo/spi/JDOImplHelperTest.java +++ b/api/src/test/java/javax/jdo/spi/JDOImplHelperTest.java @@ -17,7 +17,9 @@ package javax.jdo.spi; +import java.util.Arrays; import java.util.Collection; +import java.util.List; import java.util.Properties; import javax.jdo.Constants; import javax.jdo.JDOUserException; @@ -168,6 +170,29 @@ void testClassListenerRegistration() { Assertions.assertNull(event, "Unexpected event "); } + /** + * Test that registerAuthorizedStateManagerClasses validates and registers every element of the + * collection. The loop formerly advanced the iterator twice per iteration, registering the + * unvalidated successor of each validated element and throwing NoSuchElementException on + * odd-sized collections. + */ + @Test + void testRegisterAuthorizedStateManagerClassesOddSized() { + List> classes = Arrays.asList(String.class, Integer.class, Long.class); + // must validate and register all three elements without NoSuchElementException + JDOImplHelper.registerAuthorizedStateManagerClasses(classes); + } + + /** Test that a non-Class element is rejected with the documented ClassCastException. */ + @Test + void testRegisterAuthorizedStateManagerClassesNonClassElement() { + List elements = Arrays.asList(String.class, "not a class"); + Assertions.assertThrows( + ClassCastException.class, + () -> JDOImplHelper.registerAuthorizedStateManagerClasses(elements), + "Missing ClassCastException for non-Class element"); + } + /** Test that an unknown standard property causes JDOUserException. */ @Test void testUnknownStandardProperty() {