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

[Regression 0.10] MethodAnnotationsScanner no longer find annotated method #351

Closed
ncoquelet opened this issue Oct 8, 2021 · 3 comments
Closed

Comments

@ncoquelet
Copy link

ncoquelet commented Oct 8, 2021

Hi
After updated to 0.10 or 0.10.1, Reflections library doesn't works correctly with .getMethodsAnnotatedWith() deprecated method.
Works well with the version 0.9.11

Bellow the code to reproduce it

Thanks a lot for all your works
Cheers

package my.test;

import java.lang.reflect.Method;

import org.reflections.Reflections;
import org.reflections.scanners.MethodAnnotationsScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;

public class FailedReflection10OnAnnotatedMethod {

    public static void main(String[] args) {
        Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage("my.test")).setScanners(new MethodAnnotationsScanner()));
        for (Method method : reflections.getMethodsAnnotatedWith(AnAnnotation.class)) {
            System.out.println("found " + method.getName());
        }
    }
}
package my.test;

public class ClassWithAnnotatedMethod {

    @AnAnnotation
    public void annotatedMethod() {
    }
}
package my.test;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AnAnnotation {
}
ghost pushed a commit to drftpd-ng/drftpd that referenced this issue Oct 13, 2021
@ronmamo
Copy link
Owner

ronmamo commented Oct 14, 2021

thanks @ncoquelet , that should work without changes according to the test. will take a look and fix it on next release.

btw, the newer syntax for your case would be

Reflections reflections = new Reflections(
  new ConfigurationBuilder().forPackage("my.test").setScanners(MethodAnnotated));

Set<Method> methods = reflections.get(MethodsAnnotated.with(AnAnnotation.class).as(Method.class))

I guess the same result remains

@ncoquelet
Copy link
Author

Yes, it's the deprecated scanner that fail, using the new scanner enum works like a charm

Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage(PACKAGE_TO_SCAN)).setScanners(Scanners.MethodsAnnotated));
Reflections reflections = new Reflections(new ConfigurationBuilder().forPackage(PACKAGE_TO_SCAN).setScanners(Scanners.MethodsAnnotated));
Reflections reflections = new Reflections(PACKAGE_TO_SCAN, Scanners.MethodsAnnotated);

with the old and the new getter

Set<Method> methods = reflections.getMethodsAnnotatedWith(AnAnnotation.class)
Set<Method> methods = reflections.get(Scanners.MethodsAnnotated.with(AnAnnotation.class).as(Method.class))

We test all our usecases and we effectively switch to the last version with the new scanner and the new syntax.

@ronmamo
Copy link
Owner

ronmamo commented Oct 25, 2021

fixed in 0.10.2

thanks @ncoquelet for the perfect feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants