Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip fix'em ~all #275

Merged
merged 31 commits into from Apr 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
80a78ae
Enable getMemberFromDescriptor to extract class names when methods ha…
shyamz-22 Sep 2, 2017
f0e3a4c
Create JbossDir.java
kudrevatykh Jun 12, 2018
3212ef8
Create JbossFile.java
kudrevatykh Jun 12, 2018
570bd18
Update Vfs.java
kudrevatykh Jun 12, 2018
14f32b4
Update pom.xml
kudrevatykh Jun 12, 2018
a992e52
Updated FilterBuilder#excludePackage to use varargs; brings it in to …
Jul 29, 2018
9add02f
add repeatable annotation test
honwhy Mar 13, 2019
f3511ff
wip fix'em ~all
Jan 12, 2020
e0f8c35
Merge pull request #227 from kudrevatykh/master
ronmamo Jan 18, 2020
f309ea3
AbstractIterator to Iterator
Jan 18, 2020
2758efc
Merge branch 'release/0913' into master
ronmamo Jan 18, 2020
23bcb55
Merge pull request #262 from honwhy/master
ronmamo Jan 18, 2020
2ece435
Merge remote-tracking branch 'origin/release/0913' into release/0913
Jan 18, 2020
887b019
be minimal
Jan 18, 2020
2d25a46
windows path
Jan 18, 2020
8f81762
maven release plugins
Jan 18, 2020
7338407
Merge pull request #238 from boatmisser/exclude-package-varargs
ronmamo Jan 18, 2020
92a1efe
Merge remote-tracking branch 'origin/release/0913' into release/0913
Jan 18, 2020
94dd8a8
Merge pull request #199 from shyamz-22/hotfix/allow-method-names-with…
ronmamo Jan 18, 2020
febf2a2
Merge remote-tracking branch 'origin/release/0913' into release/0913
Jan 18, 2020
dca2b3f
predicate apply -> test
Jan 18, 2020
36f5a8b
MethodParameterNamesScanner - fix #256
Jan 18, 2020
fa4895a
Use synchronized list to avoid concurrency failures
Mar 9, 2020
b080f03
Add unit test to reproduce issue
dariocutillas Apr 24, 2020
e3c66af
Fix ConfigurationBuilder addClassLoaders
dariocutillas Apr 24, 2020
71158b6
fix custom url class loader test
Apr 26, 2020
df19baf
fix custom url class loader test
Apr 26, 2020
6df565f
fix custom url class loader test
Apr 26, 2020
481f21f
fix custom url class loader test
Apr 26, 2020
81eb4a3
Merge pull request #288 from dariocutillas/dcut_fix_ConfigurationBuilder
ronmamo Apr 26, 2020
af7ea89
Merge pull request #282 from merlinorg/master
ronmamo Apr 26, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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