Thursday, November 26, 2015

Find all Java classes implementing a given interface.... reflections!

https://code.google.com/p/reflections/

Download the ÜBER JAR here https://code.google.com/p/reflections/downloads/detail?name=reflections-0.9.9-RC1-uberjar.jar&can=2&q=


package pierre.reflections;
import java.net.URL;
import java.util.Set;

import org.reflections.Reflections;



public class FindInterfaceImplementations {
 public static void main(String[] args) {
  Reflections reflections = new Reflections();
  Set<URL> urls = reflections.getConfiguration().getUrls();
  for (URL item : urls) {
   System.out.println(item.toString());
  }
  Set<Class<? extends javax.enterprise.inject.spi.InjectionTargetFactory>> classes = reflections.getSubTypesOf(javax.enterprise.inject.spi.InjectionTargetFactory.class);
  System.out.println(classes.size());
 }

}



This version will load only classes under the "oracle" package:



package com.pierre.typequerylanguage;

import java.io.File;
import java.util.Set;

import org.reflections.Reflections;

import oracle.ucp.ConnectionAffinityCallback;

public class FindInterfaceImplementation {
 public static boolean VERBOSE = true;

 public static void main(String[] args) {
  if (VERBOSE) {
   System.out.println("searching in the following jars:");
   System.out.println(System.getProperty("java.class.path").replace(File.pathSeparatorChar, '\n'));
  }
  Reflections reflections = new Reflections("oracle");
  System.out.println("searching now");
  
  
  Set<Class<? extends ConnectionAffinityCallback>> classes = reflections.getSubTypesOf(oracle.ucp.ConnectionAffinityCallback.class);
  for (Class clazz : classes) {
   System.out.println("found: " + clazz.getCanonicalName());
  }
  System.out.println("searching done");
 }

}





No comments: