com.hazelcast.nio

## Class ClassLoaderUtil

- java.lang.Object
- - com.hazelcast.nio.ClassLoaderUtil

-     @PrivateApi
      public final class ClassLoaderUtil
      extends Object

  Utility class to deal with class loaders.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static String` | `HAZELCAST_ARRAY`
    `static String` | `HAZELCAST_BASE_PACKAGE`

  - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static Class<?>[]` | `getAllInterfaces(Class<?> clazz)`
    `static <T> T` | `getOrCreate(T instance, ClassLoader classLoader, String className)` Returns the `instance`, if not null, or constructs a new instance of the class using `newInstance(ClassLoader, String)`.
    `static boolean` | `implementsInterfaceWithSameName(Class<?> clazz, Class<?> iface)` Check whether given class implements an interface with the same name.
    `static boolean` | `isClassAvailable(ClassLoader classLoader, String className)`
    `static boolean` | `isClassDefined(String className)` Indicates whether or not the given class exists
    `static boolean` | `isInternalType(Class type)`
    `static Class<?>` | `loadClass(ClassLoader classLoaderHint, String className)`
    `static <T> T` | `newInstance(ClassLoader classLoaderHint, String className)` Creates a new instance of class with `className`, using the no-arg constructor.
    `static Class<?>` | `tryLoadClass(String className)` Tries to load the given class.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### HAZELCAST_BASE_PACKAGE

          public static final String HAZELCAST_BASE_PACKAGE

      See Also:
        Constant Field Values

    - #### HAZELCAST_ARRAY

          public static final String HAZELCAST_ARRAY

      See Also:
        Constant Field Values

  - ### Method Detail

    - #### getOrCreate

          public static <T> T getOrCreate(T instance,
                                          ClassLoader classLoader,
                                          String className)

      Returns the `instance`, if not null, or constructs a new instance of the class using `newInstance(ClassLoader, String)`.
      Parameters:
        `instance` - the instance of the class, can be null
        `classLoader` - the classloader used for class instantiation
        `className` - the name of the class being constructed. If null, null is returned.
      Returns:
        the provided `instance` or a newly constructed instance of `className` or null, if `className` was null

    - #### newInstance

          public static <T> T newInstance(ClassLoader classLoaderHint,
                                          String className)
                                   throws Exception

      Creates a new instance of class with `className`, using the no-arg constructor. Preferably uses the class loader specified in `classLoaderHint`. A constructor cache is used to reduce reflection calls.

      The implementation first chooses candidate class loaders. Then checks the constructor cache if a constructor is found for either of them. If not, it queries them and caches the used `Constructor` under the classloader key that was used to retrieve it (might not the actual classloader that loaded the returned instance, but a parent one). To choose the candidate class loaders, a peculiar, hard-to-explain-more-simply-than-reading-the-code logic is used, beware.

      Parameters:
        `classLoaderHint` - Suggested class loader to use, can be null
        `className` - Class name (can be also primitive name or array ("[Lpkg.Class]"), required
      Returns:
        New instance
      Throws:
        `Exception` - ClassNotFoundException, IllegalAccessException, InstantiationException, or InvocationTargetException

    - #### loadClass

          public static Class<?> loadClass(ClassLoader classLoaderHint,
                                           String className)
                                    throws ClassNotFoundException

      Throws:
        `ClassNotFoundException`

    - #### isClassAvailable

          public static boolean isClassAvailable(ClassLoader classLoader,
                                                 String className)

    - #### isInternalType

          public static boolean isInternalType(Class type)

    - #### tryLoadClass

          public static Class<?> tryLoadClass(String className)
                                       throws ClassNotFoundException

      Tries to load the given class.
      Parameters:
        `className` - Name of the class to load
      Returns:
        Loaded class
      Throws:
        `ClassNotFoundException` - when the class is not found

    - #### isClassDefined

          public static boolean isClassDefined(String className)

      Indicates whether or not the given class exists
      Parameters:
        `className` - Name of the class
      Returns:
        `true` if the class exists, `false` otherwise

    - #### implementsInterfaceWithSameName

          public static boolean implementsInterfaceWithSameName(Class<?> clazz,
                                                                Class<?> iface)

      Check whether given class implements an interface with the same name. It returns true even when the implemented interface is loaded by a different classloader and hence the class is not assignable into it. An interface is considered as implemented when either:
      - The class directly implements the interface
      - The class implements an interface which extends the original interface
      - One of superclasses directly implements the interface
      - One of superclasses implements an interface which extends the original interface
      This is useful for logging purposes.
      Parameters:
        `clazz` - class to check whether implements the interface
        `iface` - interface to be implemented
      Returns:
        `true` when the class implements the inteface with the same name

    - #### getAllInterfaces

          public static Class<?>[] getAllInterfaces(Class<?> clazz)

Copyright © 2019. All rights reserved.