atteo / classindex

Index classes, do not scan them!
Apache License 2.0
263 stars 43 forks source link

Sub interfaces are added to the service list #59

Open ST-DDT opened 4 years ago

ST-DDT commented 4 years ago

I have a sub-interface that extends my @IndexSubclasses interface, that class gets added to the META-INF/services file. This causes an exception in the application at runtime.

Classindex: v3.9

java.util.ServiceConfigurationError: com.example.Interface: Provider com.example.SubInterface could not be instantiated
    at java.util.ServiceLoader.fail(ServiceLoader.java:232)
    at java.util.ServiceLoader.access$100(ServiceLoader.java:185)
    at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:384)
    at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
    at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
    at com.example.Registry.init(Registry.java:78)
    ... plenty more
Caused by: java.lang.InstantiationException: com.example.SubInterface
    at java.lang.Class.newInstance(Class.java:427)
    at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:380)
    ... 73 more
Caused by: java.lang.NoSuchMethodException: com.example.SubInterface.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.newInstance(Class.java:412)
    ... 74 more

Test-Sourcen

@IndexSubclasses
public interface IndexedInterface {
}
public interface SubInterface extends IndexedInterface {
}
class IndexTest {

    @Test
    void testGetAll() {
        ServiceLoader.load(IndexedInterface.class)
                .forEach(System.out::println); // <-- Boom
    }

}