public ClasspathScanningPrimitiveTypeRegistry(ClassLoader classLoader) { Map<String, PrimitiveType> types = CACHE.computeIfAbsent(classLoader, cl -> { final Map<String, PrimitiveType> result = new ConcurrentHashMap<>(); final String[] whitelistPackages = StringUtils.split(System.getProperty("io.atomix.whitelistPackages"), ","); final ClassGraph classGraph = whitelistPackages != null ? new ClassGraph().enableClassInfo().whitelistPackages(whitelistPackages).addClassLoader(classLoader) : new ClassGraph().enableClassInfo().addClassLoader(classLoader); try (final ScanResult scanResult = classGraph.scan()) { scanResult.getClassesImplementing(PrimitiveType.class.getName()).forEach(classInfo -> { if (classInfo.isInterface() || classInfo.isAbstract() || Modifier.isPrivate(classInfo.getModifiers())) { return; } final PrimitiveType primitiveType = newInstance(classInfo.loadClass()); final PrimitiveType oldPrimitiveType = result.put(primitiveType.name(), primitiveType); if (oldPrimitiveType != null) { LOGGER.warn("Found multiple primitives types name={}, classes=[{}, {}]", primitiveType.name(), oldPrimitiveType.getClass().getName(), primitiveType.getClass().getName()); } }); } return Collections.unmodifiableMap(result); }); primitiveTypes.putAll(types); }
@SuppressWarnings("unchecked") private ClasspathScanningRegistry(ClassLoader classLoader, Set<String> whitelistPackages) { final Map<Class<? extends NamedType>, Map<String, NamedType>> registrations = CACHE.computeIfAbsent(classLoader, cl -> { final ClassGraph classGraph = !whitelistPackages.isEmpty() ? new ClassGraph().enableClassInfo().whitelistPackages(whitelistPackages.toArray(new String[0])).addClassLoader(classLoader) : new ClassGraph().enableClassInfo().addClassLoader(classLoader); try (final ScanResult scanResult = classGraph.scan()) { final Map<Class<? extends NamedType>, Map<String, NamedType>> result = new ConcurrentHashMap<>(); scanResult.getClassesImplementing(NamedType.class.getName()).forEach(classInfo -> { if (classInfo.isInterface() || classInfo.isAbstract() || Modifier.isPrivate(classInfo.getModifiers())) { return; } final Class<?> type = classInfo.loadClass(); final Class<? extends NamedType> classType = getClassType(type); final NamedType instance = newInstance(type); final NamedType oldInstance = result.computeIfAbsent(classType, t -> new HashMap<>()).put(instance.name(), instance); if (oldInstance != null) { LOGGER.warn("Found multiple types with name={}, classes=[{}, {}]", instance.name(), oldInstance.getClass().getName(), instance.getClass().getName()); } }); return result; } }); this.registrations.putAll(registrations); }
deployment.getRegistry().addPerInstanceResource(classInfo.loadClass(), "/v1"); });
/** * Load the {@link Annotation} class corresponding to this {@link AnnotationInfo} object, by calling * {@code getClassInfo().loadClass()}, then create a new instance of the annotation, with the annotation * parameter values obtained from this {@link AnnotationInfo} object. * * @return The new {@link Annotation} instance. */ public Annotation loadClassAndInstantiate() { final Class<? extends Annotation> annotationClass = getClassInfo().loadClass(Annotation.class); return (Annotation) Proxy.newProxyInstance(annotationClass.getClassLoader(), new Class[] { annotationClass }, new AnnotationInvocationHandler(annotationClass, this)); }
private static Stream<Class<?>> loadClass(ClassInfo classInfo, Class superType) { try { return Stream.of(Class.forName(classInfo.getName(), true, superType.getClassLoader())); } catch (ClassNotFoundException e) { log.warn(String.format("Auto discovered class %s could not be loaded using the same loader that loaded %s." + " Trying other loaders... For details see %s", classInfo.getName(), superType.getName(), Urls.Errors.IMPLEMENTATION_CLASS_LOADING_FAILED)); } try { return Stream.of(classInfo.loadClass()); } catch (Exception e) { log.error("Auto discovered type " + classInfo.getName() + " failed to load and will be ignored", e); return Stream.empty(); } }
/** * Convert this list of {@link ClassInfo} objects to a list of {@code Class<?>} objects. Causes the classloader * to load the class named by each {@link ClassInfo} object, if it is not already loaded. * * @param ignoreExceptions * If true, ignore any exceptions or errors thrown during classloading. If an exception or error is * thrown during classloading, no {@code Class<?>} reference is added to the output class for the * corresponding {@link ClassInfo} object, so the returned list may contain fewer items than this * input list. If false, {@link IllegalArgumentException} is thrown if the class could not be loaded. * @throws IllegalArgumentException * if ignoreExceptions is false and an exception or error was thrown while trying to load any of the * classes. * @return The loaded {@code Class<?>} objects corresponding to each {@link ClassInfo} object in this list. */ public List<Class<?>> loadClasses(final boolean ignoreExceptions) { if (this.isEmpty()) { return Collections.emptyList(); } else { final List<Class<?>> classRefs = new ArrayList<>(); // Try loading each class for (final ClassInfo classInfo : this) { final Class<?> classRef = classInfo.loadClass(ignoreExceptions); if (classRef != null) { classRefs.add(classRef); } } return classRefs.isEmpty() ? Collections.<Class<?>> emptyList() : classRefs; } }
final List<Class<T>> classRefs = new ArrayList<>(); for (final ClassInfo classInfo : this) { final Class<T> classRef = classInfo.loadClass(superclassOrInterfaceType, ignoreExceptions); if (classRef != null) { classRefs.add(classRef);
@Override public void load(Collection<PluginMetaData> plugins) { List<KotlinPluginScript> pluginScripts = new ArrayList<>(); ClassGraph classGraph = new ClassGraph().enableAllInfo(); try (ScanResult scanResult = classGraph.scan()) { ClassInfoList pluginClassList = scanResult .getSubclasses(KotlinPluginScript.class.getName()) .directOnly(); for (ClassInfo pluginClassInfo : pluginClassList) { Class<KotlinPluginScript> scriptClass = pluginClassInfo.loadClass(KotlinPluginScript.class); Constructor<KotlinPluginScript> scriptConstructor = scriptClass.getConstructor(World.class, PluginContext.class); pluginScripts.add(scriptConstructor.newInstance(world, context)); logger.info(String.format("Loaded plugin: %s", pluginDescriptor(scriptClass))); } } catch (Exception e) { throw new RuntimeException(e); } pluginScripts.forEach(script -> script.doStart(world)); }
public static void checkInitByAnnotatedClass() { if (!annotatedClassInitialized) { // look for classes annotated with @Properties try (ScanResult scanResult = new ClassGraph().enableAllInfo().scan()) { ClassInfoList classInfoList = scanResult.getClassesWithAnnotation(io.github.thingersoft.pm.api.annotations.Properties.class.getName()); for (ClassInfo mappedClassInfo : classInfoList) { Class<?> mappedClass = mappedClassInfo.loadClass(); initByAnnotatedClass(mappedClass); } } catch (IllegalArgumentException e) { throw new RuntimeException("Properties injection mapping failed", e); } } }
ClassInfoList csvList = scanResult.getClassesWithAnnotation(CsvMarshaller.class.getCanonicalName()); for (ClassInfo csvClassInfo : csvList) { final Class<?> csvClass = csvClassInfo.loadClass(); AnnotationInfo annotationInfo = csvClassInfo.getAnnotationInfo(Disabled.class.getCanonicalName()); if (annotationInfo == null) {
@SuppressWarnings("unchecked") private ClasspathScanningRegistry(ClassLoader classLoader, Set<String> whitelistPackages) { final Map<Class<? extends NamedType>, Map<String, NamedType>> registrations = CACHE.computeIfAbsent(classLoader, cl -> { final ClassGraph classGraph = !whitelistPackages.isEmpty() ? new ClassGraph().enableClassInfo().whitelistPackages(whitelistPackages.toArray(new String[0])).addClassLoader(classLoader) : new ClassGraph().enableClassInfo().addClassLoader(classLoader); try (final ScanResult scanResult = classGraph.scan()) { final Map<Class<? extends NamedType>, Map<String, NamedType>> result = new ConcurrentHashMap<>(); scanResult.getClassesImplementing(NamedType.class.getName()).forEach(classInfo -> { if (classInfo.isInterface() || classInfo.isAbstract() || Modifier.isPrivate(classInfo.getModifiers())) { return; } final Class<?> type = classInfo.loadClass(); final Class<? extends NamedType> classType = getClassType(type); final NamedType instance = newInstance(type); final NamedType oldInstance = result.computeIfAbsent(classType, t -> new HashMap<>()).put(instance.name(), instance); if (oldInstance != null) { LOGGER.warn("Found multiple types with name={}, classes=[{}, {}]", instance.name(), oldInstance.getClass().getName(), instance.getClass().getName()); } }); return result; } }); this.registrations.putAll(registrations); }
deployment.getRegistry().addPerInstanceResource(classInfo.loadClass(), "/v1"); });