diff options
-rw-r--r-- | include/jvmaccess/classpath.hxx | 169 | ||||
-rw-r--r-- | jvmaccess/source/classpath.cxx | 38 |
2 files changed, 87 insertions, 120 deletions
diff --git a/include/jvmaccess/classpath.hxx b/include/jvmaccess/classpath.hxx index f412fd59a4aa..9265cd68a9b3 100644 --- a/include/jvmaccess/classpath.hxx +++ b/include/jvmaccess/classpath.hxx @@ -20,11 +20,14 @@ #ifndef INCLUDED_JVMACCESS_CLASSPATH_HXX #define INCLUDED_JVMACCESS_CLASSPATH_HXX -#include <jvmaccess/jvmaccessdllapi.h> #include <sal/config.h> -#include <com/sun/star/uno/Reference.hxx> -#include "jni.h" +#include <jni.h> + +#include <com/sun/star/uno/Reference.hxx> +#include <jvmaccess/jvmaccessdllapi.h> +#include <rtl/ustring.hxx> +#include <sal/types.h> namespace com { namespace sun { namespace star { namespace uno { class XComponentContext; @@ -34,103 +37,69 @@ namespace jvmaccess { /** Helper functions for class path handling. -*/ -class JVMACCESS_DLLPUBLIC ClassPath { -public: - /** - translates a class path into a java.net.URL[] instance. - - @param context - a component context; must not be null. - - @param environment - a JNI environment; must not be null. - - @param classPath - a list of zero or more internal (see the - com.sun.star.uri.ExternalUriReferenceTranslator service) URI references, - where any space characters (U+0020) are ignored (and, in particular, - separate adjacent URI references). Any vnd.sun.star.expand URL - references in the list are expanded using the - com.sun.star.util.theMacroExpander singleton of the given context. - - @returns - a local reference to a java.net.URL[] instance containing the external - (see the com.sun.star.uri.ExternalUriReferenceTranslator service) - equivalents of all the URI references in the given classPath. If null, a - (still pending) JNI exception occurred. - - @throws com::sun::star::uno::RuntimeException - */ - static inline ::jobjectArray - translateToUrls( - ::com::sun::star::uno::Reference< - ::com::sun::star::uno::XComponentContext > const & context, - ::JNIEnv * environment, OUString const & classPath) - { - return - static_cast< ::jobjectArray >( - doTranslateToUrls(context, environment, classPath)); - } - - /** - loads a class via a java.net.URLClassLoader. - - @param context - a component context; must not be null. - - @param environment - a JNI environment; must not be null. - - @param classPath - a list of zero or more internal (see the - com.sun.star.uri.ExternalUriReferenceTranslator service) URI references, - where any space characters (U+0020) are ignored (and, in particular, - separate adjacent URI references). Any vnd.sun.star.expand URL - references in the list are expanded using the - com.sun.star.util.theMacroExpander singleton of the given context. - - @param name - the Java binary name of the class to load. - - @returns - a local reference to a java.lang.Class instance. If null, a (still - pending) JNI exception occurred. - - @throws com::sun::star::uno::RuntimeException - */ - static inline ::jclass loadClass( - ::com::sun::star::uno::Reference< - ::com::sun::star::uno::XComponentContext > const & context, - ::JNIEnv * environment, OUString const & classPath, - OUString const & name) - { - return - static_cast< ::jclass >( - doLoadClass(context, environment, classPath, name)); - } - -private: - ClassPath() SAL_DELETED_FUNCTION; //TODO: get rid of this class - ClassPath(ClassPath &) SAL_DELETED_FUNCTION; - ~ClassPath() SAL_DELETED_FUNCTION; - void operator =(ClassPath &) SAL_DELETED_FUNCTION; - - // Functions that replace JNIEnv, jobjectArray, and jclass with void *, so - // that their mangled C++ names do not depend on the JDK version used at - // compile time: - - static void * doTranslateToUrls( - ::com::sun::star::uno::Reference< - ::com::sun::star::uno::XComponentContext > const & context, - void * environment, OUString const & classPath); - - static void * doLoadClass( - ::com::sun::star::uno::Reference< - ::com::sun::star::uno::XComponentContext > const & context, - void * environment, OUString const & classPath, - OUString const & name); -}; + */ +namespace ClassPath { + +/** + translates a class path into a java.net.URL[] instance. + + @param context + a component context; must not be null. + + @param environment + a JNI environment; must not be null. + + @param classPath + a list of zero or more internal (see the + com.sun.star.uri.ExternalUriReferenceTranslator service) URI references, + where any space characters (U+0020) are ignored (and, in particular, separate + adjacent URI references). Any vnd.sun.star.expand URL references in the list + are expanded using the com.sun.star.util.theMacroExpander singleton of the + given context. + + @returns + a local reference to a java.net.URL[] instance containing the external (see + the com.sun.star.uri.ExternalUriReferenceTranslator service) equivalents of + all the URI references in the given classPath. If null, a (still pending) + JNI exception occurred. + + @throws com::sun::star::uno::RuntimeException + */ +JVMACCESS_DLLPUBLIC jobjectArray translateToUrls( + css::uno::Reference<css::uno::XComponentContext> const & context, + JNIEnv * environment, OUString const & classPath); + +/** + loads a class via a java.net.URLClassLoader. + + @param context + a component context; must not be null. + + @param environment + a JNI environment; must not be null. + + @param classPath + a list of zero or more internal (see the + com.sun.star.uri.ExternalUriReferenceTranslator service) URI references, + where any space characters (U+0020) are ignored (and, in particular, separate + adjacent URI references). Any vnd.sun.star.expand URL references in the list + are expanded using the com.sun.star.util.theMacroExpander singleton of the + given context. + + @param name + the Java binary name of the class to load. + + @returns + a local reference to a java.lang.Class instance. If null, a (still pending) + JNI exception occurred. + + @throws com::sun::star::uno::RuntimeException + */ +JVMACCESS_DLLPUBLIC jclass loadClass( + css::uno::Reference<css::uno::XComponentContext> const & context, + JNIEnv * environment, OUString const & classPath, OUString const & name); + +} } diff --git a/jvmaccess/source/classpath.cxx b/jvmaccess/source/classpath.cxx index d01e9cebfb1f..4b653fe5df45 100644 --- a/jvmaccess/source/classpath.cxx +++ b/jvmaccess/source/classpath.cxx @@ -38,18 +38,17 @@ #include "jni.h" -void * ::jvmaccess::ClassPath::doTranslateToUrls( +jobjectArray jvmaccess::ClassPath::translateToUrls( css::uno::Reference< css::uno::XComponentContext > const & context, - void * environment, OUString const & classPath) + JNIEnv * environment, OUString const & classPath) { OSL_ASSERT(context.is() && environment != 0); - ::JNIEnv * const env = static_cast< ::JNIEnv * >(environment); - jclass classUrl(env->FindClass("java/net/URL")); + jclass classUrl(environment->FindClass("java/net/URL")); if (classUrl == 0) { return 0; } jmethodID ctorUrl( - env->GetMethodID(classUrl, "<init>", "(Ljava/lang/String;)V")); + environment->GetMethodID(classUrl, "<init>", "(Ljava/lang/String;)V")); if (ctorUrl == 0) { return 0; } @@ -73,20 +72,20 @@ void * ::jvmaccess::ClassPath::doTranslateToUrls( } } jvalue arg; - arg.l = env->NewString( + arg.l = environment->NewString( static_cast< jchar const * >(url.getStr()), static_cast< jsize >(url.getLength())); if (arg.l == 0) { return 0; } - jobject o(env->NewObjectA(classUrl, ctorUrl, &arg)); + jobject o(environment->NewObjectA(classUrl, ctorUrl, &arg)); if (o == 0) { return 0; } urls.push_back(o); } } - jobjectArray result = env->NewObjectArray( + jobjectArray result = environment->NewObjectArray( static_cast< jsize >(urls.size()), classUrl, 0); // static_cast is ok, as each element of urls occupied at least one // character of the OUString classPath @@ -96,49 +95,48 @@ void * ::jvmaccess::ClassPath::doTranslateToUrls( jsize idx = 0; for (std::vector< jobject >::iterator i(urls.begin()); i != urls.end(); ++i) { - env->SetObjectArrayElement(result, idx++, *i); + environment->SetObjectArrayElement(result, idx++, *i); } return result; } -void * ::jvmaccess::ClassPath::doLoadClass( +jclass jvmaccess::ClassPath::loadClass( css::uno::Reference< css::uno::XComponentContext > const & context, - void * environment, OUString const & classPath, - OUString const & name) + JNIEnv * environment, OUString const & classPath, OUString const & name) { OSL_ASSERT(context.is() && environment != 0); - ::JNIEnv * const env = static_cast< ::JNIEnv * >(environment); - jclass classLoader(env->FindClass("java/net/URLClassLoader")); + jclass classLoader(environment->FindClass("java/net/URLClassLoader")); if (classLoader == 0) { return 0; } jmethodID ctorLoader( - env->GetMethodID(classLoader, "<init>", "([Ljava/net/URL;)V")); + environment->GetMethodID(classLoader, "<init>", "([Ljava/net/URL;)V")); if (ctorLoader == 0) { return 0; } jvalue arg; - arg.l = translateToUrls(context, env, classPath); + arg.l = translateToUrls(context, environment, classPath); if (arg.l == 0) { return 0; } - jobject cl = env->NewObjectA(classLoader, ctorLoader, &arg); + jobject cl = environment->NewObjectA(classLoader, ctorLoader, &arg); if (cl == 0) { return 0; } jmethodID methLoadClass( - env->GetMethodID( + environment->GetMethodID( classLoader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;")); if (methLoadClass == 0) { return 0; } - arg.l = env->NewString( + arg.l = environment->NewString( static_cast< jchar const * >(name.getStr()), static_cast< jsize >(name.getLength())); if (arg.l == 0) { return 0; } - return env->CallObjectMethodA(cl, methLoadClass, &arg); + return static_cast<jclass>( + environment->CallObjectMethodA(cl, methLoadClass, &arg)); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |