Skip to content

Commit

Permalink
Fix ReflectionsException while scanning docstrings (line#2494)
Browse files Browse the repository at this point in the history
Motivation:

`DocStringsExtractor` makes `Reflections` throw a `ReflectionsException`
if a resource folder (e.g. `META-INF/armeria/grpc`) exists but it
contains no files.

Modifications:

- Do not attempt to call `Reflections.getResources()` if the resource
  folder does not contain any files.

Result:

- No more `ReflectionsException` while scanning docstrings.
- Fixes line#2491
  • Loading branch information
trustin committed Feb 14, 2020
1 parent 8544c9d commit 87d883b
Showing 1 changed file with 17 additions and 5 deletions.
Expand Up @@ -22,21 +22,23 @@
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URL;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import org.reflections.Configuration;
import org.reflections.Reflections;
import org.reflections.Store;
import org.reflections.scanners.ResourcesScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.FilterBuilder;
import org.reflections.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.io.Resources;

import com.linecorp.armeria.common.util.UnstableApi;
Expand Down Expand Up @@ -74,22 +76,32 @@ private Map<String, String> getAllDocStrings0(ClassLoader classLoader) {
.addClassLoader(classLoader)
.setScanners(new ResourcesScanner());
if (configuration.getUrls() == null || configuration.getUrls().isEmpty()) {
return Collections.emptyMap();
// No resource folders were found.
return ImmutableMap.of();
}
final Map<String, byte[]> files = new Reflections(configuration)

final Reflections reflections = new Reflections(configuration);
final Store store = reflections.getStore();
if (!store.keySet().contains(Utils.index(ResourcesScanner.class))) {
// No resources were found.
return ImmutableMap.of();
}

final Map<String, byte[]> files = reflections
.getResources(this::acceptFile).stream()
.map(f -> {
try {
final URL url = classLoader.getResource(f);
if (url == null) {
throw new IllegalStateException("not found: " + f);
}
return new SimpleImmutableEntry<>(f, Resources.toByteArray(url));
return Maps.immutableEntry(f, Resources.toByteArray(url));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
})
.collect(toImmutableMap(Entry::getKey, Entry::getValue));

return getDocStringsFromFiles(files);
}

Expand Down

0 comments on commit 87d883b

Please sign in to comment.