Home | History | Annotate | Download | only in v8
      1 /*
      2  * Copyright 2010, The Android Open Source Project
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  *  * Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  *  * Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 #include "JavaClassJobjectV8.h"
     28 
     29 #if ENABLE(JAVA_BRIDGE)
     30 
     31 #include "JavaFieldJobjectV8.h"
     32 #include "JavaMethodJobject.h"
     33 
     34 using namespace JSC::Bindings;
     35 
     36 JavaClassJobject::JavaClassJobject(jobject anInstance)
     37 {
     38     jobject aClass = callJNIMethod<jobject>(anInstance, "getClass", "()Ljava/lang/Class;");
     39 
     40     if (!aClass) {
     41         LOG_ERROR("unable to call getClass on instance %p", anInstance);
     42         return;
     43     }
     44 
     45     JNIEnv* env = getJNIEnv();
     46 
     47     // Get the fields
     48     jarray fields = static_cast<jarray>(callJNIMethod<jobject>(aClass, "getFields", "()[Ljava/lang/reflect/Field;"));
     49     int numFields = env->GetArrayLength(fields);
     50     for (int i = 0; i < numFields; i++) {
     51         jobject aJField = env->GetObjectArrayElement(static_cast<jobjectArray>(fields), i);
     52         JavaField* aField = new JavaFieldJobject(env, aJField); // deleted in the JavaClass destructor
     53         m_fields.set(aField->name(), aField);
     54         env->DeleteLocalRef(aJField);
     55     }
     56 
     57     // Get the methods
     58     jarray methods = static_cast<jarray>(callJNIMethod<jobject>(aClass, "getMethods", "()[Ljava/lang/reflect/Method;"));
     59     int numMethods = env->GetArrayLength(methods);
     60     for (int i = 0; i < numMethods; i++) {
     61         jobject aJMethod = env->GetObjectArrayElement(static_cast<jobjectArray>(methods), i);
     62         JavaMethod* aMethod = new JavaMethodJobject(env, aJMethod); // deleted in the JavaClass destructor
     63         MethodList* methodList = m_methods.get(aMethod->name());
     64         if (!methodList) {
     65             methodList = new MethodList();
     66             m_methods.set(aMethod->name(), methodList);
     67         }
     68         methodList->append(aMethod);
     69         env->DeleteLocalRef(aJMethod);
     70     }
     71     env->DeleteLocalRef(fields);
     72     env->DeleteLocalRef(methods);
     73     env->DeleteLocalRef(aClass);
     74 }
     75 
     76 JavaClassJobject::~JavaClassJobject()
     77 {
     78     deleteAllValues(m_fields);
     79     m_fields.clear();
     80 
     81     MethodListMap::const_iterator end = m_methods.end();
     82     for (MethodListMap::const_iterator it = m_methods.begin(); it != end; ++it) {
     83         const MethodList* methodList = it->second;
     84         deleteAllValues(*methodList);
     85         delete methodList;
     86     }
     87     m_methods.clear();
     88 }
     89 
     90 MethodList JavaClassJobject::methodsNamed(const char* name) const
     91 {
     92     MethodList* methodList = m_methods.get(name);
     93 
     94     if (methodList)
     95         return *methodList;
     96     return MethodList();
     97 }
     98 
     99 JavaField* JavaClassJobject::fieldNamed(const char* name) const
    100 {
    101     return m_fields.get(name);
    102 }
    103 
    104 #endif // ENABLE(JAVA_BRIDGE)
    105