From afa4e158466f3bd4afabd2a6d24e67fe5b909f67 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Mon, 27 Jan 2020 16:03:51 +0300 Subject: [PATCH] Trying to fix #460 Actually, could not reproduce this, added some tests (they passed on initial version). --- .../classlib/impl/ServiceLoaderSupport.java | 11 +++++---- .../java/util/MultipleTestService.java | 20 ++++++++++++++++ .../java/util/MultipleTestServiceImpl1.java | 23 +++++++++++++++++++ .../java/util/MultipleTestServiceImpl2.java | 23 +++++++++++++++++++ .../classlib/java/util/ServiceLoaderTest.java | 13 +++++++++++ ...avm.classlib.java.util.MultipleTestService | 18 +++++++++++++++ 6 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 tests/src/test/java/org/teavm/classlib/java/util/MultipleTestService.java create mode 100644 tests/src/test/java/org/teavm/classlib/java/util/MultipleTestServiceImpl1.java create mode 100644 tests/src/test/java/org/teavm/classlib/java/util/MultipleTestServiceImpl2.java create mode 100644 tests/src/test/resources/META-INF/services/org.teavm.classlib.java.util.MultipleTestService diff --git a/classlib/src/main/java/org/teavm/classlib/impl/ServiceLoaderSupport.java b/classlib/src/main/java/org/teavm/classlib/impl/ServiceLoaderSupport.java index 2bea6a59f..8d740005c 100644 --- a/classlib/src/main/java/org/teavm/classlib/impl/ServiceLoaderSupport.java +++ b/classlib/src/main/java/org/teavm/classlib/impl/ServiceLoaderSupport.java @@ -61,12 +61,13 @@ public class ServiceLoaderSupport extends AbstractDependencyListener implements for (Map.Entry> entry : serviceMap.entrySet()) { writer.appendClass(entry.getKey()).append(".$$serviceList$$ = ["); List implementations = entry.getValue(); - for (int i = 0; i < implementations.size(); ++i) { - if (i > 0) { - writer.append(", "); - } - String implName = implementations.get(i); + boolean first = true; + for (String implName : implementations) { if (context.getClassSource().getClassNames().contains(implName)) { + if (!first) { + writer.append(", "); + } + first = false; writer.append("[").appendClass(implName).append(", ").appendMethodBody( new MethodReference(implName, INIT_METHOD)) .append("]"); diff --git a/tests/src/test/java/org/teavm/classlib/java/util/MultipleTestService.java b/tests/src/test/java/org/teavm/classlib/java/util/MultipleTestService.java new file mode 100644 index 000000000..e9b4e97bb --- /dev/null +++ b/tests/src/test/java/org/teavm/classlib/java/util/MultipleTestService.java @@ -0,0 +1,20 @@ +/* + * Copyright 2020 Alexey Andreev. + * + * Licensed 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.teavm.classlib.java.util; + +public interface MultipleTestService { + String foo(); +} diff --git a/tests/src/test/java/org/teavm/classlib/java/util/MultipleTestServiceImpl1.java b/tests/src/test/java/org/teavm/classlib/java/util/MultipleTestServiceImpl1.java new file mode 100644 index 000000000..2589f4858 --- /dev/null +++ b/tests/src/test/java/org/teavm/classlib/java/util/MultipleTestServiceImpl1.java @@ -0,0 +1,23 @@ +/* + * Copyright 2020 Alexey Andreev. + * + * Licensed 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.teavm.classlib.java.util; + +public class MultipleTestServiceImpl1 implements MultipleTestService { + @Override + public String foo() { + return "A"; + } +} diff --git a/tests/src/test/java/org/teavm/classlib/java/util/MultipleTestServiceImpl2.java b/tests/src/test/java/org/teavm/classlib/java/util/MultipleTestServiceImpl2.java new file mode 100644 index 000000000..444b3e01e --- /dev/null +++ b/tests/src/test/java/org/teavm/classlib/java/util/MultipleTestServiceImpl2.java @@ -0,0 +1,23 @@ +/* + * Copyright 2020 Alexey Andreev. + * + * Licensed 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.teavm.classlib.java.util; + +public class MultipleTestServiceImpl2 implements MultipleTestService { + @Override + public String foo() { + return "B"; + } +} diff --git a/tests/src/test/java/org/teavm/classlib/java/util/ServiceLoaderTest.java b/tests/src/test/java/org/teavm/classlib/java/util/ServiceLoaderTest.java index bb0732b0f..ecb3d067b 100644 --- a/tests/src/test/java/org/teavm/classlib/java/util/ServiceLoaderTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/util/ServiceLoaderTest.java @@ -16,6 +16,9 @@ package org.teavm.classlib.java.util; import static org.junit.Assert.assertEquals; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import java.util.ServiceLoader; import org.junit.Test; import org.junit.runner.RunWith; @@ -30,4 +33,14 @@ public class ServiceLoaderTest { assertEquals(TestServiceImpl.class, instance.getClass()); assertEquals(1, ((TestServiceImpl) instance).getCounter()); } + + @Test + public void loadsMultipleServices() { + List strings = new ArrayList<>(); + for (MultipleTestService instance : ServiceLoader.load(MultipleTestService.class)) { + strings.add(instance.foo()); + } + strings.sort(String::compareTo); + assertEquals(Arrays.asList("A", "B"), strings); + } } diff --git a/tests/src/test/resources/META-INF/services/org.teavm.classlib.java.util.MultipleTestService b/tests/src/test/resources/META-INF/services/org.teavm.classlib.java.util.MultipleTestService new file mode 100644 index 000000000..08a5e4bb6 --- /dev/null +++ b/tests/src/test/resources/META-INF/services/org.teavm.classlib.java.util.MultipleTestService @@ -0,0 +1,18 @@ +# +# Copyright 2017 Alexey Andreev. +# +# Licensed 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. +# + +org.teavm.classlib.java.util.MultipleTestServiceImpl1 +org.teavm.classlib.java.util.MultipleTestServiceImpl2 \ No newline at end of file