title: Querying classes with arbitrary properties in Java classpath using the Reflections library
body: <p>I have used in the past the <a href="http://code.google.com/p/reflections/" rel="nofollow">Reflections</a> library to query classes in my classpath having a particular annotation, or inheriting from a particular class.
  This is very easy to do with Reflections since the library already provide methods that answer such specific queries.</p>
  
  <p>For example, the code below illustrates how to query classes inheriting from <code>SomeType</code> and classes annotated with <code>SomeAnnotation</code>, looking for classes located in the package <code>my.package.prefix</code> (example taken from the documentation of the Reflection.java class):</p>
  
  <pre><code>Reflections reflections = new Reflections("my.package.prefix");
  Set&lt;Class&lt;? extends SomeType&gt;&gt; subTypes = reflections.getSubTypesOf(SomeType.class);
  Set&lt;Class&lt;?&gt;&gt; annotated = reflections.getTypesAnnotatedWith(SomeAnnotation.class);
  </code></pre>
  
  <p>However, in my current problem what I need to do is querying for classes having an <strong>arbitrary</strong> set of properties (e.g., a combination of a particular class name + existing annotations + whatever ). I do not know these properties in advance since they should be defined by the programmer using the library I am developing.</p>
  
  <p>I envision that part of the solution will be something like asking the programmer to define a Strategy class with a boolean method answering if the class should be in the answer set or not.</p>
  
  <p>For example, the programmer should implement an interface similar to:</p>
  
  <pre><code>public interface MyStrategy {
      public boolean match(Class clazz);
  }
  </code></pre>
  
  <p>So my question is, once I have this strategy object, how I can instruct the Reflections library to filter classes keeping only those that match according to the strategy class defined by the programmer ?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Apparently the filtering part is not the most difficult task (thanks for the answer @MvG) but actually gathering all the classes to be filtered (typically classes in a set of base packages defined by the programmer).
  So the idea is querying all the classes and then filtering them with the method: 
      <code>ReflectionUtils.getAll(allClasses, predicate)</code>.</p>
  
  <p>Then, in order to obtain the initial set of classes to be filtered, I tried with querying all the subtypes of <code>Object</code> in a particular package chosen by the programmer.</p>
  
  <p>Below the code I used for this:</p>
  
  <pre><code>ConfigurationBuilder config = new ConfigurationBuilder();
  FilterBuilder fb = new FilterBuilder();
  fb.include(FilterBuilder.prefix("my.package.prefix"));
  config.filterInputsBy(fb);
  config.setUrls(ClasspathHelper.forPackage("my.package.prefix"));
  config.setScanners(new SubTypesScanner(false)); //the first argument at the constructor of SubTypesScanner indicates if the Object class should be ignored
  Reflections reflections = new Reflections(config);
  Set&lt;Class&lt;? extends Object&gt;&gt; unfilteredClasses = reflections.getSubTypesOf(Object.class);
  </code></pre>
  
  <p>Is this the best (more efficient) solution ?</p>
  
  <p>Does someone have any alternative idea ?. Thanks in advance !</p>