com.google.common.reflect.ClassPath.getTopLevelClassesRecursive()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(10.4k)|赞(0)|评价(0)|浏览(216)

本文整理了Java中com.google.common.reflect.ClassPath.getTopLevelClassesRecursive()方法的一些代码示例,展示了ClassPath.getTopLevelClassesRecursive()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ClassPath.getTopLevelClassesRecursive()方法的具体详情如下:
包路径:com.google.common.reflect.ClassPath
类名称:ClassPath
方法名:getTopLevelClassesRecursive

ClassPath.getTopLevelClassesRecursive介绍

[英]Returns all top level classes whose package name is packageName or starts with packageName followed by a '.'.
[中]返回包名为packageName或以packageName开头,后跟“.”的所有顶级类。

代码示例

代码示例来源:origin: syncany/syncany

ImmutableSet<ClassInfo> pluginPackageSubclasses = ClassPath
  .from(Thread.currentThread().getContextClassLoader())
  .getTopLevelClassesRecursive(PLUGIN_PACKAGE_NAME);

代码示例来源:origin: checkstyle/checkstyle

/**
 * Gets checkstyle's modules in the given package recursively.
 * @param packageName the package name to use
 * @param loader the class loader used to load Checkstyle package name
 * @return the set of checkstyle's module classes
 * @throws IOException if the attempt to read class path resources failed
 * @see ModuleReflectionUtil#isCheckstyleModule(Class)
 */
private static Set<Class<?>> getCheckstyleModulesRecursive(
    String packageName, ClassLoader loader) throws IOException {
  final ClassPath classPath = ClassPath.from(loader);
  return classPath.getTopLevelClassesRecursive(packageName).stream()
      .map(ClassPath.ClassInfo::load)
      .filter(ModuleReflectionUtil::isCheckstyleModule)
      .filter(cls -> !cls.getCanonicalName()
          .startsWith("com.puppycrawl.tools.checkstyle.internal.testmodules"))
      .filter(cls -> !cls.getCanonicalName()
          .startsWith("com.puppycrawl.tools.checkstyle.packageobjectfactory"))
      .collect(Collectors.toSet());
}

代码示例来源:origin: deeplearning4j/nd4j

.getTopLevelClassesRecursive("org.nd4j.linalg.api.ops");
} catch (IOException e){

代码示例来源:origin: runelite/runelite

: classPath.getTopLevelClassesRecursive(packageName);
for (ClassInfo classInfo : classes)

代码示例来源:origin: FlowCI/flow-platform

ImmutableSet<ClassInfo> classSet = classPath.getTopLevelClassesRecursive(packageName);
Set<Class<?>> classes = new HashSet<>(classSet.size());

代码示例来源:origin: stackoverflow.com

public class OwnerFinder {
  public static void main(String[] args) {
    try {
      ClassPath classPath = ClassPath.from(OwnerFinder.class.getClassLoader());
      classPath.getTopLevelClassesRecursive("com.somepackage")
          .stream()
          .filter(c -> c.getSimpleName().equals("package-info"))
          .map(c -> c.load().getPackage().getAnnotation(PackageOwner.class))
          .forEach(a -> System.out.println(a.owner()));

    } catch(IOException e) {
      e.printStackTrace();
    }
  }
}

代码示例来源:origin: mnemonic-no/act-platform

private void bindAnnotatedClasses(String packageName, Class<? extends Annotation> annotationClass) {
 try {
  ClassPath.from(ClassLoader.getSystemClassLoader())
      .getTopLevelClassesRecursive(packageName)
      .stream()
      .map(ClassPath.ClassInfo::load)
      .filter(c -> c.getAnnotation(annotationClass) != null)
      .forEach(this::bind);
 } catch (IOException e) {
  throw new RuntimeException("Could not read classes with SystemClassLoader.", e);
 }
}

代码示例来源:origin: org.onehippo.cms7/hippo-essentials-components-hst

private void includeAnnotatedTopLevelClassesFromBeansPackage(final List<Class<?>> allClasses)
    throws IOException, ClassNotFoundException {
  if (!Strings.isNullOrEmpty(beansPackage)) {
    final ClassPath classPath = ClassPath.from(getClass().getClassLoader());
    final ImmutableSet<ClassPath.ClassInfo> topLevelClasses = classPath.getTopLevelClassesRecursive(beansPackage);
    for (ClassPath.ClassInfo topLevelClass : topLevelClasses) {
      final String name = topLevelClass.getName();
      final Class<?> clazz = Class.forName(name);
      if (clazz.isAnnotationPresent(XmlRootElement.class)) {
        allClasses.add(clazz);
      }
    }
  }
}

代码示例来源:origin: org.opendaylight.odlparent/features-test

/**
 * Returns all classes in the named package, and its sub-packages.
 */
public static Stream<Class<?>> getClasses(ClassLoader classLoader, String packageName) {
  try {
    ClassPath classPath = ClassPath.from(classLoader);
    // inspired by https://github.com/vorburger/ch.vorburger.minecraft.osgi/blob/master/ch.vorburger.minecraft.osgi/src/main/java/ch/vorburger/osgi/embedded/PackagesBuilder.java
    return classPath.getTopLevelClassesRecursive(packageName)
        .stream().map(ClassPath.ClassInfo::load)
         // to include all inner classes, including anonymous inner classes:
        .flatMap(ReflectionUtil::getDeclaredAndAnonymousInnerClass);
  } catch (IOException e) {
    throw new IllegalStateException("ClassPath.from(classLoader) failed", e);
  }
}

代码示例来源:origin: MartinHaeusler/chronos

public static Set<Class<?>> getClassesInPackages(final String[] packageNames) {
  Set<Class<?>> resultSet = Sets.newHashSet();
  for (String packageName : packageNames) {
    try {
      ClassPath classPath = ClassPath.from(Thread.currentThread().getContextClassLoader());
      Set<Class<?>> topLevelClasses = classPath.getTopLevelClassesRecursive(packageName).stream()
          .map(ci -> ci.load()).collect(Collectors.toSet());
      resultSet.addAll(topLevelClasses);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  return resultSet;
}

代码示例来源:origin: io.github.splotycode.mosaik/WebApi

public void registerTransformers(String packagePath) {
  try {
    for (ClassPath.ClassInfo classInfo : ClassPath.from(getClass().getClassLoader()).getTopLevelClassesRecursive(packagePath)) {
      Class<?> clazz = classInfo.load();
      if (ParameterResolver.class.isAssignableFrom(clazz)) {
        registerTransformer((Class<? extends ParameterResolver>) clazz);
      }
    }
  } catch (IOException e) {
    e.printStackTrace();
  }
}

代码示例来源:origin: ru.sbtqa.tag.pagefactory/page-factory-core

private static Set<Class<?>> getAllClasses() {
    Set<Class<?>> allClasses = new HashSet();

    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    try {
      for (ClassPath.ClassInfo info : ClassPath.from(loader).getTopLevelClassesRecursive(PROPERTIES.getPagesPackage())) {
        allClasses.add(info.load());
      }
    } catch (IOException ex) {
      LOG.warn("Failed to shape class info set", ex);
    }

    return allClasses;
  }
}

代码示例来源:origin: com.github.fosin/cdp-utils

/**
 * 递归的获得package下的所有类信息,不包含内部类
 *
 * @param packageName String
 * @return Collection
 */
public static ImmutableSet<ClassPath.ClassInfo> loadClasses(String packageName) {
  try {
    return from(defaultLoader()).getTopLevelClassesRecursive(packageName);
  } catch (IOException e) {
    Throwables.propagate(e);
  }
  return null;
}

代码示例来源:origin: io.github.splotycode.mosaik/Util

default void registerPackage(String path) {
  try {
    for (ClassPath.ClassInfo classInfo : ClassPath.from(getClass().getClassLoader()).getTopLevelClassesRecursive(path)) {
      Class<?> clazz = classInfo.load();
      if (getObjectClass().isAssignableFrom(clazz)) {
        register((Class<? extends T>) clazz);
      }
    }
  } catch (IOException ex) {
    ex.printStackTrace();
  }
}

代码示例来源:origin: joel-costigliola/assertj-assertions-generator

private static Set<TypeToken<?>> getPackageClassesFromClasspathJars(String packageName, ClassLoader classLoader)
  throws IOException {
 ImmutableSet<ClassInfo> classesInfo = ClassPath.from(classLoader).getTopLevelClassesRecursive(packageName);
 Set<TypeToken<?>> classesInPackage = new HashSet<>();
 for (ClassInfo classInfo : classesInfo) {
  classesInPackage.add(TypeToken.of(classInfo.load()));
 }
 Set<TypeToken<?>> filteredClassesInPackage = new HashSet<>();
 for (TypeToken<?> classFromJar : classesInPackage) {
  if (isClassCandidateToAssertionsGeneration(classFromJar)) {
   filteredClassesInPackage.add(classFromJar);
  }
 }
 return filteredClassesInPackage;
}

代码示例来源:origin: net.serenity-bdd/serenity-model

private List<String> requirementPathsFromClassesInPackage(String rootPackage) {
  List<String> requirementPaths = new ArrayList<>();
  ClassPath classpath;
  try {
    classpath = ClassPath.from(Thread.currentThread().getContextClassLoader());
  } catch (IOException e) {
    throw new CouldNotLoadRequirementsException(e);
  }
  for (ClassPath.ClassInfo classInfo : classpath.getTopLevelClassesRecursive(rootPackage)) {
    if (classRepresentsARequirementIn(classInfo)) {
      requirementPaths.add(classInfo.getName());
    }
  }
  return requirementPaths;
}

代码示例来源:origin: minnal/minnal

public void scan(Listener<Class<?>> listener) {
  try {
    ClassPath path = ClassPath.from(classLoader);
    for (String packageName : packages) {
      for (ClassInfo classInfo : path.getTopLevelClassesRecursive(packageName)) {
        Class<?> clazz = classLoader.loadClass(classInfo.getName());
        if (match(clazz)) {
          listener.handle(clazz);
        }
      }
    }
  } catch (Exception e) {
    e.printStackTrace();
    // TODO Handle exception
  }
}

代码示例来源:origin: com.baidu.hugegraph/hugegraph-common

public static Iterator<ClassInfo> classes(String... packages)
    throws IOException {
  ClassPath path = ClassPath.from(ReflectionUtil.class.getClassLoader());
  ExtendableIterator<ClassInfo> results = new ExtendableIterator<>();
  for (String p : packages) {
    results.extend(path.getTopLevelClassesRecursive(p).iterator());
  }
  return results;
}

代码示例来源:origin: NucleusPowered/Nucleus

@Test
@SuppressWarnings("unchecked")
public void testThatAnythingThatIsAConcreteModuleHasAModuleDataAnnotation() throws IOException {
  Set<ClassPath.ClassInfo> ci = ClassPath.from(this.getClass().getClassLoader())
      .getTopLevelClassesRecursive("io.github.nucleuspowered.nucleus.modules");
  Set<Class<? extends StandardModule>> sc = ci.stream().map(ClassPath.ClassInfo::load).filter(StandardModule.class::isAssignableFrom)
      .map(x -> (Class<? extends StandardModule>)x).collect(Collectors.toSet());
  List<Class<?>> moduleList = sc.stream().filter(x -> !x.isAnnotationPresent(ModuleData.class)).collect(Collectors.toList());
  if (!moduleList.isEmpty()) {
    StringBuilder sb = new StringBuilder("Some modules do not have the ModuleData annotation: ");
    moduleList.forEach(x -> sb.append(x.getName()).append(System.lineSeparator()));
    Assert.fail(sb.toString());
  }
}

代码示例来源:origin: NucleusPowered/Nucleus

@Test
@SuppressWarnings("unchecked")
public void testThatAnythingThatIsAnAbstractConfigAdapterIsAlsoANucleusConfigAdapter() throws IOException {
  Set<ClassPath.ClassInfo> ci = ClassPath.from(this.getClass().getClassLoader())
      .getTopLevelClassesRecursive("io.github.nucleuspowered.nucleus.modules");
  Set<Class<? extends AbstractConfigAdapter<?>>> sc = ci.stream().map(ClassPath.ClassInfo::load)
      .filter(AbstractConfigAdapter.class::isAssignableFrom)
      .map(x -> (Class<? extends AbstractConfigAdapter<?>>)x).collect(Collectors.toSet());
  List<Class<?>> moduleList = sc.stream().filter(x -> !NucleusConfigAdapter.class.isAssignableFrom(x)).collect(Collectors.toList());
  if (!moduleList.isEmpty()) {
    StringBuilder sb = new StringBuilder("Some config adapters are not of the NucleusConfigAdapter type: ");
    moduleList.forEach(x -> sb.append(x.getName()).append(System.lineSeparator()));
    Assert.fail(sb.toString());
  }
}

相关文章