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

MethodAnnotationsScanner is not getting configured unless there is at least one class using the annotation #297

Closed
sivaprasadreddy opened this issue Jul 16, 2020 · 7 comments

Comments

@sivaprasadreddy
Copy link

I am using reflections:0.9.12 and I want to scan for Constructors which have some annotation, let's say @Autowired.

public class UserService {
	private UserRepository repository;

	public UserService() {
	}

	@Autowired
	public UserService(UserRepository repository) {
		this.repository = repository;
	}
}
Reflections reflections = new Reflections(new ConfigurationBuilder()
		.setUrls(ClasspathHelper.forPackage("com.sivalabs.myapp"))
		.setScanners(new MethodAnnotationsScanner()));

Set<Constructor> constructorsAnnotatedWith = reflections.getConstructorsAnnotatedWith(Autowired.class);

This code is working fine if there is at least one class using @Autowired annotation. But if there is no class using @Autowired annotation in the package com.sivalabs.myapp then following exception is being thrown:

Exception in thread "main" org.reflections.ReflectionsException: Scanner MethodAnnotationsScanner was not configured
	at org.reflections.Store.get(Store.java:39)
	at org.reflections.Store.get(Store.java:61)
	at org.reflections.Store.get(Store.java:46)
	at org.reflections.Reflections.getConstructorsAnnotatedWith(Reflections.java:515)

Expectation: MethodAnnotationsScanner should be registered even when there are no classes using any particular annotation and when reflections.getConstructorsAnnotatedWith() is called it should return empty result.

@dota17
Copy link
Contributor

dota17 commented Jul 25, 2020

I wrote a test case according to your description and tried it, it can work without any exception. But when I change the parameter of the function forPackage() to wrong, the error message is the same as yours. So I think your function parameters may be wrong.

@sivaprasadreddy
Copy link
Author

The package I used ("com.sivalabs.myapp" ) is correct and I have classes exists in that package also. But even if the package name given for forPackage() doesn't exists, it should return empty Set instead of throwing exception.

@dota17
Copy link
Contributor

dota17 commented Aug 6, 2020

Yes, this exception will occur when annotations are not used at all. If I use other annotations in another class, this exception will not occur. I will try to figure this out.

@dota17
Copy link
Contributor

dota17 commented Aug 6, 2020

In reflections:0.9.12, if your scanner scans nothing from the class file, the storeMap will be empty. As a result, when reflections.getConstructorsAnnotatedWith() is called, an expection will be thrown here:

Map<String, Collection<String>> mmap = storeMap.get(index);

But this problem has been fixed in the latest master branch. When the class Store is initialized,storeMap will get an initial value。See:

protected Store(Configuration configuration) {

@veselov
Copy link

veselov commented Aug 4, 2021

Bump. Is it possible to make a release with this fix?

@ronmamo
Copy link
Owner

ronmamo commented Sep 28, 2021

apologize for this inconvenience !

scanner was not configured exception - as @dota17 mentioned, this is a known issue in 0.9.12, a simple workaround is to check if the getStore() contains index for the scanner before querying. next version 0.10 fixes this.

@ronmamo
Copy link
Owner

ronmamo commented Oct 4, 2021

fixed on 0.10

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

4 participants