mukteshkrmishra / reflections

Automatically exported from code.google.com/p/reflections
Do What The F*ck You Want To Public License
0 stars 0 forks source link

Bug in getSubTypesOf #175

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I have the following code:

String rootPackage = "blah.definitions";
Set<Class<? extends ModelDefinition>> all = new 
Reflections(rootPackage).getSubTypesOf(ModelDefinition.class);
Set<Class<? extends ModelDefinition>> adminOnly = new Reflections(rootPackage + 
".admin").getSubTypesOf(ModelDefinition.class);
Set<Class<? extends ModelDefinition>> pubOnly = new Reflections(rootPackage + 
".publisher").getSubTypesOf(ModelDefinition.class);
Set<Class<? extends ModelDefinition>> userOnly = new Reflections(rootPackage + 
".user").getSubTypesOf(ModelDefinition.class);
Set<Class<? extends ModelDefinition>> commonOnly = new Reflections(rootPackage, 
new FilterBuilder()
    .exclude(rootPackage + ".admin.*")
    .exclude(rootPackage + ".user.*")
    .exclude(rootPackage + ".publisher.*")
    .exclude(rootPackage + ".anon.*")
  ).getSubTypesOf(ModelDefinition.class);

where ModelDefinition is a custom class of which there can be subclasses in 
different subpackages that can have the same class name.

The 5 sets contain the following:

all = [class blah.definitions.AppRefModelDefinition, class 
blah.definitions.publisher.AppDefinition, class 
blah.definitions.user.ProfileModelDefinition, class 
blah.definitions.publisher.ProfileModelDefinition, class 
blah.definitions.RootAppModelDefinition, class 
blah.definitions.user.UserAccessModelDefinition, class 
blah.definitions.admin.AppDefinition]

adminOnly = []

pubOnly = [class blah.definitions.publisher.ProfileModelDefinition]

userOnly = [class blah.definitions.user.ProfileModelDefinition, class 
blah.definitions.user.UserAccessModelDefinition]

commonOnly = [class blah.definitions.AppRefModelDefinition, class 
blah.definitions.RootAppModelDefinition]

The adminOnly set is wrong because it should contain 
blah.definitions.admin.AppDefinition; the pubOnly is also wrong because it does 
not contain the blah.definitions.publisher.AppDefinition class. Note that the 
two AppDefinition both extend RootAppModelDefinition, which might be relevant.

Is this a known bug?

Original issue reported on code.google.com by amojo...@gmail.com on 1 May 2014 at 9:42

GoogleCodeExporter commented 8 years ago
Please see https://github.com/ronmamo/reflections/issues/26

To make a long story short:
1. you should always scan all the relevant transitive jars a priori.
2. you don't need to scan multiple times, but only once with a good enough 
scope.
3. discriminating types according to package infix is not considered a good 
practice, though possible.

Original comment by ronm...@gmail.com on 5 Jun 2014 at 5:42