From 9c83ccc99b761d40d611c6d2afd17384f27d6ed6 Mon Sep 17 00:00:00 2001 From: Roy Teeuwen Date: Wed, 3 Dec 2025 09:09:19 +0100 Subject: [PATCH 1/2] GROOVY-11578: Provide a default service for when serviceloader is not working, like currently in OSGi context --- .../java/org/apache/groovy/json/internal/FastStringUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/subprojects/groovy-json/src/main/java/org/apache/groovy/json/internal/FastStringUtils.java b/subprojects/groovy-json/src/main/java/org/apache/groovy/json/internal/FastStringUtils.java index 4bf9ef4edd8..e05a1ced03b 100644 --- a/subprojects/groovy-json/src/main/java/org/apache/groovy/json/internal/FastStringUtils.java +++ b/subprojects/groovy-json/src/main/java/org/apache/groovy/json/internal/FastStringUtils.java @@ -47,7 +47,7 @@ private static FastStringService loadService() { } } } - return found; + return found != null ? found : new DefaultFastStringService(); } } From ac2ce2ae7f21353e37c2b634941e69c77459bcd5 Mon Sep 17 00:00:00 2001 From: Roy Teeuwen Date: Tue, 16 Dec 2025 12:09:24 +0100 Subject: [PATCH 2/2] GROOVY-11578: Add a test case for when service loader is not available --- .../json/internal/FastStringUtilsTest.groovy | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 subprojects/groovy-json/src/test/groovy/org/apache/groovy/json/internal/FastStringUtilsTest.groovy diff --git a/subprojects/groovy-json/src/test/groovy/org/apache/groovy/json/internal/FastStringUtilsTest.groovy b/subprojects/groovy-json/src/test/groovy/org/apache/groovy/json/internal/FastStringUtilsTest.groovy new file mode 100644 index 00000000000..5c5955b82da --- /dev/null +++ b/subprojects/groovy-json/src/test/groovy/org/apache/groovy/json/internal/FastStringUtilsTest.groovy @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.groovy.json.internal + +import groovy.test.GroovyTestCase + +class FastStringUtilsTest extends GroovyTestCase { + + static class NoServiceLoaderGroovyClassLoader extends GroovyClassLoader { + + @Override + Enumeration getResources(String name) throws IOException { + if (name == "META-INF/services/org.apache.groovy.json.FastStringServiceFactory") { + return Collections.emptyEnumeration(); + } + return super.getResources(name); + } + } + + void testToCharArray() { + char[] expected = ["t", "e", "s", "t"] + assertEquals(expected, FastStringUtils.toCharArray("test")) + } + + void testToCharArrayWithNoServiceLoaderUsedTheDefaultStringService() { + ClassLoader previous = Thread.currentThread().getContextClassLoader() + try { + // Making sure that our class loader is used in the ServiceLoader class + def loader = new NoServiceLoaderGroovyClassLoader(); + Thread.currentThread().setContextClassLoader(loader) + def shell = new GroovyShell(loader) + def result = shell.evaluate(''' + import org.apache.groovy.json.internal.FastStringUtils + FastStringUtils.toCharArray("json") + ''') + assert result == ['j', 's', 'o', 'n'] as char[] + + } finally { + Thread.currentThread().setContextClassLoader(previous) + } + } +}