Java Code Examples for io.github.classgraph.ClassInfo#loadClass()

The following examples show how to use io.github.classgraph.ClassInfo#loadClass() . 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: TestDataMap.java    From justtestlah with Apache License 2.0 7 votes vote down vote up
private void initializeTestDataObjectRegistry() {
  LOG.info("Initialising test data object registry");
  LOG.info("Scanning classpath for test data classes");
  ClassGraph classGraph = new ClassGraph().enableAnnotationInfo();
  if (modelPackage != null && !modelPackage.isEmpty()) {
    classGraph = classGraph.whitelistPackages(modelPackage);
  }
  try (ScanResult scanResult = classGraph.scan()) {
    for (ClassInfo routeClassInfo :
        scanResult.getClassesWithAnnotation(TestData.class.getName())) {
      Class<?> type = routeClassInfo.loadClass();

      String name = type.getAnnotation(TestData.class).value();
      if (name.isEmpty()) {
        name = type.getSimpleName();
        name = name.substring(0, 1).toLowerCase() + name.substring(1);
      }
      LOG.info("Register class {} as {}", type, name);
      registry.register(type, name);
    }
  }
}
 
Example 2
Source File: SerializerRegistryImpl.java    From fabric-chaincode-java with Apache License 2.0 5 votes vote down vote up
/**
 * Find all the serializers that have been defined.
 *
 * @see org.hyperledger.fabric.contract.routing.RoutingRegistry#findAndSetContracts()
 * @throws IllegalAccessException
 * @throws InstantiationException
 */
public void findAndSetContents() throws InstantiationException, IllegalAccessException {

    final ClassGraph classGraph = new ClassGraph().enableClassInfo().enableAnnotationInfo();

    // set to ensure that we don't scan the same class twice
    final Set<String> seenClass = new HashSet<>();

    try (ScanResult scanResult = classGraph.scan()) {
        for (final ClassInfo classInfo : scanResult.getClassesWithAnnotation(this.annotationClass.getCanonicalName())) {
            logger.debug("Found class with contract annotation: " + classInfo.getName());
            try {
                final Class<SerializerInterface> cls = (Class<SerializerInterface>) classInfo.loadClass();
                logger.debug("Loaded class");

                final String className = cls.getCanonicalName();
                if (!seenClass.contains(className)) {
                    seenClass.add(className);
                    this.add(className, Serializer.TARGET.TRANSACTION, cls);
                }

            } catch (final IllegalArgumentException e) {
                logger.debug("Failed to load class: " + e);
            }
        }

    }

}