Java Code Examples for com.google.common.reflect.ClassPath#getTopLevelClasses()

The following examples show how to use com.google.common.reflect.ClassPath#getTopLevelClasses() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Yaml.java    From java with Apache License 2.0 6 votes vote down vote up
private static void initModelMap() throws IOException {
  initApiGroupMap();
  initApiVersionList();

  ClassPath cp = ClassPath.from(Yaml.class.getClassLoader());
  Set<ClassPath.ClassInfo> allClasses =
      cp.getTopLevelClasses("io.kubernetes.client.openapi.models");

  for (ClassPath.ClassInfo clazz : allClasses) {
    String modelName = "";
    Pair<String, String> nameParts = getApiGroup(clazz.getSimpleName());
    modelName += nameParts.getLeft() == null ? "" : nameParts.getLeft() + "/";

    nameParts = getApiVersion(nameParts.getRight());
    modelName += nameParts.getLeft() == null ? "" : nameParts.getLeft() + "/";
    modelName += nameParts.getRight();

    classes.put(modelName, clazz.load());
  }
}
 
Example 2
Source File: ASTClassInfoPrinter.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public static void main(String... args) {
  // Clear saved state for new calls.
  tree = TreeMultimap.create();
  astLookup = Sets.newHashSet();
  ClassPath cp = null;
  try {
    cp = ClassPath.from(ClassLoader.getSystemClassLoader());
  } catch (IOException e) {
    ErrorUtil.error(e.getMessage());
    System.exit(1);
  }
  for (ClassInfo c : cp.getTopLevelClasses("org.eclipse.jdt.core.dom")){
    astLookup.add(c.getSimpleName());
  }
  for (ClassInfo ci : cp.getTopLevelClasses("com.google.devtools.j2objc.ast")) {
    // Ignore package-info and JUnit tests.
    if (ci.getSimpleName().equals("package-info") || TestCase.class.isAssignableFrom(ci.load())) {
      continue;
    }
    walkSuperclassHierarchy(ci.load());
  }
  // Print hierarchy descending from Object.
  printClassHierarchy("Object", "");
}
 
Example 3
Source File: ModuleManager.java    From PIPE with MIT License 6 votes vote down vote up
/**
 * Finds all the fully qualified (ie: full package names) module classnames
 * by recursively searching the rootDirectories
 *
 * @return
 */
//only load attempt to add .class files
private Collection<Class<? extends GuiModule>> getModuleClasses() {
    Collection<Class<? extends GuiModule>> results = new ArrayList<>();
    try {
        ClassPath classPath = ClassPath.from(this.getClass().getClassLoader());
        ImmutableSet<ClassPath.ClassInfo> set = classPath.getTopLevelClasses(PIPE_GUI_PLUGIN_CONCRETE_PACKAGE);
        for (ClassPath.ClassInfo classInfo : set) {
            Class<?> clazz = classInfo.load();
            if (GuiModule.class.isAssignableFrom(clazz)) {
                results.add((Class<? extends GuiModule>) clazz);
            }
        }
    } catch (IOException e) {
        LOGGER.log(Level.SEVERE, e.getMessage());
    }
    return results;
}
 
Example 4
Source File: TestRunner.java    From calcite-avatica with Apache License 2.0 5 votes vote down vote up
/**
 * Finds all tests to run for the TCK.
 *
 * @return A list of test classes to run.
 */
List<Class<?>> getAllTestClasses() {
  try {
    ClassPath cp = ClassPath.from(getClass().getClassLoader());
    ImmutableSet<ClassInfo> classes =
        cp.getTopLevelClasses("org.apache.calcite.avatica.tck.tests");

    List<Class<?>> testClasses = new ArrayList<>(classes.size());
    for (ClassInfo classInfo : classes) {
      if (classInfo.getSimpleName().equals("package-info")) {
        continue;
      }
      Class<?> clz = Class.forName(classInfo.getName());
      if (Modifier.isAbstract(clz.getModifiers())) {
        // Ignore abstract classes
        continue;
      }
      testClasses.add(clz);
    }

    return testClasses;
  } catch (Exception e) {
    LOG.error("Failed to instantiate test classes", e);
    Unsafe.systemExit(TestRunnerExitCodes.TEST_CASE_INSTANTIATION.ordinal());
    // Unreachable..
    return null;
  }
}
 
Example 5
Source File: DelegateRegistry.java    From opc-ua-stack with Apache License 2.0 5 votes vote down vote up
private static void loadGeneratedClasses(ClassLoader classLoader) throws IOException, ClassNotFoundException {
    ClassPath classPath = ClassPath.from(classLoader);

    ImmutableSet<ClassInfo> structures =
            classPath.getTopLevelClasses("com.digitalpetri.opcua.stack.core.types.structured");

    ImmutableSet<ClassInfo> enumerations =
            classPath.getTopLevelClasses("com.digitalpetri.opcua.stack.core.types.enumerated");

    for (ClassInfo classInfo : Sets.union(structures, enumerations)) {
        Class<?> clazz = classInfo.load();
        Class.forName(clazz.getName(), true, classLoader);
    }
}
 
Example 6
Source File: InterfaceToImpl.java    From raml-module-builder with Apache License 2.0 4 votes vote down vote up
/**
 * Return the implementing class.
 *
 * @param implDir
 *          - package name where to search
 * @param interface2check
 *          - class name of the required interface
 * @return implementing class/es
 * @throws IOException
 *           - if the attempt to read class path resources (jar files or directories) failed.
 * @throws ClassNotFoundException
 *           - if no class in implDir implements the interface
 */
public static ArrayList<Class<?>> convert2Impl(String implDir, String interface2check, boolean allowMultiple) throws IOException, ClassNotFoundException {
  ArrayList<Class<?>> impl = new ArrayList<>();
  ArrayList<Class<?>> cachedClazz = clazzCache.get(implDir, interface2check);
  if(cachedClazz != null){
    log.debug("returned " +cachedClazz.size()+" class/es from cache");
    return cachedClazz;
  }
  ClassPath classPath = ClassPath.from(Thread.currentThread().getContextClassLoader());
  ImmutableSet<ClassPath.ClassInfo> classes = classPath.getTopLevelClasses(implDir);
  Class<?> userImpl = null;
  /** iterate over all classes in the org.folio.rest.impl package to find the one implementing the
   * requested interface */
  for (ClassPath.ClassInfo info : classes) {
    if(userImpl != null && impl.size() == 1){
      /** we found a user impl that matches the interface2check, we are done, since we can only have one of these */
      break;
    }
    try {
      Class<?> clazz = Class.forName(info.getName());
      if(!clazz.getSuperclass().getName().equals("java.lang.Object") && clazz.getSuperclass().getInterfaces().length > 0){ //NOSONAR
        /** user defined class which overrides one of the out of the box RMB implementations
         * set the clazz to the interface. find the correct implementation below */
        userImpl = clazz;
        clazz = clazz.getSuperclass();
      }
      /** loop over all interfaces the class implements */
      for (Class<?> anInterface : clazz.getInterfaces()) {
        if (!anInterface.getName().equals(interface2check)) { //NOSONAR
          /**if userImpl != null here then a user impl exists but does not match the requested interface, so set to null*/
          userImpl = null;
          /** class doesnt implement the requested package, continue to check if it implements other packages */
          continue;
        }
        if(userImpl != null){
          /** we are here if we found a user impl (extends) of an RMB existing interface implementation
           *  load the user implementation and remove previous impl if it was added in previous loop iterations
           * there can only be one impl of an override, but we dont know the order we will get from the classloader
           * will the default RMB impl be passed in here or the user implementation - so once we hit a class whose
           * super class extends a generated interface - clear out the previous impl if any from the array and add
           * the user's impl - once found we are done */
          impl.clear();
          impl.add(userImpl);
          break;
        }
        else if (!allowMultiple && impl.size() > 0) {
          throw new RuntimeException("Duplicate implementation of " + interface2check + " in " + implDir + ": " + impl.get(0).getName() + ", "
              + clazz.getName());
        }
        else{
          /** return the class found that implements the requested package */
          impl.add(clazz);
        }
      }
    } catch (ClassNotFoundException e) {
      log.error(e.getMessage(), e);
    }
  }
  if (impl.isEmpty()) {
    throw new ClassNotFoundException("Implementation of " + interface2check + " not found in " + implDir);
  }
  clazzCache.put(implDir, interface2check, impl);
  return impl;
}