Skip to content

Commit

Permalink
wip fix'em ~all (#275)
Browse files Browse the repository at this point in the history
* Enable getMemberFromDescriptor to extract class names when methods have spaces

* Create JbossDir.java

* Create JbossFile.java

* Update Vfs.java

* Update pom.xml

* Updated FilterBuilder#excludePackage to use varargs; brings it in to line with includePackage.

* add repeatable annotation test

* wip fix'em ~all

* AbstractIterator to Iterator

* be minimal

* windows path

* maven release plugins

* predicate apply -> test

* MethodParameterNamesScanner - fix #256

* Use synchronized list to avoid concurrency failures

* Add unit test to reproduce issue

* Fix ConfigurationBuilder addClassLoaders

* fix custom url class loader test

* fix custom url class loader test

* fix custom url class loader test

* fix custom url class loader test

Co-authored-by: shyamz-22 <shykart2203@gmail.com>
Co-authored-by: kudrevatykh <alexander@kudrevatykh.com>
Co-authored-by: burkec <christopher.burke@airnz.co.nz>
Co-authored-by: Honwhy Wang <honwhy.wang@gmail.com>
Co-authored-by: ronma <ronma@>
Co-authored-by: merlin <merlin@lo.ventures>
Co-authored-by: Darío Cutillas <dario.cutillas@neotechnology.com>
  • Loading branch information
7 people committed Apr 26, 2020
1 parent fb7786b commit 9a1ac08
Show file tree
Hide file tree
Showing 21 changed files with 503 additions and 91 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -2,4 +2,5 @@ language: java

jdk:
- openjdk8
- openjdk9
- openjdk11
65 changes: 31 additions & 34 deletions pom.xml
Expand Up @@ -62,8 +62,7 @@
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>${javassist.version}</version>
<optional>false
</optional> <!-- case: when not actually scanning with javassist or if using {@link Reflections.collect()} -->
<optional>false</optional> <!-- case: when not actually scanning with javassist or if using {@link Reflections.collect()} -->
</dependency>

<dependency>
Expand Down Expand Up @@ -103,6 +102,14 @@
<optional>true</optional> <!-- case: when other logging implementation used or logging is not needed -->
</dependency>

<dependency>
<groupId>org.jboss</groupId>
<artifactId>jboss-vfs</artifactId>
<version>3.2.12.Final</version>
<scope>provided</scope>
<optional>true</optional><!-- case: when using jboss-vfs -->
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand All @@ -112,26 +119,34 @@

</dependencies>

<!-- Release -->
<profiles>
<profile>
<id>build</id>
<id>release</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
Expand All @@ -140,40 +155,11 @@
</goals>
</execution>
</executions>
<configuration>
<doclint>none</doclint>
</configuration>
</plugin>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireProperty>
<property>gpg.keyname</property>
<message>gpg.keyname must be supplied using -D.
create a key using gpg --gen-key or use existing one.
</message>
</requireProperty>
<requireProperty>
<property>gpg.passphrase</property>
<message>gpg.passphrase must be supplied using -D.
create a key using gpg --gen-key or use existing one.
</message>
</requireProperty>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>sign-artifacts</id>
Expand All @@ -184,6 +170,17 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.8</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/org/reflections/ReflectionUtils.java
Expand Up @@ -131,14 +131,18 @@ public static Set<Field> getFields(Class<?> type, Predicate<? super Field>... pr
}

/** get all annotations of given {@code type}, up the super class hierarchy, optionally filtered by {@code predicates} */
public static <T extends AnnotatedElement> Set<Annotation> getAllAnnotations(T type, Predicate<Annotation>... predicates) {
Set<Annotation> result = new HashSet<>();
public static <T extends AnnotatedElement> Set<Annotation> getAllAnnotations(T type, Predicate<Annotation>... predicates) {
Set<Annotation> result = new LinkedHashSet<>();
List<AnnotatedElement> keys = new ArrayList();
if (type instanceof Class) {
for (Class<?> t : getAllSuperTypes((Class<?>) type)) {
result.addAll(getAnnotations(t, predicates));
keys.addAll(getAllSuperTypes((Class<?>) type));
}
for (int i = 0; i < keys.size(); i++) {
for (Annotation annotation : getAnnotations(keys.get(i), predicates)) {
if (result.add(annotation)) {
keys.add(annotation.annotationType());
}
}
} else {
result.addAll(getAnnotations(type, predicates));
}
return result;
}
Expand Down Expand Up @@ -313,10 +317,9 @@ public static Class<?> forName(String typeName, ClassLoader... classLoaders) {
}
}

if (Reflections.log != null) {
if (Reflections.log != null && Reflections.log.isTraceEnabled()) {
for (ReflectionsException reflectionsException : reflectionsExceptions) {
Reflections.log.warn("could not get type for name " + typeName + " from any class loader",
reflectionsException);
Reflections.log.trace("could not get type for name " + typeName + " from any class loader", reflectionsException);
}
}

Expand Down
38 changes: 21 additions & 17 deletions src/main/java/org/reflections/Reflections.java
Expand Up @@ -37,6 +37,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
Expand Down Expand Up @@ -126,7 +127,7 @@ public class Reflections {
*/
public Reflections(final Configuration configuration) {
this.configuration = configuration;
store = new Store();
store = new Store(configuration);

if (configuration.getScanners() != null && !configuration.getScanners().isEmpty()) {
//inject to scanners
Expand Down Expand Up @@ -184,18 +185,20 @@ public Reflections(final Object... params) {

protected Reflections() {
configuration = new ConfigurationBuilder();
store = new Store();
store = new Store(configuration);
}

//
protected void scan() {
if (configuration.getUrls() == null || configuration.getUrls().isEmpty()) {
if (log != null) log.warn("given scan urls are empty. set urls in the configuration");
if (log != null) {
log.warn("given scan urls are empty. set urls in the configuration");
}
return;
}

if (log != null && log.isDebugEnabled()) {
log.debug("going to scan these urls: {}", configuration.getUrls());
if (log != null && log.isTraceEnabled()) {
log.trace("going to scan these urls: {}", configuration.getUrls());
}

long time = System.currentTimeMillis();
Expand All @@ -207,8 +210,8 @@ protected void scan() {
try {
if (executorService != null) {
futures.add(executorService.submit(() -> {
if (log != null) {
log.debug("[{}] scanning {}", Thread.currentThread().toString(), url);
if (log != null && log.isTraceEnabled()) {
log.trace("[{}] scanning {}", Thread.currentThread().toString(), url);
}
scan(url);
}));
Expand All @@ -223,10 +226,13 @@ protected void scan() {
}
}

//todo use CompletionService
if (executorService != null) {
for (Future future : futures) {
try { future.get(); } catch (Exception e) { throw new RuntimeException(e); }
try {
future.get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
}

Expand Down Expand Up @@ -270,9 +276,9 @@ protected void scan(URL url) {
classObject = scanner.scan(file, classObject, store);
}
} catch (Exception e) {
if (log != null) {
if (log != null && log.isTraceEnabled()) {
// SLF4J will filter out Throwables from the format string arguments.
log.debug("could not scan file {} in url {} with scanner {}", file.getRelativePath(), url.toExternalForm(), scanner.getClass().getSimpleName(), e);
log.trace("could not scan file {} in url {} with scanner {}", file.getRelativePath(), url.toExternalForm(), scanner.getClass().getSimpleName(), e);
}
}
}
Expand Down Expand Up @@ -332,7 +338,6 @@ public static Reflections collect(final String packagePrefix, final Predicate<St
public Reflections collect(final InputStream inputStream) {
try {
merge(configuration.getSerializer().read(inputStream));
if (log != null) log.info("Reflections collected metadata from input stream using serializer " + configuration.getSerializer().getClass().getName());
} catch (Exception ex) {
throw new ReflectionsException("could not merge input stream", ex);
}
Expand Down Expand Up @@ -389,7 +394,9 @@ public void expandSuperTypes() {
private void expandSupertypes(Store store, String key, Class<?> type) {
for (Class<?> supertype : ReflectionUtils.getSuperTypes(type)) {
if (store.put(SubTypesScanner.class, supertype.getName(), key)) {
if (log != null) log.debug("expanded subtype {} -> {}", supertype.getName(), key);
if (log != null && log.isTraceEnabled()) {
log.trace("expanded subtype {} -> {}", supertype.getName(), key);
}
expandSupertypes(store, supertype.getName(), supertype);
}
}
Expand Down Expand Up @@ -650,10 +657,7 @@ public File save(final String filename) {
* so that it could be found later much faster using the load method
*/
public File save(final String filename, final Serializer serializer) {
File file = serializer.save(this, filename);
if (log != null)
log.info("Reflections successfully saved in " + file.getAbsolutePath() + " using " + serializer.getClass().getSimpleName());
return file;
return serializer.save(this, filename);
}

private ClassLoader[] loaders() { return configuration.getClassLoaders(); }
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/org/reflections/Store.java
@@ -1,5 +1,7 @@
package org.reflections;

import org.reflections.scanners.Scanner;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -23,8 +25,12 @@ public class Store {

private final ConcurrentHashMap<String, Map<String, Collection<String>>> storeMap;

protected Store() {
protected Store(Configuration configuration) {
storeMap = new ConcurrentHashMap<>();
for (Scanner scanner : configuration.getScanners()) {
String index = index(scanner.getClass());
storeMap.computeIfAbsent(index, s -> new ConcurrentHashMap<>());
}
}

/** return all indices */
Expand Down Expand Up @@ -115,7 +121,7 @@ public boolean put(Class<?> scannerClass, String key, String value) {

public boolean put(String index, String key, String value) {
return storeMap.computeIfAbsent(index, s -> new ConcurrentHashMap<>())
.computeIfAbsent(key, s -> new ArrayList<>())
.computeIfAbsent(key, s -> Collections.synchronizedList(new ArrayList<>()))
.add(value);
}

Expand Down
Expand Up @@ -7,10 +7,8 @@
import org.reflections.adapters.MetadataAdapter;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

import static org.reflections.util.Utils.join;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/** scans methods/constructors and indexes parameter names */
@SuppressWarnings("unchecked")
Expand All @@ -25,12 +23,16 @@ public void scan(Object cls, Store store) {
if (acceptResult(key)) {
CodeAttribute codeAttribute = ((MethodInfo) method).getCodeAttribute();
LocalVariableAttribute table = codeAttribute != null ? (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag) : null;
int length = table != null ? table.tableLength() : 0;
int i = Modifier.isStatic(((MethodInfo) method).getAccessFlags()) ? 0 : 1; //skip this
if (i < length) {
List<String> names = new ArrayList<>(length - i);
while (i < length) names.add(((MethodInfo) method).getConstPool().getUtf8Info(table.nameIndex(i++)));
put(store, key, join(names, ", "));
int length = md.getParameterNames(method).size();
if (length > 0) {
int shift = Modifier.isStatic(((MethodInfo) method).getAccessFlags()) ? 0 : 1; //skip this
String join = IntStream.range(shift, length + shift)
.mapToObj(i -> ((MethodInfo) method).getConstPool().getUtf8Info(table.nameIndex(i)))
.filter(name -> !name.startsWith("this$"))
.collect(Collectors.joining(", "));
if (!join.isEmpty()) {
put(store, key, join);
}
}
}
}
Expand Down
Expand Up @@ -7,7 +7,7 @@
* <p>key: value - {web.xml: WEB-INF/web.xml} */
public class ResourcesScanner extends AbstractScanner {
public boolean acceptsInput(String file) {
return !file.endsWith(".class"); //not a class
return !file.endsWith(".class") && !file.endsWith(".groovy") && !file.endsWith(".scala") && !file.endsWith(".kt"); //not a class
}

@Override public Object scan(Vfs.File file, Object classObject, Store store) {
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/org/reflections/util/ConfigurationBuilder.java
Expand Up @@ -110,7 +110,7 @@ else if (param instanceof Class) {
else if (param instanceof ClassLoader) { /* already taken care */ }
else if (param instanceof Predicate) { filter.add((Predicate<String>) param); }
else if (param instanceof ExecutorService) { builder.setExecutorService((ExecutorService) param); }
else if (Reflections.log != null) { throw new ReflectionsException("could not use param " + param); }
else throw new ReflectionsException("could not use param " + param);
}

if (builder.getUrls().isEmpty()) {
Expand All @@ -119,6 +119,9 @@ else if (param instanceof ClassLoader) { /* already taken care */ }
} else {
builder.addUrls(ClasspathHelper.forClassLoader()); //default urls getResources("")
}
if (builder.urls.isEmpty()) {
builder.addUrls(ClasspathHelper.forJavaClassPath());
}
}

builder.filterInputsBy(filter);
Expand Down Expand Up @@ -290,8 +293,9 @@ public ConfigurationBuilder setExpandSuperTypes(boolean expandSuperTypes) {
}

/** set class loader, might be used for resolving methods/fields */
public void setClassLoaders(ClassLoader[] classLoaders) {
public ConfigurationBuilder setClassLoaders(ClassLoader[] classLoaders) {
this.classLoaders = classLoaders;
return this;
}

/** add class loader, might be used for resolving methods/fields */
Expand All @@ -301,9 +305,9 @@ public ConfigurationBuilder addClassLoader(ClassLoader classLoader) {

/** add class loader, might be used for resolving methods/fields */
public ConfigurationBuilder addClassLoaders(ClassLoader... classLoaders) {
this.classLoaders = this.classLoaders == null ? classLoaders :
Stream.concat(Stream.concat(Arrays.stream(this.classLoaders), Arrays.stream(classLoaders)), Stream.of(ClassLoader.class))
.distinct().toArray(ClassLoader[]::new);
this.classLoaders = this.classLoaders == null
? classLoaders
: Stream.concat(Arrays.stream(this.classLoaders), Arrays.stream(classLoaders)).toArray(ClassLoader[]::new);
return this;
}

Expand Down

0 comments on commit 9a1ac08

Please sign in to comment.