Tuesday, October 4, 2016

Reflections: advanced queries on Java classpath

We have already explored Reflections here .

Another cool feature is to find programmatically all classes where a given method is invoked; here I search for all references to method oracle.ucp.common.UniversalConnectionPoolBase.registerConnectionAffinityCallback(...):


package com.pierre.typequerylanguage;

import static org.reflections.ReflectionUtils.getAllMethods;
import static org.reflections.ReflectionUtils.withPrefix;

import java.io.File;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Collection;
import java.util.Set;

import org.reflections.Reflections;
import org.reflections.scanners.FieldAnnotationsScanner;
import org.reflections.scanners.MemberUsageScanner;
import org.reflections.scanners.MethodAnnotationsScanner;
import org.reflections.scanners.MethodParameterNamesScanner;
import org.reflections.scanners.MethodParameterScanner;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.scanners.TypeAnnotationsScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.FilterBuilder;

public class FindClassesInvokingMethod{
 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'));
  }
  FilterBuilder oracleFilter = new FilterBuilder().include("oracle.*");
  Collection<URL> asList = ClasspathHelper.forJavaClassPath();
  Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(asList).filterInputsBy(oracleFilter).setScanners(new SubTypesScanner(false), new TypeAnnotationsScanner(), new FieldAnnotationsScanner(),
                new MethodAnnotationsScanner(), new MethodParameterScanner(), new MethodParameterNamesScanner(), new MemberUsageScanner()));
  ;
  System.out.println("searching now");
  
  
  Set<Method> myMethods = getAllMethods(oracle.ucp.common.UniversalConnectionPoolBase.class, withPrefix("registerConnectionAffinityCallback"));
  System.out.println(myMethods.size());
  for (Method method : myMethods) {
   System.out.println(method.getDeclaringClass());
   Set<Member> usages = reflections.getMethodUsage(method);
   for (Member member : usages) {
    System.out.println("member.getDeclaringClass() " + member.getDeclaringClass());
   }
  }
  

  System.out.println("searching done");
 }

}







No comments: