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 "JavaClassV8.h"
     28 
     29 
     30 using namespace JSC::Bindings;
     31 
     32 JavaClass::JavaClass(jobject anInstance)
     33 {
     34     jobject aClass = callJNIMethod<jobject>(anInstance, "getClass", "()Ljava/lang/Class;");
     35 
     36     if (!aClass) {
     37         fprintf(stderr, "%s:  unable to call getClass on instance %p\n", __PRETTY_FUNCTION__, anInstance);
     38         return;
     39     }
     40 
     41     jstring className = static_cast<jstring>(callJNIMethod<jobject>(aClass, "getName", "()Ljava/lang/String;"));
     42     const char* classNameC = getCharactersFromJString(className);
     43     m_name = strdup(classNameC);
     44     releaseCharactersForJString(className, classNameC);
     45 
     46     int i;
     47     JNIEnv* env = getJNIEnv();
     48 
     49     // Get the fields
     50     jarray fields = static_cast<jarray>(callJNIMethod<jobject>(aClass, "getFields", "()[Ljava/lang/reflect/Field;"));
     51     int numFields = env->GetArrayLength(fields);
     52     for (i = 0; i < numFields; i++) {
     53         jobject aJField = env->GetObjectArrayElement(static_cast<jobjectArray>(fields), i);
     54         JavaField* aField = new JavaField(env, aJField); // deleted in the JavaClass destructor
     55         {
     56             m_fields.set(aField->name().UTF8String(), aField);
     57         }
     58         env->DeleteLocalRef(aJField);
     59     }
     60 
     61     // Get the methods
     62     jarray methods = static_cast<jarray>(callJNIMethod<jobject>(aClass, "getMethods", "()[Ljava/lang/reflect/Method;"));
     63     int numMethods = env->GetArrayLength(methods);
     64     for (i = 0; i < numMethods; i++) {
     65         jobject aJMethod = env->GetObjectArrayElement(static_cast<jobjectArray>(methods), i);
     66         JavaMethod* aMethod = new JavaMethod(env, aJMethod); // deleted in the JavaClass destructor
     67         MethodList* methodList;
     68         {
     69             methodList = m_methods.get(aMethod->name().UTF8String());
     70             if (!methodList) {
     71                 methodList = new MethodList();
     72                 m_methods.set(aMethod->name().UTF8String(), methodList);
     73             }
     74         }
     75         methodList->append(aMethod);
     76         env->DeleteLocalRef(aJMethod);
     77     }
     78     env->DeleteLocalRef(fields);
     79     env->DeleteLocalRef(methods);
     80     env->DeleteLocalRef(aClass);
     81 }
     82 
     83 JavaClass::~JavaClass()
     84 {
     85     free(const_cast<char*>(m_name));
     86 
     87     deleteAllValues(m_fields);
     88     m_fields.clear();
     89 
     90     MethodListMap::const_iterator end = m_methods.end();
     91     for (MethodListMap::const_iterator it = m_methods.begin(); it != end; ++it) {
     92         const MethodList* methodList = it->second;
     93         deleteAllValues(*methodList);
     94         delete methodList;
     95     }
     96     m_methods.clear();
     97 }
     98 
     99 MethodList JavaClass::methodsNamed(const char* name) const
    100 {
    101     MethodList* methodList = m_methods.get(name);
    102 
    103     if (methodList)
    104         return *methodList;
    105     return MethodList();
    106 }
    107 
    108 JavaField* JavaClass::fieldNamed(const char* name) const
    109 {
    110     return m_fields.get(name);
    111 }
    112