Class ClassUtil


  • public final class ClassUtil
    extends java.lang.Object
    Contains useful utilities for class file manipulation. This is a static class, so there is no need to instantiate it.
    Since:
    2.1.29.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String MAPPINGS
      The location of the classmappings.xml document.
      static java.lang.String MAPPINGS_EXTRA
      The location of the classmappings-extra.xml document.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean assignable​(java.lang.String srcClassName, java.lang.String parentClassName)
      checks if srcClassName is a subclass of parentClassname.
      static <T> T buildInstance​(java.lang.Class<T> from)
      This method is used to instantiate a given class.
      static <T> T buildInstance​(java.lang.Class<T> from, java.lang.Object... initArgs)
      This method is used to instantiate a given class.
      static <T> T buildInstance​(java.lang.String className)
      This method is used to instantiate a given class.
      static <T> T buildInstance​(java.lang.String packageName, java.lang.String className)
      This method is used to instantiate a given class.
      static java.util.List<java.lang.String> classpathEntriesUnder​(java.lang.String rootPackage)
      Lists all the files in classpath under a given package.
      static boolean exists​(java.lang.String className)
      Checks if a given class exists in classpath.
      static <T> java.lang.Class<T> findClass​(java.lang.String packageName, java.lang.String className)
      It will first attempt to instantiate the class directly from the className, and will then try to prefix it with the packageName.
      static <T> java.lang.Class<T> findClass​(java.util.List<java.lang.String> packages, java.util.List<java.lang.String> externaljars, java.lang.String className)
      Attempts to find a class from a collection of packages.
      static java.util.Map<java.lang.String,​java.lang.String> getExtraClassMappings()  
      static java.lang.Class<?> getMappedClass​(java.lang.String requestedClass)
      Finds a mapped class from the c_classMappings list.
      static <T> T getMappedObject​(java.lang.String requestedClass)
      This method is used to locate and instantiate a mapped class.
      static <T> T getMappedObject​(java.lang.String requestedClass, java.lang.Object... initargs)
      This method is used to locate and instantiate a mapped class.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • MAPPINGS

        public static final java.lang.String MAPPINGS
        The location of the classmappings.xml document. It will be searched for in the classpath. Its value is ""ini/classmappings.xml"".
        See Also:
        Constant Field Values
      • MAPPINGS_EXTRA

        public static final java.lang.String MAPPINGS_EXTRA
        The location of the classmappings-extra.xml document. It will be searched for in the classpath. Its value is ""ini/classmappings-extra.xml"".
        See Also:
        Constant Field Values
    • Method Detail

      • findClass

        public static <T> java.lang.Class<T> findClass​(java.util.List<java.lang.String> packages,
                                                       java.util.List<java.lang.String> externaljars,
                                                       java.lang.String className)
                                                throws java.lang.ClassNotFoundException
        Attempts to find a class from a collection of packages. This will first attempt to find the class based on just the className parameter, but should that fail, will iterate through the "packages" -list, prefixes the package name to the className, and then tries to find the class again.
        Parameters:
        packages - A List of Strings, containing different package names.
        className - The name of the class to find.
        Returns:
        The class, if it was found.
        Throws:
        java.lang.ClassNotFoundException - if this particular class cannot be found from the list.
      • findClass

        public static <T> java.lang.Class<T> findClass​(java.lang.String packageName,
                                                       java.lang.String className)
                                                throws java.lang.ClassNotFoundException
        It will first attempt to instantiate the class directly from the className, and will then try to prefix it with the packageName.
        Parameters:
        packageName - A package name (such as "org.apache.wiki.plugins").
        className - The class name to find.
        Returns:
        The class, if it was found.
        Throws:
        java.lang.ClassNotFoundException - if this particular class cannot be found.
      • classpathEntriesUnder

        public static java.util.List<java.lang.String> classpathEntriesUnder​(java.lang.String rootPackage)
        Lists all the files in classpath under a given package.
        Parameters:
        rootPackage - the base package. Can be {code null}.
        Returns:
        all files entries in classpath under the given package
      • getMappedObject

        public static <T> T getMappedObject​(java.lang.String requestedClass)
                                     throws java.lang.ReflectiveOperationException,
                                            java.lang.IllegalArgumentException
        This method is used to locate and instantiate a mapped class. You may redefine anything in the resource file which is located in your classpath under the name ClassUtil.MAPPINGS ("ini/classmappings.xml").

        This is an extremely powerful system, which allows you to remap many of the JSPWiki core classes to your own class. Please read the documentation included in the default "ini/classmappings.xml" file to see how this method works.

        Parameters:
        requestedClass - The name of the class you wish to instantiate.
        Returns:
        An instantiated Object.
        Throws:
        java.lang.IllegalArgumentException - If the class cannot be found or instantiated.
        java.lang.ReflectiveOperationException - If the class cannot be found or instantiated.
        Since:
        2.5.40
      • getMappedObject

        public static <T> T getMappedObject​(java.lang.String requestedClass,
                                            java.lang.Object... initargs)
                                     throws java.lang.ReflectiveOperationException,
                                            java.lang.IllegalArgumentException
        This method is used to locate and instantiate a mapped class. You may redefine anything in the resource file which is located in your classpath under the name "ini/classmappings.xml".

        This is an extremely powerful system, which allows you to remap many of the JSPWiki core classes to your own class. Please read the documentation included in the default "ini/classmappings.xml" file to see how this method works.

        This method takes in an object array for the constructor arguments for classes which have more than two constructors.

        Parameters:
        requestedClass - The name of the class you wish to instantiate.
        initargs - The parameters to be passed to the constructor. May be null.
        Returns:
        An instantiated Object.
        Throws:
        java.lang.IllegalArgumentException - If the class cannot be found or instantiated.
        java.lang.ReflectiveOperationException - If the class cannot be found or instantiated.
        Since:
        2.5.40
      • getMappedClass

        public static java.lang.Class<?> getMappedClass​(java.lang.String requestedClass)
                                                 throws java.lang.ClassNotFoundException
        Finds a mapped class from the c_classMappings list. If there is no mappped class, will use the requestedClass.
        Parameters:
        requestedClass - requested class.
        Returns:
        A Class object which you can then instantiate.
        Throws:
        java.lang.ClassNotFoundException - if the class is not found.
      • assignable

        public static boolean assignable​(java.lang.String srcClassName,
                                         java.lang.String parentClassName)
        checks if srcClassName is a subclass of parentClassname.
        Parameters:
        srcClassName - expected subclass.
        parentClassName - expected parent class.
        Returns:
        true if srcClassName is a subclass of parentClassname, false otherwise.
      • exists

        public static boolean exists​(java.lang.String className)
        Checks if a given class exists in classpath.
        Parameters:
        className - the class to check for existence.
        Returns:
        true if it exists, false otherwise.
      • getExtraClassMappings

        public static java.util.Map<java.lang.String,​java.lang.String> getExtraClassMappings()
      • buildInstance

        public static <T> T buildInstance​(java.lang.String className)
                                   throws java.lang.ReflectiveOperationException
        This method is used to instantiate a given class.
        Parameters:
        className - The name of the class you wish to instantiate.
        Returns:
        An instantiated Object.
        Throws:
        java.lang.ReflectiveOperationException - If the class cannot be found or instantiated.
        Since:
        2.11.1
      • buildInstance

        public static <T> T buildInstance​(java.lang.String packageName,
                                          java.lang.String className)
                                   throws java.lang.ReflectiveOperationException
        This method is used to instantiate a given class.

        * It will first attempt to instantiate the class directly from the className, and will then try to prefix it with the packageName.

        Parameters:
        packageName - A package name (such as "org.apache.wiki.plugins").
        className - The class name to find.
        Returns:
        An instantiated Object.
        Throws:
        java.lang.ReflectiveOperationException - If the class cannot be found or instantiated.
        Since:
        2.11.1
      • buildInstance

        public static <T> T buildInstance​(java.lang.Class<T> from)
                                   throws java.lang.ReflectiveOperationException
        This method is used to instantiate a given class.
        Parameters:
        from - The name of the class you wish to instantiate.
        Returns:
        An instantiated Object.
        Throws:
        java.lang.ReflectiveOperationException - If the class cannot be found or instantiated.
        Since:
        2.11.1
      • buildInstance

        public static <T> T buildInstance​(java.lang.Class<T> from,
                                          java.lang.Object... initArgs)
                                   throws java.lang.ReflectiveOperationException
        This method is used to instantiate a given class.

        This method takes in an object array for the constructor arguments for classes which have more than two constructors.

        Parameters:
        from - The name of the class you wish to instantiate.
        initArgs - The parameters to be passed to the constructor. May be null.
        Returns:
        An instantiated Object.
        Throws:
        java.lang.ReflectiveOperationException - If the class cannot be found or instantiated.
        Since:
        2.11.1