Trying to fix #460

Actually, could not reproduce this, added some tests (they passed on initial version).
This commit is contained in:
Alexey Andreev 2020-01-27 16:03:51 +03:00
parent c30e2d3c24
commit afa4e15846
6 changed files with 103 additions and 5 deletions

View File

@ -61,12 +61,13 @@ public class ServiceLoaderSupport extends AbstractDependencyListener implements
for (Map.Entry<String, List<String>> entry : serviceMap.entrySet()) { for (Map.Entry<String, List<String>> entry : serviceMap.entrySet()) {
writer.appendClass(entry.getKey()).append(".$$serviceList$$ = ["); writer.appendClass(entry.getKey()).append(".$$serviceList$$ = [");
List<String> implementations = entry.getValue(); List<String> implementations = entry.getValue();
for (int i = 0; i < implementations.size(); ++i) { boolean first = true;
if (i > 0) { for (String implName : implementations) {
writer.append(", ");
}
String implName = implementations.get(i);
if (context.getClassSource().getClassNames().contains(implName)) { if (context.getClassSource().getClassNames().contains(implName)) {
if (!first) {
writer.append(", ");
}
first = false;
writer.append("[").appendClass(implName).append(", ").appendMethodBody( writer.append("[").appendClass(implName).append(", ").appendMethodBody(
new MethodReference(implName, INIT_METHOD)) new MethodReference(implName, INIT_METHOD))
.append("]"); .append("]");

View File

@ -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();
}

View File

@ -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";
}
}

View File

@ -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";
}
}

View File

@ -16,6 +16,9 @@
package org.teavm.classlib.java.util; package org.teavm.classlib.java.util;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ServiceLoader; import java.util.ServiceLoader;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -30,4 +33,14 @@ public class ServiceLoaderTest {
assertEquals(TestServiceImpl.class, instance.getClass()); assertEquals(TestServiceImpl.class, instance.getClass());
assertEquals(1, ((TestServiceImpl) instance).getCounter()); assertEquals(1, ((TestServiceImpl) instance).getCounter());
} }
@Test
public void loadsMultipleServices() {
List<String> strings = new ArrayList<>();
for (MultipleTestService instance : ServiceLoader.load(MultipleTestService.class)) {
strings.add(instance.foo());
}
strings.sort(String::compareTo);
assertEquals(Arrays.asList("A", "B"), strings);
}
} }

View File

@ -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