public static ClassLoader findClassLoader() { return LoaderUtil.getThreadContextClassLoader(); }
/** * Determines if a named Class can be loaded or not. * * @param className The class name. * @return {@code true} if the class could be found or {@code false} otherwise. */ private static boolean isClassAvailable(final String className) { try { return LoaderUtil.loadClass(className) != null; } catch (final Throwable e) { return false; } }
private static Properties loadPropertiesFile(final String fileName) { final Properties props = new Properties(); for (final URL url : LoaderUtil.findResources(fileName)) { try (final InputStream in = url.openStream()) { props.load(in); } catch (IOException e) { LowLevelLogUtil.logException("Unable to read " + url, e); } } return props; }
/** * Loads and instantiates a Class using the default constructor. * * @param className The class name. * @return new instance of the class. * @throws ClassNotFoundException if the class isn't available to the usual ClassLoaders * @throws IllegalAccessException if the class can't be instantiated through a public constructor * @throws InstantiationException if there was an exception whilst instantiating the class * @throws NoSuchMethodException if there isn't a no-args constructor on the class * @throws InvocationTargetException if there was an exception whilst constructing the class * @since 2.1 */ @SuppressWarnings("unchecked") public static <T> T newInstanceOf(final String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { return newInstanceOf((Class<T>) loadClass(className)); }
/** * Loads a class by name. This method respects the {@link #IGNORE_TCCL_PROPERTY} Log4j property. If this property is * specified and set to anything besides {@code false}, then the default ClassLoader will be used. * * @param className The class name. * @return the Class for the given name. * @throws ClassNotFoundException if the specified class name could not be found * @since 2.1 */ public static Class<?> loadClass(final String className) throws ClassNotFoundException { if (isIgnoreTccl()) { return Class.forName(className); } try { return getThreadContextClassLoader().loadClass(className); } catch (final Throwable ignored) { return Class.forName(className); } }
private ProviderUtil() { for (ClassLoader classLoader : LoaderUtil.getClassLoaders()) { try { loadProviders(classLoader); } catch (Throwable ex) { LOGGER.debug("Unable to retrieve provider from ClassLoader {}", classLoader, ex); } } for (final LoaderUtil.UrlResource resource : LoaderUtil.findUrlResources(PROVIDER_RESOURCE)) { loadProvider(resource.getUrl(), resource.getClassLoader()); } }
/** * Loads and instantiates a derived class using its default constructor. * * @param className The class name. * @param clazz The class to cast it to. * @param <T> The type of the class to check. * @return new instance of the class cast to {@code T} * @throws ClassNotFoundException if the class isn't available to the usual ClassLoaders * @throws IllegalAccessException if the class can't be instantiated through a public constructor * @throws InstantiationException if there was an exception whilst instantiating the class * @throws NoSuchMethodException if there isn't a no-args constructor on the class * @throws InvocationTargetException if there was an exception whilst constructing the class * @throws ClassCastException if the constructed object isn't type compatible with {@code T} * @since 2.1 */ public static <T> T newCheckedInstanceOf(final String className, final Class<T> clazz) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { return clazz.cast(newInstanceOf(className)); }
/** * Loads and instantiates a class given by a property name. * * @param propertyName The property name to look up a class name for. * @param clazz The class to cast it to. * @param <T> The type to cast it to. * @return new instance of the class given in the property or {@code null} if the property was unset. * @throws ClassNotFoundException if the class isn't available to the usual ClassLoaders * @throws IllegalAccessException if the class can't be instantiated through a public constructor * @throws InstantiationException if there was an exception whilst instantiating the class * @throws NoSuchMethodException if there isn't a no-args constructor on the class * @throws InvocationTargetException if there was an exception whilst constructing the class * @throws ClassCastException if the constructed object isn't type compatible with {@code T} * @since 2.5 */ public static <T> T newCheckedInstanceOfProperty(final String propertyName, final Class<T> clazz) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { final String className = PropertiesUtil.getProperties().getStringProperty(propertyName); if (className == null) { return null; } return newCheckedInstanceOf(className, clazz); }
/** * Finds classpath {@linkplain URL resources}. * * @param resource the name of the resource to find. * @return a Collection of URLs matching the resource name. If no resources could be found, then this will be empty. * @since 2.1 */ public static Collection<URL> findResources(final String resource) { final Collection<UrlResource> urlResources = findUrlResources(resource); final Collection<URL> resources = new LinkedHashSet<>(urlResources.size()); for (final UrlResource urlResource : urlResources) { resources.add(urlResource.getUrl()); } return resources; }
/** * Loads a class by name. This method respects the {@link # IGNORE_TCCL_PROPERTY} Log4j property. If this property * is specified and set to anything besides {@code false}, then the default ClassLoader will be used. * * @param className The class name. * @return the Class for the given name. * @throws ClassNotFoundException if the specified class name could not be found * @since 2.1 */ public static Class<?> loadClass(final String className) throws ClassNotFoundException { if (isIgnoreTccl()) { return Class.forName(className); } try { return getThreadContextClassLoader().loadClass(className); } catch (final Throwable ignored) { return Class.forName(className); } }
/** * Loads and instantiates a Class using the default constructor. * * @param className The class name. * @return new instance of the class. * @throws ClassNotFoundException if the class isn't available to the usual ClassLoaders * @throws IllegalAccessException if the class can't be instantiated through a public constructor * @throws InstantiationException if there was an exception whilst instantiating the class * @throws NoSuchMethodException if there isn't a no-args constructor on the class * @throws InvocationTargetException if there was an exception whilst constructing the class * @since 2.1 */ @SuppressWarnings("unchecked") public static <T> T newInstanceOf(final String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { return newInstanceOf((Class<T>) loadClass(className)); }
/** * Loads and instantiates a Class using the default constructor. * * @param className The class name. * @return new instance of the class. * @throws ClassNotFoundException if the class isn't available to the usual ClassLoaders * @throws IllegalAccessException if the class can't be instantiated through a public constructor * @throws InstantiationException if there was an exception whilst instantiating the class * @throws NoSuchMethodException if there isn't a no-args constructor on the class * @throws InvocationTargetException if there was an exception whilst constructing the class */ public static Object newInstanceOf(final String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { return LoaderUtil.newInstanceOf(className); }
/** * Loads, instantiates, and casts a Class using the default constructor. * * @param className The class name. * @param clazz The class to cast it to. * @param <T> The type to cast it to. * @return new instance of the class cast to {@code T} * @throws ClassNotFoundException if the class isn't available to the usual ClassLoaders * @throws IllegalAccessException if the class can't be instantiated through a public constructor * @throws InstantiationException if there was an exception whilst instantiating the class * @throws NoSuchMethodException if there isn't a no-args constructor on the class * @throws InvocationTargetException if there was an exception whilst constructing the class * @throws ClassCastException if the constructed object isn't type compatible with {@code T} */ public static <T> T newCheckedInstanceOf(final String className, final Class<T> clazz) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { return LoaderUtil.newCheckedInstanceOf(className, clazz); }
/** * Finds classpath {@linkplain URL resources}. * * @param resource the name of the resource to find. * @return a Collection of URLs matching the resource name. If no resources could be found, then this will be empty. * @since 2.1 */ public static Collection<URL> findResources(final String resource) { final Collection<UrlResource> urlResources = findUrlResources(resource); final Collection<URL> resources = new LinkedHashSet<>(urlResources.size()); for (final UrlResource urlResource : urlResources) { resources.add(urlResource.getUrl()); } return resources; }
private static EventDataConverter createConverter() { try { LoaderUtil.loadClass("org.slf4j.ext.EventData"); return new EventDataConverter(); } catch (final ClassNotFoundException cnfe) { return null; } }
public TestClassLoader() { super(LoaderUtil.getThreadContextClassLoader()); }
/** * Loads a class by name. This method respects the {@link #IGNORE_TCCL_PROPERTY} Log4j property. If this property is * specified and set to anything besides {@code false}, then the default ClassLoader will be used. * * @param className The class name. * @return the Class for the given name. * @throws ClassNotFoundException if the specified class name could not be found * @since 2.1 */ public static Class<?> loadClass(final String className) throws ClassNotFoundException { if (isIgnoreTccl()) { return Class.forName(className); } try { return getThreadContextClassLoader().loadClass(className); } catch (final Throwable ignored) { return Class.forName(className); } }
/** * Loads and instantiates a derived class using its default constructor. * * @param className The class name. * @param clazz The class to cast it to. * @param <T> The type of the class to check. * @return new instance of the class cast to {@code T} * @throws ClassNotFoundException if the class isn't available to the usual ClassLoaders * @throws IllegalAccessException if the class can't be instantiated through a public constructor * @throws InstantiationException if there was an exception whilst instantiating the class * @throws NoSuchMethodException if there isn't a no-args constructor on the class * @throws InvocationTargetException if there was an exception whilst constructing the class * @throws ClassCastException if the constructed object isn't type compatible with {@code T} * @since 2.1 */ public static <T> T newCheckedInstanceOf(final String className, final Class<T> clazz) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { return clazz.cast(newInstanceOf(className)); }
/** * Constructs a PropertiesUtil for a given properties file name on the classpath. The properties specified in this * file are used by default. If a property is not defined in this file, then the equivalent system property is used. * * @param propertiesFileName the location of properties file to load */ public PropertiesUtil(final String propertiesFileName) { final Properties properties = new Properties(); for (final URL url : LoaderUtil.findResources(propertiesFileName)) { try (final InputStream in = url.openStream()) { properties.load(in); } catch (final IOException ioe) { LowLevelLogUtil.logException("Unable to read " + url.toString(), ioe); } } this.props = properties; }
/** * Loads and instantiates a class given by a property name. * * @param propertyName The property name to look up a class name for. * @param clazz The class to cast it to. * @param <T> The type to cast it to. * @return new instance of the class given in the property or {@code null} if the property was unset. * @throws ClassNotFoundException if the class isn't available to the usual ClassLoaders * @throws IllegalAccessException if the class can't be instantiated through a public constructor * @throws InstantiationException if there was an exception whilst instantiating the class * @throws NoSuchMethodException if there isn't a no-args constructor on the class * @throws InvocationTargetException if there was an exception whilst constructing the class * @throws ClassCastException if the constructed object isn't type compatible with {@code T} * @since 2.5 */ public static <T> T newCheckedInstanceOfProperty(final String propertyName, final Class<T> clazz) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { final String className = PropertiesUtil.getProperties().getStringProperty(propertyName); if (className == null) { return null; } return newCheckedInstanceOf(className, clazz); }