Class RJavaClassLoader

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class RJavaClassLoader
    extends java.net.URLClassLoader
    Class loader used internally by rJava The class manages the class paths and the native libraries (jri, ...)
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static RJavaClassLoader primaryLoader
      singleton
      boolean useSystem
      Should the system class loader be used to resolve classes as well as this class loader
      static boolean verbose
      Print debug messages if is set to true
    • Constructor Summary

      Constructors 
      Constructor Description
      RJavaClassLoader​(java.lang.String path, java.lang.String libpath)
      Constructor.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addClassPath​(java.lang.String cp)
      adds an entry to the class path
      void addClassPath​(java.lang.String[] cp)
      adds several entries to the class path
      void addRLibrary​(java.lang.String name, java.lang.String path)
      add a library to path mapping for a native library
      void bootClass​(java.lang.String cName, java.lang.String mName, java.lang.String[] args)
      Boots the specified method of the specified class
      protected java.lang.Class findClass​(java.lang.String name)  
      protected java.lang.String findLibrary​(java.lang.String name)  
      java.net.URL findResource​(java.lang.String name)  
      java.lang.String[] getClassPath()  
      static RJavaClassLoader getPrimaryLoader()
      Returns the singleton instance of RJavaClassLoader
      static void main​(java.lang.String[] args)
      main method
      static void setDebug​(int level)
      Set the debug level.
      static byte[] toByte​(java.lang.Object object)
      Serialize an object to a byte array.
      java.lang.Object toObject​(byte[] byteArray)
      Deserialize an object from a byte array.
      static java.lang.Object toObjectPL​(byte[] byteArray)
      converts the byte array into an Object using the primary RJavaClassLoader
      static java.lang.String u2w​(java.lang.String fn)
      Utility to convert paths for windows.
      • Methods inherited from class java.net.URLClassLoader

        addURL, close, definePackage, findResources, getPermissions, getResourceAsStream, getURLs, newInstance, newInstance
      • Methods inherited from class java.security.SecureClassLoader

        defineClass, defineClass
      • Methods inherited from class java.lang.ClassLoader

        clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findLoadedClass, findResource, findSystemClass, getClassLoadingLock, getDefinedPackage, getDefinedPackages, getName, getPackage, getPackages, getParent, getPlatformClassLoader, getResource, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, loadClass, loadClass, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
      • Methods inherited from class java.lang.Object

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

      • verbose

        public static boolean verbose
        Print debug messages if is set to true
      • useSystem

        public boolean useSystem
        Should the system class loader be used to resolve classes as well as this class loader
    • Constructor Detail

      • RJavaClassLoader

        public RJavaClassLoader​(java.lang.String path,
                                java.lang.String libpath)
        Constructor. The first time an RJavaClassLoader is created, it is cached as the primary loader.
        Parameters:
        path - path of the rJava package
        libpath - lib sub directory of the rJava package
    • Method Detail

      • getPrimaryLoader

        public static RJavaClassLoader getPrimaryLoader()
        Returns the singleton instance of RJavaClassLoader
      • findClass

        protected java.lang.Class findClass​(java.lang.String name)
                                     throws java.lang.ClassNotFoundException
        Overrides:
        findClass in class java.net.URLClassLoader
        Throws:
        java.lang.ClassNotFoundException
      • findResource

        public java.net.URL findResource​(java.lang.String name)
        Overrides:
        findResource in class java.net.URLClassLoader
      • addRLibrary

        public void addRLibrary​(java.lang.String name,
                                java.lang.String path)
        add a library to path mapping for a native library
      • addClassPath

        public void addClassPath​(java.lang.String cp)
        adds an entry to the class path
      • addClassPath

        public void addClassPath​(java.lang.String[] cp)
        adds several entries to the class path
      • getClassPath

        public java.lang.String[] getClassPath()
        Returns:
        the array of class paths used by this class loader
      • findLibrary

        protected java.lang.String findLibrary​(java.lang.String name)
        Overrides:
        findLibrary in class java.lang.ClassLoader
      • bootClass

        public void bootClass​(java.lang.String cName,
                              java.lang.String mName,
                              java.lang.String[] args)
                       throws java.lang.IllegalAccessException,
                              java.lang.reflect.InvocationTargetException,
                              java.lang.NoSuchMethodException,
                              java.lang.ClassNotFoundException
        Boots the specified method of the specified class
        Parameters:
        cName - class to boot
        mName - method to boot (typically main). The method must take a String[] as parameter
        args - arguments to pass to the method
        Throws:
        java.lang.IllegalAccessException
        java.lang.reflect.InvocationTargetException
        java.lang.NoSuchMethodException
        java.lang.ClassNotFoundException
      • setDebug

        public static void setDebug​(int level)
        Set the debug level. At the moment, there is only verbose (level > 0) or quiet
        Parameters:
        level - debug level. verbose (>0), quiet otherwise
      • u2w

        public static java.lang.String u2w​(java.lang.String fn)
        Utility to convert paths for windows. Converts / to the path separator in use
        Parameters:
        fn - file name
      • main

        public static void main​(java.lang.String[] args)
        main method

        This uses the system properties:

        • rjava.path : path of the rJava package
        • rjava.lib : lib sub directory of the rJava package
        • main.class : main class to "boot", assumes Main if not specified
        • rjava.class.path : set of paths to populate the initiate the class path

        and boots the "main" method of the specified main.class, passing the args down to the booted class

        This makes sure R and rJava are known by the class loader

      • toByte

        public static byte[] toByte​(java.lang.Object object)
                             throws java.lang.Exception
        Serialize an object to a byte array. (code by CB)
        Parameters:
        object - object to serialize
        Returns:
        byte array that represents the object
        Throws:
        java.lang.Exception
      • toObject

        public java.lang.Object toObject​(byte[] byteArray)
                                  throws java.lang.Exception
        Deserialize an object from a byte array. (code by CB)
        Parameters:
        byteArray -
        Returns:
        the object that is represented by the byte array
        Throws:
        java.lang.Exception
      • toObjectPL

        public static java.lang.Object toObjectPL​(byte[] byteArray)
                                           throws java.lang.Exception
        converts the byte array into an Object using the primary RJavaClassLoader
        Throws:
        java.lang.Exception